Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11171 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

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