Free cookie consent management tool by TermsFeed Policy Generator

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

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

added RowNames for IStringConvertibleMatrix
and fixed cloning and ctors of concrete matrixes (ticket #968)

File size: 5.9 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
70    public virtual int Rows {
71      get { return matrix.GetLength(0); }
72      protected set {
73        if (value != Rows) {
74          T[,] newArray = new T[value, Columns];
75          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
76          matrix = newArray;
77          OnReset();
78        }
79      }
80    }
81    public virtual int Columns {
82      get { return matrix.GetLength(1); }
83      protected set {
84        if (value != Columns) {
85          T[,] newArray = new T[Rows, value];
86          for (int i = 0; i < Rows; i++)
87            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
88          matrix = newArray;
89          OnReset();
90        }
91      }
92    }
93    public virtual T this[int rowIndex, int columnIndex] {
94      get { return matrix[rowIndex, columnIndex]; }
95      set {
96        if (!value.Equals(matrix[rowIndex, columnIndex])) {
97          matrix[rowIndex, columnIndex] = value;
98          OnItemChanged(rowIndex, columnIndex);
99        }
100      }
101    }
102
103    protected ValueTypeMatrix() {
104      matrix = new T[0, 0];
105      columnNames = new List<string>();
106      rowNames = new List<string>();
107    }
108    protected ValueTypeMatrix(int rows, int columns) {
109      matrix = new T[rows, columns];
110      columnNames = new List<string>();
111      rowNames = new List<string>();
112    }
113    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
114      : this(rows, columns) {
115      ColumnNames = columnNames;
116    }
117    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
118      : this(rows, columns, columnNames) {
119      RowNames = rowNames;
120    }
121    protected ValueTypeMatrix(T[,] elements) {
122      if (elements == null) throw new ArgumentNullException();
123      matrix = (T[,])elements.Clone();
124      columnNames = new List<string>();
125      rowNames = new List<string>();
126    }
127    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
128      : this(elements) {
129      ColumnNames = columnNames;
130    }
131    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
132      : this(elements,columnNames) {
133      RowNames = rowNames;
134    }
135
136    public override IDeepCloneable Clone(Cloner cloner) {
137      ValueTypeMatrix<T> clone = (ValueTypeMatrix<T>)base.Clone(cloner);
138      clone.matrix = (T[,])matrix.Clone();
139      clone.columnNames = new List<string>(columnNames);
140      clone.rowNames = new List<string>(rowNames);
141      return clone;
142    }
143
144    public override string ToString() {
145      StringBuilder sb = new StringBuilder();
146      sb.Append("[");
147      if (matrix.Length > 0) {
148        for (int i = 0; i < Rows; i++) {
149          sb.Append("[").Append(matrix[i, 0].ToString());
150          for (int j = 1; j < Columns; j++)
151            sb.Append(";").Append(matrix[i, j].ToString());
152          sb.Append("]");
153        }
154      }
155      sb.Append("]");
156      return sb.ToString();
157    }
158
159    public virtual IEnumerator GetEnumerator() {
160      return matrix.GetEnumerator();
161    }
162
163    public event EventHandler<EventArgs<int, int>> ItemChanged;
164    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
165      if (ItemChanged != null)
166        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
167      OnToStringChanged();
168    }
169    public event EventHandler Reset;
170    protected virtual void OnReset() {
171      if (Reset != null)
172        Reset(this, EventArgs.Empty);
173      OnToStringChanged();
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.