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 Remove(MapNode<MT, V> node)
128 CustomAVLTree::Remove(node);
129 if(class(MT).type == structClass)
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 *))));
145 MapNode<MT, V> n = node;
147 // Adjust node pointer for non-standard AVLNode
148 if(class(MT).type == structClass)
149 n = (MapNode<MT, V>)(((byte *) node) + class(MT).structSize - sizeof(node.AVLNode::key));
156 void Delete(MapNode<MT, V> node)
158 MapNode<MT, V> n = node;
160 // Adjust node pointer for non-standard AVLNode
161 if(class(MT).type == structClass)
162 n = (MapNode<MT, V>)(((byte *) node) + class(MT).structSize - sizeof(node.AVLNode::key));
169 MapNode<MT, V> Find(V value)
171 return (MapNode<MT, V>)Container::Find(value);
174 MapNode<MT, V> GetAtPosition(const MT pos, bool create, bool * justAdded)
176 MapNode<MT, V> node = root ? root.Find(class(MT), pos) : null;
179 Class Tclass = class(MT);
180 void (* onCopy)(void *, void *, void *) = Tclass._vTbl[__ecereVMethodID_class_OnCopy];
181 if(class(MT).type == structClass || class(V).type == structClass)
183 uint size = sizeof(class MapNode<MT, V>);
185 if(class(MT).type == structClass) size += class(MT).typeSize - sizeof(node.AVLNode::key);
186 if(class(V).type == structClass)
187 size += class(V).typeSize - sizeof(uint64); //sizeof(*&node.value); // TODO: Simplify code generation for this sizeof
188 node = (MapNode<MT, V>)new0 byte[size];
192 node = MapNode<MT, V> { key = pos };
194 if((Tclass.type == systemClass && !Tclass.byValueSystemClass) || Tclass.type == bitClass || Tclass.type == enumClass || Tclass.type == unitClass)
195 onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(Tclass.typeSize), (byte *)&pos + __ENDIAN_PAD(Tclass.typeSize));
197 onCopy(Tclass, (byte *)&node.key + __ENDIAN_PAD(sizeof(void *)), (void *)pos);
198 CustomAVLTree::Add((T)node);
199 if(justAdded) *justAdded = true;
204 void Copy(Container<T> source)
208 if(!eClass_IsDerived(source._class, class(Map)))
210 for(i = source.GetFirst(); i; i = source.GetNext(i))
212 MapNode<MT, V> srcNode = (MapNode<MT, V>)source.GetData(i);
213 MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
214 SetData(destNode, srcNode.value);
216 // ADDED THIS HERE TO FREE BUILTIN CONTAINERS ASSIGNED TO A MAP
217 if(source._class == class(BuiltInContainer))
222 public property Map mapSrc
228 if(eClass_IsDerived(value._class, class(Map)))
230 for(i = value.GetFirst(); i; i = value.GetNext(i))
232 MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
233 MapNode<MT, V> destNode = (MapNode<MT, V>)GetAtPosition(srcNode.key, true, null);
234 SetData(destNode, GetData(srcNode));
240 void OnSerialize(IOChannel channel)
242 uint count = GetCount();
244 Class Kclass = class(MT);
245 Class Dclass = class(V);
246 bool kIsNormalClass = (Kclass.type == normalClass) && Kclass.structSize;
247 bool dIsNormalClass = (Dclass.type == normalClass) && Dclass.structSize;
250 for(i = GetFirst(); i; i = GetNext(i))
252 MapNode<MT, V> srcNode = (MapNode<MT, V>)i;
253 MT key = GetKey((MapNode<KT, V>)srcNode);
254 D data = GetData(srcNode);
255 Class kEclass = kIsNormalClass ? ((Instance)key)._class : Kclass;
256 Class dEclass = dIsNormalClass ? ((Instance)data)._class : Dclass;
258 ((void (*)(void *, void *, void *))(void *)kEclass._vTbl[__ecereVMethodID_class_OnSerialize])(kEclass,
259 ((Kclass.type == systemClass && !Kclass.byValueSystemClass) || Kclass.type == bitClass || Kclass.type == enumClass || Kclass.type == unitClass) ? &key : (void *)key, channel);
260 ((void (*)(void *, void *, void *))(void *)dEclass._vTbl[__ecereVMethodID_class_OnSerialize])(dEclass,
261 ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, channel);
265 void OnUnserialize(IOChannel channel)
268 thisclass container = eInstance_New(_class.fullName);
269 Class Kclass = class(MT);
270 Class Dclass = class(V);
273 for(c = 0; c < count; c++)
275 MapNode<MT, V> destNode;
278 ((void (*)(void *, void *, void *))(void *)Kclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Kclass, &key, channel);
279 ((void (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnUnserialize])(Dclass, &data, channel);
280 destNode = (MapNode<MT, V>)container.GetAtPosition(key, true, null);
281 container.SetData(destNode, data);