Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/16/10 04:24:01 (14 years ago)
Author:
swagner
Message:

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs

    r3048 r3054  
    3131  [Creatable("Test")]
    3232  [StorableClass]
    33   public sealed class StringMatrix : Item, IEnumerable, IStringConvertibleMatrix {
     33  public class StringMatrix : Item, IEnumerable, IStringConvertibleMatrix {
    3434    [Storable]
    35     private string[,] array;
     35    protected string[,] matrix;
    3636
    37     public int Rows {
    38       get { return array.GetLength(0); }
    39       private set {
     37    public virtual int Rows {
     38      get { return matrix.GetLength(0); }
     39      protected set {
    4040        if (value != Rows) {
    41           string[,] newArray = new string[value, Columns];
    42           Array.Copy(array, newArray, Math.Min(value * Columns, array.Length));
    43           array = newArray;
     41          string[,] newMatrix = new string[value, Columns];
     42          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
     43          matrix = newMatrix;
    4444          OnReset();
    4545        }
    4646      }
    4747    }
    48     public int Columns {
    49       get { return array.GetLength(1); }
    50       private set {
     48    public virtual int Columns {
     49      get { return matrix.GetLength(1); }
     50      protected set {
    5151        if (value != Columns) {
    52           string[,] newArray = new string[Rows, value];
     52          string[,] newMatrix = new string[Rows, value];
    5353          for (int i = 0; i < Rows; i++)
    54             Array.Copy(array, i * Columns, newArray, i * value, Math.Min(value, Columns));
    55           array = newArray;
     54            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
     55          matrix = newMatrix;
    5656          OnReset();
    5757        }
    5858      }
    5959    }
    60     public string this[int rowIndex, int columnIndex] {
    61       get { return array[rowIndex, columnIndex]; }
     60    public virtual string this[int rowIndex, int columnIndex] {
     61      get { return matrix[rowIndex, columnIndex]; }
    6262      set {
    63         if (value != array[rowIndex, columnIndex]) {
    64           if ((value != null) || (array[rowIndex, columnIndex] != string.Empty)) {
    65             array[rowIndex, columnIndex] = value != null ? value : string.Empty;
     63        if (value != matrix[rowIndex, columnIndex]) {
     64          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
     65            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
    6666            OnItemChanged(rowIndex, columnIndex);
    6767          }
     
    7171
    7272    public StringMatrix() {
    73       array = new string[0, 0];
     73      matrix = new string[0, 0];
    7474    }
    7575    public StringMatrix(int rows, int columns) {
    76       array = new string[rows, columns];
    77       for (int i = 0; i < array.GetLength(0); i++) {
    78         for (int j = 0; j < array.GetLength(1); j++)
    79           array[i, j] = string.Empty;
     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;
    8080      }
    8181    }
    8282    public StringMatrix(string[,] elements) {
    8383      if (elements == null) throw new ArgumentNullException();
    84       array = new string[elements.GetLength(0), elements.GetLength(1)];
    85       for (int i = 0; i < array.GetLength(0); i++) {
    86         for (int j = 0; j < array.GetLength(1); j++)
    87           array[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
     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];
    8888      }
    89     }
    90     private StringMatrix(StringMatrix elements) {
    91       if (elements == null) throw new ArgumentNullException();
    92       array = (string[,])elements.array.Clone();
    9389    }
    9490
    9591    public override IDeepCloneable Clone(Cloner cloner) {
    96       StringMatrix clone = new StringMatrix(this);
     92      StringMatrix clone = new StringMatrix();
    9793      cloner.RegisterClonedObject(this, clone);
     94      clone.matrix = (string[,])matrix.Clone();
    9895      return clone;
    9996    }
     
    10299      StringBuilder sb = new StringBuilder();
    103100      sb.Append("[");
    104       if (array.Length > 0) {
     101      if (matrix.Length > 0) {
    105102        for (int i = 0; i < Rows; i++) {
    106           sb.Append("[").Append(array[i, 0]);
     103          sb.Append("[").Append(matrix[i, 0]);
    107104          for (int j = 1; j < Columns; j++)
    108             sb.Append(";").Append(array[i, j]);
     105            sb.Append(";").Append(matrix[i, j]);
    109106          sb.Append("]");
    110107        }
     
    114111    }
    115112
    116     public IEnumerator GetEnumerator() {
    117       return array.GetEnumerator();
     113    public virtual IEnumerator GetEnumerator() {
     114      return matrix.GetEnumerator();
     115    }
     116
     117    protected virtual bool Validate(string value, out string errorMessage) {
     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    }
     126    protected virtual string GetValue(int rowIndex, int columIndex) {
     127      return this[rowIndex, columIndex];
     128    }
     129    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
     130      if (value != null) {
     131        this[rowIndex, columnIndex] = value;
     132        return true;
     133      } else {
     134        return false;
     135      }
     136    }
     137
     138    public event EventHandler<EventArgs<int, int>> ItemChanged;
     139    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
     140      if (ItemChanged != null)
     141        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
     142      OnToStringChanged();
     143    }
     144    public event EventHandler Reset;
     145    protected virtual void OnReset() {
     146      if (Reset != null)
     147        Reset(this, EventArgs.Empty);
     148      OnToStringChanged();
    118149    }
    119150
     
    127158      set { Columns = value; }
    128159    }
    129 
    130160    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
    131       if (value == null) {
    132         errorMessage = "Invalid Value (string must not be null)";
    133         return false;
    134       } else {
    135         errorMessage = string.Empty;
    136         return true;
    137       }
     161      return Validate(value, out errorMessage);
    138162    }
    139163    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
    140       return this[rowIndex, columIndex];
     164      return GetValue(rowIndex, columIndex);
    141165    }
    142166    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
    143       if (value != null) {
    144         this[rowIndex, columnIndex] = value;
    145         return true;
    146       } else {
    147         return false;
    148       }
    149     }
    150     public event EventHandler<EventArgs<int, int>> ItemChanged;
    151     private void OnItemChanged(int rowIndex, int columnIndex) {
    152       if (ItemChanged != null)
    153         ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
    154       OnToStringChanged();
    155     }
    156     public event EventHandler Reset;
    157     private void OnReset() {
    158       if (Reset != null)
    159         Reset(this, EventArgs.Empty);
    160       OnToStringChanged();
     167      return SetValue(value, rowIndex, columnIndex);
    161168    }
    162169    #endregion
Note: See TracChangeset for help on using the changeset viewer.