Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeMatrix.cs @ 3430

Last change on this file since 3430 was 3430, checked in by swagner, 14 years ago

Added ReadOnly property to all items of HeuristicLab.Data (#969)

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