Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3320 was 3320, checked in by mkommend, 14 years ago

added events for changes of column and row names (ticket #968)

File size: 7.4 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 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    private List<string> columnNames;
45    public IEnumerable<string> ColumnNames {
46      get { return this.columnNames; }
47      set {
48        if (value == null || value.Count() == 0)
49          columnNames = new List<string>();
50        else if (value.Count() != Columns)
51          throw new ArgumentException("A column name must be specified for each column.");
52        else
53          columnNames = new List<string>(value);
54      }
55    }
56    [Storable]
57    private List<string> rowNames;
58    public IEnumerable<string> RowNames {
59      get { return this.rowNames; }
60      set {
61        if (value == null || value.Count() == 0)
62          rowNames = new List<string>();
63        else if (value.Count() != Rows)
64          throw new ArgumentException("A row name must be specified for each row.");
65        else
66          rowNames = new List<string>(value);
67      }
68    }
69    [Storable]
70    private bool sortableView;
71    public bool SortableView {
72      get { return sortableView; }
73      set {
74        if (value != sortableView) {
75          sortableView = value;
76          OnSortableViewChanged();
77        }
78      }
79    }
80
81    public virtual int Rows {
82      get { return matrix.GetLength(0); }
83      protected set {
84        if (value != Rows) {
85          T[,] newArray = new T[value, Columns];
86          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
87          matrix = newArray;
88          while (rowNames.Count > value)
89            rowNames.RemoveAt(rowNames.Count - 1);
90          while (rowNames.Count < value)
91            rowNames.Add("Row " + rowNames.Count);
92          OnReset();
93        }
94      }
95    }
96    public virtual int Columns {
97      get { return matrix.GetLength(1); }
98      protected set {
99        if (value != Columns) {
100          T[,] newArray = new T[Rows, value];
101          for (int i = 0; i < Rows; i++)
102            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
103          matrix = newArray;
104          while (columnNames.Count > value)
105            columnNames.RemoveAt(columnNames.Count - 1);
106          while (columnNames.Count < value)
107            columnNames.Add("Column " + columnNames.Count);
108          OnReset();
109        }
110      }
111    }
112    public virtual T this[int rowIndex, int columnIndex] {
113      get { return matrix[rowIndex, columnIndex]; }
114      set {
115        if (!value.Equals(matrix[rowIndex, columnIndex])) {
116          matrix[rowIndex, columnIndex] = value;
117          OnItemChanged(rowIndex, columnIndex);
118        }
119      }
120    }
121
122    protected ValueTypeMatrix() {
123      matrix = new T[0, 0];
124      columnNames = new List<string>();
125      rowNames = new List<string>();
126      sortableView = false;
127    }
128    protected ValueTypeMatrix(int rows, int columns) {
129      matrix = new T[rows, columns];
130      columnNames = new List<string>();
131      rowNames = new List<string>();
132      sortableView = false;
133    }
134    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
135      : this(rows, columns) {
136      ColumnNames = columnNames;
137    }
138    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
139      : this(rows, columns, columnNames) {
140      RowNames = rowNames;
141    }
142    protected ValueTypeMatrix(T[,] elements) {
143      if (elements == null) throw new ArgumentNullException();
144      matrix = (T[,])elements.Clone();
145      columnNames = new List<string>();
146      rowNames = new List<string>();
147      sortableView = false;
148    }
149    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
150      : this(elements) {
151      ColumnNames = columnNames;
152    }
153    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
154      : this(elements,columnNames) {
155      RowNames = rowNames;
156    }
157
158    public override IDeepCloneable Clone(Cloner cloner) {
159      ValueTypeMatrix<T> clone = (ValueTypeMatrix<T>)base.Clone(cloner);
160      clone.SortableView = SortableView;
161      clone.matrix = (T[,])matrix.Clone();
162      clone.columnNames = new List<string>(columnNames);
163      clone.rowNames = new List<string>(rowNames);
164      return clone;
165    }
166
167    public override string ToString() {
168      StringBuilder sb = new StringBuilder();
169      sb.Append("[");
170      if (matrix.Length > 0) {
171        for (int i = 0; i < Rows; i++) {
172          sb.Append("[").Append(matrix[i, 0].ToString());
173          for (int j = 1; j < Columns; j++)
174            sb.Append(";").Append(matrix[i, j].ToString());
175          sb.Append("]");
176        }
177      }
178      sb.Append("]");
179      return sb.ToString();
180    }
181
182    public virtual IEnumerator GetEnumerator() {
183      return matrix.GetEnumerator();
184    }
185
186    public event EventHandler ColumnNamesChanged;
187    protected virtual void OnColumnNamesChanged() {
188      EventHandler handler = ColumnNamesChanged;
189      if(handler!=null)
190        handler(this,EventArgs.Empty);
191    }
192    public event EventHandler RowNamesChanged;
193    protected virtual void OnRowNamesChanged() {
194      EventHandler handler = RowNamesChanged;
195      if (handler != null)
196        handler(this, EventArgs.Empty);
197    }
198    public event EventHandler SortableViewChanged;
199    protected virtual void OnSortableViewChanged() {
200      EventHandler handler = SortableViewChanged;
201      if (handler != null)
202        handler(this, EventArgs.Empty);
203    }
204    public event EventHandler<EventArgs<int, int>> ItemChanged;
205    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
206      if (ItemChanged != null)
207        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
208      OnToStringChanged();
209    }
210    public event EventHandler Reset;
211    protected virtual void OnReset() {
212      if (Reset != null)
213        Reset(this, EventArgs.Empty);
214      OnToStringChanged();
215    }
216  }
217}
Note: See TracBrowser for help on using the repository browser.