]>
Commit | Line | Data |
---|---|---|
16d7c050 QY |
1 | /* |
2 | * Graph data structure. | |
3 | * | |
4 | * -- | |
5 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
6 | * | |
7 | * This file is part of GNU Zebra. | |
8 | * | |
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 | |
12 | * later version. | |
13 | * | |
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. | |
18 | * | |
896014f4 DL |
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 | |
16d7c050 QY |
22 | */ |
23 | ||
24 | #ifndef _ZEBRA_COMMAND_GRAPH_H | |
25 | #define _ZEBRA_COMMAND_GRAPH_H | |
26 | ||
9428e089 | 27 | #include <stdbool.h> |
16d7c050 QY |
28 | #include "vector.h" |
29 | ||
d62a17ae | 30 | struct graph { |
31 | vector nodes; | |
1eb5e8dc | 32 | }; |
16d7c050 | 33 | |
d62a17ae | 34 | struct graph_node { |
35 | vector from; // nodes which have edges to this node | |
36 | vector to; // nodes which this node has edges to | |
16d7c050 | 37 | |
d62a17ae | 38 | void *data; // node data |
39 | void (*del)(void *data); // deletion callback | |
16d7c050 QY |
40 | }; |
41 | ||
d62a17ae | 42 | struct graph *graph_new(void); |
1eb5e8dc | 43 | |
16d7c050 QY |
44 | /** |
45 | * Creates a new node. | |
46 | * | |
47 | * @struct graph the graph this node exists in | |
48 | * @param[in] data this node's data | |
49 | * @param[in] del data deletion callback | |
50 | * @return the new node | |
51 | */ | |
d62a17ae | 52 | struct graph_node *graph_new_node(struct graph *graph, void *data, |
53 | void (*del)(void *)); | |
16d7c050 QY |
54 | |
55 | /** | |
56 | * Deletes a node. | |
57 | * | |
58 | * Before deletion, this function removes all edges to and from this node from | |
59 | * any neighbor nodes. | |
60 | * | |
16d7c050 QY |
61 | * If *data and *del are non-null, the following call is made: |
62 | * (*node->del) (node->data); | |
63 | * | |
ba06a3a0 | 64 | * @param[in] graph the graph this node belongs to |
16d7c050 QY |
65 | * @param[out] node pointer to node to delete |
66 | */ | |
d62a17ae | 67 | void graph_delete_node(struct graph *graph, struct graph_node *node); |
16d7c050 QY |
68 | |
69 | /** | |
70 | * Makes a directed edge between two nodes. | |
71 | * | |
72 | * @param[in] from | |
73 | * @param[in] to | |
74 | * @return to | |
75 | */ | |
d62a17ae | 76 | struct graph_node *graph_add_edge(struct graph_node *from, |
77 | struct graph_node *to); | |
16d7c050 QY |
78 | |
79 | /** | |
ba06a3a0 | 80 | * Removes a directed edge between two nodes. |
16d7c050 | 81 | * |
ba06a3a0 QY |
82 | * @param[in] from |
83 | * @param[in] to | |
84 | */ | |
d62a17ae | 85 | void graph_remove_edge(struct graph_node *from, struct graph_node *to); |
ba06a3a0 QY |
86 | |
87 | /** | |
88 | * Deletes a graph. | |
16d7c050 QY |
89 | * Calls graph_delete_node on each node before freeing the graph struct itself. |
90 | * | |
91 | * @param graph the graph to delete | |
92 | */ | |
d62a17ae | 93 | void graph_delete_graph(struct graph *graph); |
16d7c050 | 94 | |
9428e089 QY |
95 | /* |
96 | * Finds a node in the graph. | |
97 | * | |
98 | * @param[in] graph the graph to search in | |
99 | * @param[in] data the data to key off | |
100 | * @return the first graph node whose data pointer matches `data` | |
101 | */ | |
102 | struct graph_node *graph_find_node(struct graph *graph, void *data); | |
103 | ||
104 | ||
105 | /* | |
106 | * Determines whether two nodes have a directed edge between them. | |
107 | * | |
108 | * @param from | |
109 | * @param to | |
110 | * @return whether there is a directed edge from `from` to `to`. | |
111 | */ | |
112 | bool graph_has_edge(struct graph_node *from, struct graph_node *to); | |
113 | ||
16d7c050 | 114 | #endif /* _ZEBRA_COMMAND_GRAPH_H */ |