]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add graph_find_node 2038/head
authorQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 6 Apr 2018 21:58:00 +0000 (17:58 -0400)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 6 Apr 2018 21:59:39 +0000 (17:59 -0400)
Allows finding a graph node by its data pointer.

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

index 945a58e68850bfd2086c4a2362469629cdff8b7f..a9cc43f7c1e5d4933256c32d1c3acbb33ea44ff6 100644 (file)
@@ -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;
 }
index 10ee00feda3ec1e452f3c0c35fa282fb0eec2313..d6dfef5a636a65be815ca09328b25d3dbee478ac 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef _ZEBRA_COMMAND_GRAPH_H
 #define _ZEBRA_COMMAND_GRAPH_H
 
+#include <stdbool.h>
 #include "vector.h"
 
 struct graph {
@@ -91,4 +92,23 @@ void graph_remove_edge(struct graph_node *from, struct graph_node *to);
  */
 void graph_delete_graph(struct graph *graph);
 
+/*
+ * Finds a node in the graph.
+ *
+ * @param[in] graph the graph to search in
+ * @param[in] data the data to key off
+ * @return the first graph node whose data pointer matches `data`
+ */
+struct graph_node *graph_find_node(struct graph *graph, void *data);
+
+
+/*
+ * Determines whether two nodes have a directed edge between them.
+ *
+ * @param from
+ * @param to
+ * @return whether there is a directed edge from `from` to `to`.
+ */
+bool graph_has_edge(struct graph_node *from, struct graph_node *to);
+
 #endif /* _ZEBRA_COMMAND_GRAPH_H */