Changeset 17831
- Timestamp:
- 02/02/21 16:57:26 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3087_Ceres_Integration/HeuristicLab.Data/3.3/TriangularMatrix.cs
r17180 r17831 26 26 using HeuristicLab.Core; 27 27 using HEAL.Attic; 28 using System.Collections.ObjectModel; 28 29 29 30 namespace HeuristicLab.Data { 30 31 [Item("TriangularMatrix", "Represents a lower triangular matrix.")] 31 32 [StorableType("5C09A4FC-887E-4C40-8926-81325C09FA67")] 32 public class TriangularMatrix<T> : ValueType Matrix<T>, IStringConvertibleMatrixwhere T : struct {33 public class TriangularMatrix<T> : ValueTypeArray<T>, IStringConvertibleArray where T : struct { 33 34 [Storable] 34 private readonly T[] storage; 35 private readonly int dimension; 36 public int Dimension { get { return dimension; } } 35 37 36 private readonly int dimension; 37 private readonly long capacity; 38 public ReadOnlyCollection<T> Data { 39 get { return Array.AsReadOnly(array); } 40 } 38 41 39 42 private TriangularMatrix() { } 40 43 41 public TriangularMatrix(int dimension) {44 public TriangularMatrix(int dimension) : base(dimension * (dimension + 1) / 2) { 42 45 this.dimension = dimension; 43 capacity = (long)dimension * (dimension + 1) / 2; 44 storage = new T[capacity]; 45 46 readOnly = true; 46 resizable = false; 47 47 } 48 48 … … 51 51 52 52 protected TriangularMatrix(TriangularMatrix<T> original, Cloner cloner) : base(original, cloner) { 53 capacity = original.capacity;54 53 dimension = original.dimension; 55 storage = (T[])original.storage.Clone();54 array = (T[])original.array.Clone(); 56 55 } 57 56 … … 60 59 } 61 60 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 74 61 // the indexing rule for the (lower-)triangular matrix is that always i <= j, otherwise an IndexOutOfBounds exception will occur 75 public overrideT this[int rowIndex, int columnIndex] {62 public T this[int rowIndex, int columnIndex] { 76 63 get { 77 64 // provide symmetry of returned values 78 65 if (columnIndex > rowIndex) return this[columnIndex, rowIndex]; 79 return storage[rowIndex * (rowIndex + 1) / 2 + columnIndex];66 return array[rowIndex * (rowIndex + 1) / 2 + columnIndex]; 80 67 } 81 68 set { 82 69 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; 84 71 } 85 }86 87 public T this[int index] {88 get { return storage[index]; }89 set { storage[index] = value; }90 72 } 91 73 … … 113 95 114 96 public override IEnumerator<T> GetEnumerator() { 115 return storage.Cast<T>().GetEnumerator();97 return array.Cast<T>().GetEnumerator(); 116 98 } 117 99 … … 186 168 } 187 169 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); 192 173 } 193 174 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(); 197 177 } 198 178 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)); 209 185 } 210 186 #endregion
Note: See TracChangeset
for help on using the changeset viewer.