Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 8.7 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", "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    [StorableConstructor]
135    protected ValueTypeMatrix(bool deserializing) : base(deserializing) { }
136    protected ValueTypeMatrix(ValueTypeMatrix<T> original, Cloner cloner)
137      : base(original, cloner) {
138      this.matrix = (T[,])original.matrix.Clone();
139      this.columnNames = new List<string>(original.columnNames);
140      this.rowNames = new List<string>(original.rowNames);
141      this.sortableView = original.sortableView;
142      this.readOnly = original.readOnly;
143    }
144    protected ValueTypeMatrix() {
145      matrix = new T[0, 0];
146      columnNames = new List<string>();
147      rowNames = new List<string>();
148      sortableView = false;
149      readOnly = false;
150    }
151    protected ValueTypeMatrix(int rows, int columns) {
152      matrix = new T[rows, columns];
153      columnNames = new List<string>();
154      rowNames = new List<string>();
155      sortableView = false;
156      readOnly = false;
157    }
158    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
159      : this(rows, columns) {
160      ColumnNames = columnNames;
161    }
162    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
163      : this(rows, columns, columnNames) {
164      RowNames = rowNames;
165    }
166    protected ValueTypeMatrix(T[,] elements) {
167      if (elements == null) throw new ArgumentNullException();
168      matrix = (T[,])elements.Clone();
169      columnNames = new List<string>();
170      rowNames = new List<string>();
171      sortableView = false;
172      readOnly = false;
173    }
174    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
175      : this(elements) {
176      ColumnNames = columnNames;
177    }
178    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
179      : this(elements,columnNames) {
180      RowNames = rowNames;
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.