1
2
3
4
5
6
7
8 package org.xwt.util;
9
10
11
12
13
14
15
16 public class BalancedTree {
17
18
19
20
21 private int root = 0;
22
23 private int cached_index = -1;
24 private int cached_slot = -1;
25
26
27
28
29 public final int treeSize() { return root == 0 ? 0 : size[root]; }
30
31
32 public final synchronized void insertNode(int index, Object o) {
33 if(o == null) throw new Error("can't insert nulls in the balanced tree");
34 cached_slot = cached_index = -1;
35 if (index < 0) index = 0;
36 if (index > treeSize()) index = treeSize();
37 int arg = allocateSlot(o);
38 if (root != 0) {
39 insert(index, arg, root, 0, false, false);
40 } else {
41 root = arg;
42 left[arg] = right[arg] = parent[arg] = 0;
43 size[arg] = 1;
44 }
45 }
46
47
48 public final synchronized void replaceNode(int index, Object o) {
49 if(o == null) throw new Error("can't insert nulls in the balanced tree");
50 cached_slot = cached_index = -1;
51 if(root == 0) throw new Error("called replaceNode() on an empty tree");
52 if (index < 0) index = 0;
53 if (index >= treeSize()) index = treeSize() - 1;
54 int arg = allocateSlot(o);
55 insert(index, arg, root, 0, true, false);
56 }
57
58
59 public final synchronized int indexNode(Object o) {
60 if(o == null) return -1;
61 if (cached_slot != -1 && objects[cached_slot] == o) return cached_index;
62
63 int slot = getSlot(o);
64 if(slot == -1) return -1;
65
66 int index = 0;
67 while(true) {
68
69 index += sizeof(left[slot]);
70
71 while(left[parent[slot]] == slot) slot = parent[slot];
72
73 slot = parent[slot];
74
75 if(slot == 0) break;
76
77 index++;
78 }
79 return index;
80 }
81
82
83 public final synchronized Object getNode(int index) {
84 if (index == cached_index) return objects[cached_slot];
85
86 if (cached_index != -1) {
87 int distance = Math.abs(index - cached_index);
88
89
90
91 if ((distance < 16) && ((2 << distance) < treeSize())) {
92 while(cached_index > index) { cached_slot = prev(cached_slot); cached_index--; }
93 while(cached_index < index) { cached_slot = next(cached_slot); cached_index++; }
94 return objects[cached_slot];
95 }
96 }
97
102 return objects[get(index, root)];
103 }
104
105
106 public final synchronized Object deleteNode(int index) {
107 cached_slot = cached_index = -1;
108
109 int del = delete(index, root, 0);
110 left[del] = right[del] = size[del] = parent[del] = 0;
111 Object ret = objects[del];
112 objects[del] = null;
113 return ret;
114 }
115
116 public final synchronized void clear() {
117 if(root == 0) return;
118 int i = leftmost(root);
119 do {
120 int next = next(i);
121 objects[i] = null;
122 left[i] = right[i] = size[i] = parent[i] = 0;
123 i = next;
124 } while(i != 0);
125 root = 0;
126 }
127
128 protected void finalize() { clear(); }
129
130
131
132
133 private final static int NUM_SLOTS = 64 * 1024;
134
135
136
144 private static Object[] objects = new Object[NUM_SLOTS];
145
146
147
148
149
150
151
152
153
154 private static int[] left = new int[NUM_SLOTS];
155 private static int[] right = new int[NUM_SLOTS];
156
157
158 private static int[] parent = new int[NUM_SLOTS];
159
160
161 private static int[] size = new int[NUM_SLOTS];
162
163
164
165
166
167 private int getSlot(Object o, boolean alloc) {
168
169
170
171 int dest = Math.abs(o.hashCode() ^ this.hashCode()) % objects.length;
172 Object search = alloc ? null : o;
173 int odest = dest;
174 boolean plus = true;
175 int tries = 1;
176 while (objects[dest] != search || !(alloc || root(dest) == root)) {
177 if (dest == 0) dest++;
178 dest = Math.abs((odest + (plus ? 1 : -1) * tries * tries) % objects.length);
179 if (plus) tries++;
180 plus = !plus;
181
182 }
183 return dest;
184 }
185
186
187 private int getSlot(Object o) { return getSlot(o,false); }
188
189
190 private int allocateSlot(Object o) {
191 int slot = getSlot(o, true);
192
193 objects[slot] = o;
194 return slot;
195 }
196
197
198
199
200
201 private final int leftmost(int slot) { return left[slot] <= 0 ? slot : leftmost(left[slot]); }
202 private final int rightmost(int slot) { return right[slot] <= 0 ? slot : rightmost(right[slot]); }
203 private final int next(int slot) { return right[slot] <= 0 ? -1 * right[slot] : leftmost(right[slot]); }
204 private final int prev(int slot) { return left[slot] <= 0 ? -1 * left[slot] : rightmost(left[slot]); }
205 private final int sizeof(int slot) { return slot <= 0 ? 0 : size[slot]; }
206 private final int root(int slot) { return parent[slot] == 0 ? slot : root(parent[slot]); }
207
208
209
210
211
212
213
214
215
216
217
218
219 private void rotate(boolean toTheLeft, int b, int p) {
220 int[] left = toTheLeft ? BalancedTree.left : BalancedTree.right;
221 int[] right = toTheLeft ? BalancedTree.right : BalancedTree.left;
222 int d = right[b];
223 int c = left[d];
224 if (d <= 0) throw new Error("rotation error");
225 left[d] = b;
226 if(size[b] <= 3)
227 right[b] = -d;
228 else
229 right[b] = c;
230 parent[b] = d;
231 parent[d] = p;
232 if(c > 0) parent[c] = b;
233 if (p == 0) root = d;
234 else if (left[p] == b) left[p] = d;
235 else if (right[p] == b) right[p] = d;
236 else throw new Error("rotate called with invalid parent");
237 size[b] = 1 + sizeof(left[b]) + sizeof(right[b]);
238 size[d] = 1 + sizeof(left[d]) + sizeof(right[d]);
239 }
240
241 private void balance(int slot, int p) {
242 if (slot <= 0) return;
243 size[slot] = 1 + sizeof(left[slot]) + sizeof(right[slot]);
244 if (sizeof(left[slot]) - 1 > 2 * sizeof(right[slot])) rotate(false, slot, p);
245 else if (sizeof(left[slot]) * 2 < sizeof(right[slot]) - 1) rotate(true, slot, p);
246 }
247
248
249
250
251
252 private void insert(int index, int arg, int slot, int p, boolean replace, boolean wentLeft) {
253 int diff = slot <= 0 ? 0 : index - sizeof(left[slot]);
254 if (slot > 0 && diff != 0) {
255 if (diff < 0) insert(index, arg, left[slot], slot, replace, true);
256 else insert(index - sizeof(left[slot]) - 1, arg, right[slot], slot, replace, false);
257 balance(slot, p);
258 return;
259 }
260
261 if (size[arg] != 0) throw new Error("double insertion");
262
263
264 if (replace) {
265 if (diff != 0) throw new Error("this should never happen");
266 if (p == 0) root = arg;
267 else if (left[p] == slot) left[p] = arg;
268 else if (right[p] == slot) right[p] = arg;
269 left[arg] = left[slot];
270 right[arg] = right[slot];
271 size[arg] = size[slot];
272 parent[arg] = parent[slot];
273 if(left[slot] > 0) parent[left[slot]] = arg;
274 if(right[slot] > 0) parent[right[slot]] = arg;
275 objects[slot] = null;
276 left[slot] = right[slot] = size[slot] = parent[slot] = 0;
277
278
279 } else if (slot <= 0) {
280 int[] left = wentLeft ? BalancedTree.left : BalancedTree.right;
281 int[] right = wentLeft ? BalancedTree.right : BalancedTree.left;
282 left[arg] = slot;
283 left[p] = arg;
284 right[arg] = -1 * p;
285 parent[arg] = p;
286 balance(arg, p);
287
288
289 } else {
290 left[arg] = left[slot];
291 left[slot] = -1 * arg;
292 right[arg] = slot;
293 parent[arg] = parent[slot];
294 parent[slot] = arg;
295 if (slot == root) {
296 root = arg;
297 balance(slot, arg);
298 balance(arg, 0);
299 } else {
300 if (left[p] == slot) left[p] = arg;
301 else if (right[p] == slot) right[p] = arg;
302 else throw new Error("should never happen");
303 balance(slot, arg);
304 balance(arg, p);
305 }
306 }
307 }
308
309
310
311
312 private int get(int index, int slot) {
313 int diff = index - sizeof(left[slot]);
314 if (diff > 0) return get(diff - 1, right[slot]);
315 else if (diff < 0) return get(index, left[slot]);
316 else return slot;
317 }
318
319
320
321
322 private int delete(int index, int slot, int p) {
323 int diff = index - sizeof(left[slot]);
324 if (diff < 0) {
325 int ret = delete(index, left[slot], slot);
326 balance(slot, p);
327 return ret;
328
329 } else if (diff > 0) {
330 int ret = delete(diff - 1, right[slot], slot);
331 balance(slot, p);
332 return ret;
333
334
335 } else {
336
337
338 if (left[slot] <= 0 && right[slot] <= 0) {
339 if (p == 0) root = 0;
340 else {
341 int[] side = left[p] == slot ? left : right;
342 side[p] = side[slot];
343 }
344
345
346 } else if (left[slot] <= 0) {
347 if (p == 0) root = right[slot];
348 else (left[p] == slot ? left : right)[p] = right[slot];
349 parent[right[slot]] = p;
350 left[leftmost(right[slot])] = left[slot];
351 balance(right[slot], p);
352
353
354 } else if (right[slot] <= 0) {
355 if (p == 0) root = left[slot];
356 else (left[p] == slot ? left : right)[p] = left[slot];
357 parent[left[slot]] = p;
358 right[rightmost(left[slot])] = right[slot];
359 balance(left[slot], p);
360
361
362 } else {
363 int left_childs_rightmost = delete(sizeof(left[slot]) - 1, left[slot], slot);
364 left[left_childs_rightmost] = left[slot];
365 right[left_childs_rightmost] = right[slot];
366 if(left[slot] > 0) parent[left[slot]] = left_childs_rightmost;
367 if(right[slot] > 0) parent[right[slot]] = left_childs_rightmost;
368 parent[left_childs_rightmost] = parent[slot];
369 if (p == 0) root = left_childs_rightmost;
370 else (left[p] == slot ? left : right)[p] = left_childs_rightmost;
371 balance(left_childs_rightmost, p);
372 }
373
374 return slot;
375 }
376 }
377
378
379
380 public void printTree() {
381 if(root == 0) System.err.println("Tree is empty");
382 else printTree(root,0,false);
383 }
384 private void printTree(int node,int indent,boolean l) {
385 for(int i=0;i<indent;i++) System.err.print(" ");
386 if(node < 0) System.err.println((l?"Prev: " : "Next: ") + -node);
387 else if(node == 0) System.err.println(l ? "Start" : "End");
388 else {
389 System.err.print("" + node + ": " + objects[node]);
390 System.err.println(" Parent: " + parent[node]);
391 printTree(left[node],indent+1,true);
392 printTree(right[node],indent+1,false);
393 }
394 }
395 }
396