- Added balanced tree management stuff as well. (It is not finished yet.)
[tinc] / lib / rbl.c
1 /*
2     rbl.c -- red-black tree + linked list convenience
3     Copyright (C) 2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000 Guus Sliepen <guus@sliepen.warande.net>
5
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.
10
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.
15
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.
19
20     $Id: rbl.c,v 1.1.2.1 2000/11/16 09:18:38 guus Exp $
21 */
22
23 rbl_t *new_rbl(rbltree_t *tree)
24 {
25   rbl_t *rbl;
26
27   rbl = xmalloc(sizeof(*rbl));
28
29   if(rbl)
30     {
31       memset(rbl, 0, sizeof(*rbl));
32       rbl->tree = tree;
33     }
34     
35   return rbl;
36 }
37
38 void free_rbl(rbl_t *rbl)
39 {
40   free(rbl);
41 }
42
43 rbl_t rbl_search_closest(rbltree_t *tree, void *data)
44 {
45   rbl_t *rbl, *next;
46   int result;
47   
48   for(next = rbltree->head; next; next = rbl)
49     {
50       result = rbltree->compare(rbl->data, data)
51       if(result < 0)
52         next = rbl->left;
53       else if(result > 0)
54         next = rbl->right;
55       else
56         break;
57     }
58     
59   return rbl;
60 }
61
62 rbl_t rbl_search(rbltree_t *tree, void *data)
63 {
64   rbl_t *rbl, *next;
65   int result;
66   
67   for(next = rbltree->head; next; next = rbl)
68     {
69       result = rbltree->compare(rbl->data, data)
70       if(result < 0)
71         next = rbl->left;
72       else if(result > 0)
73         next = rbl->right;
74       else
75         return rbl;
76     }
77     
78   return NULL;
79 }
80
81 rbl_t rbl_insert(rbltree_t *tree, void *data)
82 {
83   rbl_t *rbl;
84   
85 }