v->alloced = size;
v->active = 0;
+ v->count = 0;
v->index = XCALLOC(MTYPE_VECTOR_INDEX, sizeof(void *) * size);
return v;
}
new->active = v->active;
new->alloced = v->alloced;
+ new->count = v->count;
size = sizeof(void *) * (v->alloced);
new->index = XCALLOC(MTYPE_VECTOR_INDEX, size);
{
unsigned int i;
+ if (v->active == v->count)
+ return v->active;
+
if (v->active == 0)
return 0;
i = vector_empty_slot(v);
vector_ensure(v, i);
+ if (v->index[i])
+ v->count--;
+ if (val)
+ v->count++;
v->index[i] = val;
if (v->active <= i)
{
vector_ensure(v, i);
+ if (v->index[i])
+ v->count--;
+ if (val)
+ v->count++;
v->index[i] = val;
if (v->active <= i)
if (i >= v->alloced)
return;
+ if (v->index[i])
+ v->count--;
+
v->index[i] = NULL;
if (i + 1 == v->active) {
if (ix >= v->active)
return;
+ if (v->index[ix])
+ v->count--;
+
int n = (--v->active) - ix;
memmove(&v->index[ix], &v->index[ix + 1], n * sizeof(void *));
for (i = 0; i < v->active; i++)
if (v->index[i] == val) {
v->index[i] = NULL;
+ v->count--;
break;
}
while (i && v->index[--i] == NULL);
}
-/* Count the number of not emplty slot. */
-unsigned int vector_count(vector v)
-{
- unsigned int i;
- unsigned count = 0;
-
- for (i = 0; i < v->active; i++)
- if (v->index[i] != NULL)
- count++;
-
- return count;
-}
-
void vector_to_array(vector v, void ***dest, int *argc)
{
*dest = XCALLOC(MTYPE_TMP, sizeof(void *) * v->active);
struct _vector {
unsigned int active; /* number of active slots */
unsigned int alloced; /* number of allocated slot */
+ unsigned int count;
void **index; /* index to data */
};
typedef struct _vector *vector;
extern void vector_remove(vector v, unsigned int ix);
extern void vector_compact(vector v);
-extern unsigned int vector_count(vector v);
+static inline unsigned int vector_count(vector v)
+{
+ return v->count;
+}
+
extern void vector_free(vector v);
extern vector vector_copy(vector v);