Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3054 was 3054, checked in by swagner, 14 years ago

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File size: 3.8 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.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Data {
30  [Item("ValueTypeMatrix<T>", "An abstract base class for representing matrices of value types.")]
31  [StorableClass]
32  public abstract class ValueTypeMatrix<T> : Item, IEnumerable where T : struct {
33    [Storable]
34    protected T[,] matrix;
35
36    public virtual int Rows {
37      get { return matrix.GetLength(0); }
38      protected set {
39        if (value != Rows) {
40          T[,] newArray = new T[value, Columns];
41          Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
42          matrix = newArray;
43          OnReset();
44        }
45      }
46    }
47    public virtual int Columns {
48      get { return matrix.GetLength(1); }
49      protected set {
50        if (value != Columns) {
51          T[,] newArray = new T[Rows, value];
52          for (int i = 0; i < Rows; i++)
53            Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
54          matrix = newArray;
55          OnReset();
56        }
57      }
58    }
59    public virtual T this[int rowIndex, int columnIndex] {
60      get { return matrix[rowIndex, columnIndex]; }
61      set {
62        if (!value.Equals(matrix[rowIndex, columnIndex])) {
63          matrix[rowIndex, columnIndex] = value;
64          OnItemChanged(rowIndex, columnIndex);
65        }
66      }
67    }
68
69    protected ValueTypeMatrix() {
70      matrix = new T[0, 0];
71    }
72    protected ValueTypeMatrix(int rows, int columns) {
73      matrix = new T[rows, columns];
74    }
75    protected ValueTypeMatrix(T[,] elements) {
76      if (elements == null) throw new ArgumentNullException();
77      matrix = (T[,])elements.Clone();
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      ValueTypeMatrix<T> clone = (ValueTypeMatrix<T>)base.Clone(cloner);
82      clone.matrix = (T[,])matrix.Clone();
83      return clone;
84    }
85
86    public override string ToString() {
87      StringBuilder sb = new StringBuilder();
88      sb.Append("[");
89      if (matrix.Length > 0) {
90        for (int i = 0; i < Rows; i++) {
91          sb.Append("[").Append(matrix[i, 0].ToString());
92          for (int j = 1; j < Columns; j++)
93            sb.Append(";").Append(matrix[i, j].ToString());
94          sb.Append("]");
95        }
96      }
97      sb.Append("]");
98      return sb.ToString();
99    }
100
101    public virtual IEnumerator GetEnumerator() {
102      return matrix.GetEnumerator();
103    }
104
105    public event EventHandler<EventArgs<int, int>> ItemChanged;
106    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
107      if (ItemChanged != null)
108        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
109      OnToStringChanged();
110    }
111    public event EventHandler Reset;
112    protected virtual void OnReset() {
113      if (Reset != null)
114        Reset(this, EventArgs.Empty);
115      OnToStringChanged();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.