Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/StringMatrix.cs @ 17253

Last change on this file since 17253 was 17253, checked in by abeham, 5 years ago

#2521: worked on refactoring PTSP

File size: 12.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Drawing;
26using System.Linq;
27using System.Text;
28using HEAL.Attic;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.Data {
33  [Item("StringMatrix", "Represents a matrix of strings.")]
34  [StorableType("0BFE5727-D2CF-418C-94BE-A8A0BBA195D8")]
35  public class StringMatrix : Item, IEnumerable<string>, IStringConvertibleMatrix {
36    private const int maximumToStringLength = 100;
37
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
40    }
41
42    [Storable]
43    protected string[,] matrix;
44
45    [Storable]
46    protected List<string> columnNames;
47    public virtual IEnumerable<string> ColumnNames {
48      get { return this.columnNames; }
49      set {
50        if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. StringMatrix is read-only.");
51        if (value == null || value.Count() == 0)
52          columnNames = new List<string>();
53        else if (value.Count() != Columns)
54          throw new ArgumentException("A column name must be specified for each column .");
55        else
56          columnNames = new List<string>(value);
57        OnColumnNamesChanged();
58      }
59    }
60    [Storable]
61    protected List<string> rowNames;
62    public virtual IEnumerable<string> RowNames {
63      get { return this.rowNames; }
64      set {
65        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. StringMatrix is read-only.");
66        if (value == null || value.Count() == 0)
67          rowNames = new List<string>();
68        else if (value.Count() != Rows)
69          throw new ArgumentException("A row name must be specified for each row.");
70        else
71          rowNames = new List<string>(value);
72        OnRowNamesChanged();
73      }
74    }
75    [Storable]
76    protected bool sortableView;
77    public virtual bool SortableView {
78      get { return sortableView; }
79      set {
80        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. StringMatrix is read-only.");
81        if (value != sortableView) {
82          sortableView = value;
83          OnSortableViewChanged();
84        }
85      }
86    }
87
88    public virtual int Rows {
89      get { return matrix.GetLength(0); }
90      protected set {
91        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. StringMatrix is read-only.");
92        if (value != Rows) {
93          string[,] newMatrix = new string[value, Columns];
94          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
95          matrix = newMatrix;
96          while (rowNames.Count > value)
97            rowNames.RemoveAt(rowNames.Count - 1);
98          while (rowNames.Count < value)
99            rowNames.Add("Row " + rowNames.Count);
100          OnRowsChanged();
101          OnRowNamesChanged();
102          OnReset();
103        }
104      }
105    }
106    public virtual int Columns {
107      get { return matrix.GetLength(1); }
108      protected set {
109        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. StringMatrix is read-only.");
110        if (value != Columns) {
111          string[,] newMatrix = new string[Rows, value];
112          for (int i = 0; i < Rows; i++)
113            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
114          matrix = newMatrix;
115          while (columnNames.Count > value)
116            columnNames.RemoveAt(columnNames.Count - 1);
117          while (columnNames.Count < value)
118            columnNames.Add("Column " + columnNames.Count);
119          OnColumnsChanged();
120          OnColumnNamesChanged();
121          OnReset();
122        }
123      }
124    }
125    public virtual string this[int rowIndex, int columnIndex] {
126      get { return matrix[rowIndex, columnIndex]; }
127      set {
128        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
129        if (value != matrix[rowIndex, columnIndex]) {
130          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
131            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
132            OnItemChanged(rowIndex, columnIndex);
133          }
134        }
135      }
136    }
137
138    [Storable]
139    protected bool readOnly;
140    public virtual bool ReadOnly {
141      get { return readOnly; }
142    }
143
144    [StorableConstructor]
145    protected StringMatrix(StorableConstructorFlag _) : base(_) { }
146    protected StringMatrix(StringMatrix original, Cloner cloner)
147      : base(original, cloner) {
148      this.matrix = (string[,])original.matrix.Clone();
149      this.columnNames = new List<string>(original.columnNames);
150      this.rowNames = new List<string>(original.rowNames);
151      this.sortableView = original.sortableView;
152      this.readOnly = original.readOnly;
153    }
154    public StringMatrix() {
155      matrix = new string[0, 0];
156      columnNames = new List<string>();
157      rowNames = new List<string>();
158      sortableView = false;
159      readOnly = false;
160    }
161    public StringMatrix(int rows, int columns) {
162      matrix = new string[rows, columns];
163      for (int i = 0; i < matrix.GetLength(0); i++) {
164        for (int j = 0; j < matrix.GetLength(1); j++)
165          matrix[i, j] = string.Empty;
166      }
167      columnNames = new List<string>();
168      rowNames = new List<string>();
169      sortableView = false;
170      readOnly = false;
171    }
172    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
173      : this(rows, columns) {
174      ColumnNames = columnNames;
175    }
176    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
177      : this(rows, columns, columnNames) {
178      RowNames = rowNames;
179    }
180    public StringMatrix(string[,] elements, bool @readonly = false) {
181      if (elements == null) throw new ArgumentNullException();
182      matrix = new string[elements.GetLength(0), elements.GetLength(1)];
183      for (int i = 0; i < matrix.GetLength(0); i++) {
184        for (int j = 0; j < matrix.GetLength(1); j++)
185          matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
186      }
187      columnNames = new List<string>();
188      rowNames = new List<string>();
189      sortableView = false;
190      readOnly = @readonly;
191    }
192    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames, bool @readonly = false)
193      : this(elements, @readonly) {
194      ColumnNames = columnNames;
195    }
196    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames, bool @readonly = false)
197      : this(elements, columnNames, @readonly ) {
198      RowNames = rowNames;
199    }
200
201    public override IDeepCloneable Clone(Cloner cloner) {
202      return new StringMatrix(this, cloner);
203    }
204
205    public virtual StringMatrix AsReadOnly() {
206      if (ReadOnly) return this;
207      var clone = (StringMatrix)this.Clone();
208      clone.readOnly = true;
209      return clone;
210    }
211
212    public string[,] CloneAsMatrix() {
213      return (string[,])matrix.Clone();
214    }
215
216    public virtual IEnumerable<string> GetRow(int row) {
217      for (var col = 0; col < Columns; col++) {
218        yield return matrix[row, col];
219      }
220    }
221
222    public virtual IEnumerable<string> GetColumn(int col) {
223      for (var row = 0; row < Rows; row++) {
224        yield return matrix[row, col];
225      }
226    }
227
228    public override string ToString() {
229      if (matrix.Length == 0) return "[]";
230
231      StringBuilder sb = new StringBuilder();
232      sb.Append("[");
233      for (int i = 0; i < Rows; i++) {
234        sb.Append("[").Append(matrix[i, 0]);
235        for (int j = 1; j < Columns; j++)
236          sb.Append(";").Append(matrix[i, j]);
237        sb.Append("]");
238
239        if (sb.Length > maximumToStringLength) {
240          sb.Append("[...]");
241          break;
242        }
243      }
244      sb.Append("]");
245
246      return sb.ToString();
247    }
248
249    public virtual IEnumerator<string> GetEnumerator() {
250      return matrix.Cast<string>().GetEnumerator();
251    }
252
253    IEnumerator IEnumerable.GetEnumerator() {
254      return GetEnumerator();
255    }
256
257    protected virtual bool Validate(string value, out string errorMessage) {
258      if (value == null) {
259        errorMessage = "Invalid Value (string must not be null)";
260        return false;
261      } else {
262        errorMessage = string.Empty;
263        return true;
264      }
265    }
266    protected virtual string GetValue(int rowIndex, int columIndex) {
267      return this[rowIndex, columIndex];
268    }
269    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
270      if (value != null) {
271        this[rowIndex, columnIndex] = value;
272        return true;
273      } else {
274        return false;
275      }
276    }
277
278    #region events
279    public event EventHandler ColumnsChanged;
280    protected virtual void OnColumnsChanged() {
281      EventHandler handler = ColumnsChanged;
282      if (handler != null)
283        handler(this, EventArgs.Empty);
284    }
285    public event EventHandler RowsChanged;
286    protected virtual void OnRowsChanged() {
287      EventHandler handler = RowsChanged;
288      if (handler != null)
289        handler(this, EventArgs.Empty);
290    }
291    public event EventHandler ColumnNamesChanged;
292    protected virtual void OnColumnNamesChanged() {
293      EventHandler handler = ColumnNamesChanged;
294      if (handler != null)
295        handler(this, EventArgs.Empty);
296    }
297    public event EventHandler RowNamesChanged;
298    protected virtual void OnRowNamesChanged() {
299      EventHandler handler = RowNamesChanged;
300      if (handler != null)
301        handler(this, EventArgs.Empty);
302    }
303    public event EventHandler SortableViewChanged;
304    protected virtual void OnSortableViewChanged() {
305      EventHandler handler = SortableViewChanged;
306      if (handler != null)
307        handler(this, EventArgs.Empty);
308    }
309    public event EventHandler<EventArgs<int, int>> ItemChanged;
310    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
311      if (ItemChanged != null)
312        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
313
314      //approximation to avoid firing of unnecessary ToStringChangedEvents
315      //columnIndex is not used, because always full rows are returned in the ToString method
316      if (rowIndex * Columns < maximumToStringLength)
317        OnToStringChanged();
318    }
319    public event EventHandler Reset;
320    protected virtual void OnReset() {
321      if (Reset != null)
322        Reset(this, EventArgs.Empty);
323      OnToStringChanged();
324    }
325    #endregion
326
327    #region IStringConvertibleMatrix Members
328    int IStringConvertibleMatrix.Rows {
329      get { return Rows; }
330      set { Rows = value; }
331    }
332    int IStringConvertibleMatrix.Columns {
333      get { return Columns; }
334      set { Columns = value; }
335    }
336    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
337      return Validate(value, out errorMessage);
338    }
339    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
340      return GetValue(rowIndex, columIndex);
341    }
342    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
343      return SetValue(value, rowIndex, columnIndex);
344    }
345    #endregion
346  }
347}
Note: See TracBrowser for help on using the repository browser.