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