]>
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 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
21 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
22 | * 02111-1307, USA. | |
23 | */ | |
24 | ||
25 | #ifndef _ZEBRA_COMMAND_GRAPH_H | |
26 | #define _ZEBRA_COMMAND_GRAPH_H | |
27 | ||
28 | #include "vector.h" | |
29 | ||
16d7c050 QY |
30 | struct graph |
31 | { | |
1eb5e8dc QY |
32 | vector nodes; |
33 | }; | |
16d7c050 | 34 | |
16d7c050 QY |
35 | struct graph_node |
36 | { | |
37 | vector from; // nodes which have edges to this node | |
38 | vector to; // nodes which this node has edges to | |
39 | ||
40 | void *data; // node data | |
1eb5e8dc | 41 | void (*del) (void *data); // deletion callback |
16d7c050 QY |
42 | }; |
43 | ||
1eb5e8dc QY |
44 | struct graph * |
45 | graph_new (void); | |
46 | ||
16d7c050 QY |
47 | /** |
48 | * Creates a new node. | |
49 | * | |
50 | * @struct graph the graph this node exists in | |
51 | * @param[in] data this node's data | |
52 | * @param[in] del data deletion callback | |
53 | * @return the new node | |
54 | */ | |
55 | struct graph_node * | |
56 | graph_new_node (struct graph *graph, void *data, void (*del) (void*)); | |
57 | ||
58 | /** | |
59 | * Deletes a node. | |
60 | * | |
61 | * Before deletion, this function removes all edges to and from this node from | |
62 | * any neighbor nodes. | |
63 | * | |
16d7c050 QY |
64 | * If *data and *del are non-null, the following call is made: |
65 | * (*node->del) (node->data); | |
66 | * | |
ba06a3a0 | 67 | * @param[in] graph the graph this node belongs to |
16d7c050 QY |
68 | * @param[out] node pointer to node to delete |
69 | */ | |
70 | void | |
71 | graph_delete_node (struct graph *graph, struct graph_node *node); | |
72 | ||
73 | /** | |
74 | * Makes a directed edge between two nodes. | |
75 | * | |
76 | * @param[in] from | |
77 | * @param[in] to | |
78 | * @return to | |
79 | */ | |
80 | struct graph_node * | |
81 | graph_add_edge (struct graph_node *from, struct graph_node *to); | |
82 | ||
83 | /** | |
ba06a3a0 | 84 | * Removes a directed edge between two nodes. |
16d7c050 | 85 | * |
ba06a3a0 QY |
86 | * @param[in] from |
87 | * @param[in] to | |
88 | */ | |
89 | void | |
90 | graph_remove_edge (struct graph_node *from, struct graph_node *to); | |
91 | ||
92 | /** | |
93 | * Deletes a graph. | |
16d7c050 QY |
94 | * Calls graph_delete_node on each node before freeing the graph struct itself. |
95 | * | |
96 | * @param graph the graph to delete | |
97 | */ | |
98 | void | |
99 | graph_delete_graph (struct graph *graph); | |
100 | ||
101 | #endif /* _ZEBRA_COMMAND_GRAPH_H */ |