]>
git.proxmox.com Git - mirror_frr.git/blob - lib/graph.h
2 * Graph data structure.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
7 * This file is part of GNU Zebra.
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifndef _ZEBRA_COMMAND_GRAPH_H
25 #define _ZEBRA_COMMAND_GRAPH_H
40 vector from
; // nodes which have edges to this node
41 vector to
; // nodes which this node has edges to
43 void *data
; // node data
44 void (*del
)(void *data
); // deletion callback
47 struct graph
*graph_new(void);
52 * @struct graph the graph this node exists in
53 * @param[in] data this node's data
54 * @param[in] del data deletion callback
55 * @return the new node
57 struct graph_node
*graph_new_node(struct graph
*graph
, void *data
,
63 * Before deletion, this function removes all edges to and from this node from
66 * If *data and *del are non-null, the following call is made:
67 * (*node->del) (node->data);
69 * @param[in] graph the graph this node belongs to
70 * @param[out] node pointer to node to delete
72 void graph_delete_node(struct graph
*graph
, struct graph_node
*node
);
75 * Makes a directed edge between two nodes.
81 struct graph_node
*graph_add_edge(struct graph_node
*from
,
82 struct graph_node
*to
);
85 * Removes a directed edge between two nodes.
90 void graph_remove_edge(struct graph_node
*from
, struct graph_node
*to
);
94 * Calls graph_delete_node on each node before freeing the graph struct itself.
96 * @param graph the graph to delete
98 void graph_delete_graph(struct graph
*graph
);
101 * Finds a node in the graph.
103 * @param[in] graph the graph to search in
104 * @param[in] data the data to key off
105 * @return the first graph node whose data pointer matches `data`
107 struct graph_node
*graph_find_node(struct graph
*graph
, void *data
);
111 * Determines whether two nodes have a directed edge between them.
115 * @return whether there is a directed edge from `from` to `to`.
117 bool graph_has_edge(struct graph_node
*from
, struct graph_node
*to
);
120 * Depth-first search.
122 * Performs a depth-first traversal of the given graph, visiting each node
123 * exactly once and calling the user-provided callback for each visit.
125 * @param graph the graph to operate on
126 * @param start the node to take as the root
127 * @param dfs_cb callback called for each node visited in the traversal
128 * @param arg argument to provide to dfs_cb
130 void graph_dfs(struct graph
*graph
, struct graph_node
*start
,
131 void (*dfs_cb
)(struct graph_node
*, void *), void *arg
);
133 #ifndef BUILDING_CLIPPY
135 * Clippy relies on a small subset of sources in lib/, but it cannot link
136 * libfrr since clippy itself is required to build libfrr. Instead it directly
137 * includes the sources it needs. One of these is the command graph
138 * implementation, which wraps this graph implementation. Since we need to use
139 * the buffer.[ch] sources here, which indirectly rely on most of libfrr, we
140 * have to ignore them when compiling clippy to avoid build dependency issues.
142 * TODO: Fix clippy build.
146 * Default node printer for use with graph_dump_dot.
148 * @param gn the node to print
149 * @param buf the buffer to print into
151 void graph_dump_dot_default_print_cb(struct graph_node
*gn
, struct buffer
*buf
);
154 * Prints a graph in the DOT language.
156 * The generated output is produced from a depth-first traversal of the graph.
158 * @param graph the graph to print
159 * @param start the node to take as the root
160 * @param pcb callback called for each node in the traversal that should
161 * print the node in the DOT language. Passing NULL for this argument
162 * will use the default printer. See graph_dump_dot_default_print_cb for
164 * @return representation of graph in DOT language, allocated with MTYPE_TMP.
165 * Caller is responsible for freeing this string.
167 char *graph_dump_dot(struct graph
*graph
, struct graph_node
*start
,
168 void (*pcb
)(struct graph_node
*, struct buffer
*buf
));
170 #endif /* BUILDING_CLIPPY */
176 #endif /* _ZEBRA_COMMAND_GRAPH_H */