Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Data/3.3/ValueTypeMatrix.cs @ 16654

Last change on this file since 16654 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("ValueTypeMatrix", "An abstract base class for representing matrices of value types.")]
34  [StorableClass]
35  public abstract class ValueTypeMatrix<T> : Item, IEnumerable<T> where T : struct {
36    private const int maximumToStringLength = 100;
37
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
40    }
41
42    [Storable]
43    protected T[,] matrix;
44
45    [Storable]
46    protected List<string> columnNames;
47    public virtual IEnumerable<string> ColumnNames {
48      get { return this.columnNames; }
49      set {
50        if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. ValueTypeMatrix is read-only.");
51        if (value == null || value.Count() == 0)
52          columnNames = new List<string>();
53        else if (value.Count() != Columns)
54          throw new ArgumentException("A column name must be specified for each column.");
55        else
56          columnNames = new List<string>(value);
57        OnColumnNamesChanged();
58      }
59    }
60    [Storable]
61    protected List<string> rowNames;
62    public virtual IEnumerable<string> RowNames {
63      get { return this.rowNames; }
64      set {
65        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. ValueTypeMatrix is read-only.");
66        if (value == null || value.Count() == 0)
67          rowNames = new List<string>();
68        else if (value.Count() != Rows)
69          throw new ArgumentException("A row name must be specified for each row.");
70        else
71          rowNames = new List<string>(value);
72        OnRowNamesChanged();
73      }
74    }
75    [Storable]
76    protected bool sortableView;
77    public virtual bool SortableView {
78      get { return sortableView; }
79      set {
80        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. ValueTypeMatrix is read-only.");
81        if (value != sortableView) {
82          sortableView = value;
83          OnSortableViewChanged();
84        }
85      }
86    }
87
88    public virtual int Rows {
89      get { return matrix.GetLength(0); }
90      protected set {
91        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. ValueTypeMatrix is read-only.");
92        if (value != Rows) {
93          T[,] newArray = new T[value, Columns];
94          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
95          matrix = newArray;
96          while (rowNames.Count > value)
97            rowNames.RemoveAt(rowNames.Count - 1);
98          while (rowNames.Count < value)
99            rowNames.Add("Row " + rowNames.Count);
100          OnRowsChanged();
101          OnRowNamesChanged();
102          OnReset();
103        }
104      }
105    }
106    public virtual int Columns {
107      get { return matrix.GetLength(1); }
108      protected set {
109        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. ValueTypeMatrix is read-only.");
110        if (value != Columns) {
111          T[,] newArray = new T[Rows, value];
112          for (int i = 0; i < Rows; i++)
113            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
114          matrix = newArray;
115          while (columnNames.Count > value)
116            columnNames.RemoveAt(columnNames.Count - 1);
117          while (columnNames.Count < value)
118            columnNames.Add("Column " + columnNames.Count);
119          OnColumnsChanged();
120          OnColumnNamesChanged();
121          OnReset();
122        }
123      }
124    }
125    public virtual T this[int rowIndex, int columnIndex] {
126      get { return matrix[rowIndex, columnIndex]; }
127      set {
128        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
129        if (!value.Equals(matrix[rowIndex, columnIndex])) {
130          matrix[rowIndex, columnIndex] = value;
131          OnItemChanged(rowIndex, columnIndex);
132        }
133      }
134    }
135
136    [Storable]
137    protected bool readOnly;
138    public virtual bool ReadOnly {
139      get { return readOnly; }
140    }
141
142    [StorableConstructor]
143    protected ValueTypeMatrix(bool deserializing) : base(deserializing) { }
144    protected ValueTypeMatrix(ValueTypeMatrix<T> original, Cloner cloner)
145      : base(original, cloner) {
146      this.matrix = (T[,])original.matrix.Clone();
147      this.columnNames = new List<string>(original.columnNames);
148      this.rowNames = new List<string>(original.rowNames);
149      this.sortableView = original.sortableView;
150      this.readOnly = original.readOnly;
151    }
152    protected ValueTypeMatrix() {
153      matrix = new T[0, 0];
154      columnNames = new List<string>();
155      rowNames = new List<string>();
156      sortableView = false;
157      readOnly = false;
158    }
159    protected ValueTypeMatrix(int rows, int columns) {
160      matrix = new T[rows, columns];
161      columnNames = new List<string>();
162      rowNames = new List<string>();
163      sortableView = false;
164      readOnly = false;
165    }
166    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
167      : this(rows, columns) {
168      ColumnNames = columnNames;
169    }
170    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
171      : this(rows, columns, columnNames) {
172      RowNames = rowNames;
173    }
174    protected ValueTypeMatrix(T[,] elements) {
175      if (elements == null) throw new ArgumentNullException();
176      matrix = (T[,])elements.Clone();
177      columnNames = new List<string>();
178      rowNames = new List<string>();
179      sortableView = false;
180      readOnly = false;
181    }
182    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
183      : this(elements) {
184      ColumnNames = columnNames;
185    }
186    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
187      : this(elements, columnNames) {
188      RowNames = rowNames;
189    }
190
191    public virtual ValueTypeMatrix<T> AsReadOnly() {
192      ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone();
193      readOnlyValueTypeMatrix.readOnly = true;
194      return readOnlyValueTypeMatrix;
195    }
196
197   
198    public T[,] CloneAsMatrix() {
199      //mkommend: this works because T must be a value type (struct constraint);
200      return (T[,])matrix.Clone();
201    }
202
203    public virtual IEnumerable<T> GetRow(int row) {
204      for (var col = 0; col < Columns; col++) {
205        yield return matrix[row, col];
206      }
207    }
208
209    public virtual IEnumerable<T> GetColumn(int col) {
210      for (var row = 0; row < Rows; row++) {
211        yield return matrix[row, col];
212      }
213    }
214
215    public override string ToString() {
216      if (matrix.Length == 0) return "[]";
217
218      StringBuilder sb = new StringBuilder();
219      sb.Append("[");
220      for (int i = 0; i < Rows; i++) {
221        sb.Append("[").Append(matrix[i, 0].ToString());
222        for (int j = 1; j < Columns; j++)
223          sb.Append(";").Append(matrix[i, j].ToString());
224        sb.Append("]");
225
226        if (sb.Length > maximumToStringLength) {
227          sb.Append("[...]");
228          break;
229        }
230      }
231      sb.Append("]");
232
233      return sb.ToString();
234    }
235
236    public virtual IEnumerator<T> GetEnumerator() {
237      return matrix.Cast<T>().GetEnumerator();
238    }
239
240    IEnumerator IEnumerable.GetEnumerator() {
241      return GetEnumerator();
242    }
243
244    #region events
245    public event EventHandler ColumnsChanged;
246    protected virtual void OnColumnsChanged() {
247      EventHandler handler = ColumnsChanged;
248      if (handler != null)
249        handler(this, EventArgs.Empty);
250    }
251    public event EventHandler RowsChanged;
252    protected virtual void OnRowsChanged() {
253      EventHandler handler = RowsChanged;
254      if (handler != null)
255        handler(this, EventArgs.Empty);
256    }
257    public event EventHandler ColumnNamesChanged;
258    protected virtual void OnColumnNamesChanged() {
259      EventHandler handler = ColumnNamesChanged;
260      if (handler != null)
261        handler(this, EventArgs.Empty);
262    }
263    public event EventHandler RowNamesChanged;
264    protected virtual void OnRowNamesChanged() {
265      EventHandler handler = RowNamesChanged;
266      if (handler != null)
267        handler(this, EventArgs.Empty);
268    }
269    public event EventHandler SortableViewChanged;
270    protected virtual void OnSortableViewChanged() {
271      EventHandler handler = SortableViewChanged;
272      if (handler != null)
273        handler(this, EventArgs.Empty);
274    }
275    public event EventHandler<EventArgs<int, int>> ItemChanged;
276    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
277      if (ItemChanged != null)
278        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
279
280      //approximation to avoid firing of unnecessary ToStringChangedEvents
281      //columnIndex is not used, because always full rows are returned in the ToString method
282      if (rowIndex * Columns < maximumToStringLength)
283        OnToStringChanged();
284    }
285    public event EventHandler Reset;
286    protected virtual void OnReset() {
287      if (Reset != null)
288        Reset(this, EventArgs.Empty);
289      OnToStringChanged();
290    }
291    #endregion
292  }
293}
Note: See TracBrowser for help on using the repository browser.