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 ***
95 int result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass,
96 ((Tclass.type == systemClass && !Tclass.byValueSystemClass) || 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,
97 ((Tclass.type == systemClass && !Tclass.byValueSystemClass) || 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);
113 for(n = this; n; n = n.parent)
115 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
116 if(newDepth == n.depth)
135 for(n = this; n; n = n.parent)
137 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
138 if(newDepth == n.depth)
149 public thisclass Find(Class Tclass, const T key)
153 // *** NEED COMPARISON OPERATOR SUPPORT HERE INVOKING OnCompare, AS WELL AS TYPE INFORMATION PASSED ***
154 int result = ((int (*)(void *, void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnCompare])(Tclass,
155 ((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass) ? (((byte *)&(uint64)key) + __ENDIAN_PAD(Tclass.typeSize)) : (void *)key,
156 ((Tclass.type == systemClass && !Tclass.byValueSystemClass) || 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);
167 thisclass FindAll(const T key)
169 AVLNode<T> result = null;
170 // *** FIND ALL COMPARES KEY FOR EQUALITY, NOT USING OnCompare ***
171 if(this.key == key) result = this;
172 if(!result && left) result = left.FindAll(key);
173 if(!result && right) result = right.FindAll(key);
177 void RemoveSwap(thisclass swap)
181 swap.left.parent = swap.parent;
182 if(swap == swap.parent.left)
183 swap.parent.left = swap.left;
184 else if(swap == swap.parent.right)
185 swap.parent.right = swap.left;
190 swap.right.parent = swap.parent;
191 if(swap == swap.parent.left)
192 swap.parent.left = swap.right;
193 else if(swap == swap.parent.right)
194 swap.parent.right = swap.right;
198 if(swap == swap.parent.left) swap.parent.left = null;
199 else if(swap == swap.parent.right) swap.parent.right = null;
203 for(n = swap.parent; n; n = n.parent)
205 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
206 if(newDepth == n.depth)
221 swap.parent = parent;
226 if(this == parent.left) parent.left = swap;
227 else if(this == parent.right) parent.right = swap;
231 thisclass RemoveSwapLeft()
233 thisclass swap = left ? left.maximum : right;
234 thisclass swapParent = null;
235 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
238 if(this == parent.left) parent.left = null;
239 else if(this == parent.right) parent.right = null;
243 for(n = swap ? swap : parent; n; n = n.parent)
245 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
246 if(newDepth == n.depth && n != swap)
251 if(swapParent && swapParent != this)
252 return swapParent.Rebalance();
254 return swap.Rebalance();
256 return parent.Rebalance();
261 thisclass RemoveSwapRight()
264 thisclass swap = right ? right.minimum : left;
265 thisclass swapParent = null;
266 if(swap) { swapParent = swap.parent; RemoveSwap(swap); }
269 if(this == parent.left) parent.left = null;
270 else if(this == parent.right) parent.right = null;
274 for(n = swap ? swap : parent; n; n = n.parent)
276 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
278 if(newDepth == n.depth && n != swap)
283 if(swapParent && swapParent != this)
284 result = swapParent.Rebalance();
286 result = swap.Rebalance();
288 result = parent.Rebalance();
294 property int balanceFactor
298 int leftDepth = left ? (left.depth+1) : 0;
299 int rightDepth = right ? (right.depth+1) : 0;
300 return rightDepth - leftDepth;
304 thisclass Rebalance()
308 int factor = balanceFactor;
311 if(left.balanceFactor == 1)
318 if(right.balanceFactor == -1)
330 void SingleRotateRight()
334 if(this == parent.left)
336 else if(this == parent.right)
339 left.parent = parent;
342 if(left) left.parent = this;
345 depth = Max(left ? (left.depth+1) : 0, right ? (right.depth+1) : 0);
346 parent.depth = Max(parent.left ? (parent.left.depth+1) : 0, parent.right ? (parent.right.depth+1) : 0);
349 for(n = parent.parent; n; n = n.parent)
351 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
352 if(newDepth == n.depth)
359 void SingleRotateLeft()
363 if(this == parent.right)
364 parent.right = right;
365 else if(this == parent.left)
368 right.parent = parent;
371 if(right) right.parent = this;
374 depth = Max(left ? (left.depth+1) : 0, right ? (right.depth+1) : 0);
375 parent.depth = Max(parent.left ? (parent.left.depth+1) : 0, parent.right ? (parent.right.depth+1) : 0);
378 for(n = parent.parent; n; n = n.parent)
380 int newDepth = Max(n.left ? (n.left.depth+1) : 0, n.right ? (n.right.depth+1) : 0);
381 if(newDepth == n.depth)
388 void DoubleRotateRight()
390 left.SingleRotateLeft();
394 void DoubleRotateLeft()
396 right.SingleRotateRight();
401 public class CustomAVLTree<class BT:AVLNode, class KT = uint64> : Container<BT>
409 IteratorPointer GetFirst() { return (IteratorPointer) (root ? root.minimum : null); }
410 IteratorPointer GetLast() { return (IteratorPointer) (root ? root.maximum : null); }
411 IteratorPointer GetPrev(IteratorPointer node) { return ((BT)node).prev; }
412 IteratorPointer GetNext(IteratorPointer node) { return ((BT)node).next; }
413 BT GetData(IteratorPointer node) { return (BT)node; }
414 bool SetData(IteratorPointer node, BT data)
416 // Not supported for CustomAVLTree
420 IteratorPointer Add(BT node)
426 Class Tclass = class(BT).templateArgs[0].dataTypeClass;
429 Tclass = class(BT).templateArgs[0].dataTypeClass =
430 eSystem_FindClass(__thisModule.application, class(BT).templateArgs[0].dataTypeString);
432 if(root.Add(Tclass /*class(BT).templateArgs[0].dataTypeClass*/, node))
433 root = node.Rebalance();
438 return (IteratorPointer)node;
441 void Remove(IteratorPointer node)
443 BT parent = ((BT)node).parent;
445 if(parent || root == (BT)node)
447 root = ((BT)node).RemoveSwapRight();
449 ((BT)node).parent = null;
453 void Delete(IteratorPointer _item)
456 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
457 CustomAVLTree::Remove(_item);
466 // THIS SHOULDN'T BE CALLING THE VIRTUAL FUNCTION
467 CustomAVLTree::Remove(item);
472 IteratorPointer Find(BT value)
474 return (IteratorPointer)value;