Changeset 3054 for trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs
- Timestamp:
- 03/16/10 04:24:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs
r3048 r3054 31 31 [Creatable("Test")] 32 32 [StorableClass] 33 public sealedclass StringMatrix : Item, IEnumerable, IStringConvertibleMatrix {33 public class StringMatrix : Item, IEnumerable, IStringConvertibleMatrix { 34 34 [Storable] 35 pr ivate string[,] array;35 protected string[,] matrix; 36 36 37 public int Rows {38 get { return array.GetLength(0); }39 pr ivateset {37 public virtual int Rows { 38 get { return matrix.GetLength(0); } 39 protected set { 40 40 if (value != Rows) { 41 string[,] new Array= 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; 44 44 OnReset(); 45 45 } 46 46 } 47 47 } 48 public int Columns {49 get { return array.GetLength(1); }50 pr ivateset {48 public virtual int Columns { 49 get { return matrix.GetLength(1); } 50 protected set { 51 51 if (value != Columns) { 52 string[,] new Array= new string[Rows, value];52 string[,] newMatrix = new string[Rows, value]; 53 53 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; 56 56 OnReset(); 57 57 } 58 58 } 59 59 } 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]; } 62 62 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; 66 66 OnItemChanged(rowIndex, columnIndex); 67 67 } … … 71 71 72 72 public StringMatrix() { 73 array= new string[0, 0];73 matrix = new string[0, 0]; 74 74 } 75 75 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; 80 80 } 81 81 } 82 82 public StringMatrix(string[,] elements) { 83 83 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]; 88 88 } 89 }90 private StringMatrix(StringMatrix elements) {91 if (elements == null) throw new ArgumentNullException();92 array = (string[,])elements.array.Clone();93 89 } 94 90 95 91 public override IDeepCloneable Clone(Cloner cloner) { 96 StringMatrix clone = new StringMatrix( this);92 StringMatrix clone = new StringMatrix(); 97 93 cloner.RegisterClonedObject(this, clone); 94 clone.matrix = (string[,])matrix.Clone(); 98 95 return clone; 99 96 } … … 102 99 StringBuilder sb = new StringBuilder(); 103 100 sb.Append("["); 104 if ( array.Length > 0) {101 if (matrix.Length > 0) { 105 102 for (int i = 0; i < Rows; i++) { 106 sb.Append("[").Append( array[i, 0]);103 sb.Append("[").Append(matrix[i, 0]); 107 104 for (int j = 1; j < Columns; j++) 108 sb.Append(";").Append( array[i, j]);105 sb.Append(";").Append(matrix[i, j]); 109 106 sb.Append("]"); 110 107 } … … 114 111 } 115 112 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(); 118 149 } 119 150 … … 127 158 set { Columns = value; } 128 159 } 129 130 160 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); 138 162 } 139 163 string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) { 140 return this[rowIndex, columIndex];164 return GetValue(rowIndex, columIndex); 141 165 } 142 166 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); 161 168 } 162 169 #endregion
Note: See TracChangeset
for help on using the changeset viewer.