Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringMatrixData.cs @ 2883

Last change on this file since 2883 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 5.9 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.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Data {
32  [Item("StringMatrixData", "Represents a matrix of strings.")]
33  [Creatable("Test")]
34  public sealed class StringMatrixData : Item, IEnumerable, IStringConvertibleMatrixData {
35    [Storable]
36    private string[,] array;
37
38    public int Rows {
39      get { return array.GetLength(0); }
40      private set {
41        if (value != Rows) {
42          string[,] newArray = new string[value, Columns];
43          Array.Copy(array, newArray, Math.Min(value * Columns, array.Length));
44          array = newArray;
45          OnReset();
46        }
47      }
48    }
49    public int Columns {
50      get { return array.GetLength(1); }
51      private set {
52        if (value != Columns) {
53          string[,] newArray = new string[Rows, value];
54          for (int i = 0; i < Rows; i++)
55            Array.Copy(array, i * Columns, newArray, i * value, Math.Min(value, Columns));
56          array = newArray;
57          OnReset();
58        }
59      }
60    }
61    public string this[int rowIndex, int columnIndex] {
62      get { return array[rowIndex, columnIndex]; }
63      set {
64        if (value != array[rowIndex, columnIndex]) {
65          if ((value != null) || (array[rowIndex, columnIndex] != string.Empty)) {
66            array[rowIndex, columnIndex] = value != null ? value : string.Empty;
67            OnItemChanged(rowIndex, columnIndex);
68          }
69        }
70      }
71    }
72
73    public StringMatrixData() {
74      array = new string[0, 0];
75    }
76    public StringMatrixData(int rows, int columns) {
77      array = new string[rows, columns];
78      for (int i = 0; i < array.GetLength(0); i++) {
79        for (int j = 0; j < array.GetLength(1); j++)
80          array[i, j] = string.Empty;
81      }
82    }
83    public StringMatrixData(string[,] elements) {
84      if (elements == null) throw new ArgumentNullException();
85      array = new string[elements.GetLength(0), elements.GetLength(1)];
86      for (int i = 0; i < array.GetLength(0); i++) {
87        for (int j = 0; j < array.GetLength(1); j++)
88          array[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
89      }
90    }
91    private StringMatrixData(StringMatrixData elements) {
92      if (elements == null) throw new ArgumentNullException();
93      array = (string[,])elements.array.Clone();
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      StringMatrixData clone = new StringMatrixData(this);
98      cloner.RegisterClonedObject(this, clone);
99      return clone;
100    }
101
102    public override string ToString() {
103      StringBuilder sb = new StringBuilder();
104      sb.Append("[");
105      if (array.Length > 0) {
106        for (int i = 0; i < Rows; i++) {
107          sb.Append("[").Append(array[i, 0]);
108          for (int j = 1; j < Columns; j++)
109            sb.Append(";").Append(array[i, j]);
110          sb.Append("]");
111        }
112      }
113      sb.Append("]");
114      return sb.ToString();
115    }
116
117    public IEnumerator GetEnumerator() {
118      return array.GetEnumerator();
119    }
120
121    #region IStringConvertibleMatrixData Members
122    StringConvertibleArrayDataDimensions IStringConvertibleMatrixData.Dimensions {
123      get { return StringConvertibleArrayDataDimensions.Both; }
124    }
125    int IStringConvertibleMatrixData.Rows {
126      get { return Rows; }
127      set { Rows = value; }
128    }
129    int IStringConvertibleMatrixData.Columns {
130      get { return Columns; }
131      set { Columns = value; }
132    }
133
134    bool IStringConvertibleMatrixData.Validate(string value, out string errorMessage) {
135      if (value == null) {
136        errorMessage = "Invalid Value (string must not be null)";
137        return false;
138      } else {
139        errorMessage = string.Empty;
140        return true;
141      }
142    }
143    string IStringConvertibleMatrixData.GetValue(int rowIndex, int columIndex) {
144      return this[rowIndex, columIndex];
145    }
146    bool IStringConvertibleMatrixData.SetValue(string value, int rowIndex, int columnIndex) {
147      if (value != null) {
148        this[rowIndex, columnIndex] = value;
149        return true;
150      } else {
151        return false;
152      }
153    }
154    private event EventHandler<EventArgs<int, int>> ItemChanged;
155    event EventHandler<EventArgs<int, int>> IStringConvertibleMatrixData.ItemChanged {
156      add { ItemChanged += value; }
157      remove { ItemChanged -= value; }
158    }
159    private void OnItemChanged(int rowIndex, int columnIndex) {
160      if (ItemChanged != null)
161        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
162      OnChanged();
163    }
164    private event EventHandler Reset;
165    event EventHandler IStringConvertibleMatrixData.Reset {
166      add { Reset += value; }
167      remove { Reset -= value; }
168    }
169    private void OnReset() {
170      if (Reset != null)
171        Reset(this, EventArgs.Empty);
172      OnChanged();
173    }
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.