From: Quentin Young Date: Fri, 25 May 2018 22:49:53 +0000 (+0000) Subject: lib: add vector_remove() to vector.[ch] X-Git-Tag: frr-6.1-dev~356^2~5 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=62bece44494e5b7aefb5102bd1109e1e1376c3b3;p=matthieu%2Ffrr.git lib: add vector_remove() to vector.[ch] 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 --- diff --git a/lib/command.c b/lib/command.c index ad0479dc9d..3bd578cf01 100644 --- a/lib/command.c +++ b/lib/command.c @@ -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; diff --git a/lib/graph.c b/lib/graph.c index a9e35b46ff..4bc3eb82b8 100644 --- a/lib/graph.c +++ b/lib/graph.c @@ -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; } } diff --git a/lib/vector.c b/lib/vector.c index ebac2b46e9..696e260cdf 100644 --- a/lib/vector.c +++ b/lib/vector.c @@ -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; diff --git a/lib/vector.h b/lib/vector.h index cc28fda480..21732a300e 100644 --- a/lib/vector.h +++ b/lib/vector.h @@ -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);