2 edge.c -- edge tree management
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "splay_tree.h"
25 #include "meshlink_internal.h"
31 static int edge_compare(const edge_t *a, const edge_t *b) {
32 return strcmp(a->to->name, b->to->name);
35 static int edge_weight_compare(const edge_t *a, const edge_t *b) {
38 result = a->weight - b->weight;
44 result = strcmp(a->from->name, b->from->name);
50 return strcmp(a->to->name, b->to->name);
53 void init_edges(meshlink_handle_t *mesh) {
54 mesh->edges = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL);
57 splay_tree_t *new_edge_tree(void) {
58 return splay_alloc_tree((splay_compare_t) edge_compare, (splay_action_t) free_edge);
61 void free_edge_tree(splay_tree_t *edge_tree) {
62 splay_delete_tree(edge_tree);
65 void exit_edges(meshlink_handle_t *mesh) {
67 splay_delete_tree(mesh->edges);
73 /* Creation and deletion of connection elements */
75 edge_t *new_edge(void) {
76 return xzalloc(sizeof(edge_t));
79 void free_edge(edge_t *e) {
80 sockaddrfree(&e->address);
85 void edge_add(meshlink_handle_t *mesh, edge_t *e) {
86 splay_insert(mesh->edges, e);
87 splay_insert(e->from->edge_tree, e);
89 e->reverse = lookup_edge(e->to, e->from);
92 e->reverse->reverse = e;
96 void edge_del(meshlink_handle_t *mesh, edge_t *e) {
98 e->reverse->reverse = NULL;
101 splay_delete(mesh->edges, e);
102 splay_delete(e->from->edge_tree, e);
105 edge_t *lookup_edge(node_t *from, node_t *to) {
111 return splay_search(from->edge_tree, &v);