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)
90 uint64 newKey = (uint64)node.key;
92 Tclass = class(uint64);
95 // *** NEED COMPARISON OPERATOR SUPPORT HERE INVOKING OnCompare, AS WELL AS TYPE INFORMATION PASSED ***
96 int result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass,
97 (Tclass.type == systemClass || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass || Tclass.type == structClass) ? (((byte *)&(uint64)node.key) + __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize)) : (void *)node.key,
98 (Tclass.type == systemClass || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass || Tclass.type == structClass) ? (((byte *)&(uint64)key) + __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize)) : (void *)key);
114 for(n = this; n; n = n.parent)
116 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
117 if(newDepth == n.depth)
136 for(n = this; n; n = n.parent)
138 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
139 if(newDepth == n.depth)
150 public thisclass Find(Class Tclass, T key)
154 // *** NEED COMPARISON OPERATOR SUPPORT HERE INVOKING OnCompare, AS WELL AS TYPE INFORMATION PASSED ***
155 int result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass,
156 (Tclass.type == systemClass || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass) ? (((byte *)&(uint64)key) + __ENDIAN_PAD(Tclass.typeSize)) : (void *)key,
157 (Tclass.type == systemClass || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass || Tclass.type == structClass) ? (((byte *)&(uint64)this.key) + __ENDIAN_PAD((Tclass.type == structClass) ? sizeof(void *) : Tclass.typeSize)) : (void *)this.key);
168 thisclass FindAll(T key)
170 AVLNode<T> result = null;
171 // *** FIND ALL COMPARES KEY FOR EQUALITY, NOT USING OnCompare ***
172 if(this.key == key) result = this;
173 if(!result && left) result = left.FindAll(key);
174 if(!result && right) result = right.FindAll(key);
178 void RemoveSwap(thisclass swap)
182 swap.left.parent = swap.parent;
183 if(swap == swap.parent.left)
184 swap.parent.left = swap.left;
185 else if(swap == swap.parent.right)
186 swap.parent.right = swap.left;
191 swap.right.parent = swap.parent;
192 if(swap == swap.parent.left)
193 swap.parent.left = swap.right;
194 else if(swap == swap.parent.right)
195 swap.parent.right = swap.right;
199 if(swap == swap.parent.left) swap.parent.left = null;
200 else if(swap == swap.parent.right) swap.parent.right = null;
204 for(n = swap.parent; n; n = n.parent)
206 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
207 if(newDepth == n.depth)
222 swap.parent = parent;
227 if(this == parent.left) parent.left = swap;
228 else if(this == parent.right) parent.right = swap;
232 thisclass RemoveSwapLeft()
234 thisclass swap = left ? left.maximum : right;
235 thisclass swapParent = null;
236 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
239 if(this == parent.left) parent.left = null;
240 else if(this == parent.right) parent.right = null;
244 for(n = swap ? swap : parent; n; n = n.parent)
246 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
247 if(newDepth == n.depth && n != swap)
252 if(swapParent && swapParent != this)
253 return swapParent.Rebalance();
255 return swap.Rebalance();
257 return parent.Rebalance();
262 thisclass RemoveSwapRight()
265 thisclass swap = right ? right.minimum : left;
266 thisclass swapParent = null;
267 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
270 if(this == parent.left) parent.left = null;
271 else if(this == parent.right) parent.right = null;
275 for(n = swap ? swap : parent; n; n = n.parent)
277 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
279 if(newDepth == n.depth && n != swap)
284 if(swapParent && swapParent != this)
285 result = swapParent.Rebalance();
287 result = swap.Rebalance();
289 result = parent.Rebalance();
295 property int balanceFactor
299 int leftDepth = left ? (left.depth+1) : 0;
300 int rightDepth = right ? (right.depth+1) : 0;
301 return rightDepth - leftDepth;
305 thisclass Rebalance()
309 int factor = balanceFactor;
312 if(left.balanceFactor == 1)
319 if(right.balanceFactor == -1)
331 void SingleRotateRight()
335 if(this == parent.left)
337 else if(this == parent.right)
340 left.parent = parent;
343 if(left) left.parent = this;
346 depth = Max(left ? (left.depth+1) : 0, right ? (right.depth+1) : 0);
347 parent.depth = Max(parent.left ? (parent.left.depth+1) : 0, parent.right ? (parent.right.depth+1) : 0);
350 for(n = parent.parent; n; n = n.parent)
352 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
353 if(newDepth == n.depth)
360 void SingleRotateLeft()
364 if(this == parent.right)
365 parent.right = right;
366 else if(this == parent.left)
369 right.parent = parent;
372 if(right) right.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 DoubleRotateRight()
391 left.SingleRotateLeft();
395 void DoubleRotateLeft()
397 right.SingleRotateRight();
402 public class CustomAVLTree<class BT:AVLNode, class KT = uint64> : Container<BT>
410 IteratorPointer GetFirst() { return (IteratorPointer) (root ? root.minimum : null); }
411 IteratorPointer GetLast() { return (IteratorPointer) (root ? root.maximum : null); }
412 IteratorPointer GetPrev(IteratorPointer node) { return ((BT)node).prev; }
413 IteratorPointer GetNext(IteratorPointer node) { return ((BT)node).next; }
414 BT GetData(IteratorPointer node) { return (BT)node; }
415 bool SetData(IteratorPointer node, BT data)
417 // Not supported for CustomAVLTree
421 IteratorPointer Add(BT node)
427 Class Tclass = class(BT).templateArgs[0].dataTypeClass;
430 Tclass = class(BT).templateArgs[0].dataTypeClass =
431 eSystem_FindClass(__thisModule.application, class(BT).templateArgs[0].dataTypeString);
433 if(root.Add(Tclass /*class(BT).templateArgs[0].dataTypeClass*/, node))
434 root = node.Rebalance();
439 return (IteratorPointer)node;
442 void Remove(IteratorPointer node)
444 BT parent = ((BT)node).parent;
446 if(parent || root == (BT)node)
448 root = ((BT)node).RemoveSwapRight();
450 ((BT)node).parent = null;
454 void Delete(IteratorPointer _item)
457 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
458 CustomAVLTree::Remove(_item);
467 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
468 CustomAVLTree::Remove(item);
473 IteratorPointer Find(BT value)
475 return (IteratorPointer)value;