ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / ecere / src / com / containers / AVLTree.ec
1 namespace com;
2
3 import "CustomAVLTree"
4
5 public class AVLTree<class AT> : CustomAVLTree<BT = AVLNode<AT>, KT = AT, T = AT, D = AT>
6 {
7    class_fixed
8
9    AT GetData(AVLNode<AT> node)
10    {
11       return node ? ((class(AT).type == structClass) ? (AT)(((byte *)&(uint64)node.key) + __ENDIAN_PAD(sizeof(void *))) : node.key) : (AT)0;
12    }
13
14    bool SetData(AVLNode<AT> node, AT value)
15    {
16       if(!Find(value))
17       {
18          Remove(node);
19          if(class(AT).type == structClass)
20             memcpy((void *)(((byte *)&(uint64)node.key) + __ENDIAN_PAD(sizeof(void *))), (void *)value, class(AT).structSize);
21          else
22             node.key = value;
23          AVLTree::Add((AT)node);
24          return true;
25       }
26       return false;
27    }
28
29    AVLNode<AT> Add(AT value)
30    {
31       AVLNode<AT> node;
32       // TODO: Optimize this here to use FindEx/AddEx...
33       if(class(AT).type == structClass)
34       {
35          node = (AVLNode<AT>)new0 byte[sizeof(class AVLNode) + class(AT).structSize - sizeof(node.key)];
36          memcpy((void *)(((byte *)&(uint64)node.key) + __ENDIAN_PAD(sizeof(void *))), (void *)value, class(AT).structSize);
37       }
38       else
39          node = (AVLNode<AT>)AVLNode { key = value };
40       if(!CustomAVLTree::Add((AT)node))
41          delete node;
42       return node;
43    }
44
45    void Remove(AVLNode<AT> node)
46    {
47       CustomAVLTree::Remove(node);
48       delete node;
49    }
50
51    AVLNode<AT> Find(const AT key)
52    {
53       AVLNode<AT> root = this.root;
54       return root ? root.Find(class(AT), key) : null;
55    }
56
57    // *** FIND ALL COMPARES KEY FOR EQUALITY, NOT USING OnCompare ***
58    AVLNode<AT> FindAll(const AT key)
59    {
60       AVLNode<AT> root = this.root;
61       return root ? root.FindAll(key) : null;
62    }
63 };