7 extern int __ecereVMethodID_class_OnCompare;
8 extern int __ecereVMethodID_class_OnCopy;
11 public class AVLNode<class T> : IteratorPointer
15 thisclass parent, left, right;
20 property thisclass prev
28 if(parent && this == parent.right)
37 property thisclass next
41 thisclass right = this.right;
46 thisclass parent = this.parent;
47 if(parent && this == parent.left)
56 property thisclass minimum
58 get { while(left) this = left; return this; }
61 property thisclass maximum
63 get { while(right) this = right; return this; }
68 get { return 1 + (left ? left.count : 0) + (right ? right.count : 0); }
70 property int depthProp
74 int leftDepth = left ? (left.depthProp+1) : 0;
75 int rightDepth = right ? (right.depthProp+1) : 0;
76 return Max(leftDepth, rightDepth);
83 if (left) left.Free();
84 if (right) right.Free();
88 bool Add(Class Tclass, thisclass node)
91 Tclass = class(uint64);
94 // *** NEED COMPARISON OPERATOR SUPPORT HERE INVOKING OnCompare, AS WELL AS TYPE INFORMATION PASSED ***
97 if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass || Tclass.type == structClass)
99 a = (byte *)&node.key;
101 a += __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize);
102 b += __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize);
106 a = (byte *)(uintptr)node.key;
107 b = (byte *)(uintptr)key;
110 result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass, a, b);
126 for(n = this; n; n = n.parent)
128 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
129 if(newDepth == n.depth)
148 for(n = this; n; n = n.parent)
150 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
151 if(newDepth == n.depth)
162 public thisclass Find(Class Tclass, const T key)
166 // *** NEED COMPARISON OPERATOR SUPPORT HERE INVOKING OnCompare, AS WELL AS TYPE INFORMATION PASSED ***
169 if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass)
171 a = (byte *)&(uint64)key;
172 a += __ENDIAN_PAD(Tclass.typeSize);
175 a = (byte *)(uintptr)key;
177 if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass || Tclass.type == structClass)
179 b = (byte *)&this.key;
180 b += __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize);
183 b = (byte *)(uintptr)this.key;
185 result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass, a, b);
197 thisclass FindAll(const T key)
199 AVLNode<T> result = null;
200 // *** FIND ALL COMPARES KEY FOR EQUALITY, NOT USING OnCompare ***
201 if(this.key == key) result = this;
202 if(!result && left) result = left.FindAll(key);
203 if(!result && right) result = right.FindAll(key);
207 void RemoveSwap(thisclass swap)
211 swap.left.parent = swap.parent;
212 if(swap == swap.parent.left)
213 swap.parent.left = swap.left;
214 else if(swap == swap.parent.right)
215 swap.parent.right = swap.left;
220 swap.right.parent = swap.parent;
221 if(swap == swap.parent.left)
222 swap.parent.left = swap.right;
223 else if(swap == swap.parent.right)
224 swap.parent.right = swap.right;
228 if(swap == swap.parent.left) swap.parent.left = null;
229 else if(swap == swap.parent.right) swap.parent.right = null;
233 for(n = swap.parent; n; n = n.parent)
235 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
236 if(newDepth == n.depth)
251 swap.parent = parent;
256 if(this == parent.left) parent.left = swap;
257 else if(this == parent.right) parent.right = swap;
261 thisclass RemoveSwapLeft()
263 thisclass swap = left ? left.maximum : right;
264 thisclass swapParent = null;
265 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
268 if(this == parent.left) parent.left = null;
269 else if(this == parent.right) parent.right = null;
273 for(n = swap ? swap : parent; n; n = n.parent)
275 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
276 if(newDepth == n.depth && n != swap)
281 if(swapParent && swapParent != this)
282 return swapParent.Rebalance();
284 return swap.Rebalance();
286 return parent.Rebalance();
291 thisclass RemoveSwapRight()
294 thisclass swap = right ? right.minimum : left;
295 thisclass swapParent = null;
296 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
299 if(this == parent.left) parent.left = null;
300 else if(this == parent.right) parent.right = null;
304 for(n = swap ? swap : parent; n; n = n.parent)
306 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
308 if(newDepth == n.depth && n != swap)
313 if(swapParent && swapParent != this)
314 result = swapParent.Rebalance();
316 result = swap.Rebalance();
318 result = parent.Rebalance();
324 property int balanceFactor
328 int leftDepth = left ? (left.depth+1) : 0;
329 int rightDepth = right ? (right.depth+1) : 0;
330 return rightDepth - leftDepth;
334 thisclass Rebalance()
338 int factor = balanceFactor;
341 if(left.balanceFactor == 1)
348 if(right.balanceFactor == -1)
360 void SingleRotateRight()
364 if(this == parent.left)
366 else if(this == parent.right)
369 left.parent = parent;
372 if(left) left.parent = this;
375 depth = Max(left ? (left.depth+1) : 0, right ? (right.depth+1) : 0);
376 parent.depth = Max(parent.left ? (parent.left.depth+1) : 0, parent.right ? (parent.right.depth+1) : 0);
379 for(n = parent.parent; n; n = n.parent)
381 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
382 if(newDepth == n.depth)
389 void SingleRotateLeft()
393 if(this == parent.right)
394 parent.right = right;
395 else if(this == parent.left)
398 right.parent = parent;
401 if(right) right.parent = this;
404 depth = Max(left ? (left.depth+1) : 0, right ? (right.depth+1) : 0);
405 parent.depth = Max(parent.left ? (parent.left.depth+1) : 0, parent.right ? (parent.right.depth+1) : 0);
408 for(n = parent.parent; n; n = n.parent)
410 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
411 if(newDepth == n.depth)
418 void DoubleRotateRight()
420 left.SingleRotateLeft();
424 void DoubleRotateLeft()
426 right.SingleRotateRight();
431 public class CustomAVLTree<class BT:AVLNode, class KT = uint64> : Container<BT, I = KT>
439 IteratorPointer GetFirst() { return (IteratorPointer) (root ? root.minimum : null); }
440 IteratorPointer GetLast() { return (IteratorPointer) (root ? root.maximum : null); }
441 IteratorPointer GetPrev(IteratorPointer node) { return ((BT)node).prev; }
442 IteratorPointer GetNext(IteratorPointer node) { return ((BT)node).next; }
443 BT GetData(IteratorPointer node) { return (BT)node; }
444 bool SetData(IteratorPointer node, BT data)
446 // Not supported for CustomAVLTree
450 IteratorPointer Add(BT node)
456 Class Tclass = class(BT).templateArgs[0].dataTypeClass;
459 Tclass = class(BT).templateArgs[0].dataTypeClass =
460 eSystem_FindClass(__thisModule.application, class(BT).templateArgs[0].dataTypeString);
462 if(root.Add(Tclass /*class(BT).templateArgs[0].dataTypeClass*/, node))
463 root = node.Rebalance();
468 return (IteratorPointer)node;
471 void Remove(IteratorPointer node)
473 BT parent = ((BT)node).parent;
475 if(parent || root == (BT)node)
477 root = ((BT)node).RemoveSwapRight();
479 ((BT)node).parent = null;
483 void Delete(IteratorPointer _item)
486 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
487 CustomAVLTree::Remove(_item);
496 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
497 CustomAVLTree::Remove(item);
502 IteratorPointer Find(BT value)
504 return (IteratorPointer)value;
507 BT GetAtPosition(const KT pos, bool create, bool * justAdded)
509 return root ? root.Find(class(KT), pos) : null;