Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/08 14:13:06 (16 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/DoubleArrayData.cs

    r763 r885  
    5656      Data = data;
    5757    }
     58    /// <summary>
     59    /// Copy constructor to create clones (deep).
     60    /// </summary>
     61    /// <param name="original">The original instance to be cloned.</param>
     62    public DoubleArrayData(DoubleArrayData original) : this(original, new Dictionary<Guid, object>()) { }
     63    /// <summary>
     64    /// Copy constructor to create clones (deep) reusing already cloned object references.
     65    /// </summary>
     66    /// <param name="original">The instance to be cloned.</param>
     67    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     68    protected DoubleArrayData(DoubleArrayData original, IDictionary<Guid, object> clonedObjects)
     69      : base(original, clonedObjects) { }
    5870
    5971    /// <summary>
     
    6375    public override IView CreateView() {
    6476      return new DoubleArrayDataView(this);
     77    }
     78
     79    /// <summary>
     80    /// Clones an instance using the copy constructor
     81    /// reusing already cloned object references.
     82    /// </summary>
     83    /// <param name="clonedObjects">Already cloned objects (for referential integrity).</param>
     84    /// <returns>A cloned (deep) instance</returns>
     85    public override object Clone(IDictionary<Guid, object> clonedObjects) {
     86      return new DoubleArrayData(this, clonedObjects);
    6587    }
    6688
     
    7496    /// <param name="persistedObjects">A dictionary of all already persisted objects. (Needed to avoid cycles.)</param>
    7597    /// <returns>The saved <see cref="XmlNode"></see>.</returns>
    76     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
     98    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    7799      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    78100      node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat);
    79101      return node;
    80102    }
    81    
     103
    82104    /// <summary>
    83105    /// Loads the persisted double array from the specified <paramref name="node"/>.
     
    89111    /// <param name="node">The <see cref="XmlNode"></see> where the instance is saved.</param>
    90112    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
    91     public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     113    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    92114      base.Populate(node, restoredObjects);
    93115      string[] tokens = node.InnerText.Split(';');
    94116      double[] data = new double[tokens.Length];
    95       for(int i = 0; i < data.Length; i++)
    96         if(double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) == false) {
     117      for (int i = 0; i < data.Length; i++)
     118        if (double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) == false) {
    97119          throw new FormatException("Can't parse " + tokens[i] + " as double value.");
    98120        }
     
    109131    private string ToString(NumberFormatInfo format) {
    110132      StringBuilder builder = new StringBuilder();
    111       for(int i = 0; i < Data.Length; i++) {
     133      for (int i = 0; i < Data.Length; i++) {
    112134        builder.Append(";");
    113135        builder.Append(Data[i].ToString("r", format));
    114136      }
    115       if(builder.Length > 0)
     137      if (builder.Length > 0)
    116138        builder.Remove(0, 1);
    117139      return builder.ToString();
Note: See TracChangeset for help on using the changeset viewer.