Free cookie consent management tool by TermsFeed Policy Generator

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

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

added ColumnNames property in IStringConvertibleMatrix and in classes implementing this interface (ticket #968)

File size: 4.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    protected 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 columnName must be for each column specified.");
52        else
53          columnNames = new List<string>(value);
54      }
55    }
56
57    public virtual int Rows {
58      get { return matrix.GetLength(0); }
59      protected set {
60        if (value != Rows) {
61          T[,] newArray = new T[value, Columns];
62          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
63          matrix = newArray;
64          OnReset();
65        }
66      }
67    }
68    public virtual int Columns {
69      get { return matrix.GetLength(1); }
70      protected set {
71        if (value != Columns) {
72          T[,] newArray = new T[Rows, value];
73          for (int i = 0; i < Rows; i++)
74            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
75          matrix = newArray;
76          OnReset();
77        }
78      }
79    }
80    public virtual T this[int rowIndex, int columnIndex] {
81      get { return matrix[rowIndex, columnIndex]; }
82      set {
83        if (!value.Equals(matrix[rowIndex, columnIndex])) {
84          matrix[rowIndex, columnIndex] = value;
85          OnItemChanged(rowIndex, columnIndex);
86        }
87      }
88    }
89
90    protected ValueTypeMatrix() {
91      matrix = new T[0, 0];
92      columnNames = new List<string>();
93    }
94    protected ValueTypeMatrix(int rows, int columns) {
95      matrix = new T[rows, columns];
96      columnNames = new List<string>();
97    }
98    protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
99      : this(rows, columns) {
100      ColumnNames = columnNames;
101    }
102    protected ValueTypeMatrix(T[,] elements) {
103      if (elements == null) throw new ArgumentNullException();
104      matrix = (T[,])elements.Clone();
105      columnNames = new List<string>();
106    }
107    protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
108      : this(elements) {
109      ColumnNames = columnNames;
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      ValueTypeMatrix<T> clone = (ValueTypeMatrix<T>)base.Clone(cloner);
114      clone.matrix = (T[,])matrix.Clone();
115      return clone;
116    }
117
118    public override string ToString() {
119      StringBuilder sb = new StringBuilder();
120      sb.Append("[");
121      if (matrix.Length > 0) {
122        for (int i = 0; i < Rows; i++) {
123          sb.Append("[").Append(matrix[i, 0].ToString());
124          for (int j = 1; j < Columns; j++)
125            sb.Append(";").Append(matrix[i, j].ToString());
126          sb.Append("]");
127        }
128      }
129      sb.Append("]");
130      return sb.ToString();
131    }
132
133    public virtual IEnumerator GetEnumerator() {
134      return matrix.GetEnumerator();
135    }
136
137    public event EventHandler<EventArgs<int, int>> ItemChanged;
138    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
139      if (ItemChanged != null)
140        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
141      OnToStringChanged();
142    }
143    public event EventHandler Reset;
144    protected virtual void OnReset() {
145      if (Reset != null)
146        Reset(this, EventArgs.Empty);
147      OnToStringChanged();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.