Changeset 17831


Ignore:
Timestamp:
02/02/21 16:57:26 (16 months ago)
Author:
bburlacu
Message:

#3100: Use ValueTypeArray as base class for TriangularMatrix. A Data property exposes the underlying array as a ReadOnlyCollection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3087_Ceres_Integration/HeuristicLab.Data/3.3/TriangularMatrix.cs

    r17180 r17831  
    2626using HeuristicLab.Core;
    2727using HEAL.Attic;
     28using System.Collections.ObjectModel;
    2829
    2930namespace HeuristicLab.Data {
    3031  [Item("TriangularMatrix", "Represents a lower triangular matrix.")]
    3132  [StorableType("5C09A4FC-887E-4C40-8926-81325C09FA67")]
    32   public class TriangularMatrix<T> : ValueTypeMatrix<T>, IStringConvertibleMatrix where T : struct {
     33  public class TriangularMatrix<T> : ValueTypeArray<T>, IStringConvertibleArray where T : struct {
    3334    [Storable]
    34     private readonly T[] storage;
     35    private readonly int dimension;
     36    public int Dimension { get { return dimension; } }
    3537
    36     private readonly int dimension;
    37     private readonly long capacity;
     38    public ReadOnlyCollection<T> Data {
     39      get { return Array.AsReadOnly(array); }
     40    }
    3841
    3942    private TriangularMatrix() { }
    4043
    41     public TriangularMatrix(int dimension) {
     44    public TriangularMatrix(int dimension) : base(dimension * (dimension + 1) / 2) {
    4245      this.dimension = dimension;
    43       capacity = (long)dimension * (dimension + 1) / 2;
    44       storage = new T[capacity];
    45 
    46       readOnly = true;
     46      resizable = false;
    4747    }
    4848
     
    5151
    5252    protected TriangularMatrix(TriangularMatrix<T> original, Cloner cloner) : base(original, cloner) {
    53       capacity = original.capacity;
    5453      dimension = original.dimension;
    55       storage = (T[])original.storage.Clone();
     54      array = (T[])original.array.Clone();
    5655    }
    5756
     
    6059    }
    6160
    62     public override int Rows {
    63       get { return dimension; }
    64       protected set { throw new NotSupportedException(); }
    65     }
    66 
    67     public override int Columns {
    68       get { return dimension; }
    69       protected set { throw new NotSupportedException(); }
    70     }
    71 
    72     public int Length { get { return storage.Length; } }
    73 
    7461    // the indexing rule for the (lower-)triangular matrix is that always i <= j, otherwise an IndexOutOfBounds exception will occur
    75     public override T this[int rowIndex, int columnIndex] {
     62    public T this[int rowIndex, int columnIndex] {
    7663      get {
    7764        // provide symmetry of returned values
    7865        if (columnIndex > rowIndex) return this[columnIndex, rowIndex];
    79         return storage[rowIndex * (rowIndex + 1) / 2 + columnIndex];
     66        return array[rowIndex * (rowIndex + 1) / 2 + columnIndex];
    8067      }
    8168      set {
    8269        if (columnIndex > rowIndex) this[columnIndex, rowIndex] = value;
    83         else storage[rowIndex * (rowIndex + 1) / 2 + columnIndex] = value;
     70        else array[rowIndex * (rowIndex + 1) / 2 + columnIndex] = value;
    8471      }
    85     }
    86 
    87     public T this[int index] {
    88       get { return storage[index]; }
    89       set { storage[index] = value; }
    9072    }
    9173
     
    11395
    11496    public override IEnumerator<T> GetEnumerator() {
    115       return storage.Cast<T>().GetEnumerator();
     97      return array.Cast<T>().GetEnumerator();
    11698    }
    11799
     
    186168    }
    187169
    188     #region IStringConvertibleMatrix Members
    189     int IStringConvertibleMatrix.Rows {
    190       get { return dimension; }
    191       set { throw new NotSupportedException("The triangular matrix does not support changing the number of rows."); }
     170    #region IStringConvertibleArray members
     171    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
     172      return Validate(value, out errorMessage);
    192173    }
    193174
    194     int IStringConvertibleMatrix.Columns {
    195       get { return dimension; }
    196       set { throw new NotSupportedException("The triangular matrix does not support changing the number of columns."); }
     175    public string GetValue(int index) {
     176      return array[index].ToString();
    197177    }
    198178
    199     string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
    200       return GetValue(rowIndex, columnIndex);
    201     }
    202 
    203     bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
    204       return SetValue(value, rowIndex, columnIndex);
    205     }
    206 
    207     bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
    208       return Validate(value, out errorMessage);
     179    public bool SetValue(string value, int index) {
     180      if (TryParse(value, out T val)) {
     181        this[index] = val;
     182        return true;
     183      }
     184      throw new ArgumentException("Coult not parse value " + value + " as " + typeof(T));
    209185    }
    210186    #endregion
Note: See TracChangeset for help on using the changeset viewer.