- Timestamp:
- 12/02/08 14:13:06 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/CloningRefactorBranch/HeuristicLab.Data/ItemDictionary_T.cs
r737 r885 1 1 using System; 2 using System.Collections; 2 using System.Collections; 3 3 using System.Collections.Generic; 4 4 using System.Text; … … 12 12 /// <typeparam name="K">The type of the keys, which must implement the interface <see cref="IItem"/>.</typeparam> 13 13 /// <typeparam name="V">The type of the values, which must imlement the interface <see cref="IItem"/>.</typeparam> 14 public class ItemDictionary<K, V> : ItemBase, IDictionary<K,V>14 public class ItemDictionary<K, V> : ItemBase, IDictionary<K, V> 15 15 where K : IItem 16 where V : IItem {16 where V : IItem { 17 17 private Dictionary<K, V> dict; 18 18 … … 34 34 } 35 35 36 /// <summary> 37 /// Copy constructor to create clones (deep). 38 /// </summary> 39 /// <param name="original">The original instance to be cloned.</param> 40 public ItemDictionary(ItemDictionary<K, V> original) : this(original, new Dictionary<Guid, object>()) { } 41 42 /// <summary> 43 /// Copy constructor to create clones (deep) reusing already cloned object references. 44 /// </summary> 45 /// <param name="original">The instance to be cloned.</param> 46 /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param> 47 protected ItemDictionary(ItemDictionary<K, V> original, IDictionary<Guid, object> clonedObjects) 48 : base(original, clonedObjects) { 49 foreach (KeyValuePair<K, V> item in original.dict) { 50 this.dict.Add((K)Auxiliary.Clone(item.Key, clonedObjects), (V)Auxiliary.Clone(item.Value, clonedObjects)); 51 } 52 } 53 36 54 #region ItemBase Members 37 55 /// <summary> … … 40 58 /// <returns>The created instance as <see cref="ItemDictionaryView<K,V>"/>.</returns> 41 59 public override IView CreateView() { 42 return new ItemDictionaryView<K, V>(this);43 } 44 45 /// <summary> 46 /// Clones the current instance and adds it to the dictionary <paramref name="clonedObjects"/>.60 return new ItemDictionaryView<K, V>(this); 61 } 62 63 /// <summary> 64 /// Clones the current instance using the copy constructor. 47 65 /// </summary> 48 66 /// <remarks>Also the keys and values in the dictionary are cloned and saved to the dictionary <paramref name="clonedObjects"/>, … … 51 69 /// <returns>The cloned instance as <see cref="ItemDictionary<K,V>"/>.</returns> 52 70 public override object Clone(IDictionary<Guid, object> clonedObjects) { 53 ItemDictionary<K,V> clone = new ItemDictionary<K,V>(); 54 clonedObjects.Add(Guid, clone); 55 foreach (KeyValuePair<K, V> item in dict) { 56 clone.dict.Add((K) Auxiliary.Clone(item.Key, clonedObjects), (V) Auxiliary.Clone(item.Value, clonedObjects)); 57 } 58 return clone; 71 return new ItemDictionary<K, V>(this, clonedObjects); 59 72 } 60 73 … … 74 87 foreach (KeyValuePair<K, V> item in dict) { 75 88 XmlNode keyNode = PersistenceManager.Persist("Key", item.Key, document, persistedObjects); 76 XmlNode valueNode = PersistenceManager.Persist("Val", item.Value, document, persistedObjects); 77 XmlNode pairNode = document.CreateNode(XmlNodeType.Element, "KeyValuePair", null); 78 pairNode.AppendChild(keyNode); 79 pairNode.AppendChild(valueNode); 80 node.AppendChild(pairNode); 81 } 82 return node; 89 XmlNode valueNode = PersistenceManager.Persist("Val", item.Value, document, persistedObjects); 90 XmlNode pairNode = document.CreateNode(XmlNodeType.Element, "KeyValuePair", null); 91 pairNode.AppendChild(keyNode); 92 pairNode.AppendChild(valueNode); 93 node.AppendChild(pairNode); 94 } 95 return node; 83 96 } 84 97 … … 94 107 base.Populate(node, restoredObjects); 95 108 for (int i = 0; i < node.ChildNodes.Count; i++) { 96 K key = (K) 97 V val = (V) 98 dict[key] = val; 109 K key = (K)PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Key"), restoredObjects); 110 V val = (V)PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Val"), restoredObjects); 111 dict[key] = val; 99 112 } 100 113 } … … 154 167 /// <c>false</c> if the key was not found.</returns> 155 168 public bool Remove(K key) { 156 V value = dict[key]; 169 V value = dict[key]; 157 170 bool removed = dict.Remove(key); 158 OnItemRemoved(key, value); 159 return removed; 171 OnItemRemoved(key, value); 172 return removed; 160 173 } 161 174 … … 191 204 public void Add(KeyValuePair<K, V> item) { 192 205 dict.Add(item.Key, item.Value); 193 OnItemAdded(item.Key, item.Value); 206 OnItemAdded(item.Key, item.Value); 194 207 } 195 208 … … 201 214 public void Clear() { 202 215 dict.Clear(); 203 OnCleared(); 216 OnCleared(); 204 217 } 205 218 … … 220 233 /// <param name="arrayIndex"></param> 221 234 public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) { 222 throw new NotImplementedException(); 235 throw new NotImplementedException(); 223 236 } 224 237 … … 246 259 if (removed) { 247 260 OnItemRemoved(item.Key, item.Value); 248 } 249 return removed; 261 } 262 return removed; 250 263 } 251 264 #endregion … … 328 341 public bool Equals(T x, T y) { 329 342 if (x is IComparable) { 330 return (((IComparable) x).CompareTo(y) == 0);343 return (((IComparable)x).CompareTo(y) == 0); 331 344 } 332 345 if (y is IComparable) { 333 return (((IComparable) y).CompareTo(x) == 0);346 return (((IComparable)y).CompareTo(x) == 0); 334 347 } 335 return x.Equals(y); 348 return x.Equals(y); 336 349 } 337 350 … … 343 356 public int GetHashCode(T obj) { 344 357 if (obj is IObjectData) { 345 return ((IObjectData) obj).Data.GetHashCode();358 return ((IObjectData)obj).Data.GetHashCode(); 346 359 } 347 return obj.Guid.GetHashCode(); 360 return obj.Guid.GetHashCode(); 348 361 } 349 362 }
Note: See TracChangeset
for help on using the changeset viewer.