]>
git.proxmox.com Git - mirror_frr.git/blob - lib/graph.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Graph data structure.
6 * Copyright (C) 2016 Cumulus Networks, Inc.
9 #ifndef _ZEBRA_COMMAND_GRAPH_H
10 #define _ZEBRA_COMMAND_GRAPH_H
25 vector from
; // nodes which have edges to this node
26 vector to
; // nodes which this node has edges to
28 void *data
; // node data
29 void (*del
)(void *data
); // deletion callback
32 struct graph
*graph_new(void);
37 * @struct graph the graph this node exists in
38 * @param[in] data this node's data
39 * @param[in] del data deletion callback
40 * @return the new node
42 struct graph_node
*graph_new_node(struct graph
*graph
, void *data
,
48 * Before deletion, this function removes all edges to and from this node from
51 * If *data and *del are non-null, the following call is made:
52 * (*node->del) (node->data);
54 * @param[in] graph the graph this node belongs to
55 * @param[out] node pointer to node to delete
57 void graph_delete_node(struct graph
*graph
, struct graph_node
*node
);
60 * Makes a directed edge between two nodes.
66 struct graph_node
*graph_add_edge(struct graph_node
*from
,
67 struct graph_node
*to
);
70 * Removes a directed edge between two nodes.
75 void graph_remove_edge(struct graph_node
*from
, struct graph_node
*to
);
79 * Calls graph_delete_node on each node before freeing the graph struct itself.
81 * @param graph the graph to delete
83 void graph_delete_graph(struct graph
*graph
);
86 * Finds a node in the graph.
88 * @param[in] graph the graph to search in
89 * @param[in] data the data to key off
90 * @return the first graph node whose data pointer matches `data`
92 struct graph_node
*graph_find_node(struct graph
*graph
, void *data
);
96 * Determines whether two nodes have a directed edge between them.
100 * @return whether there is a directed edge from `from` to `to`.
102 bool graph_has_edge(struct graph_node
*from
, struct graph_node
*to
);
105 * Depth-first search.
107 * Performs a depth-first traversal of the given graph, visiting each node
108 * exactly once and calling the user-provided callback for each visit.
110 * @param graph the graph to operate on
111 * @param start the node to take as the root
112 * @param dfs_cb callback called for each node visited in the traversal
113 * @param arg argument to provide to dfs_cb
115 void graph_dfs(struct graph
*graph
, struct graph_node
*start
,
116 void (*dfs_cb
)(struct graph_node
*, void *), void *arg
);
118 #ifndef BUILDING_CLIPPY
120 * Clippy relies on a small subset of sources in lib/, but it cannot link
121 * libfrr since clippy itself is required to build libfrr. Instead it directly
122 * includes the sources it needs. One of these is the command graph
123 * implementation, which wraps this graph implementation. Since we need to use
124 * the buffer.[ch] sources here, which indirectly rely on most of libfrr, we
125 * have to ignore them when compiling clippy to avoid build dependency issues.
127 * TODO: Fix clippy build.
131 * Default node printer for use with graph_dump_dot.
133 * @param gn the node to print
134 * @param buf the buffer to print into
136 void graph_dump_dot_default_print_cb(struct graph_node
*gn
, struct buffer
*buf
);
139 * Prints a graph in the DOT language.
141 * The generated output is produced from a depth-first traversal of the graph.
143 * @param graph the graph to print
144 * @param start the node to take as the root
145 * @param pcb callback called for each node in the traversal that should
146 * print the node in the DOT language. Passing NULL for this argument
147 * will use the default printer. See graph_dump_dot_default_print_cb for
149 * @return representation of graph in DOT language, allocated with MTYPE_TMP.
150 * Caller is responsible for freeing this string.
152 char *graph_dump_dot(struct graph
*graph
, struct graph_node
*start
,
153 void (*pcb
)(struct graph_node
*, struct buffer
*buf
));
155 #endif /* BUILDING_CLIPPY */
161 #endif /* _ZEBRA_COMMAND_GRAPH_H */