2 edge.c -- edge tree management
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4 2000-2002 Ivo Timmermans <ivo@o2w.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: edge.c,v 1.1.2.13 2002/09/06 10:23:52 guus Exp $
32 #include "net.h" /* Don't ask. */
44 avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
46 int edge_compare(edge_t *a, edge_t *b)
48 return strcmp(a->to->name, b->to->name);
51 int edge_weight_compare(edge_t *a, edge_t *b)
55 result = a->weight - b->weight;
60 return edge_compare(a, b);
66 edge_weight_tree = avl_alloc_tree((avl_compare_t)edge_weight_compare, NULL);
70 avl_tree_t *new_edge_tree(void)
73 return avl_alloc_tree((avl_compare_t)edge_compare, NULL);
77 void free_edge_tree(avl_tree_t *edge_tree)
80 avl_delete_tree(edge_tree);
87 avl_delete_tree(edge_weight_tree);
91 /* Creation and deletion of connection elements */
93 edge_t *new_edge(void)
97 e = (edge_t *)xmalloc_and_zero(sizeof(*e));
102 void free_edge(edge_t *e)
109 void edge_add(edge_t *e)
112 avl_insert(edge_weight_tree, e);
113 avl_insert(e->from->edge_tree, e);
115 e->reverse = lookup_edge(e->to, e->from);
117 e->reverse->reverse = e;
121 void edge_del(edge_t *e)
125 e->reverse->reverse = NULL;
127 avl_delete(edge_weight_tree, e);
128 avl_delete(e->from->edge_tree, e);
132 edge_t *lookup_edge(node_t *from, node_t *to)
139 return avl_search(from->edge_tree, &v);
142 void dump_edges(void)
144 avl_node_t *node, *node2;
149 syslog(LOG_DEBUG, _("Edges:"));
151 for(node = node_tree->head; node; node = node->next)
153 n = (node_t *)node->data;
154 for(node2 = n->edge_tree->head; node2; node2 = node2->next)
156 e = (edge_t *)node2->data;
157 address = sockaddr2hostname(&e->address);
158 syslog(LOG_DEBUG, _(" %s to %s at %s options %lx weight %d"),
159 e->from->name, e->to->name, address,
160 e->options, e->weight);
165 syslog(LOG_DEBUG, _("End of edges."));