Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights to 2012 (#1716)

File size: 9.4 KB
RevLine 
[2677]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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 {
[7201]36    public static new Image StaticItemImage {
[5287]37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
[3306]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);
[5150]55        OnColumnNamesChanged();
[3308]56      }
57    }
[3310]58    [Storable]
[3430]59    protected List<string> rowNames;
60    public virtual IEnumerable<string> RowNames {
[3310]61      get { return this.rowNames; }
62      set {
[3430]63        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. ValueTypeMatrix is read-only.");
[3310]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);
[5150]70        OnRowNamesChanged();
[3310]71      }
72    }
[3320]73    [Storable]
[3430]74    protected bool sortableView;
75    public virtual bool SortableView {
[3320]76      get { return sortableView; }
77      set {
[3430]78        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. ValueTypeMatrix is read-only.");
[3320]79        if (value != sortableView) {
80          sortableView = value;
81          OnSortableViewChanged();
82        }
83      }
84    }
[3308]85
[3054]86    public virtual int Rows {
87      get { return matrix.GetLength(0); }
[2677]88      protected set {
[3430]89        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. ValueTypeMatrix is read-only.");
[2677]90        if (value != Rows) {
91          T[,] newArray = new T[value, Columns];
[3054]92          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
93          matrix = newArray;
[3320]94          while (rowNames.Count > value)
95            rowNames.RemoveAt(rowNames.Count - 1);
96          while (rowNames.Count < value)
97            rowNames.Add("Row " + rowNames.Count);
[5150]98          OnRowsChanged();
99          OnRowNamesChanged();
[2677]100          OnReset();
101        }
102      }
103    }
[3054]104    public virtual int Columns {
105      get { return matrix.GetLength(1); }
[2677]106      protected set {
[3430]107        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. ValueTypeMatrix is read-only.");
[2677]108        if (value != Columns) {
109          T[,] newArray = new T[Rows, value];
110          for (int i = 0; i < Rows; i++)
[3054]111            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
112          matrix = newArray;
[3320]113          while (columnNames.Count > value)
114            columnNames.RemoveAt(columnNames.Count - 1);
115          while (columnNames.Count < value)
116            columnNames.Add("Column " + columnNames.Count);
[5150]117          OnColumnsChanged();
118          OnColumnNamesChanged();
[2677]119          OnReset();
120        }
121      }
122    }
[3054]123    public virtual T this[int rowIndex, int columnIndex] {
124      get { return matrix[rowIndex, columnIndex]; }
[2677]125      set {
[3430]126        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
[3054]127        if (!value.Equals(matrix[rowIndex, columnIndex])) {
128          matrix[rowIndex, columnIndex] = value;
[2677]129          OnItemChanged(rowIndex, columnIndex);
130        }
131      }
132    }
133
[3430]134    [Storable]
135    protected bool readOnly;
136    public virtual bool ReadOnly {
137      get { return readOnly; }
138    }
139
[4722]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    }
[3054]150    protected ValueTypeMatrix() {
151      matrix = new T[0, 0];
[3308]152      columnNames = new List<string>();
[3310]153      rowNames = new List<string>();
[3320]154      sortableView = false;
[3430]155      readOnly = false;
[2677]156    }
[3054]157    protected ValueTypeMatrix(int rows, int columns) {
158      matrix = new T[rows, columns];
[3308]159      columnNames = new List<string>();
[3310]160      rowNames = new List<string>();
[3320]161      sortableView = false;
[3430]162      readOnly = false;
[2677]163    }
[3308]164    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
165      : this(rows, columns) {
166      ColumnNames = columnNames;
167    }
[3430]168    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
[3310]169      : this(rows, columns, columnNames) {
170      RowNames = rowNames;
171    }
[3054]172    protected ValueTypeMatrix(T[,] elements) {
[2677]173      if (elements == null) throw new ArgumentNullException();
[3054]174      matrix = (T[,])elements.Clone();
[3308]175      columnNames = new List<string>();
[3310]176      rowNames = new List<string>();
[3320]177      sortableView = false;
[3430]178      readOnly = false;
[2677]179    }
[3308]180    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
181      : this(elements) {
182      ColumnNames = columnNames;
183    }
[3310]184    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
[5150]185      : this(elements, columnNames) {
[3310]186      RowNames = rowNames;
187    }
[2677]188
[3430]189    public virtual ValueTypeMatrix<T> AsReadOnly() {
190      ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone();
191      readOnlyValueTypeMatrix.readOnly = true;
192      return readOnlyValueTypeMatrix;
193    }
194
[2677]195    public override string ToString() {
196      StringBuilder sb = new StringBuilder();
197      sb.Append("[");
[3054]198      if (matrix.Length > 0) {
[2677]199        for (int i = 0; i < Rows; i++) {
[3054]200          sb.Append("[").Append(matrix[i, 0].ToString());
[2677]201          for (int j = 1; j < Columns; j++)
[3054]202            sb.Append(";").Append(matrix[i, j].ToString());
[2677]203          sb.Append("]");
204        }
205      }
206      sb.Append("]");
207      return sb.ToString();
208    }
209
[3430]210    public virtual IEnumerator<T> GetEnumerator() {
211      return matrix.Cast<T>().GetEnumerator();
[2677]212    }
213
[3430]214    IEnumerator IEnumerable.GetEnumerator() {
215      return GetEnumerator();
216    }
217
[5150]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    }
[3320]231    public event EventHandler ColumnNamesChanged;
232    protected virtual void OnColumnNamesChanged() {
233      EventHandler handler = ColumnNamesChanged;
[5150]234      if (handler != null)
235        handler(this, EventArgs.Empty);
[3320]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    }
[2973]249    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3054]250    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[2677]251      if (ItemChanged != null)
252        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
[2932]253      OnToStringChanged();
[2677]254    }
[2973]255    public event EventHandler Reset;
[3054]256    protected virtual void OnReset() {
[2677]257      if (Reset != null)
258        Reset(this, EventArgs.Empty);
[2932]259      OnToStringChanged();
[2677]260    }
[5150]261    #endregion
[2677]262  }
263}
Note: See TracBrowser for help on using the repository browser.