]>
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 | ||
27 | #include "vector.h" | |
28 | ||
d62a17ae | 29 | struct graph { |
30 | vector nodes; | |
1eb5e8dc | 31 | }; |
16d7c050 | 32 | |
d62a17ae | 33 | struct graph_node { |
34 | vector from; // nodes which have edges to this node | |
35 | vector to; // nodes which this node has edges to | |
16d7c050 | 36 | |
d62a17ae | 37 | void *data; // node data |
38 | void (*del)(void *data); // deletion callback | |
16d7c050 QY |
39 | }; |
40 | ||
d62a17ae | 41 | struct graph *graph_new(void); |
1eb5e8dc | 42 | |
16d7c050 QY |
43 | /** |
44 | * Creates a new node. | |
45 | * | |
46 | * @struct graph the graph this node exists in | |
47 | * @param[in] data this node's data | |
48 | * @param[in] del data deletion callback | |
49 | * @return the new node | |
50 | */ | |
d62a17ae | 51 | struct graph_node *graph_new_node(struct graph *graph, void *data, |
52 | void (*del)(void *)); | |
16d7c050 QY |
53 | |
54 | /** | |
55 | * Deletes a node. | |
56 | * | |
57 | * Before deletion, this function removes all edges to and from this node from | |
58 | * any neighbor nodes. | |
59 | * | |
16d7c050 QY |
60 | * If *data and *del are non-null, the following call is made: |
61 | * (*node->del) (node->data); | |
62 | * | |
ba06a3a0 | 63 | * @param[in] graph the graph this node belongs to |
16d7c050 QY |
64 | * @param[out] node pointer to node to delete |
65 | */ | |
d62a17ae | 66 | void graph_delete_node(struct graph *graph, struct graph_node *node); |
16d7c050 QY |
67 | |
68 | /** | |
69 | * Makes a directed edge between two nodes. | |
70 | * | |
71 | * @param[in] from | |
72 | * @param[in] to | |
73 | * @return to | |
74 | */ | |
d62a17ae | 75 | struct graph_node *graph_add_edge(struct graph_node *from, |
76 | struct graph_node *to); | |
16d7c050 QY |
77 | |
78 | /** | |
ba06a3a0 | 79 | * Removes a directed edge between two nodes. |
16d7c050 | 80 | * |
ba06a3a0 QY |
81 | * @param[in] from |
82 | * @param[in] to | |
83 | */ | |
d62a17ae | 84 | void graph_remove_edge(struct graph_node *from, struct graph_node *to); |
ba06a3a0 QY |
85 | |
86 | /** | |
87 | * Deletes a graph. | |
16d7c050 QY |
88 | * Calls graph_delete_node on each node before freeing the graph struct itself. |
89 | * | |
90 | * @param graph the graph to delete | |
91 | */ | |
d62a17ae | 92 | void graph_delete_graph(struct graph *graph); |
16d7c050 QY |
93 | |
94 | #endif /* _ZEBRA_COMMAND_GRAPH_H */ |