diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-04-06 17:58:00 -0400 |
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-04-06 17:59:39 -0400 |
| commit | 9428e08906c415d538dc22104a70889f3152a074 (patch) | |
| tree | e45224077e2002052fc4ab3d1262e7d49c2e9356 /lib/graph.c | |
| parent | 52483fa6ff0957032f73c6b6c4aa3402476a5b90 (diff) | |
lib: add graph_find_node
Allows finding a graph node by its data pointer.
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/graph.c')
| -rw-r--r-- | lib/graph.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/lib/graph.c b/lib/graph.c index 945a58e688..a9cc43f7c1 100644 --- a/lib/graph.c +++ b/lib/graph.c @@ -34,6 +34,15 @@ struct graph *graph_new() return graph; } +void graph_delete_graph(struct graph *graph) +{ + for (unsigned int i = vector_active(graph->nodes); i--; /**/) + graph_delete_node(graph, vector_slot(graph->nodes, i)); + + vector_free(graph->nodes); + XFREE(MTYPE_GRAPH, graph); +} + struct graph_node *graph_new_node(struct graph *graph, void *data, void (*del)(void *)) { @@ -127,12 +136,24 @@ void graph_remove_edge(struct graph_node *from, struct graph_node *to) } } -void graph_delete_graph(struct graph *graph) +struct graph_node *graph_find_node(struct graph *graph, void *data) { - // delete each node in the graph - for (unsigned int i = vector_active(graph->nodes); i--; /**/) - graph_delete_node(graph, vector_slot(graph->nodes, i)); + struct graph_node *g; - vector_free(graph->nodes); - XFREE(MTYPE_GRAPH, graph); + for (unsigned int i = vector_active(graph->nodes); i--; /**/) { + g = vector_slot(graph->nodes, i); + if (g->data == data) + return g; + } + + return NULL; +} + +bool graph_has_edge(struct graph_node *from, struct graph_node *to) +{ + for (unsigned int i = vector_active(from->to); i--; /**/) + if (vector_slot(from->to, i) == to) + return true; + + return false; } |
