Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 8.7 KB
RevLine 
[2677]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2677]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;
[3308]24using System.Collections.Generic;
[3306]25using System.Drawing;
[3308]26using System.Linq;
[2677]27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
[3822]33  [Item("ValueTypeMatrix", "An abstract base class for representing matrices of value types.")]
[3017]34  [StorableClass]
[3430]35  public abstract class ValueTypeMatrix<T> : Item, IEnumerable<T> where T : struct {
[3306]36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
[2694]40    [Storable]
[3054]41    protected T[,] matrix;
[2677]42
[3308]43    [Storable]
[3430]44    protected List<string> columnNames;
45    public virtual IEnumerable<string> ColumnNames {
[3308]46      get { return this.columnNames; }
47      set {
[3430]48        if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. ValueTypeMatrix is read-only.");
[3308]49        if (value == null || value.Count() == 0)
50          columnNames = new List<string>();
51        else if (value.Count() != Columns)
[3310]52          throw new ArgumentException("A column name must be specified for each column.");
[3308]53        else
54          columnNames = new List<string>(value);
55      }
56    }
[3310]57    [Storable]
[3430]58    protected List<string> rowNames;
59    public virtual IEnumerable<string> RowNames {
[3310]60      get { return this.rowNames; }
61      set {
[3430]62        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. ValueTypeMatrix is read-only.");
[3310]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    }
[3320]71    [Storable]
[3430]72    protected bool sortableView;
73    public virtual bool SortableView {
[3320]74      get { return sortableView; }
75      set {
[3430]76        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. ValueTypeMatrix is read-only.");
[3320]77        if (value != sortableView) {
78          sortableView = value;
79          OnSortableViewChanged();
80        }
81      }
82    }
[3308]83
[3054]84    public virtual int Rows {
85      get { return matrix.GetLength(0); }
[2677]86      protected set {
[3430]87        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. ValueTypeMatrix is read-only.");
[2677]88        if (value != Rows) {
89          T[,] newArray = new T[value, Columns];
[3054]90          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
91          matrix = newArray;
[3320]92          while (rowNames.Count > value)
93            rowNames.RemoveAt(rowNames.Count - 1);
94          while (rowNames.Count < value)
95            rowNames.Add("Row " + rowNames.Count);
[2677]96          OnReset();
97        }
98      }
99    }
[3054]100    public virtual int Columns {
101      get { return matrix.GetLength(1); }
[2677]102      protected set {
[3430]103        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. ValueTypeMatrix is read-only.");
[2677]104        if (value != Columns) {
105          T[,] newArray = new T[Rows, value];
106          for (int i = 0; i < Rows; i++)
[3054]107            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
108          matrix = newArray;
[3320]109          while (columnNames.Count > value)
110            columnNames.RemoveAt(columnNames.Count - 1);
111          while (columnNames.Count < value)
112            columnNames.Add("Column " + columnNames.Count);
[2677]113          OnReset();
114        }
115      }
116    }
[3054]117    public virtual T this[int rowIndex, int columnIndex] {
118      get { return matrix[rowIndex, columnIndex]; }
[2677]119      set {
[3430]120        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
[3054]121        if (!value.Equals(matrix[rowIndex, columnIndex])) {
122          matrix[rowIndex, columnIndex] = value;
[2677]123          OnItemChanged(rowIndex, columnIndex);
124        }
125      }
126    }
127
[3430]128    [Storable]
129    protected bool readOnly;
130    public virtual bool ReadOnly {
131      get { return readOnly; }
132    }
133
[4722]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    }
[3054]144    protected ValueTypeMatrix() {
145      matrix = new T[0, 0];
[3308]146      columnNames = new List<string>();
[3310]147      rowNames = new List<string>();
[3320]148      sortableView = false;
[3430]149      readOnly = false;
[2677]150    }
[3054]151    protected ValueTypeMatrix(int rows, int columns) {
152      matrix = new T[rows, columns];
[3308]153      columnNames = new List<string>();
[3310]154      rowNames = new List<string>();
[3320]155      sortableView = false;
[3430]156      readOnly = false;
[2677]157    }
[3308]158    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
159      : this(rows, columns) {
160      ColumnNames = columnNames;
161    }
[3430]162    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
[3310]163      : this(rows, columns, columnNames) {
164      RowNames = rowNames;
165    }
[3054]166    protected ValueTypeMatrix(T[,] elements) {
[2677]167      if (elements == null) throw new ArgumentNullException();
[3054]168      matrix = (T[,])elements.Clone();
[3308]169      columnNames = new List<string>();
[3310]170      rowNames = new List<string>();
[3320]171      sortableView = false;
[3430]172      readOnly = false;
[2677]173    }
[3308]174    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
175      : this(elements) {
176      ColumnNames = columnNames;
177    }
[3310]178    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
179      : this(elements,columnNames) {
180      RowNames = rowNames;
181    }
[2677]182
[3430]183    public virtual ValueTypeMatrix<T> AsReadOnly() {
184      ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone();
185      readOnlyValueTypeMatrix.readOnly = true;
186      return readOnlyValueTypeMatrix;
187    }
188
[2677]189    public override string ToString() {
190      StringBuilder sb = new StringBuilder();
191      sb.Append("[");
[3054]192      if (matrix.Length > 0) {
[2677]193        for (int i = 0; i < Rows; i++) {
[3054]194          sb.Append("[").Append(matrix[i, 0].ToString());
[2677]195          for (int j = 1; j < Columns; j++)
[3054]196            sb.Append(";").Append(matrix[i, j].ToString());
[2677]197          sb.Append("]");
198        }
199      }
200      sb.Append("]");
201      return sb.ToString();
202    }
203
[3430]204    public virtual IEnumerator<T> GetEnumerator() {
205      return matrix.Cast<T>().GetEnumerator();
[2677]206    }
207
[3430]208    IEnumerator IEnumerable.GetEnumerator() {
209      return GetEnumerator();
210    }
211
[3320]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    }
[2973]230    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3054]231    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[2677]232      if (ItemChanged != null)
233        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
[2932]234      OnToStringChanged();
[2677]235    }
[2973]236    public event EventHandler Reset;
[3054]237    protected virtual void OnReset() {
[2677]238      if (Reset != null)
239        Reset(this, EventArgs.Empty);
[2932]240      OnToStringChanged();
[2677]241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.