]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: add vector_remove() to vector.[ch]
authorQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 25 May 2018 22:49:53 +0000 (22:49 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Wed, 6 Jun 2018 16:16:12 +0000 (16:16 +0000)
An optimized version of this has already been implemented within graph.c
that assumes some specialized constraints for that code. It's generally
useful so this change implements a general purpose version of it.

This fixes cmd_make_strvec() that was broken by some code shuffling in
previous commits.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/command.c
lib/graph.c
lib/vector.c
lib/vector.h

index ad0479dc9d8b526b8aafc3e2557f43c68f7a4798..3bd578cf0139437abf32c846236369852ad1c5c0 100644 (file)
@@ -290,7 +290,8 @@ vector cmd_make_strvec(const char *string)
        for (unsigned int i = 0; i < vector_active(result); i++) {
                if (strlen(vector_slot(result, i)) == 0) {
                        XFREE(MTYPE_TMP, vector_slot(result, i));
-                       vector_unset(result, i);
+                       vector_remove(result, i);
+                       --i;
                }
        }
        return result;
index a9e35b46ff3339315854af4b98eb79d35b15199f..4bc3eb82b8521b9b4bc4dd331ef824d1fa8fbe3d 100644 (file)
@@ -60,7 +60,7 @@ struct graph_node *graph_new_node(struct graph *graph, void *data,
        return node;
 }
 
-static void vector_remove(vector v, unsigned int ix)
+static void graph_vector_remove(vector v, unsigned int ix)
 {
        if (ix >= v->active)
                return;
@@ -105,7 +105,7 @@ void graph_delete_node(struct graph *graph, struct graph_node *node)
        // remove node from graph->nodes
        for (unsigned int i = vector_active(graph->nodes); i--; /**/)
                if (vector_slot(graph->nodes, i) == node) {
-                       vector_remove(graph->nodes, i);
+                       graph_vector_remove(graph->nodes, i);
                        break;
                }
 
@@ -126,13 +126,13 @@ void graph_remove_edge(struct graph_node *from, struct graph_node *to)
        // remove from from to->from
        for (unsigned int i = vector_active(to->from); i--; /**/)
                if (vector_slot(to->from, i) == from) {
-                       vector_remove(to->from, i);
+                       graph_vector_remove(to->from, i);
                        break;
                }
        // remove to from from->to
        for (unsigned int i = vector_active(from->to); i--; /**/)
                if (vector_slot(from->to, i) == to) {
-                       vector_remove(from->to, i);
+                       graph_vector_remove(from->to, i);
                        break;
                }
 }
index ebac2b46e902866a785235cf02c01f61ec088101..696e260cdf3c227aed3aca7edfe59d41d66340d3 100644 (file)
@@ -153,6 +153,17 @@ void vector_unset(vector v, unsigned int i)
        }
 }
 
+void vector_remove(vector v, unsigned int ix)
+{
+       if (ix >= v->active)
+               return;
+
+       int n = (--v->active) - ix;
+
+       memmove(&v->index[ix], &v->index[ix + 1], n * sizeof(void *));
+       v->index[v->active] = NULL;
+}
+
 void vector_unset_value(vector v, void *val)
 {
        size_t i;
index cc28fda480835d33bda0e37c8550f19b37bb67c4..21732a300e6b4249f5e3ca440773ea992fcbf1b4 100644 (file)
@@ -52,6 +52,7 @@ extern int vector_set(vector v, void *val);
 extern int vector_set_index(vector v, unsigned int i, void *val);
 extern void vector_unset(vector v, unsigned int i);
 extern void vector_unset_value(vector v, void *val);
+extern void vector_remove(vector v, unsigned int ix);
 
 extern unsigned int vector_count(vector v);
 extern void vector_free(vector v);