Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/08 14:13:06 (15 years ago)
Author:
gkronber
Message:

Refactored cloning in HL.Core, HL.Data and HL.Constraints

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CloningRefactorBranch/HeuristicLab.Data/ItemDictionary_T.cs

    r737 r885  
    11using System;
    2 using System.Collections; 
     2using System.Collections;
    33using System.Collections.Generic;
    44using System.Text;
     
    1212  /// <typeparam name="K">The type of the keys, which must implement the interface <see cref="IItem"/>.</typeparam>
    1313  /// <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>
    1515    where K : IItem
    16     where V : IItem{
     16    where V : IItem {
    1717    private Dictionary<K, V> dict;
    1818
     
    3434    }
    3535
     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
    3654    #region ItemBase Members
    3755    /// <summary>
     
    4058    /// <returns>The created instance as <see cref="ItemDictionaryView&lt;K,V&gt;"/>.</returns>
    4159    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.
    4765    /// </summary>
    4866    /// <remarks>Also the keys and values in the dictionary are cloned and saved to the dictionary <paramref name="clonedObjects"/>,
     
    5169    /// <returns>The cloned instance as <see cref="ItemDictionary&lt;K,V&gt;"/>.</returns>
    5270    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);
    5972    }
    6073
     
    7487      foreach (KeyValuePair<K, V> item in dict) {
    7588        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;
    8396    }
    8497
     
    94107      base.Populate(node, restoredObjects);
    95108      for (int i = 0; i < node.ChildNodes.Count; i++) {
    96         K key = (K) PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Key"), restoredObjects);
    97         V val = (V) PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Val"), restoredObjects);
    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;
    99112      }
    100113    }
     
    154167    /// <c>false</c> if the key was not found.</returns>
    155168    public bool Remove(K key) {
    156       V value = dict[key]; 
     169      V value = dict[key];
    157170      bool removed = dict.Remove(key);
    158       OnItemRemoved(key, value); 
    159       return removed; 
     171      OnItemRemoved(key, value);
     172      return removed;
    160173    }
    161174
     
    191204    public void Add(KeyValuePair<K, V> item) {
    192205      dict.Add(item.Key, item.Value);
    193       OnItemAdded(item.Key, item.Value); 
     206      OnItemAdded(item.Key, item.Value);
    194207    }
    195208
     
    201214    public void Clear() {
    202215      dict.Clear();
    203       OnCleared(); 
     216      OnCleared();
    204217    }
    205218
     
    220233    /// <param name="arrayIndex"></param>
    221234    public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) {
    222       throw new NotImplementedException(); 
     235      throw new NotImplementedException();
    223236    }
    224237
     
    246259      if (removed) {
    247260        OnItemRemoved(item.Key, item.Value);
    248       } 
    249       return removed; 
     261      }
     262      return removed;
    250263    }
    251264    #endregion
     
    328341      public bool Equals(T x, T y) {
    329342        if (x is IComparable) {
    330           return (((IComparable) x).CompareTo(y) == 0);
     343          return (((IComparable)x).CompareTo(y) == 0);
    331344        }
    332345        if (y is IComparable) {
    333           return (((IComparable) y).CompareTo(x) == 0);
     346          return (((IComparable)y).CompareTo(x) == 0);
    334347        }
    335         return x.Equals(y); 
     348        return x.Equals(y);
    336349      }
    337350
     
    343356      public int GetHashCode(T obj) {
    344357        if (obj is IObjectData) {
    345           return ((IObjectData) obj).Data.GetHashCode();
     358          return ((IObjectData)obj).Data.GetHashCode();
    346359        }
    347         return obj.Guid.GetHashCode(); 
     360        return obj.Guid.GetHashCode();
    348361      }
    349362    }
Note: See TracChangeset for help on using the changeset viewer.