[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2694] | 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.Text;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Data {
|
---|
[3048] | 30 | [Item("StringMatrix", "Represents a matrix of strings.")]
|
---|
[3017] | 31 | [StorableClass]
|
---|
[3054] | 32 | public class StringMatrix : Item, IEnumerable, IStringConvertibleMatrix {
|
---|
[2694] | 33 | [Storable]
|
---|
[3054] | 34 | protected string[,] matrix;
|
---|
[2694] | 35 |
|
---|
[3054] | 36 | public virtual int Rows {
|
---|
| 37 | get { return matrix.GetLength(0); }
|
---|
| 38 | protected set {
|
---|
[2694] | 39 | if (value != Rows) {
|
---|
[3054] | 40 | string[,] newMatrix = new string[value, Columns];
|
---|
| 41 | Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
|
---|
| 42 | matrix = newMatrix;
|
---|
[2694] | 43 | OnReset();
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[3054] | 47 | public virtual int Columns {
|
---|
| 48 | get { return matrix.GetLength(1); }
|
---|
| 49 | protected set {
|
---|
[2694] | 50 | if (value != Columns) {
|
---|
[3054] | 51 | string[,] newMatrix = new string[Rows, value];
|
---|
[2694] | 52 | for (int i = 0; i < Rows; i++)
|
---|
[3054] | 53 | Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
|
---|
| 54 | matrix = newMatrix;
|
---|
[2694] | 55 | OnReset();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[3054] | 59 | public virtual string this[int rowIndex, int columnIndex] {
|
---|
| 60 | get { return matrix[rowIndex, columnIndex]; }
|
---|
[2694] | 61 | set {
|
---|
[3054] | 62 | if (value != matrix[rowIndex, columnIndex]) {
|
---|
| 63 | if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
|
---|
| 64 | matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
[2694] | 65 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[3048] | 71 | public StringMatrix() {
|
---|
[3054] | 72 | matrix = new string[0, 0];
|
---|
[2694] | 73 | }
|
---|
[3048] | 74 | public StringMatrix(int rows, int columns) {
|
---|
[3054] | 75 | matrix = new string[rows, columns];
|
---|
| 76 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 77 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 78 | matrix[i, j] = string.Empty;
|
---|
[2694] | 79 | }
|
---|
| 80 | }
|
---|
[3048] | 81 | public StringMatrix(string[,] elements) {
|
---|
[2694] | 82 | if (elements == null) throw new ArgumentNullException();
|
---|
[3054] | 83 | matrix = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
| 84 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 85 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 86 | matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
[2694] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 91 | StringMatrix clone = new StringMatrix();
|
---|
[2694] | 92 | cloner.RegisterClonedObject(this, clone);
|
---|
[3054] | 93 | clone.matrix = (string[,])matrix.Clone();
|
---|
[2694] | 94 | return clone;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public override string ToString() {
|
---|
| 98 | StringBuilder sb = new StringBuilder();
|
---|
| 99 | sb.Append("[");
|
---|
[3054] | 100 | if (matrix.Length > 0) {
|
---|
[2694] | 101 | for (int i = 0; i < Rows; i++) {
|
---|
[3054] | 102 | sb.Append("[").Append(matrix[i, 0]);
|
---|
[2694] | 103 | for (int j = 1; j < Columns; j++)
|
---|
[3054] | 104 | sb.Append(";").Append(matrix[i, j]);
|
---|
[2694] | 105 | sb.Append("]");
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | sb.Append("]");
|
---|
| 109 | return sb.ToString();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[3054] | 112 | public virtual IEnumerator GetEnumerator() {
|
---|
| 113 | return matrix.GetEnumerator();
|
---|
[2694] | 114 | }
|
---|
| 115 |
|
---|
[3054] | 116 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 117 | if (value == null) {
|
---|
| 118 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 119 | return false;
|
---|
| 120 | } else {
|
---|
| 121 | errorMessage = string.Empty;
|
---|
| 122 | return true;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[3054] | 125 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
[2694] | 126 | return this[rowIndex, columIndex];
|
---|
| 127 | }
|
---|
[3054] | 128 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[2694] | 129 | if (value != null) {
|
---|
| 130 | this[rowIndex, columnIndex] = value;
|
---|
| 131 | return true;
|
---|
| 132 | } else {
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[3054] | 136 |
|
---|
[2973] | 137 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[3054] | 138 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[2694] | 139 | if (ItemChanged != null)
|
---|
| 140 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[2932] | 141 | OnToStringChanged();
|
---|
[2694] | 142 | }
|
---|
[2973] | 143 | public event EventHandler Reset;
|
---|
[3054] | 144 | protected virtual void OnReset() {
|
---|
[2694] | 145 | if (Reset != null)
|
---|
| 146 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 147 | OnToStringChanged();
|
---|
[2694] | 148 | }
|
---|
[3054] | 149 |
|
---|
| 150 | #region IStringConvertibleMatrix Members
|
---|
| 151 | int IStringConvertibleMatrix.Rows {
|
---|
| 152 | get { return Rows; }
|
---|
| 153 | set { Rows = value; }
|
---|
| 154 | }
|
---|
| 155 | int IStringConvertibleMatrix.Columns {
|
---|
| 156 | get { return Columns; }
|
---|
| 157 | set { Columns = value; }
|
---|
| 158 | }
|
---|
| 159 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
| 160 | return Validate(value, out errorMessage);
|
---|
| 161 | }
|
---|
| 162 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
| 163 | return GetValue(rowIndex, columIndex);
|
---|
| 164 | }
|
---|
| 165 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 166 | return SetValue(value, rowIndex, columnIndex);
|
---|
| 167 | }
|
---|
[2694] | 168 | #endregion
|
---|
| 169 | }
|
---|
| 170 | }
|
---|