]>
git.proxmox.com Git - mirror_frr.git/blob - lib/graph.c
2 * Graph data structure.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
7 * This file is part of GNU Zebra.
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
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.
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
27 DEFINE_MTYPE_STATIC(LIB
, GRAPH
, "Graph")
28 DEFINE_MTYPE_STATIC(LIB
, GRAPH_NODE
, "Graph Node")
32 struct graph
*graph
= XCALLOC (MTYPE_GRAPH
, sizeof(struct graph
));
33 graph
->nodes
= vector_init (VECTOR_MIN_SIZE
);
39 graph_new_node (struct graph
*graph
, void *data
, void (*del
) (void*))
41 struct graph_node
*node
=
42 XCALLOC(MTYPE_GRAPH_NODE
, sizeof(struct graph_node
));
44 node
->from
= vector_init (VECTOR_MIN_SIZE
);
45 node
->to
= vector_init (VECTOR_MIN_SIZE
);
49 vector_set (graph
->nodes
, node
);
55 vector_remove (vector v
, unsigned int ix
)
60 /* v->active is guaranteed >= 1 because ix can't be lower than 0
61 * and v->active is > ix. */
63 /* if ix == v->active--, we set the item to itself, then to NULL...
64 * still correct, no check neccessary. */
65 v
->index
[ix
] = v
->index
[v
->active
];
66 v
->index
[v
->active
] = NULL
;
70 graph_delete_node (struct graph
*graph
, struct graph_node
*node
)
75 struct graph_node
*adj
;
77 // remove all edges from other nodes to us
78 for (unsigned int i
= vector_active (node
->from
); i
--; /**/)
80 adj
= vector_slot (node
->from
, i
);
81 graph_remove_edge (adj
, node
);
84 // remove all edges from us to other nodes
85 for (unsigned int i
= vector_active (node
->to
); i
--; /**/)
87 adj
= vector_slot (node
->to
, i
);
88 graph_remove_edge (node
, adj
);
91 // if there is a deletion callback, call it
92 if (node
->del
&& node
->data
)
93 (*node
->del
) (node
->data
);
95 // free adjacency lists
96 vector_free (node
->to
);
97 vector_free (node
->from
);
99 // remove node from graph->nodes
100 for (unsigned int i
= vector_active (graph
->nodes
); i
--; /**/)
101 if (vector_slot (graph
->nodes
, i
) == node
)
103 vector_remove (graph
->nodes
, i
);
107 // free the node itself
108 XFREE (MTYPE_GRAPH_NODE
, node
);
112 graph_add_edge (struct graph_node
*from
, struct graph_node
*to
)
114 vector_set (from
->to
, to
);
115 vector_set (to
->from
, from
);
120 graph_remove_edge (struct graph_node
*from
, struct graph_node
*to
)
122 // remove from from to->from
123 for (unsigned int i
= vector_active (to
->from
); i
--; /**/)
124 if (vector_slot (to
->from
, i
) == from
)
126 vector_remove (to
->from
, i
);
129 // remove to from from->to
130 for (unsigned int i
= vector_active (from
->to
); i
--; /**/)
131 if (vector_slot (from
->to
, i
) == to
)
133 vector_remove (from
->to
, i
);
139 graph_delete_graph (struct graph
*graph
)
141 // delete each node in the graph
142 for (unsigned int i
= vector_active (graph
->nodes
); i
--; /**/)
143 graph_delete_node (graph
, vector_slot (graph
->nodes
, i
));
145 vector_free (graph
->nodes
);
146 XFREE (MTYPE_GRAPH
, graph
);