1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <ccan/tlist/tlist.h>
6 #include <ccan/typesafe_cb/typesafe_cb.h>
14 /* strust tlist_dgraph_edge: a list of edges */
15 TLIST_TYPE(dgraph_edge, struct dgraph_edge);
18 * struct dgraph_node - a node in a directed graph.
19 * @edge: edges indexed by enum dgraph_dir.
21 * edge[DGRAPH_FROM] edges have n[DGRAPH_FROM] == this.
22 * edge[DGRAPH_TO] edges have n[DGRAPH_TO] == this.
25 struct tlist_dgraph_edge edge[2];
29 * struct dgraph_edge - an edge in a directed graph.
30 * @list: our nodes's list of edges, indexed by enum dgraph_dir.
31 * @n: the nodes, indexed by enum dgraph_dir.
34 struct list_node list[2];
35 struct dgraph_node *n[2];
38 void dgraph_init_node(struct dgraph_node *n);
39 void dgraph_clear_node(struct dgraph_node *n);
40 void dgraph_add_edge(struct dgraph_node *from, struct dgraph_node *to);
41 bool dgraph_del_edge(struct dgraph_node *from, struct dgraph_node *to);
43 #ifdef CCAN_DGRAPH_DEBUG
44 #define dgraph_debug(n) dgraph_check((n), __func__)
46 #define dgraph_debug(n) (n)
49 /* You can dgraph_clear_node() the node you're on safely. */
50 #define dgraph_traverse_from(n, fn, arg) \
51 dgraph_traverse(dgraph_debug(n), DGRAPH_FROM, \
52 typesafe_cb_preargs(bool, void *, (fn), (arg), \
53 struct dgraph_node *), \
56 #define dgraph_traverse_to(n, fn, arg) \
57 dgraph_traverse(dgraph_debug(n), DGRAPH_TO, \
58 typesafe_cb_preargs(bool, void *, (fn), (arg), \
59 struct dgraph_node *), \
62 void dgraph_traverse(const struct dgraph_node *n,
64 bool (*fn)(struct dgraph_node *from, void *data),
67 #define dgraph_for_each_edge(n, i, dir) \
68 tlist_for_each(&dgraph_debug(n)->edge[dir], i, list[dir])
70 #define dgraph_for_each_edge_safe(n, i, next, dir) \
71 tlist_for_each_safe(&dgraph_debug(n)->edge[dir], i, next, list[dir])
74 * dgraph_check - check a graph for consistency
75 * @n: the dgraph_node to check.
76 * @abortstr: the string to print on aborting, or NULL.
78 * Returns @n if the list is consistent, NULL if not (it can never
79 * return NULL if @abortstr is set).
81 * See also: tlist_check()
83 struct dgraph_node *dgraph_check(const struct dgraph_node *n,
84 const char *abortstr);
85 #endif /* CCAN_DGRAPH_H */