annotate src/redblack.c @ 128:3a7d8d2f0585

modify configure for -lrt
author Naoya OYAMA <naoya.oyama@gmail.com>
date Tue, 05 Oct 2010 01:55:54 +0900
parents e413158cae13
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
125
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1 static char rcsid[]="$Id: redblack.c,v 1.9 2003/10/24 01:31:21 damo Exp $";
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
2
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
3 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
4 Redblack balanced tree algorithm
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
5 Copyright (C) Damian Ivereigh 2000
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
6
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
8 it under the terms of the GNU Lesser General Public License as published by
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
9 the Free Software Foundation; either version 2.1 of the License, or
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
10 (at your option) any later version. See the file COPYING for details.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
11
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
15 GNU General Public License for more details.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
16
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
17 You should have received a copy of the GNU Lesser General Public License
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
18 along with this program; if not, write to the Free Software
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
20 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
21
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
22 /* Implement the red/black tree structure. It is designed to emulate
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
23 ** the standard tsearch() stuff. i.e. the calling conventions are
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
24 ** exactly the same
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
25 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
26
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
27 #include <stddef.h>
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
28 #include <stdlib.h>
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
29 #include <unistd.h>
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
30 #include "redblack.h"
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
31
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
32 #define assert(expr)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
33
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
34 /* Uncomment this if you would rather use a raw sbrk to get memory
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
35 ** (however the memory is never released again (only re-used). Can't
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
36 ** see any point in using this these days.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
37 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
38 /* #define USE_SBRK */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
39
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
40 enum nodecolour { BLACK, RED };
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
41
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
42 struct RB_ENTRY(node)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
43 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
44 struct RB_ENTRY(node) *left; /* Left down */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
45 struct RB_ENTRY(node) *right; /* Right down */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
46 struct RB_ENTRY(node) *up; /* Up */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
47 enum nodecolour colour; /* Node colour */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
48 #ifdef RB_INLINE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
49 RB_ENTRY(data_t) key; /* User's key (and data) */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
50 #define RB_GET(x,y) &x->y
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
51 #define RB_SET(x,y,v) x->y = *(v)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
52 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
53 const RB_ENTRY(data_t) *key; /* Pointer to user's key (and data) */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
54 #define RB_GET(x,y) x->y
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
55 #define RB_SET(x,y,v) x->y = v
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
56 #endif /* RB_INLINE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
57 };
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
58
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
59 /* Dummy (sentinel) node, so that we can make X->left->up = X
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
60 ** We then use this instead of NULL to mean the top or bottom
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
61 ** end of the rb tree. It is a black node.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
62 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
63 ** Initialization of the last field in this initializer is left implicit
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
64 ** because it could be of any type. We count on the compiler to zero it.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
65 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
66 struct RB_ENTRY(node) RB_ENTRY(_null)={&RB_ENTRY(_null), &RB_ENTRY(_null), &RB_ENTRY(_null), BLACK, &RB_ENTRY(_null)};
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
67 #define RBNULL (&RB_ENTRY(_null))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
68
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
69 #if defined(USE_SBRK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
70
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
71 static struct RB_ENTRY(node) *RB_ENTRY(_alloc)();
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
72 static void RB_ENTRY(_free)(struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
73
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
74 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
75
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
76 static struct RB_ENTRY(node) *RB_ENTRY(_alloc)() {return (struct RB_ENTRY(node) *) malloc(sizeof(struct RB_ENTRY(node)));}
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
77 static void RB_ENTRY(_free)(struct RB_ENTRY(node) *x) {free(x);}
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
78
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
79 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
80
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
81 /* These functions are always needed */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
82 static void RB_ENTRY(_left_rotate)(struct RB_ENTRY(node) **, struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
83 static void RB_ENTRY(_right_rotate)(struct RB_ENTRY(node) **, struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
84 static struct RB_ENTRY(node) *RB_ENTRY(_successor)(const struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
85 static struct RB_ENTRY(node) *RB_ENTRY(_predecessor)(const struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
86 static struct RB_ENTRY(node) *RB_ENTRY(_traverse)(int, const RB_ENTRY(data_t) * , struct RB_ENTRY(tree) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
87
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
88 /* These functions may not be needed */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
89 #ifndef no_lookup
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
90 static struct RB_ENTRY(node) *RB_ENTRY(_lookup)(int, const RB_ENTRY(data_t) * , struct RB_ENTRY(tree) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
91 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
92
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
93 #ifndef no_destroy
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
94 static void RB_ENTRY(_destroy)(struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
95 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
96
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
97 #ifndef no_delete
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
98 static void RB_ENTRY(_delete)(struct RB_ENTRY(node) **, struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
99 static void RB_ENTRY(_delete_fix)(struct RB_ENTRY(node) **, struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
100 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
101
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
102 #ifndef no_walk
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
103 static void RB_ENTRY(_walk)(const struct RB_ENTRY(node) *, void (*)(const RB_ENTRY(data_t) *, const VISIT, const int, void *), void *, int);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
104 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
105
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
106 #ifndef no_readlist
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
107 static RBLIST *RB_ENTRY(_openlist)(const struct RB_ENTRY(node) *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
108 static const RB_ENTRY(data_t) * RB_ENTRY(_readlist)(RBLIST *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
109 static void RB_ENTRY(_closelist)(RBLIST *);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
110 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
111
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
112 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
113 ** OK here we go, the balanced tree stuff. The algorithm is the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
114 ** fairly standard red/black taken from "Introduction to Algorithms"
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
115 ** by Cormen, Leiserson & Rivest. Maybe one of these days I will
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
116 ** fully understand all this stuff.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
117 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
118 ** Basically a red/black balanced tree has the following properties:-
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
119 ** 1) Every node is either red or black (colour is RED or BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
120 ** 2) A leaf (RBNULL pointer) is considered black
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
121 ** 3) If a node is red then its children are black
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
122 ** 4) Every path from a node to a leaf contains the same no
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
123 ** of black nodes
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
124 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
125 ** 3) & 4) above guarantee that the longest path (alternating
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
126 ** red and black nodes) is only twice as long as the shortest
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
127 ** path (all black nodes). Thus the tree remains fairly balanced.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
128 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
129
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
130 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
131 * Initialise a tree. Identifies the comparison routine and any config
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
132 * data that must be sent to it when called.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
133 * Returns a pointer to the top of the tree.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
134 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
135 #ifndef RB_CUSTOMIZE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
136 RB_STATIC struct RB_ENTRY(tree) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
137 rbinit(int (*cmp)(const void *, const void *, const void *), const void *config)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
138 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
139 RB_STATIC struct RB_ENTRY(tree) *RB_ENTRY(init)(void)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
140 #endif /* RB_CUSTOMIZE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
141 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
142 struct RB_ENTRY(tree) *retval;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
143 char c;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
144
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
145 c=rcsid[0]; /* This does nothing but shutup the -Wall */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
146
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
147 if ((retval=(struct RB_ENTRY(tree) *) malloc(sizeof(struct RB_ENTRY(tree))))==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
148 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
149
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
150 #ifndef RB_CUSTOMIZE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
151 retval->rb_cmp=cmp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
152 retval->rb_config=config;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
153 #endif /* RB_CUSTOMIZE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
154 retval->rb_root=RBNULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
155
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
156 return(retval);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
157 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
158
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
159 #ifndef no_destroy
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
160 RB_STATIC void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
161 RB_ENTRY(destroy)(struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
162 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
163 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
164 return;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
165
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
166 if (rbinfo->rb_root!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
167 RB_ENTRY(_destroy)(rbinfo->rb_root);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
168
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
169 free(rbinfo);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
170 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
171 #endif /* no_destroy */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
172
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
173 #ifndef no_search
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
174 RB_STATIC const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
175 RB_ENTRY(search)(const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
176 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
177 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
178
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
179 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
180 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
181
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
182 x=RB_ENTRY(_traverse)(1, key, rbinfo);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
183
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
184 return((x==RBNULL) ? NULL : RB_GET(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
185 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
186 #endif /* no_search */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
187
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
188 #ifndef no_find
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
189 RB_STATIC const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
190 RB_ENTRY(find)(const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
191 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
192 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
193
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
194 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
195 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
196
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
197 /* If we have a NULL root (empty tree) then just return */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
198 if (rbinfo->rb_root==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
199 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
200
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
201 x=RB_ENTRY(_traverse)(0, key, rbinfo);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
202
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
203 return((x==RBNULL) ? NULL : RB_GET(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
204 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
205 #endif /* no_find */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
206
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
207 #ifndef no_delete
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
208 RB_STATIC const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
209 RB_ENTRY(delete)(const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
210 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
211 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
212 const RB_ENTRY(data_t) * y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
213
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
214 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
215 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
216
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
217 x=RB_ENTRY(_traverse)(0, key, rbinfo);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
218
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
219 if (x==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
220 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
221 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
222 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
223 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
224 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
225 y=RB_GET(x, key);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
226 RB_ENTRY(_delete)(&rbinfo->rb_root, x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
227
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
228 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
229 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
230 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
231 #endif /* no_delete */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
232
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
233 #ifndef no_walk
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
234 RB_STATIC void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
235 RB_ENTRY(walk)(const struct RB_ENTRY(tree) *rbinfo, void (*action)(const RB_ENTRY(data_t) *, const VISIT, const int, void *), void *arg)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
236 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
237 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
238 return;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
239
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
240 RB_ENTRY(_walk)(rbinfo->rb_root, action, arg, 0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
241 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
242 #endif /* no_walk */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
243
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
244 #ifndef no_readlist
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
245 RB_STATIC RBLIST *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
246 RB_ENTRY(openlist)(const struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
247 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
248 if (rbinfo==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
249 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
250
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
251 return(RB_ENTRY(_openlist)(rbinfo->rb_root));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
252 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
253
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
254 RB_STATIC const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
255 RB_ENTRY(readlist)(RBLIST *rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
256 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
257 if (rblistp==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
258 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
259
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
260 return(RB_ENTRY(_readlist)(rblistp));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
261 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
262
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
263 RB_STATIC void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
264 RB_ENTRY(closelist)(RBLIST *rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
265 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
266 if (rblistp==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
267 return;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
268
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
269 RB_ENTRY(_closelist)(rblistp);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
270 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
271 #endif /* no_readlist */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
272
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
273 #ifndef no_lookup
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
274 RB_STATIC const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
275 RB_ENTRY(lookup)(int mode, const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
276 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
277 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
278
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
279 /* If we have a NULL root (empty tree) then just return NULL */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
280 if (rbinfo==NULL || rbinfo->rb_root==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
281 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
282
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
283 x=RB_ENTRY(_lookup)(mode, key, rbinfo);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
284
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
285 return((x==RBNULL) ? NULL : RB_GET(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
286 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
287 #endif /* no_lookup */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
288
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
289 /* --------------------------------------------------------------------- */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
290
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
291 /* Search for and if not found and insert is true, will add a new
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
292 ** node in. Returns a pointer to the new node, or the node found
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
293 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
294 static struct RB_ENTRY(node) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
295 RB_ENTRY(_traverse)(int insert, const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
296 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
297 struct RB_ENTRY(node) *x,*y,*z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
298 int cmp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
299 int found=0;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
300 int cmpmods();
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
301
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
302 y=RBNULL; /* points to the parent of x */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
303 x=rbinfo->rb_root;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
304
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
305 /* walk x down the tree */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
306 while(x!=RBNULL && found==0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
307 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
308 y=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
309 /* printf("key=%s, RB_GET(x, key)=%s\n", key, RB_GET(x, key)); */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
310 #ifndef RB_CUSTOMIZE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
311 cmp=RB_CMP(key, RB_GET(x, key), rbinfo->rb_config);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
312 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
313 cmp=RB_CMP(key, RB_GET(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
314 #endif /* RB_CUSTOMIZE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
315
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
316 if (cmp<0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
317 x=x->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
318 else if (cmp>0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
319 x=x->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
320 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
321 found=1;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
322 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
323
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
324 if (found || !insert)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
325 return(x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
326
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
327 if ((z=RB_ENTRY(_alloc)())==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
328 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
329 /* Whoops, no memory */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
330 return(RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
331 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
332
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
333 RB_SET(z, key, key);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
334 z->up=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
335 if (y==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
336 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
337 rbinfo->rb_root=z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
338 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
339 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
340 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
341 #ifndef RB_CUSTOMIZE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
342 cmp=RB_CMP(RB_GET(z, key), RB_GET(y, key), rbinfo->rb_config);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
343 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
344 cmp=RB_CMP(RB_GET(z, key), RB_GET(y, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
345 #endif /* RB_CUSTOMIZE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
346 if (cmp<0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
347 y->left=z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
348 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
349 y->right=z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
350 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
351
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
352 z->left=RBNULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
353 z->right=RBNULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
354
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
355 /* colour this new node red */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
356 z->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
357
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
358 /* Having added a red node, we must now walk back up the tree balancing
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
359 ** it, by a series of rotations and changing of colours
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
360 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
361 x=z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
362
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
363 /* While we are not at the top and our parent node is red
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
364 ** N.B. Since the root node is garanteed black, then we
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
365 ** are also going to stop if we are the child of the root
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
366 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
367
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
368 while(x != rbinfo->rb_root && (x->up->colour == RED))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
369 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
370 /* if our parent is on the left side of our grandparent */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
371 if (x->up == x->up->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
372 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
373 /* get the right side of our grandparent (uncle?) */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
374 y=x->up->up->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
375 if (y->colour == RED)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
376 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
377 /* make our parent black */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
378 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
379 /* make our uncle black */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
380 y->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
381 /* make our grandparent red */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
382 x->up->up->colour = RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
383
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
384 /* now consider our grandparent */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
385 x=x->up->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
386 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
387 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
388 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
389 /* if we are on the right side of our parent */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
390 if (x == x->up->right)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
391 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
392 /* Move up to our parent */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
393 x=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
394 RB_ENTRY(_left_rotate)(&rbinfo->rb_root, x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
395 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
396
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
397 /* make our parent black */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
398 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
399 /* make our grandparent red */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
400 x->up->up->colour = RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
401 /* right rotate our grandparent */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
402 RB_ENTRY(_right_rotate)(&rbinfo->rb_root, x->up->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
403 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
404 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
405 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
406 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
407 /* everything here is the same as above, but
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
408 ** exchanging left for right
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
409 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
410
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
411 y=x->up->up->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
412 if (y->colour == RED)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
413 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
414 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
415 y->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
416 x->up->up->colour = RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
417
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
418 x=x->up->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
419 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
420 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
421 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
422 if (x == x->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
423 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
424 x=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
425 RB_ENTRY(_right_rotate)(&rbinfo->rb_root, x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
426 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
427
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
428 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
429 x->up->up->colour = RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
430 RB_ENTRY(_left_rotate)(&rbinfo->rb_root, x->up->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
431 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
432 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
433 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
434
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
435 /* Set the root node black */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
436 (rbinfo->rb_root)->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
437
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
438 return(z);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
439 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
440
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
441 #ifndef no_lookup
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
442 /* Search for a key according to mode (see redblack.h)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
443 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
444 static struct RB_ENTRY(node) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
445 RB_ENTRY(_lookup)(int mode, const RB_ENTRY(data_t) *key, struct RB_ENTRY(tree) *rbinfo)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
446 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
447 struct RB_ENTRY(node) *x,*y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
448 int cmp=0;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
449 int found=0;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
450
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
451 y=RBNULL; /* points to the parent of x */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
452 x=rbinfo->rb_root;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
453
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
454 if (mode==RB_LUFIRST)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
455 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
456 /* Keep going left until we hit a NULL */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
457 while(x!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
458 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
459 y=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
460 x=x->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
461 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
462
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
463 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
464 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
465 else if (mode==RB_LULAST)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
466 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
467 /* Keep going right until we hit a NULL */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
468 while(x!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
469 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
470 y=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
471 x=x->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
472 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
473
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
474 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
475 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
476
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
477 /* walk x down the tree */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
478 while(x!=RBNULL && found==0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
479 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
480 y=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
481 /* printf("key=%s, RB_GET(x, key)=%s\n", key, RB_GET(x, key)); */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
482 #ifndef RB_CUSTOMIZE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
483 cmp=RB_CMP(key, RB_GET(x, key), rbinfo->rb_config);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
484 #else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
485 cmp=RB_CMP(key, RB_GET(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
486 #endif /* RB_CUSTOMIZE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
487
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
488
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
489 if (cmp<0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
490 x=x->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
491 else if (cmp>0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
492 x=x->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
493 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
494 found=1;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
495 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
496
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
497 if (found && (mode==RB_LUEQUAL || mode==RB_LUGTEQ || mode==RB_LULTEQ))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
498 return(x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
499
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
500 if (!found && (mode==RB_LUEQUAL || mode==RB_LUNEXT || mode==RB_LUPREV))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
501 return(RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
502
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
503 if (mode==RB_LUGTEQ || (!found && mode==RB_LUGREAT))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
504 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
505 if (cmp>0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
506 return(RB_ENTRY(_successor)(y));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
507 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
508 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
509 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
510
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
511 if (mode==RB_LULTEQ || (!found && mode==RB_LULESS))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
512 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
513 if (cmp<0)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
514 return(RB_ENTRY(_predecessor)(y));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
515 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
516 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
517 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
518
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
519 if (mode==RB_LUNEXT || (found && mode==RB_LUGREAT))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
520 return(RB_ENTRY(_successor)(x));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
521
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
522 if (mode==RB_LUPREV || (found && mode==RB_LULESS))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
523 return(RB_ENTRY(_predecessor)(x));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
524
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
525 /* Shouldn't get here */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
526 return(RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
527 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
528 #endif /* no_lookup */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
529
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
530 #ifndef no_destroy
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
531 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
532 * Destroy all the elements blow us in the tree
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
533 * only useful as part of a complete tree destroy.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
534 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
535 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
536 RB_ENTRY(_destroy)(struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
537 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
538 if (x!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
539 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
540 if (x->left!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
541 RB_ENTRY(_destroy)(x->left);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
542 if (x->right!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
543 RB_ENTRY(_destroy)(x->right);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
544 RB_ENTRY(_free)(x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
545 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
546 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
547 #endif /* no_destroy */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
548
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
549 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
550 ** Rotate our tree thus:-
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
551 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
552 ** X rb_left_rotate(X)---> Y
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
553 ** / \ / \
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
554 ** A Y <---rb_right_rotate(Y) X C
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
555 ** / \ / \
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
556 ** B C A B
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
557 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
558 ** N.B. This does not change the ordering.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
559 **
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
560 ** We assume that neither X or Y is NULL
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
561 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
562
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
563 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
564 RB_ENTRY(_left_rotate)(struct RB_ENTRY(node) **rootp, struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
565 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
566 struct RB_ENTRY(node) *y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
567
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
568 assert(x!=RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
569 assert(x->right!=RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
570
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
571 y=x->right; /* set Y */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
572
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
573 /* Turn Y's left subtree into X's right subtree (move B)*/
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
574 x->right = y->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
575
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
576 /* If B is not null, set it's parent to be X */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
577 if (y->left != RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
578 y->left->up = x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
579
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
580 /* Set Y's parent to be what X's parent was */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
581 y->up = x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
582
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
583 /* if X was the root */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
584 if (x->up == RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
585 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
586 *rootp=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
587 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
588 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
589 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
590 /* Set X's parent's left or right pointer to be Y */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
591 if (x == x->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
592 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
593 x->up->left=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
594 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
595 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
596 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
597 x->up->right=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
598 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
599 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
600
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
601 /* Put X on Y's left */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
602 y->left=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
603
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
604 /* Set X's parent to be Y */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
605 x->up = y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
606 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
607
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
608 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
609 RB_ENTRY(_right_rotate)(struct RB_ENTRY(node) **rootp, struct RB_ENTRY(node) *y)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
610 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
611 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
612
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
613 assert(y!=RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
614 assert(y->left!=RBNULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
615
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
616 x=y->left; /* set X */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
617
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
618 /* Turn X's right subtree into Y's left subtree (move B) */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
619 y->left = x->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
620
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
621 /* If B is not null, set it's parent to be Y */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
622 if (x->right != RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
623 x->right->up = y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
624
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
625 /* Set X's parent to be what Y's parent was */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
626 x->up = y->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
627
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
628 /* if Y was the root */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
629 if (y->up == RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
630 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
631 *rootp=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
632 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
633 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
634 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
635 /* Set Y's parent's left or right pointer to be X */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
636 if (y == y->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
637 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
638 y->up->left=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
639 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
640 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
641 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
642 y->up->right=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
643 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
644 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
645
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
646 /* Put Y on X's right */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
647 x->right=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
648
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
649 /* Set Y's parent to be X */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
650 y->up = x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
651 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
652
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
653 /* Return a pointer to the smallest key greater than x
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
654 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
655 static struct RB_ENTRY(node) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
656 RB_ENTRY(_successor)(const struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
657 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
658 struct RB_ENTRY(node) *y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
659
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
660 if (x->right!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
661 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
662 /* If right is not NULL then go right one and
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
663 ** then keep going left until we find a node with
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
664 ** no left pointer.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
665 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
666 for (y=x->right; y->left!=RBNULL; y=y->left);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
667 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
668 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
669 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
670 /* Go up the tree until we get to a node that is on the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
671 ** left of its parent (or the root) and then return the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
672 ** parent.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
673 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
674 y=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
675 while(y!=RBNULL && x==y->right)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
676 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
677 x=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
678 y=y->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
679 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
680 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
681 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
682 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
683
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
684 /* Return a pointer to the largest key smaller than x
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
685 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
686 static struct RB_ENTRY(node) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
687 RB_ENTRY(_predecessor)(const struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
688 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
689 struct RB_ENTRY(node) *y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
690
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
691 if (x->left!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
692 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
693 /* If left is not NULL then go left one and
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
694 ** then keep going right until we find a node with
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
695 ** no right pointer.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
696 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
697 for (y=x->left; y->right!=RBNULL; y=y->right);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
698 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
699 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
700 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
701 /* Go up the tree until we get to a node that is on the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
702 ** right of its parent (or the root) and then return the
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
703 ** parent.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
704 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
705 y=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
706 while(y!=RBNULL && x==y->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
707 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
708 x=y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
709 y=y->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
710 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
711 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
712 return(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
713 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
714
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
715 #ifndef no_delete
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
716 /* Delete the node z, and free up the space
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
717 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
718 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
719 RB_ENTRY(_delete)(struct RB_ENTRY(node) **rootp, struct RB_ENTRY(node) *z)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
720 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
721 struct RB_ENTRY(node) *x, *y;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
722
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
723 if (z->left == RBNULL || z->right == RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
724 y=z;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
725 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
726 y=RB_ENTRY(_successor)(z);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
727
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
728 if (y->left != RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
729 x=y->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
730 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
731 x=y->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
732
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
733 x->up = y->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
734
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
735 if (y->up == RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
736 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
737 *rootp=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
738 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
739 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
740 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
741 if (y==y->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
742 y->up->left = x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
743 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
744 y->up->right = x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
745 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
746
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
747 if (y!=z)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
748 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
749 RB_SET(z, key, RB_GET(y, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
750 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
751
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
752 if (y->colour == BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
753 RB_ENTRY(_delete_fix)(rootp, x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
754
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
755 RB_ENTRY(_free)(y);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
756 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
757
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
758 /* Restore the reb-black properties after a delete */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
759 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
760 RB_ENTRY(_delete_fix)(struct RB_ENTRY(node) **rootp, struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
761 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
762 struct RB_ENTRY(node) *w;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
763
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
764 while (x!=*rootp && x->colour==BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
765 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
766 if (x==x->up->left)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
767 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
768 w=x->up->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
769 if (w->colour==RED)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
770 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
771 w->colour=BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
772 x->up->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
773 rb_left_rotate(rootp, x->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
774 w=x->up->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
775 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
776
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
777 if (w->left->colour==BLACK && w->right->colour==BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
778 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
779 w->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
780 x=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
781 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
782 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
783 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
784 if (w->right->colour == BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
785 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
786 w->left->colour=BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
787 w->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
788 RB_ENTRY(_right_rotate)(rootp, w);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
789 w=x->up->right;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
790 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
791
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
792
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
793 w->colour=x->up->colour;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
794 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
795 w->right->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
796 RB_ENTRY(_left_rotate)(rootp, x->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
797 x=*rootp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
798 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
799 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
800 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
801 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
802 w=x->up->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
803 if (w->colour==RED)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
804 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
805 w->colour=BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
806 x->up->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
807 RB_ENTRY(_right_rotate)(rootp, x->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
808 w=x->up->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
809 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
810
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
811 if (w->right->colour==BLACK && w->left->colour==BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
812 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
813 w->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
814 x=x->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
815 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
816 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
817 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
818 if (w->left->colour == BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
819 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
820 w->right->colour=BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
821 w->colour=RED;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
822 RB_ENTRY(_left_rotate)(rootp, w);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
823 w=x->up->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
824 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
825
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
826 w->colour=x->up->colour;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
827 x->up->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
828 w->left->colour = BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
829 RB_ENTRY(_right_rotate)(rootp, x->up);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
830 x=*rootp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
831 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
832 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
833 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
834
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
835 x->colour=BLACK;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
836 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
837 #endif /* no_delete */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
838
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
839 #ifndef no_walk
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
840 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
841 RB_ENTRY(_walk)(const struct RB_ENTRY(node) *x, void (*action)(const RB_ENTRY(data_t) *, const VISIT, const int, void *), void *arg, int level)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
842 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
843 if (x==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
844 return;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
845
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
846 if (x->left==RBNULL && x->right==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
847 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
848 /* leaf */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
849 (*action)(RB_GET(x, key), leaf, level, arg);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
850 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
851 else
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
852 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
853 (*action)(RB_GET(x, key), preorder, level, arg);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
854
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
855 RB_ENTRY(_walk)(x->left, action, arg, level+1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
856
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
857 (*action)(RB_GET(x, key), postorder, level, arg);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
858
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
859 RB_ENTRY(_walk)(x->right, action, arg, level+1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
860
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
861 (*action)(RB_GET(x, key), endorder, level, arg);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
862 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
863 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
864 #endif /* no_walk */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
865
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
866 #ifndef no_readlist
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
867 static RBLIST *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
868 RB_ENTRY(_openlist)(const struct RB_ENTRY(node) *rootp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
869 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
870 RBLIST *rblistp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
871
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
872 rblistp=(RBLIST *) malloc(sizeof(RBLIST));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
873 if (!rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
874 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
875
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
876 rblistp->rootp=rootp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
877 rblistp->nextp=rootp;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
878
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
879 if (rootp!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
880 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
881 while(rblistp->nextp->left!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
882 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
883 rblistp->nextp=rblistp->nextp->left;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
884 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
885 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
886
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
887 return(rblistp);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
888 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
889
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
890 static const RB_ENTRY(data_t) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
891 RB_ENTRY(_readlist)(RBLIST *rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
892 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
893 const RB_ENTRY(data_t) *key=NULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
894
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
895 if (rblistp!=NULL && rblistp->nextp!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
896 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
897 key=RB_GET(rblistp->nextp, key);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
898 rblistp->nextp=RB_ENTRY(_successor)(rblistp->nextp);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
899 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
900
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
901 return(key);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
902 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
903
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
904 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
905 rb_closelist(RBLIST *rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
906 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
907 if (rblistp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
908 free(rblistp);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
909 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
910 #endif /* no_readlist */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
911
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
912 #if defined(RB_USE_SBRK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
913 /* Allocate space for our nodes, allowing us to get space from
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
914 ** sbrk in larger chucks.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
915 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
916 static struct RB_ENTRY(node) *rbfreep=NULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
917
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
918 #define RB_ENTRY(NODE)ALLOC_CHUNK_SIZE 1000
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
919 static struct RB_ENTRY(node) *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
920 RB_ENTRY(_alloc)()
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
921 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
922 struct RB_ENTRY(node) *x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
923 int i;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
924
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
925 if (rbfreep==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
926 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
927 /* must grab some more space */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
928 rbfreep=(struct RB_ENTRY(node) *) sbrk(sizeof(struct RB_ENTRY(node)) * RB_ENTRY(NODE)ALLOC_CHUNK_SIZE);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
929
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
930 if (rbfreep==(struct RB_ENTRY(node) *) -1)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
931 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
932 return(NULL);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
933 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
934
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
935 /* tie them together in a linked list (use the up pointer) */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
936 for (i=0, x=rbfreep; i<RB_ENTRY(NODE)ALLOC_CHUNK_SIZE-1; i++, x++)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
937 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
938 x->up = (x+1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
939 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
940 x->up=NULL;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
941 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
942
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
943 x=rbfreep;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
944 rbfreep = rbfreep->up;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
945 #ifdef RB_ALLOC
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
946 RB_ALLOC(ACCESS(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
947 #endif /* RB_ALLOC */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
948 return(x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
949 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
950
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
951 /* free (dealloc) an RB_ENTRY(node) structure - add it onto the front of the list
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
952 ** N.B. RB_ENTRY(node) need not have been allocated through rb_alloc()
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
953 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
954 static void
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
955 RB_ENTRY(_free)(struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
956 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
957 #ifdef RB_FREE
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
958 RB_FREE(ACCESS(x, key));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
959 #endif /* RB_FREE */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
960 x->up=rbfreep;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
961 rbfreep=x;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
962 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
963
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
964 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
965
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
966 #if 0
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
967 int
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
968 RB_ENTRY(_check)(struct RB_ENTRY(node) *rootp)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
969 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
970 if (rootp==NULL || rootp==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
971 return(0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
972
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
973 if (rootp->up!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
974 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
975 fprintf(stderr, "Root up pointer not RBNULL");
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
976 dumptree(rootp, 0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
977 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
978 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
979
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
980 if (RB_ENTRY(_check)1(rootp))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
981 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
982 RB_ENTRY(dumptree)(rootp, 0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
983 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
984 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
985
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
986 if (RB_ENTRY(count_black)(rootp)==-1)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
987 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
988 RB_ENTRY(dumptree)(rootp, 0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
989 return(-1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
990 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
991
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
992 return(0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
993 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
994
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
995 int
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
996 RB_ENTRY(_check1)(struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
997 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
998 if (x->left==NULL || x->right==NULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
999 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1000 fprintf(stderr, "Left or right is NULL");
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1001 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1002 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1003
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1004 if (x->colour==RED)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1005 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1006 if (x->left->colour!=BLACK && x->right->colour!=BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1007 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1008 fprintf(stderr, "Children of red node not both black, x=%ld", x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1009 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1010 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1011 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1012
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1013 if (x->left != RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1014 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1015 if (x->left->up != x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1016 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1017 fprintf(stderr, "x->left->up != x, x=%ld", x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1018 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1019 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1020
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1021 if (rb_check1(x->left))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1022 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1023 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1024
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1025 if (x->right != RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1026 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1027 if (x->right->up != x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1028 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1029 fprintf(stderr, "x->right->up != x, x=%ld", x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1030 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1031 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1032
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1033 if (rb_check1(x->right))
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1034 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1035 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1036 return(0);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1037 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1038
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1039 RB_ENTRY(count_black)(struct RB_ENTRY(node) *x)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1040 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1041 int nleft, nright;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1042
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1043 if (x==RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1044 return(1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1045
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1046 nleft=RB_ENTRY(count_black)(x->left);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1047 nright=RB_ENTRY(count_black)(x->right);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1048
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1049 if (nleft==-1 || nright==-1)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1050 return(-1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1051
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1052 if (nleft != nright)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1053 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1054 fprintf(stderr, "Black count not equal on left & right, x=%ld", x);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1055 return(-1);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1056 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1057
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1058 if (x->colour == BLACK)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1059 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1060 nleft++;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1061 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1062
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1063 return(nleft);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1064 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1065
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1066 RB_ENTRY(dumptree)(struct RB_ENTRY(node) *x, int n)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1067 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1068 char *prkey();
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1069
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1070 if (x!=NULL && x!=RBNULL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1071 {
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1072 n++;
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1073 fprintf(stderr, "Tree: %*s %ld: left=%ld, right=%ld, colour=%s, key=%s",
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1074 n,
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1075 "",
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1076 x,
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1077 x->left,
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1078 x->right,
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1079 (x->colour==BLACK) ? "BLACK" : "RED",
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1080 prkey(RB_GET(x, key)));
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1081
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1082 RB_ENTRY(dumptree)(x->left, n);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1083 RB_ENTRY(dumptree)(x->right, n);
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1084 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1085 }
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1086 #endif
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1087
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1088 /*
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1089 * $Log: redblack.c,v $
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1090 * Revision 1.9 2003/10/24 01:31:21 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1091 * Patches from Eric Raymond: %prefix is implemented.  Various other small
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1092 * changes avoid stepping on global namespaces and improve the documentation.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1093 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1094 * Revision 1.8 2002/08/26 05:33:47 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1095 * Some minor fixes:-
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1096 * Stopped ./configure warning about stuff being in the wrong order
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1097 * Fixed compiler warning about const (not sure about this)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1098 * Changed directory of redblack.c in documentation
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1099 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1100 * Revision 1.7 2002/08/26 03:11:40 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1101 * Fixed up a bunch of compiler warnings when compiling example4
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1102 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1103 * Tidies up the Makefile.am & Specfile.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1104 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1105 * Renamed redblack to rbgen
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1106 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1107 * Revision 1.6 2002/08/26 01:03:35 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1108 * Patch from Eric Raymond to change the way the library is used:-
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1109 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1110 * Eric's idea is to convert libredblack into a piece of in-line code
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1111 * generated by another program. This should be faster, smaller and easier
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1112 * to use.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1113 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1114 * This is the first check-in of his code before I start futzing with it!
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1115 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1116 * Revision 1.5 2002/01/30 07:54:53 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1117 * Fixed up the libtool versioning stuff (finally)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1118 * Fixed bug 500600 (not detecting a NULL return from malloc)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1119 * Fixed bug 509485 (no longer needs search.h)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1120 * Cleaned up debugging section
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1121 * Allow multiple inclusions of redblack.h
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1122 * Thanks to Matthias Andree for reporting (and fixing) these
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1123 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1124 * Revision 1.4 2000/06/06 14:43:43 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1125 * Added all the rbwalk & rbopenlist stuff. Fixed up malloc instead of sbrk.
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1126 * Added two new examples
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1127 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1128 * Revision 1.3 2000/05/24 06:45:27 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1129 * Converted everything over to using const
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1130 * Added a new example1.c file to demonstrate the worst case scenario
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1131 * Minor fixups of the spec file
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1132 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1133 * Revision 1.2 2000/05/24 06:17:10 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1134 * Fixed up the License (now the LGPL)
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1135 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1136 * Revision 1.1 2000/05/24 04:15:53 damo
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1137 * Initial import of files. Versions are now all over the place. Oh well
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1138 *
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1139 */
e413158cae13 Add ushare project files.
naoyan@johnstown.minaminoshima.org
parents:
diff changeset
1140