Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14710 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 override string ToString() {
204      if (matrix.Length == 0) return "[]";
205
206      StringBuilder sb = new StringBuilder();
207      sb.Append("[");
208      for (int i = 0; i < Rows; i++) {
209        sb.Append("[").Append(matrix[i, 0].ToString());
210        for (int j = 1; j < Columns; j++)
211          sb.Append(";").Append(matrix[i, j].ToString());
212        sb.Append("]");
213
214        if (sb.Length > maximumToStringLength) {
215          sb.Append("[...]");
216          break;
217        }
218      }
219      sb.Append("]");
220
221      return sb.ToString();
222    }
223
224    public virtual IEnumerator<T> GetEnumerator() {
225      return matrix.Cast<T>().GetEnumerator();
226    }
227
228    IEnumerator IEnumerable.GetEnumerator() {
229      return GetEnumerator();
230    }
231
232    #region events
233    public event EventHandler ColumnsChanged;
234    protected virtual void OnColumnsChanged() {
235      EventHandler handler = ColumnsChanged;
236      if (handler != null)
237        handler(this, EventArgs.Empty);
238    }
239    public event EventHandler RowsChanged;
240    protected virtual void OnRowsChanged() {
241      EventHandler handler = RowsChanged;
242      if (handler != null)
243        handler(this, EventArgs.Empty);
244    }
245    public event EventHandler ColumnNamesChanged;
246    protected virtual void OnColumnNamesChanged() {
247      EventHandler handler = ColumnNamesChanged;
248      if (handler != null)
249        handler(this, EventArgs.Empty);
250    }
251    public event EventHandler RowNamesChanged;
252    protected virtual void OnRowNamesChanged() {
253      EventHandler handler = RowNamesChanged;
254      if (handler != null)
255        handler(this, EventArgs.Empty);
256    }
257    public event EventHandler SortableViewChanged;
258    protected virtual void OnSortableViewChanged() {
259      EventHandler handler = SortableViewChanged;
260      if (handler != null)
261        handler(this, EventArgs.Empty);
262    }
263    public event EventHandler<EventArgs<int, int>> ItemChanged;
264    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
265      if (ItemChanged != null)
266        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
267
268      //approximation to avoid firing of unnecessary ToStringChangedEvents
269      //columnIndex is not used, because always full rows are returned in the ToString method
270      if (rowIndex * Columns < maximumToStringLength)
271        OnToStringChanged();
272    }
273    public event EventHandler Reset;
274    protected virtual void OnReset() {
275      if (Reset != null)
276        Reset(this, EventArgs.Empty);
277      OnToStringChanged();
278    }
279    #endregion
280  }
281}
Note: See TracBrowser for help on using the repository browser.