Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeMatrixData.cs @ 2994

Last change on this file since 2994 was 2994, checked in by epitzer, 14 years ago

Make StorableClass attribute compulsory for StorableSerializer to work, add named property StorableClassType to choose between Empty and MarkedOnly, later other options will be added. (#548)

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