summaryrefslogtreecommitdiffstats
path: root/libc/stdlib/malloc/avlmacro.h
blob: 8050172ccb7af00531e4e3a0607a58a7fbf0047a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* MACRO-CODED FAST FIXED AVL TREES IMPLEMENTATION IN C              */
/* COPYRIGHT (C) 1998 VALERY SHCHEDRIN                               */
/* IT IS DISTRIBUTED UNDER GLPL (GNU GENERAL LIBRARY PUBLIC LICENSE) */

#define balance(objname, pr, root, ht_changed) \
  { \
    objname *p; \
    p = *root; \
    if (p->bal_##pr < -1) \
    { \
      if (p->l_##pr->bal_##pr == 1) \
      { \
	objname *pp; \
	pp=p->l_##pr; *root=p->l_##pr->r_##pr; p->l_##pr = (*root)->r_##pr; \
	(*root)->r_##pr = p; pp->r_##pr = (*root)->l_##pr; \
	p = *root; p->l_##pr = pp; \
	if (p->bal_##pr > 0) p->l_##pr ->bal_##pr = -p->bal_##pr ; \
	else p->l_##pr ->bal_##pr = 0; \
	if (p->bal_##pr < 0) p->r_##pr ->bal_##pr = -p->bal_##pr ; \
	else p->r_##pr ->bal_##pr = 0; \
	p->bal_##pr = 0; \
	ht_changed = 1; \
      } \
      else \
      { \
	ht_changed = (p->l_##pr ->bal_##pr)?1:0; \
	*root = p->l_##pr ; \
	p->l_##pr = (*root)->r_##pr ; (*root)->r_##pr = p; \
	p->bal_##pr = - (++((*root)->bal_##pr )); \
      } \
    } \
    else if (p->bal_##pr > 1) \
    { \
      if (p->r_##pr->bal_##pr == -1) \
      { \
	objname *pp; \
	pp=p->r_##pr ; *root=p->r_##pr ->l_##pr ; p->r_##pr =(*root)->l_##pr ; \
	(*root)->l_##pr = p; pp->l_##pr = (*root)->r_##pr ; \
	p = *root; p->r_##pr = pp; \
	if (p->bal_##pr > 0) p->l_##pr ->bal_##pr = -p->bal_##pr ; \
	else p->l_##pr ->bal_##pr = 0; \
	if (p->bal_##pr < 0) p->r_##pr ->bal_##pr = -p->bal_##pr ; \
	else p->r_##pr ->bal_##pr = 0; \
	p->bal_##pr = 0; \
	ht_changed = 1; \
      } \
      else \
      { \
	ht_changed = (p->r_##pr ->bal_##pr)?1:0; \
	*root = p->r_##pr ; \
	p->r_##pr = (*root)->l_##pr ; (*root)->l_##pr = p; \
	p->bal_##pr = - (--((*root)->bal_##pr )); \
      } \
    } else ht_changed = 0; \
  }

#define Avl_r_insert_proto(objname, pr, COMPARE) \
  static int Avl_##objname##pr##_r_insert(objname **root) \
  { \
    int i; /* height increase */ \
    if (!*root) \
    { \
      *root = Avl_##objname##pr##_new_node; \
      Avl_##objname##pr##_new_node = NULL; \
      return 1; \
    } \
    COMPARE(i, Avl_##objname##pr##_new_node, *root); \
    \
    if (i < 0) \
    { /* insert into the left subtree */ \
      i = -Avl_##objname##pr##_r_insert(&((*root)->l_##pr)); \
      if (Avl_##objname##pr##_new_node != NULL) return 0; /* already there */ \
    } \
    else if (i > 0) \
    { /* insert into the right subtree */ \
      i = Avl_##objname##pr##_r_insert(&((*root)->r_##pr)); \
      if (Avl_##objname##pr##_new_node != NULL) return 0; /* already there */ \
    } \
    else \
    { /* found */ \
      Avl_##objname##pr##_new_node = *root; \
      return 0; \
    } \
    if (!i) return 0; \
    (*root)->bal_##pr += i; /* update balance factor */ \
    if ((*root)->bal_##pr) \
    { \
      balance(objname,pr,root,i); \
      return 1-i; \
    } \
    else return 0; \
  }

#define Avl_r_delete_proto(objname,pr,COMPARE) \
  static int Avl_##objname##pr##_r_delete(objname **root) \
  { \
    int i; /* height decrease */ \
    \
    if (!*root) return 0; /* not found */ \
    \
    COMPARE(i, Avl_##objname##pr##_new_node, *root); \
    \
    if (i < 0) \
      i = -Avl_##objname##pr##_r_delete(&((*root)->l_##pr)); \
    else if (i > 0) \
      i =  Avl_##objname##pr##_r_delete(&((*root)->r_##pr)); \
    else \
    { \
      if (!(*root)->l_##pr) \
      { \
	*root = (*root)->r_##pr; \
	return 1; \
      } \
      else if (!(*root)->r_##pr) \
      { \
	*root = (*root)->l_##pr; \
	return 1; \
      } \
      else \
      { \
	i = Avl_##objname##pr##_r_delfix(&((*root)->r_##pr)); \
	Avl_##objname##pr##_new_node->l_##pr = (*root)->l_##pr; \
	Avl_##objname##pr##_new_node->r_##pr = (*root)->r_##pr; \
	Avl_##objname##pr##_new_node->bal_##pr = (*root)->bal_##pr; \
	*root = Avl_##objname##pr##_new_node; \
      } \
    } \
    if (!i) return 0; \
    (*root)->bal_##pr -= i; \
    if ((*root)->bal_##pr) \
    { \
      balance(objname,pr,root,i); \
      return i; \
    } \
    return 1; \
  }

#define Avl_r_delfix_proto(objname,pr) \
  static int Avl_##objname##pr##_r_delfix(objname **root) \
  { \
    int i; /* height decrease */ \
    \
    if (!(*root)->l_##pr) \
    { \
      Avl_##objname##pr##_new_node = *root; \
      *root = (*root)->r_##pr; \
      return 1; \
    } \
    i = -Avl_##objname##pr##_r_delfix(&((*root)->l_##pr)); \
    if (!i) return 0; \
    (*root)->bal_##pr -= i; \
    if ((*root)->bal_##pr) \
    { \
      balance(objname,pr,root,i); \
      return i; \
    } \
    return 1; \
  }

#define Avl_ins_proto(alias,objname,pr) \
  static objname *##alias##_ins(objname *data) \
  { \
    Avl_##objname##pr##_new_node = data; \
    (data)->l_##pr = NULL; \
    (data)->r_##pr = NULL; \
    (data)->bal_##pr = 0; \
    Avl_##objname##pr##_r_insert(&Avl_##objname##pr##_tree); \
    if (Avl_##objname##pr##_new_node) \
      return Avl_##objname##pr##_new_node; \
    return NULL; \
  }

#define Avl_del_proto(alias,objname,pr) \
  static void alias##_del(objname *data) \
  { \
    Avl_##objname##pr##_new_node = data; \
    Avl_##objname##pr##_r_delete(&Avl_##objname##pr##_tree); \
  }

#define Avl_replace_proto(alias,objname,pr,COMPARE) \
  static void alias##_replace(objname *data) \
  { \
    objname **p = &Avl_##objname##pr##_tree; \
    int cmp; \
    while (*p) \
    { \
      COMPARE(cmp, data, *p); \
      if (cmp < 0) \
	p = &((*p)->l_##pr); \
      else if (cmp > 0) \
	p = &((*p)->r_##pr); \
      else \
      { \
	(data)->l_##pr = (*p)->l_##pr; \
	(data)->r_##pr = (*p)->r_##pr; \
	(data)->bal_##pr = (*p)->bal_##pr; \
	*p = data; \
	return; \
      } \
    } \
  }

#define Avl_Root(objname,pr) Avl_##objname##pr##_tree

#define Avl_Tree(alias,objname,pr,COMPARE) \
static objname *Avl_##objname##pr##_tree = NULL; \
static objname *Avl_##objname##pr##_new_node; \
Avl_r_insert_proto(objname,pr,COMPARE) \
Avl_r_delfix_proto(objname,pr) \
Avl_r_delete_proto(objname,pr,COMPARE) \
Avl_ins_proto(alias,objname,pr) \
Avl_del_proto(alias,objname,pr) \
Avl_replace_proto(alias,objname,pr,COMPARE)