summaryrefslogtreecommitdiffstats
path: root/lib/graph.h
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2016-09-01 23:27:33 +0200
committerQuentin Young <qlyoung@cumulusnetworks.com>2016-09-01 23:30:17 +0200
commit16d7c05093f0f3a24d09f9780afc1ea114e73f94 (patch)
treef6a88a5e089c841e4adfc5e0d98d416fece72424 /lib/graph.h
parentlib: Refactor CLI interface function names (diff)
downloadfrr-16d7c05093f0f3a24d09f9780afc1ea114e73f94.tar.xz
frr-16d7c05093f0f3a24d09f9780afc1ea114e73f94.zip
lib: Generalize graph to work for any data type
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/graph.h')
-rw-r--r--lib/graph.h99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/graph.h b/lib/graph.h
new file mode 100644
index 000000000..7c0f84800
--- /dev/null
+++ b/lib/graph.h
@@ -0,0 +1,99 @@
+/*
+ * Graph data structure.
+ *
+ * --
+ * Copyright (C) 2016 Cumulus Networks, Inc.
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_COMMAND_GRAPH_H
+#define _ZEBRA_COMMAND_GRAPH_H
+
+#include "vector.h"
+
+/**
+ * Graph structure.
+ */
+struct graph
+{
+ vector nodes; // all nodes in the graph
+}
+
+/**
+ * Graph node / vertex.
+ */
+struct graph_node
+{
+ vector from; // nodes which have edges to this node
+ vector to; // nodes which this node has edges to
+
+ void *data; // node data
+ void (*del) (void *data) // deletion callback
+};
+
+/**
+ * Creates a new node.
+ *
+ * @struct graph the graph this node exists in
+ * @param[in] data this node's data
+ * @param[in] del data deletion callback
+ * @return the new node
+ */
+struct graph_node *
+graph_new_node (struct graph *graph, void *data, void (*del) (void*));
+
+/**
+ * Deletes a node.
+ *
+ * Before deletion, this function removes all edges to and from this node from
+ * any neighbor nodes.
+ *
+ * If, as a result of this operation, any neighbor node has no edges either to
+ * or from itself, that node will be deleted as well. If the graph topology is
+ * e.g. a star this will result in the deletion of all nodes.
+ *
+ * If *data and *del are non-null, the following call is made:
+ * (*node->del) (node->data);
+ *
+ * @param[out] node pointer to node to delete
+ */
+void
+graph_delete_node (struct graph *graph, struct graph_node *node);
+
+/**
+ * Makes a directed edge between two nodes.
+ *
+ * @param[in] from
+ * @param[in] to
+ * @return to
+ */
+struct graph_node *
+graph_add_edge (struct graph_node *from, struct graph_node *to);
+
+/**
+ * Deletes a graph.
+ *
+ * Calls graph_delete_node on each node before freeing the graph struct itself.
+ *
+ * @param graph the graph to delete
+ */
+void
+graph_delete_graph (struct graph *graph);
+
+#endif /* _ZEBRA_COMMAND_GRAPH_H */