[2677] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2677] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
[3308] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3308] | 26 | using System.Linq;
|
---|
[2677] | 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[3822] | 33 | [Item("ValueTypeMatrix", "An abstract base class for representing matrices of value types.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3430] | 35 | public abstract class ValueTypeMatrix<T> : Item, IEnumerable<T> where T : struct {
|
---|
[9432] | 36 | private const int maximumToStringLength = 100;
|
---|
| 37 |
|
---|
[7201] | 38 | public static new Image StaticItemImage {
|
---|
[5287] | 39 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[3306] | 40 | }
|
---|
| 41 |
|
---|
[2694] | 42 | [Storable]
|
---|
[3054] | 43 | protected T[,] matrix;
|
---|
[2677] | 44 |
|
---|
[3308] | 45 | [Storable]
|
---|
[3430] | 46 | protected List<string> columnNames;
|
---|
| 47 | public virtual IEnumerable<string> ColumnNames {
|
---|
[3308] | 48 | get { return this.columnNames; }
|
---|
| 49 | set {
|
---|
[3430] | 50 | if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. ValueTypeMatrix is read-only.");
|
---|
[3308] | 51 | if (value == null || value.Count() == 0)
|
---|
| 52 | columnNames = new List<string>();
|
---|
| 53 | else if (value.Count() != Columns)
|
---|
[3310] | 54 | throw new ArgumentException("A column name must be specified for each column.");
|
---|
[3308] | 55 | else
|
---|
| 56 | columnNames = new List<string>(value);
|
---|
[5150] | 57 | OnColumnNamesChanged();
|
---|
[3308] | 58 | }
|
---|
| 59 | }
|
---|
[3310] | 60 | [Storable]
|
---|
[3430] | 61 | protected List<string> rowNames;
|
---|
| 62 | public virtual IEnumerable<string> RowNames {
|
---|
[3310] | 63 | get { return this.rowNames; }
|
---|
| 64 | set {
|
---|
[3430] | 65 | if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. ValueTypeMatrix is read-only.");
|
---|
[3310] | 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);
|
---|
[5150] | 72 | OnRowNamesChanged();
|
---|
[3310] | 73 | }
|
---|
| 74 | }
|
---|
[3320] | 75 | [Storable]
|
---|
[3430] | 76 | protected bool sortableView;
|
---|
| 77 | public virtual bool SortableView {
|
---|
[3320] | 78 | get { return sortableView; }
|
---|
| 79 | set {
|
---|
[3430] | 80 | if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. ValueTypeMatrix is read-only.");
|
---|
[3320] | 81 | if (value != sortableView) {
|
---|
| 82 | sortableView = value;
|
---|
| 83 | OnSortableViewChanged();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
[3308] | 87 |
|
---|
[3054] | 88 | public virtual int Rows {
|
---|
| 89 | get { return matrix.GetLength(0); }
|
---|
[2677] | 90 | protected set {
|
---|
[3430] | 91 | if (ReadOnly) throw new NotSupportedException("Rows cannot be set. ValueTypeMatrix is read-only.");
|
---|
[2677] | 92 | if (value != Rows) {
|
---|
| 93 | T[,] newArray = new T[value, Columns];
|
---|
[3054] | 94 | Array.Copy(matrix, newArray, Math.Min(value * Columns, matrix.Length));
|
---|
| 95 | matrix = newArray;
|
---|
[3320] | 96 | while (rowNames.Count > value)
|
---|
| 97 | rowNames.RemoveAt(rowNames.Count - 1);
|
---|
| 98 | while (rowNames.Count < value)
|
---|
| 99 | rowNames.Add("Row " + rowNames.Count);
|
---|
[5150] | 100 | OnRowsChanged();
|
---|
| 101 | OnRowNamesChanged();
|
---|
[2677] | 102 | OnReset();
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[3054] | 106 | public virtual int Columns {
|
---|
| 107 | get { return matrix.GetLength(1); }
|
---|
[2677] | 108 | protected set {
|
---|
[3430] | 109 | if (ReadOnly) throw new NotSupportedException("Columns cannot be set. ValueTypeMatrix is read-only.");
|
---|
[2677] | 110 | if (value != Columns) {
|
---|
| 111 | T[,] newArray = new T[Rows, value];
|
---|
| 112 | for (int i = 0; i < Rows; i++)
|
---|
[3054] | 113 | Array.Copy(matrix, i * Columns, newArray, i * value, Math.Min(value, Columns));
|
---|
| 114 | matrix = newArray;
|
---|
[3320] | 115 | while (columnNames.Count > value)
|
---|
| 116 | columnNames.RemoveAt(columnNames.Count - 1);
|
---|
| 117 | while (columnNames.Count < value)
|
---|
| 118 | columnNames.Add("Column " + columnNames.Count);
|
---|
[5150] | 119 | OnColumnsChanged();
|
---|
| 120 | OnColumnNamesChanged();
|
---|
[2677] | 121 | OnReset();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[3054] | 125 | public virtual T this[int rowIndex, int columnIndex] {
|
---|
| 126 | get { return matrix[rowIndex, columnIndex]; }
|
---|
[2677] | 127 | set {
|
---|
[3430] | 128 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
|
---|
[3054] | 129 | if (!value.Equals(matrix[rowIndex, columnIndex])) {
|
---|
| 130 | matrix[rowIndex, columnIndex] = value;
|
---|
[2677] | 131 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[3430] | 136 | [Storable]
|
---|
| 137 | protected bool readOnly;
|
---|
| 138 | public virtual bool ReadOnly {
|
---|
| 139 | get { return readOnly; }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[4722] | 142 | [StorableConstructor]
|
---|
| 143 | protected ValueTypeMatrix(bool deserializing) : base(deserializing) { }
|
---|
| 144 | protected ValueTypeMatrix(ValueTypeMatrix<T> original, Cloner cloner)
|
---|
| 145 | : base(original, cloner) {
|
---|
| 146 | this.matrix = (T[,])original.matrix.Clone();
|
---|
| 147 | this.columnNames = new List<string>(original.columnNames);
|
---|
| 148 | this.rowNames = new List<string>(original.rowNames);
|
---|
| 149 | this.sortableView = original.sortableView;
|
---|
| 150 | this.readOnly = original.readOnly;
|
---|
| 151 | }
|
---|
[3054] | 152 | protected ValueTypeMatrix() {
|
---|
| 153 | matrix = new T[0, 0];
|
---|
[3308] | 154 | columnNames = new List<string>();
|
---|
[3310] | 155 | rowNames = new List<string>();
|
---|
[3320] | 156 | sortableView = false;
|
---|
[3430] | 157 | readOnly = false;
|
---|
[2677] | 158 | }
|
---|
[3054] | 159 | protected ValueTypeMatrix(int rows, int columns) {
|
---|
| 160 | matrix = new T[rows, columns];
|
---|
[3308] | 161 | columnNames = new List<string>();
|
---|
[3310] | 162 | rowNames = new List<string>();
|
---|
[3320] | 163 | sortableView = false;
|
---|
[3430] | 164 | readOnly = false;
|
---|
[2677] | 165 | }
|
---|
[3308] | 166 | protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames)
|
---|
| 167 | : this(rows, columns) {
|
---|
| 168 | ColumnNames = columnNames;
|
---|
| 169 | }
|
---|
[3430] | 170 | protected ValueTypeMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
[3310] | 171 | : this(rows, columns, columnNames) {
|
---|
| 172 | RowNames = rowNames;
|
---|
| 173 | }
|
---|
[3054] | 174 | protected ValueTypeMatrix(T[,] elements) {
|
---|
[2677] | 175 | if (elements == null) throw new ArgumentNullException();
|
---|
[3054] | 176 | matrix = (T[,])elements.Clone();
|
---|
[3308] | 177 | columnNames = new List<string>();
|
---|
[3310] | 178 | rowNames = new List<string>();
|
---|
[3320] | 179 | sortableView = false;
|
---|
[3430] | 180 | readOnly = false;
|
---|
[2677] | 181 | }
|
---|
[3308] | 182 | protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames)
|
---|
| 183 | : this(elements) {
|
---|
| 184 | ColumnNames = columnNames;
|
---|
| 185 | }
|
---|
[3310] | 186 | protected ValueTypeMatrix(T[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
[5150] | 187 | : this(elements, columnNames) {
|
---|
[3310] | 188 | RowNames = rowNames;
|
---|
| 189 | }
|
---|
[2677] | 190 |
|
---|
[3430] | 191 | public virtual ValueTypeMatrix<T> AsReadOnly() {
|
---|
| 192 | ValueTypeMatrix<T> readOnlyValueTypeMatrix = (ValueTypeMatrix<T>)this.Clone();
|
---|
| 193 | readOnlyValueTypeMatrix.readOnly = true;
|
---|
| 194 | return readOnlyValueTypeMatrix;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[14082] | 197 |
|
---|
| 198 | public T[,] CloneAsMatrix() {
|
---|
| 199 | //mkommend: this works because T must be a value type (struct constraint);
|
---|
| 200 | return (T[,])matrix.Clone();
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[14672] | 203 | public virtual IEnumerable<T> GetRow(int row) {
|
---|
| 204 | for (var col = 0; col < Columns; col++) {
|
---|
| 205 | yield return matrix[row, col];
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | public virtual IEnumerable<T> GetColumn(int col) {
|
---|
| 210 | for (var row = 0; row < Rows; row++) {
|
---|
| 211 | yield return matrix[row, col];
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[2677] | 215 | public override string ToString() {
|
---|
[9432] | 216 | if (matrix.Length == 0) return "[]";
|
---|
| 217 |
|
---|
[2677] | 218 | StringBuilder sb = new StringBuilder();
|
---|
| 219 | sb.Append("[");
|
---|
[9432] | 220 | for (int i = 0; i < Rows; i++) {
|
---|
| 221 | sb.Append("[").Append(matrix[i, 0].ToString());
|
---|
| 222 | for (int j = 1; j < Columns; j++)
|
---|
| 223 | sb.Append(";").Append(matrix[i, j].ToString());
|
---|
| 224 | sb.Append("]");
|
---|
| 225 |
|
---|
| 226 | if (sb.Length > maximumToStringLength) {
|
---|
| 227 | sb.Append("[...]");
|
---|
| 228 | break;
|
---|
[2677] | 229 | }
|
---|
| 230 | }
|
---|
| 231 | sb.Append("]");
|
---|
[9432] | 232 |
|
---|
[2677] | 233 | return sb.ToString();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[3430] | 236 | public virtual IEnumerator<T> GetEnumerator() {
|
---|
| 237 | return matrix.Cast<T>().GetEnumerator();
|
---|
[2677] | 238 | }
|
---|
| 239 |
|
---|
[3430] | 240 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 241 | return GetEnumerator();
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[5150] | 244 | #region events
|
---|
| 245 | public event EventHandler ColumnsChanged;
|
---|
| 246 | protected virtual void OnColumnsChanged() {
|
---|
| 247 | EventHandler handler = ColumnsChanged;
|
---|
| 248 | if (handler != null)
|
---|
| 249 | handler(this, EventArgs.Empty);
|
---|
| 250 | }
|
---|
| 251 | public event EventHandler RowsChanged;
|
---|
| 252 | protected virtual void OnRowsChanged() {
|
---|
| 253 | EventHandler handler = RowsChanged;
|
---|
| 254 | if (handler != null)
|
---|
| 255 | handler(this, EventArgs.Empty);
|
---|
| 256 | }
|
---|
[3320] | 257 | public event EventHandler ColumnNamesChanged;
|
---|
| 258 | protected virtual void OnColumnNamesChanged() {
|
---|
| 259 | EventHandler handler = ColumnNamesChanged;
|
---|
[5150] | 260 | if (handler != null)
|
---|
| 261 | handler(this, EventArgs.Empty);
|
---|
[3320] | 262 | }
|
---|
| 263 | public event EventHandler RowNamesChanged;
|
---|
| 264 | protected virtual void OnRowNamesChanged() {
|
---|
| 265 | EventHandler handler = RowNamesChanged;
|
---|
| 266 | if (handler != null)
|
---|
| 267 | handler(this, EventArgs.Empty);
|
---|
| 268 | }
|
---|
| 269 | public event EventHandler SortableViewChanged;
|
---|
| 270 | protected virtual void OnSortableViewChanged() {
|
---|
| 271 | EventHandler handler = SortableViewChanged;
|
---|
| 272 | if (handler != null)
|
---|
| 273 | handler(this, EventArgs.Empty);
|
---|
| 274 | }
|
---|
[2973] | 275 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[3054] | 276 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[2677] | 277 | if (ItemChanged != null)
|
---|
| 278 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[9432] | 279 |
|
---|
| 280 | //approximation to avoid firing of unnecessary ToStringChangedEvents
|
---|
| 281 | //columnIndex is not used, because always full rows are returned in the ToString method
|
---|
| 282 | if (rowIndex * Columns < maximumToStringLength)
|
---|
| 283 | OnToStringChanged();
|
---|
[2677] | 284 | }
|
---|
[2973] | 285 | public event EventHandler Reset;
|
---|
[3054] | 286 | protected virtual void OnReset() {
|
---|
[2677] | 287 | if (Reset != null)
|
---|
| 288 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 289 | OnToStringChanged();
|
---|
[2677] | 290 | }
|
---|
[5150] | 291 | #endregion
|
---|
[2677] | 292 | }
|
---|
| 293 | }
|
---|