2a186ee2a149e14904b43f2689f9879d6cb177ab
[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 Remove(MapNode<MT, V> node)
127    {
128       CustomAVLTree::Remove(node);
129       if(class(MT).type == structClass)
130       {
131          // TODO: Make this easier...
132          Class Tclass = class(MT);
133          ((void (*)(void *, void *))(void *)Tclass._vTbl[__ecereVMethodID_class_OnFree])(Tclass, (((byte *)&node.key) + __ENDIAN_PAD(sizeof(void *))));
134       }
135       else
136          delete node.key;
137       delete node;
138    }
139
140    void Free()
141    {
142       MapNode<MT, V> node = root;
143       uintsize offset = class(MT).type == structClass ? class(MT).structSize - sizeof(node.AVLNode::key) : 0;
144       while(node)
145       {
146          if(node.left)
147          {
148             MapNode<MT, V> left = node.left;
149             node.left = null;
150             node = left;
151          }
152          else if(node.right)
153          {
154             MapNode<MT, V> right = node.right;
155             node.right = null;
156             node = right;
157          }
158          else
159          {
160             MapNode<MT, V> parent = node.parent;
161             MapNode<MT, V> n = (MapNode<MT, V>)((byte *)node + offset);
162             delete n.value;
163             delete node;
164
165             node = parent;
166          }
167       }
168       root = null;
169       count = 0;
170    }
171
172    void Delete(MapNode<MT, V> node)
173    {
174       MapNode<MT, V> n = node;
175
176       // Adjust node pointer for non-standard AVLNode
177       if(class(MT).type == structClass)
178          n = (MapNode<MT, V>)(((byte *) node) + class(MT).structSize - sizeof(node.AVLNode::key));
179
180       delete n.value;
181
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          else
215             onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(sizeof(void *)), (void *)pos);
216          CustomAVLTree::AddEx((T)node, (T)addNode, addSide);
217          if(justAdded) *justAdded = true;
218       }
219       return node;
220    }
221
222    void Copy(Container<T> source)
223    {
224       IteratorPointer i;
225       RemoveAll();
226       if(!eClass_IsDerived(source._class, class(Map)))
227       {
228          for(i = source.GetFirst(); i; i = source.GetNext(i))
229          {
230             MapNode<MT, V> srcNode = (MapNode<MT, V>)source.GetData(i);
231             MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
232             SetData(destNode, srcNode.value);
233          }
234          // ADDED THIS HERE TO FREE BUILTIN CONTAINERS ASSIGNED TO A MAP
235          if(source._class == class(BuiltInContainer))
236             source.Free();
237       }
238    }
239
240    public property Map mapSrc
241    {
242       set
243       {
244          IteratorPointer i;
245          RemoveAll();
246          if(eClass_IsDerived(value._class, class(Map)))
247          {
248             for(i = value.GetFirst(); i; i = value.GetNext(i))
249             {
250                MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
251                MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
252                SetData(destNode, GetData(srcNode));
253             }
254          }
255       }
256    }
257
258    void OnSerialize(IOChannel channel)
259    {
260       uint count = GetCount();
261       IteratorPointer i;
262       Class Kclass = class(MT);
263       Class Dclass = class(V);
264       bool kIsNormalClass = (Kclass.type == normalClass) && Kclass.structSize;
265       bool dIsNormalClass = (Dclass.type == normalClass) && Dclass.structSize;
266
267       channel.Put(count);
268       for(i = GetFirst(); i; i = GetNext(i))
269       {
270          MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
271          MT key = GetKey((MapNode<KT, V>)srcNode);
272          D data = GetData(srcNode);
273          Class kEclass = kIsNormalClass ? ((Instance)key)._class : Kclass;
274          Class dEclass = dIsNormalClass ? ((Instance)data)._class : Dclass;
275
276          ((void (*)(void *, void *, void *))(void *)kEclass._vTbl[__ecereVMethodID_class_OnSerialize])(kEclass,
277             ((Kclass.type == systemClass && !Kclass.byValueSystemClass) || Kclass.type == bitClass || Kclass.type == enumClass || Kclass.type == unitClass) ? &key : (void *)key, channel);
278          ((void (*)(void *, void *, void *))(void *)dEclass._vTbl[__ecereVMethodID_class_OnSerialize])(dEclass,
279             ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, channel);
280       }
281    }
282
283    void OnUnserialize(IOChannel channel)
284    {
285       uint c, count;
286       thisclass container = eInstance_New(_class.fullName);
287       Class Kclass = class(MT);
288       Class Dclass = class(V);
289
290       channel.Get(count);
291       for(c = 0; c < count; c++)
292       {
293          MapNode<MT, V> destNode;
294          MT key = (KT)0;
295          D data = (D)0;
296          ((void (*)(void *, void *, void *))(void *)Kclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Kclass, &key, channel);
297          ((void (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Dclass, &data, channel);
298          destNode = (MapNode<MT, V>)container.GetAtPosition(key, true, null);
299          container.SetData(destNode, data);
300       }
301       this = container;
302    }
303 }