Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Data/3.3/ValueTypeMatrix.cs

Last change on this file was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    public static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
38    }
39
40    [Storable]
41    protected T[,] matrix;
42
43    [Storable]
44    protected List<string> columnNames;
45    public virtual IEnumerable<string> ColumnNames {
46      get { return this.columnNames; }
47      set {
48        if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. ValueTypeMatrix is read-only.");
49        if (value == null || value.Count() == 0)
50          columnNames = new List<string>();
51        else if (value.Count() != Columns)
52          throw new ArgumentException("A column name must be specified for each column.");
53        else
54          columnNames = new List<string>(value);
55        OnColumnNamesChanged();
56      }
57    }
58    [Storable]
59    protected List<string> rowNames;
60    public virtual IEnumerable<string> RowNames {
61      get { return this.rowNames; }
62      set {
63        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. ValueTypeMatrix is read-only.");
64        if (value == null || value.Count() == 0)
65          rowNames = new List<string>();
66        else if (value.Count() != Rows)
67          throw new ArgumentException("A row name must be specified for each row.");
68        else
69          rowNames = new List<string>(value);
70        OnRowNamesChanged();
71      }
72    }
73    [Storable]
74    protected bool sortableView;
75    public virtual bool SortableView {
76      get { return sortableView; }
77      set {
78        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. ValueTypeMatrix is read-only.");
79        if (value != sortableView) {
80          sortableView = value;
81          OnSortableViewChanged();
82        }
83      }
84    }
85
86    public virtual int Rows {
87      get { return matrix.GetLength(0); }
88      protected set {
89        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. ValueTypeMatrix is read-only.");
90        if (value != Rows) {
91          T[,] newArray = new T[value, Columns];
92          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
93          matrix = newArray;
94          while (rowNames.Count > value)
95            rowNames.RemoveAt(rowNames.Count - 1);
96          while (rowNames.Count < value)
97            rowNames.Add("Row " + rowNames.Count);
98          OnRowsChanged();
99          OnRowNamesChanged();
100          OnReset();
101        }
102      }
103    }
104    public virtual int Columns {
105      get { return matrix.GetLength(1); }
106      protected set {
107        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. ValueTypeMatrix is read-only.");
108        if (value != Columns) {
109          T[,] newArray = new T[Rows, value];
110          for (int i = 0; i < Rows; i++)
111            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
112          matrix = newArray;
113          while (columnNames.Count > value)
114            columnNames.RemoveAt(columnNames.Count - 1);
115          while (columnNames.Count < value)
116            columnNames.Add("Column " + columnNames.Count);
117          OnColumnsChanged();
118          OnColumnNamesChanged();
119          OnReset();
120        }
121      }
122    }
123    public virtual T this[int rowIndex, int columnIndex] {
124      get { return matrix[rowIndex, columnIndex]; }
125      set {
126        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
127        if (!value.Equals(matrix[rowIndex, columnIndex])) {
128          matrix[rowIndex, columnIndex] = value;
129          OnItemChanged(rowIndex, columnIndex);
130        }
131      }
132    }
133
134    [Storable]
135    protected bool readOnly;
136    public virtual bool ReadOnly {
137      get { return readOnly; }
138    }
139
140    [StorableConstructor]
141    protected ValueTypeMatrix(bool deserializing) : base(deserializing) { }
142    protected ValueTypeMatrix(ValueTypeMatrix<T> original, Cloner cloner)
143      : base(original, cloner) {
144      this.matrix = (T[,])original.matrix.Clone();
145      this.columnNames = new List<string>(original.columnNames);
146      this.rowNames = new List<string>(original.rowNames);
147      this.sortableView = original.sortableView;
148      this.readOnly = original.readOnly;
149    }
150    protected ValueTypeMatrix() {
151      matrix = new T[0, 0];
152      columnNames = new List<string>();
153      rowNames = new List<string>();
154      sortableView = false;
155      readOnly = false;
156    }
157    protected ValueTypeMatrix(int rows, int columns) {
158      matrix = new T[rows, columns];
159      columnNames = new List<string>();
160      rowNames = new List<string>();
161      sortableView = false;
162      readOnly = false;
163    }
164    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
165      : this(rows, columns) {
166      ColumnNames = columnNames;
167    }
168    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
169      : this(rows, columns, columnNames) {
170      RowNames = rowNames;
171    }
172    protected ValueTypeMatrix(T[,] elements) {
173      if (elements == null) throw new ArgumentNullException();
174      matrix = (T[,])elements.Clone();
175      columnNames = new List<string>();
176      rowNames = new List<string>();
177      sortableView = false;
178      readOnly = false;
179    }
180    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
181      : this(elements) {
182      ColumnNames = columnNames;
183    }
184    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
185      : this(elements, columnNames) {
186      RowNames = rowNames;
187    }
188
189    public virtual ValueTypeMatrix<T> AsReadOnly() {
190      ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone();
191      readOnlyValueTypeMatrix.readOnly = true;
192      return readOnlyValueTypeMatrix;
193    }
194
195    public override string ToString() {
196      StringBuilder sb = new StringBuilder();
197      sb.Append("[");
198      if (matrix.Length > 0) {
199        for (int i = 0; i < Rows; i++) {
200          sb.Append("[").Append(matrix[i, 0].ToString());
201          for (int j = 1; j < Columns; j++)
202            sb.Append(";").Append(matrix[i, j].ToString());
203          sb.Append("]");
204        }
205      }
206      sb.Append("]");
207      return sb.ToString();
208    }
209
210    public virtual IEnumerator<T> GetEnumerator() {
211      return matrix.Cast<T>().GetEnumerator();
212    }
213
214    IEnumerator IEnumerable.GetEnumerator() {
215      return GetEnumerator();
216    }
217
218    #region events
219    public event EventHandler ColumnsChanged;
220    protected virtual void OnColumnsChanged() {
221      EventHandler handler = ColumnsChanged;
222      if (handler != null)
223        handler(this, EventArgs.Empty);
224    }
225    public event EventHandler RowsChanged;
226    protected virtual void OnRowsChanged() {
227      EventHandler handler = RowsChanged;
228      if (handler != null)
229        handler(this, EventArgs.Empty);
230    }
231    public event EventHandler ColumnNamesChanged;
232    protected virtual void OnColumnNamesChanged() {
233      EventHandler handler = ColumnNamesChanged;
234      if (handler != null)
235        handler(this, EventArgs.Empty);
236    }
237    public event EventHandler RowNamesChanged;
238    protected virtual void OnRowNamesChanged() {
239      EventHandler handler = RowNamesChanged;
240      if (handler != null)
241        handler(this, EventArgs.Empty);
242    }
243    public event EventHandler SortableViewChanged;
244    protected virtual void OnSortableViewChanged() {
245      EventHandler handler = SortableViewChanged;
246      if (handler != null)
247        handler(this, EventArgs.Empty);
248    }
249    public event EventHandler<EventArgs<int, int>> ItemChanged;
250    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
251      if (ItemChanged != null)
252        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
253      OnToStringChanged();
254    }
255    public event EventHandler Reset;
256    protected virtual void OnReset() {
257      if (Reset != null)
258        Reset(this, EventArgs.Empty);
259      OnToStringChanged();
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.