Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs @ 3310

Last change on this file since 3310 was 3310, checked in by mkommend, 14 years ago

added RowNames for IStringConvertibleMatrix
and fixed cloning and ctors of concrete matrixes (ticket #968)

File size: 8.0 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.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("StringMatrix", "Represents a matrix of strings.")]
34  [StorableClass]
35  public class StringMatrix : Item, IEnumerable, IStringConvertibleMatrix {
36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
40    [Storable]
41    protected string[,] matrix;
42
43    [Storable]
44    private List<string> columnNames;
45    public IEnumerable<string> ColumnNames {
46      get { return this.columnNames; }
47      set {
48        if (value == null || value.Count() == 0)
49          columnNames = new List<string>();
50        else if (value.Count() != Columns)
51          throw new ArgumentException("A column name must be specified for each column .");
52        else
53          columnNames = new List<string>(value);
54      }
55    }
56    [Storable]
57    private List<string> rowNames;
58    public IEnumerable<string> RowNames {
59      get { return this.rowNames; }
60      set {
61        if (value == null || value.Count() == 0)
62          rowNames = new List<string>();
63        else if (value.Count() != Rows)
64          throw new ArgumentException("A row name must be specified for each row.");
65        else
66          rowNames = new List<string>(value);
67      }
68    }
69
70    public virtual int Rows {
71      get { return matrix.GetLength(0); }
72      protected set {
73        if (value != Rows) {
74          string[,] newMatrix = new string[value, Columns];
75          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
76          matrix = newMatrix;
77          OnReset();
78        }
79      }
80    }
81    public virtual int Columns {
82      get { return matrix.GetLength(1); }
83      protected set {
84        if (value != Columns) {
85          string[,] newMatrix = new string[Rows, value];
86          for (int i = 0; i < Rows; i++)
87            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
88          matrix = newMatrix;
89          OnReset();
90        }
91      }
92    }
93    public virtual string this[int rowIndex, int columnIndex] {
94      get { return matrix[rowIndex, columnIndex]; }
95      set {
96        if (value != matrix[rowIndex, columnIndex]) {
97          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
98            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
99            OnItemChanged(rowIndex, columnIndex);
100          }
101        }
102      }
103    }
104
105    public StringMatrix() {
106      matrix = new string[0, 0];
107      columnNames = new List<string>();
108      rowNames = new List<string>();
109    }
110    public StringMatrix(int rows, int columns) {
111      matrix = new string[rows, columns];
112      for (int i = 0; i < matrix.GetLength(0); i++) {
113        for (int j = 0; j < matrix.GetLength(1); j++)
114          matrix[i, j] = string.Empty;
115      }
116      columnNames = new List<string>();
117      rowNames = new List<string>();
118    }
119    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
120      : this(rows, columns) {
121      ColumnNames = columnNames;
122    }
123    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
124      : this(rows, columns,columnNames) {
125      RowNames = rowNames;
126    }
127    public StringMatrix(string[,] elements) {
128      if (elements == null) throw new ArgumentNullException();
129      matrix = new string[elements.GetLength(0), elements.GetLength(1)];
130      for (int i = 0; i < matrix.GetLength(0); i++) {
131        for (int j = 0; j < matrix.GetLength(1); j++)
132          matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
133      }
134      columnNames = new List<string>();
135      rowNames = new List<string>();
136    }
137    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
138      : this(elements) {
139      ColumnNames = columnNames;
140    }
141    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
142      : this(elements,columnNames) {
143      RowNames = rowNames;
144    }
145
146    public override IDeepCloneable Clone(Cloner cloner) {
147      StringMatrix clone = new StringMatrix();
148      cloner.RegisterClonedObject(this, clone);
149      clone.matrix = (string[,])matrix.Clone();
150      clone.columnNames = new List<string>(columnNames);
151      clone.rowNames = new List<string>(rowNames);
152      return clone;
153    }
154
155    public override string ToString() {
156      StringBuilder sb = new StringBuilder();
157      sb.Append("[");
158      if (matrix.Length > 0) {
159        for (int i = 0; i < Rows; i++) {
160          sb.Append("[").Append(matrix[i, 0]);
161          for (int j = 1; j < Columns; j++)
162            sb.Append(";").Append(matrix[i, j]);
163          sb.Append("]");
164        }
165      }
166      sb.Append("]");
167      return sb.ToString();
168    }
169
170    public virtual IEnumerator GetEnumerator() {
171      return matrix.GetEnumerator();
172    }
173
174    protected virtual bool Validate(string value, out string errorMessage) {
175      if (value == null) {
176        errorMessage = "Invalid Value (string must not be null)";
177        return false;
178      } else {
179        errorMessage = string.Empty;
180        return true;
181      }
182    }
183    protected virtual string GetValue(int rowIndex, int columIndex) {
184      return this[rowIndex, columIndex];
185    }
186    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
187      if (value != null) {
188        this[rowIndex, columnIndex] = value;
189        return true;
190      } else {
191        return false;
192      }
193    }
194
195    public event EventHandler<EventArgs<int, int>> ItemChanged;
196    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
197      if (ItemChanged != null)
198        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
199      OnToStringChanged();
200    }
201    public event EventHandler Reset;
202    protected virtual void OnReset() {
203      if (Reset != null)
204        Reset(this, EventArgs.Empty);
205      OnToStringChanged();
206    }
207
208    #region IStringConvertibleMatrix Members
209    int IStringConvertibleMatrix.Rows {
210      get { return Rows; }
211      set { Rows = value; }
212    }
213    int IStringConvertibleMatrix.Columns {
214      get { return Columns; }
215      set { Columns = value; }
216    }
217    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
218      get { return this.ColumnNames; }
219      set { this.ColumnNames = value; }
220    }
221    IEnumerable<string> IStringConvertibleMatrix.RowNames {
222      get { return this.RowNames; }
223      set { this.RowNames = value; }
224    }
225    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
226      return Validate(value, out errorMessage);
227    }
228    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
229      return GetValue(rowIndex, columIndex);
230    }
231    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
232      return SetValue(value, rowIndex, columnIndex);
233    }
234    #endregion
235  }
236}
Note: See TracBrowser for help on using the repository browser.