3 import "instance" // TOFIX: This is required to build Debug on Ubuntu 10.04, GCC 4.4.3
7 extern int __ecereVMethodID_class_OnCopy;
8 extern int __ecereVMethodID_class_OnFree;
9 extern int __ecereVMethodID_class_OnSerialize;
10 extern int __ecereVMethodID_class_OnUnserialize;
13 public class MapNode<class KT, class V> : private AVLNode<KT>
19 // THIS IS MISSING CODE FOR struct KEYS
22 get { return AVLNode::key; }
23 set { AVLNode::key = value; }
27 get { return this ? this.value : (V)0; }
28 set { this.value = value; }
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; } }
39 public struct MapIterator<class KT, class V> : Iterator<V, IT = KT>
43 set { container = (Container<V, IT>)value; }
44 get { return (Map<KT, V>)container; }
48 get { return ((Map<KT, V>)container).GetKey((MapNode<KT, V>)pointer); }
52 get { return container.GetData(pointer); }
53 set { container.SetData(pointer, value); }
57 public class Map<class MT, class V> : CustomAVLTree<MapNode<MT, V>, I = MT, D = V, KT = MT>
61 MT GetKey(MapNode<KT, V> node)
63 if(class(MT).type == structClass)
64 return (MT)(((byte *)&(uint64)node.key) + __ENDIAN_PAD(sizeof(void *)));
68 V GetData(MapNode<MT, V> node)
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;
80 bool SetData(MapNode<MT, V> node, MT value)
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));
86 if(class(V).type == structClass)
87 memcpy((void *)&node.value, (void *)value, class(V).structSize);
93 MapNode<MT, V> Add(BT _newNode)
95 MapNode<MT, V> newNode = (MapNode<MT, V>) _newNode;
96 if(class(MT).type == structClass || class(V).type == structClass)
98 MapNode<MT, V> realNode = (MapNode<MT, V>)GetAtPosition(newNode.key, true, null);
99 SetData(realNode, newNode.value);
104 MapNode<MT, V> node = root ? root.Find(class(MT), (T)newNode.key) : null;
107 Class Tclass = class(MT);
108 void (* onCopy)(void *, void *, void *) = Tclass._vTbl[__ecereVMethodID_class_OnCopy];
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));
113 onCopy(Tclass, (byte *)&newNode.key + __ENDIAN_PAD(sizeof(void *)), (void *)newNode.key);
115 CustomAVLTree::Add((T)newNode);
126 void FreeKey(MapNode<MT, V> node)
128 if(class(MT).type == structClass)
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 *))));
140 MapNode<MT, V> node = root;
145 MapNode<MT, V> left = node.left;
151 MapNode<MT, V> right = node.right;
157 MapNode<MT, V> parent = node.parent;
168 void Remove(MapNode<MT, V> node)
170 CustomAVLTree::Remove(node);
177 MapNode<MT, V> node = root;
182 MapNode<MT, V> left = node.left;
188 MapNode<MT, V> right = node.right;
194 MapNode<MT, V> parent = node.parent;
195 V value = GetData(node);
207 void Delete(MapNode<MT, V> node)
209 V value = GetData(node);
215 MapNode<MT, V> Find(V value)
217 return (MapNode<MT, V>)Container::Find(value);
220 MapNode<MT, V> GetAtPosition(const MT pos, bool create, bool * justAdded)
222 AVLNode addNode = null;
223 AddSide addSide = compare;
224 MapNode<MT, V> node = root ? root.FindEx(class(MT), pos, &addNode, &addSide) : null;
227 Class Tclass = class(MT);
228 void (* onCopy)(void *, void *, void *) = Tclass._vTbl[__ecereVMethodID_class_OnCopy];
229 if(class(MT).type == structClass || class(V).type == structClass)
231 uint size = sizeof(class MapNode<MT, V>);
233 if(class(MT).type == structClass) size += class(MT).typeSize - sizeof(node.AVLNode::key);
234 if(class(V).type == structClass)
235 size += class(V).typeSize - sizeof(uint64); //sizeof(*&node.value); // TODO: Simplify code generation for this sizeof
236 node = (MapNode<MT, V>)new0 byte[size];
240 node = MapNode<MT, V> { key = pos };
242 if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass)
243 // onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&pos + __ENDIAN_PAD(Tclass.typeSize));
244 memcpy((byte *)&node.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&pos + __ENDIAN_PAD(Tclass.typeSize), Tclass.typeSize);
246 onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(sizeof(void *)), (void *)pos);
247 CustomAVLTree::AddEx((T)(uintptr)node, (T)(uintptr)addNode, addSide);
248 if(justAdded) *justAdded = true;
253 void Copy(Container<T> source)
257 if(!eClass_IsDerived(source._class, class(Map)))
259 for(i = source.GetFirst(); i; i = source.GetNext(i))
261 MapNode<MT, V> srcNode = (MapNode<MT, V>)source.GetData(i);
262 MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
263 SetData(destNode, srcNode.value);
265 // ADDED THIS HERE TO FREE BUILTIN CONTAINERS ASSIGNED TO A MAP
266 if(source._class == class(BuiltInContainer))
271 public property Map mapSrc
277 if(value && eClass_IsDerived(value._class, class(Map)))
279 for(i = value.GetFirst(); i; i = value.GetNext(i))
281 MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
282 MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
283 SetData(destNode, GetData(srcNode));
289 void OnSerialize(IOChannel channel)
291 uint count = GetCount();
293 Class Kclass = class(MT);
294 Class Dclass = class(V);
295 bool kIsNormalClass = (Kclass.type == normalClass) && Kclass.structSize;
296 bool dIsNormalClass = (Dclass.type == normalClass) && Dclass.structSize;
299 for(i = GetFirst(); i; i = GetNext(i))
301 MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
302 MT key = GetKey((MapNode<KT, V>)srcNode);
303 D data = GetData(srcNode);
304 Class kEclass = kIsNormalClass ? ((Instance)key)._class : Kclass;
305 Class dEclass = dIsNormalClass ? ((Instance)data)._class : Dclass;
307 ((void (*)(void *, void *, void *))(void *)kEclass._vTbl[__ecereVMethodID_class_OnSerialize])(kEclass,
308 ((Kclass.type == systemClass && !Kclass.byValueSystemClass) || Kclass.type == bitClass || Kclass.type == enumClass || Kclass.type == unitClass) ? &key : (void *)key, channel);
309 ((void (*)(void *, void *, void *))(void *)dEclass._vTbl[__ecereVMethodID_class_OnSerialize])(dEclass,
310 ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, channel);
314 void OnUnserialize(IOChannel channel)
317 thisclass container = eInstance_New(_class.fullName);
318 Class Kclass = class(MT);
319 Class Dclass = class(V);
322 for(c = 0; c < count; c++)
324 MapNode<MT, V> destNode;
327 ((void (*)(void *, void *, void *))(void *)Kclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Kclass, &key, channel);
328 ((void (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Dclass, &data, channel);
329 destNode = (MapNode<MT, V>)container.GetAtPosition(key, true, null);
330 container.SetData(destNode, data);