2 edge.c -- edge management
4 Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5 2003-2004 Ivo Timmermans <ivo@tinc-vpn.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "support/avl.h"
29 #include "support/xalloc.h"
33 static int edge_compare(const edge_t *a, const edge_t *b) {
34 return strcmp(a->to->name, b->to->name);
37 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
38 return (a->weight - b->weight) ?: strcmp(a->from->name, b->from->name) ?: strcmp(a->to->name, b->to->name);
41 bool edge_init(void) {
42 edges = avl_tree_new((avl_compare_t)edge_weight_compare, NULL);
47 bool edge_exit(void) {
53 avl_tree_t *edge_tree_new(void) {
54 return avl_tree_new((avl_compare_t)edge_compare, (avl_action_t)edge_free);
57 void edge_tree_free(avl_tree_t *edge_tree) {
58 avl_tree_free(edge_tree);
61 edge_t *edge_new(void) {
64 return clear(new(edge));
67 void edge_free(edge_t *edge) {
71 void edge_add(edge_t *edge) {
72 avl_add(edge->from->edges, edge);
75 edge->reverse = edge_get(edge->to, edge->from);
78 edge->reverse->reverse = edge;
81 void edge_del(edge_t *edge) {
83 edge->reverse->reverse = NULL;
86 avl_del(edge->from->edges, edge);
89 edge_t *edge_get(node_t *from, node_t *to) {
95 return avl_get(from->edges, &search);