summaryrefslogtreecommitdiff
path: root/lib/vector.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vector.c')
-rw-r--r--lib/vector.c44
1 files changed, 31 insertions, 13 deletions
diff --git a/lib/vector.c b/lib/vector.c
index 565c49fd59..38f9b1b85f 100644
--- a/lib/vector.c
+++ b/lib/vector.c
@@ -37,6 +37,7 @@ vector vector_init(unsigned int size)
v->alloced = size;
v->active = 0;
+ v->count = 0;
v->index = XCALLOC(MTYPE_VECTOR_INDEX, sizeof(void *) * size);
return v;
}
@@ -54,6 +55,7 @@ vector vector_copy(vector 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);
@@ -84,6 +86,9 @@ int vector_empty_slot(vector v)
{
unsigned int i;
+ if (v->active == v->count)
+ return v->active;
+
if (v->active == 0)
return 0;
@@ -102,6 +107,10 @@ int vector_set(vector v, void *val)
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)
@@ -115,6 +124,10 @@ int vector_set_index(vector v, unsigned int i, void *val)
{
vector_ensure(v, i);
+ if (v->index[i])
+ v->count--;
+ if (val)
+ v->count++;
v->index[i] = val;
if (v->active <= i)
@@ -123,6 +136,17 @@ int vector_set_index(vector v, unsigned int i, void *val)
return i;
}
+/* Make a specified index slot active and return its address. */
+void **vector_get_index(vector v, unsigned int i)
+{
+ vector_ensure(v, i);
+
+ if (v->active <= i)
+ v->active = i + 1;
+
+ return &v->index[i];
+}
+
/* Look up vector. */
void *vector_lookup(vector v, unsigned int i)
{
@@ -144,6 +168,9 @@ void vector_unset(vector v, unsigned int i)
if (i >= v->alloced)
return;
+ if (v->index[i])
+ v->count--;
+
v->index[i] = NULL;
if (i + 1 == v->active) {
@@ -158,6 +185,9 @@ void vector_remove(vector v, unsigned int ix)
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 *));
@@ -181,6 +211,7 @@ void vector_unset_value(vector v, void *val)
for (i = 0; i < v->active; i++)
if (v->index[i] == val) {
v->index[i] = NULL;
+ v->count--;
break;
}
@@ -190,19 +221,6 @@ void vector_unset_value(vector v, void *val)
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);