ecere/com/Map;: Fixed memory leaks
[sdk] / ecere / src / com / containers / Map.ec
1 namespace com;
2
3 import "instance"  // TOFIX: This is required to build Debug on Ubuntu 10.04, GCC 4.4.3
4 import "CustomAVLTree"
5
6 default:
7 extern int __ecereVMethodID_class_OnCopy;
8 extern int __ecereVMethodID_class_OnFree;
9 extern int __ecereVMethodID_class_OnSerialize;
10 extern int __ecereVMethodID_class_OnUnserialize;
11 private:
12
13 public class MapNode<class KT, class V> : private AVLNode<KT>
14 {
15 class_fixed
16
17 public:
18    // public(key)
19    // THIS IS MISSING CODE FOR struct KEYS
20    property const KT key
21    {
22       get { return AVLNode::key; }
23       set { AVLNode::key = value; }
24    };
25    property V value
26    {
27       get { return this ? this.value : (V)0; }
28       set { this.value = value; }
29    };
30    V value;
31
32    // BECAUSE WE'RE PRIVATELY INHERITING, UNTIL public() works
33    property thisclass prev { get { return (MapNode<KT,V>)AVLNode::prev; } }
34    property thisclass next { get { return (MapNode<KT,V>)AVLNode::next; } }
35    property thisclass minimum { get { return (MapNode<KT,V>)AVLNode::minimum; } }
36    property thisclass maximum { get { return (MapNode<KT,V>)AVLNode::maximum; } }
37 }
38
39 public struct MapIterator<class KT, class V> : Iterator<V, IT = KT>
40 {
41    property Map map
42    {
43       set { container = (Container<V, IT>)value; }
44       get { return (Map<KT, V>)container; }
45    }
46    property const KT key
47    {
48       get { return ((Map<KT, V>)container).GetKey((MapNode<KT, V>)pointer); }
49    }
50    property V value
51    {
52       get { return container.GetData(pointer); }
53       set { container.SetData(pointer, value); }
54    }
55 };
56
57 public class Map<class MT, class V> : CustomAVLTree<MapNode<MT, V>, I = MT, D = V, KT = MT>
58 {
59    class_fixed
60
61    MT GetKey(MapNode<KT, V> node)
62    {
63       if(class(MT).type == structClass)
64          return (MT)(((byte *)&(uint64)node.key) + __ENDIAN_PAD(sizeof(void *)));
65       return node.key;
66    }
67
68    V GetData(MapNode<MT, V> node)
69    {
70       if(node)
71       {
72          // Adjust node pointer for non-standard AVLNode
73          if(class(MT).type == structClass)
74             node = (MapNode<MT, V>)(((byte *) node) + class(MT).structSize - sizeof(node.AVLNode::key));
75          return (class(V).type == structClass) ? (MT)&node.value : node.value;
76       }
77       return (MT)0;
78    }
79
80    bool SetData(MapNode<MT, V> node, MT value)
81    {
82       // Adjust node pointer for non-standard AVLNode
83       if(class(MT).type == structClass)
84          node = (MapNode<MT, V>)(((byte *) node) + class(MT).structSize - sizeof(node.AVLNode::key));
85
86       if(class(V).type == structClass)
87          memcpy((void *)&node.value, (void *)value, class(V).structSize);
88       else
89          node.value = value;
90       return true;
91    }
92
93    MapNode<MT, V> Add(BT _newNode)
94    {
95       MapNode<MT, V> newNode = (MapNode<MT, V>) _newNode;
96       if(class(MT).type == structClass || class(V).type == structClass)
97       {
98          MapNode<MT, V> realNode = (MapNode<MT, V>)GetAtPosition(newNode.key, true, null);
99          SetData(realNode, newNode.value);
100          return newNode;
101       }
102       else
103       {
104          MapNode<MT, V> node = root ? root.Find(class(MT), (T)newNode.key) : null;
105          if(!node)
106          {
107             Class Tclass = class(MT);
108             void (* onCopy)(void *, void *, void *) = Tclass._vTbl[__ecereVMethodID_class_OnCopy];
109             // Copy key here
110             if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass)
111                onCopy(Tclass, (byte *)&newNode.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&newNode.key + __ENDIAN_PAD(Tclass.typeSize));
112             else
113                onCopy(Tclass, (byte *)&newNode.key + __ENDIAN_PAD(sizeof(void *)), (void *)newNode.key);
114
115             CustomAVLTree::Add((T)newNode);
116             return newNode;
117          }
118          else
119          {
120             delete newNode;
121             return null;
122          }
123       }
124    }
125
126    void FreeKey(MapNode<MT, V> node)
127    {
128       if(class(MT).type == structClass)
129       {
130          // TODO: Make this easier...
131          Class Tclass = class(MT);
132          ((void (*)(void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnFree])(Tclass, (((byte *)&node.key) + __ENDIAN_PAD(sizeof(void *))));
133       }
134       else
135          delete node.key;
136    }
137
138    void Remove(MapNode<MT, V> node)
139    {
140       CustomAVLTree::Remove(node);
141       FreeKey(node);
142       delete node;
143    }
144
145    void Free()
146    {
147       MapNode<MT, V> node = root;
148       while(node)
149       {
150          if(node.left)
151          {
152             MapNode<MT, V> left = node.left;
153             node.left = null;
154             node = left;
155          }
156          else if(node.right)
157          {
158             MapNode<MT, V> right = node.right;
159             node.right = null;
160             node = right;
161          }
162          else
163          {
164             MapNode<MT, V> parent = node.parent;
165             V value = GetData(node);
166             delete value;
167             FreeKey(node);
168             delete node;
169
170             node = parent;
171          }
172       }
173       root = null;
174       count = 0;
175    }
176
177    void Delete(MapNode<MT, V> node)
178    {
179       V value = GetData(node);
180       delete value;
181       FreeKey(node);
182       Remove(node);
183    }
184
185    MapNode<MT, V> Find(V value)
186    {
187       return (MapNode<MT, V>)Container::Find(value);
188    }
189
190    MapNode<MT, V> GetAtPosition(const MT pos, bool create, bool * justAdded)
191    {
192       AVLNode addNode = null;
193       AddSide addSide = compare;
194       MapNode<MT, V> node = root ? root.FindEx(class(MT), pos, &addNode, &addSide) : null;
195       if(!node && create)
196       {
197          Class Tclass = class(MT);
198          void (* onCopy)(void *, void *, void *) = Tclass._vTbl[__ecereVMethodID_class_OnCopy];
199          if(class(MT).type == structClass || class(V).type == structClass)
200          {
201             uint size = sizeof(class MapNode<MT, V>);
202
203             if(class(MT).type == structClass) size += class(MT).typeSize - sizeof(node.AVLNode::key);
204             if(class(V).type == structClass)
205                size += class(V).typeSize - sizeof(uint64); //sizeof(*&node.value);  // TODO: Simplify code generation for this sizeof
206             node = (MapNode<MT, V>)new0 byte[size];
207          }
208          else
209          {
210             node = MapNode<MT, V> { key = pos };
211          }
212          if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass)
213             // onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&pos + __ENDIAN_PAD(Tclass.typeSize));
214             memcpy((byte *)&node.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&pos + __ENDIAN_PAD(Tclass.typeSize), Tclass.typeSize);
215          else
216             onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(sizeof(void *)), (void *)pos);
217          CustomAVLTree::AddEx((T)(uintptr)node, (T)(uintptr)addNode, addSide);
218          if(justAdded) *justAdded = true;
219       }
220       return node;
221    }
222
223    void Copy(Container<T> source)
224    {
225       IteratorPointer i;
226       RemoveAll();
227       if(!eClass_IsDerived(source._class, class(Map)))
228       {
229          for(i = source.GetFirst(); i; i = source.GetNext(i))
230          {
231             MapNode<MT, V> srcNode = (MapNode<MT, V>)source.GetData(i);
232             MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
233             SetData(destNode, srcNode.value);
234          }
235          // ADDED THIS HERE TO FREE BUILTIN CONTAINERS ASSIGNED TO A MAP
236          if(source._class == class(BuiltInContainer))
237             source.Free();
238       }
239    }
240
241    public property Map mapSrc
242    {
243       set
244       {
245          IteratorPointer i;
246          RemoveAll();
247          if(value && eClass_IsDerived(value._class, class(Map)))
248          {
249             for(i = value.GetFirst(); i; i = value.GetNext(i))
250             {
251                MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
252                MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
253                SetData(destNode, GetData(srcNode));
254             }
255          }
256       }
257    }
258
259    void OnSerialize(IOChannel channel)
260    {
261       uint count = GetCount();
262       IteratorPointer i;
263       Class Kclass = class(MT);
264       Class Dclass = class(V);
265       bool kIsNormalClass = (Kclass.type == normalClass) && Kclass.structSize;
266       bool dIsNormalClass = (Dclass.type == normalClass) && Dclass.structSize;
267
268       channel.Put(count);
269       for(i = GetFirst(); i; i = GetNext(i))
270       {
271          MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
272          MT key = GetKey((MapNode<KT, V>)srcNode);
273          D data = GetData(srcNode);
274          Class kEclass = kIsNormalClass ? ((Instance)key)._class : Kclass;
275          Class dEclass = dIsNormalClass ? ((Instance)data)._class : Dclass;
276
277          ((void (*)(void *, void *, void *))(void *)kEclass._vTbl[__ecereVMethodID_class_OnSerialize])(kEclass,
278             ((Kclass.type == systemClass && !Kclass.byValueSystemClass) || Kclass.type == bitClass || Kclass.type == enumClass || Kclass.type == unitClass) ? &key : (void *)key, channel);
279          ((void (*)(void *, void *, void *))(void *)dEclass._vTbl[__ecereVMethodID_class_OnSerialize])(dEclass,
280             ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, channel);
281       }
282    }
283
284    void OnUnserialize(IOChannel channel)
285    {
286       uint c, count;
287       thisclass container = eInstance_New(_class.fullName);
288       Class Kclass = class(MT);
289       Class Dclass = class(V);
290
291       channel.Get(count);
292       for(c = 0; c < count; c++)
293       {
294          MapNode<MT, V> destNode;
295          MT key = (KT)0;
296          D data = (D)0;
297          ((void (*)(void *, void *, void *))(void *)Kclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Kclass, &key, channel);
298          ((void (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Dclass, &data, channel);
299          destNode = (MapNode<MT, V>)container.GetAtPosition(key, true, null);
300          container.SetData(destNode, data);
301       }
302       this = container;
303    }
304 }