[2677] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2677] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Data {
|
---|
| 32 | [Item("ValueTypeMatrixData<T>", "A base class for representing matrices of value types.")]
|
---|
| 33 | public class ValueTypeMatrixData<T> : Item, IEnumerable where T : struct {
|
---|
[2694] | 34 | [Storable]
|
---|
[2677] | 35 | private T[,] array;
|
---|
| 36 |
|
---|
| 37 | public int Rows {
|
---|
| 38 | get { return array.GetLength(0); }
|
---|
| 39 | protected set {
|
---|
| 40 | if (value != Rows) {
|
---|
| 41 | T[,] newArray = new T[value, Columns];
|
---|
| 42 | Array.Copy(array, newArray, Math.Min(value * Columns, array.Length));
|
---|
| 43 | array = newArray;
|
---|
| 44 | OnReset();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | public int Columns {
|
---|
| 49 | get { return array.GetLength(1); }
|
---|
| 50 | protected set {
|
---|
| 51 | if (value != Columns) {
|
---|
| 52 | T[,] newArray = new T[Rows, value];
|
---|
| 53 | for (int i = 0; i < Rows; i++)
|
---|
| 54 | Array.Copy(array, i * Columns, newArray, i * value, Math.Min(value, Columns));
|
---|
| 55 | array = newArray;
|
---|
| 56 | OnReset();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | public T this[int rowIndex, int columnIndex] {
|
---|
| 61 | get { return array[rowIndex, columnIndex]; }
|
---|
| 62 | set {
|
---|
| 63 | if (!value.Equals(array[rowIndex, columnIndex])) {
|
---|
| 64 | array[rowIndex, columnIndex] = value;
|
---|
| 65 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public ValueTypeMatrixData() {
|
---|
| 71 | array = new T[0, 0];
|
---|
| 72 | }
|
---|
| 73 | public ValueTypeMatrixData(int rows, int columns) {
|
---|
| 74 | array = new T[rows, columns];
|
---|
| 75 | }
|
---|
| 76 | public ValueTypeMatrixData(T[,] elements) {
|
---|
| 77 | if (elements == null) throw new ArgumentNullException();
|
---|
| 78 | array = (T[,])elements.Clone();
|
---|
| 79 | }
|
---|
| 80 | protected ValueTypeMatrixData(ValueTypeMatrixData<T> elements) {
|
---|
| 81 | if (elements == null) throw new ArgumentNullException();
|
---|
| 82 | array = (T[,])elements.array.Clone();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 86 | ValueTypeMatrixData<T> clone = (ValueTypeMatrixData<T>)base.Clone(cloner);
|
---|
| 87 | clone.array = (T[,])array.Clone();
|
---|
| 88 | return clone;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public override string ToString() {
|
---|
| 92 | StringBuilder sb = new StringBuilder();
|
---|
| 93 | sb.Append("[");
|
---|
| 94 | if (array.Length > 0) {
|
---|
| 95 | for (int i = 0; i < Rows; i++) {
|
---|
| 96 | sb.Append("[").Append(array[i, 0].ToString());
|
---|
| 97 | for (int j = 1; j < Columns; j++)
|
---|
| 98 | sb.Append(";").Append(array[i, j].ToString());
|
---|
| 99 | sb.Append("]");
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | sb.Append("]");
|
---|
| 103 | return sb.ToString();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public IEnumerator GetEnumerator() {
|
---|
| 107 | return array.GetEnumerator();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | protected event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 111 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
| 112 | if (ItemChanged != null)
|
---|
| 113 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
| 114 | OnChanged();
|
---|
| 115 | }
|
---|
| 116 | protected event EventHandler Reset;
|
---|
| 117 | private void OnReset() {
|
---|
| 118 | if (Reset != null)
|
---|
| 119 | Reset(this, EventArgs.Empty);
|
---|
| 120 | OnChanged();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|