Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12009 was 12009, checked in by ascheibe, 9 years ago

#2212 updated copyright year

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