[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2669] | 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;
|
---|
[3430] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3430] | 26 | using System.Linq;
|
---|
[2669] | 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("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[9308] | 35 | public abstract class ValueTypeArray<T> : Item, IEnumerable<T>, IStringConvertibleArray where T : struct {
|
---|
[7201] | 36 | public static new Image StaticItemImage {
|
---|
[5287] | 37 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[3306] | 38 | }
|
---|
| 39 |
|
---|
[2694] | 40 | [Storable]
|
---|
[3054] | 41 | protected T[] array;
|
---|
[2669] | 42 |
|
---|
[9308] | 43 | [Storable]
|
---|
| 44 | protected List<string> columnNames;
|
---|
| 45 | public virtual IEnumerable<string> ColumnNames {
|
---|
| 46 | get { return this.columnNames; }
|
---|
| 47 | set {
|
---|
| 48 | if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. ValueTypeArray is read-only.");
|
---|
| 49 | if (value == null || value.Count() == 0)
|
---|
| 50 | columnNames = new List<string>();
|
---|
| 51 | else if (value.Count() != Length)
|
---|
| 52 | throw new ArgumentException("A column name must be specified for each column.");
|
---|
| 53 | else
|
---|
| 54 | columnNames = new List<string>(value);
|
---|
| 55 | OnColumnNamesChanged();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[3054] | 59 | public virtual int Length {
|
---|
[2669] | 60 | get { return array.Length; }
|
---|
[8587] | 61 | #region Mono Compatibility
|
---|
| 62 | // this setter should be protected, but the Mono compiler couldn't handle it
|
---|
| 63 | set {
|
---|
[3430] | 64 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
|
---|
[2677] | 65 | if (value != Length) {
|
---|
| 66 | Array.Resize<T>(ref array, value);
|
---|
[9308] | 67 | while (columnNames.Count > value)
|
---|
| 68 | columnNames.RemoveAt(columnNames.Count - 1);
|
---|
| 69 | while (columnNames.Count < value)
|
---|
| 70 | columnNames.Add("Column " + columnNames.Count);
|
---|
| 71 | OnColumnNamesChanged();
|
---|
[2677] | 72 | OnReset();
|
---|
| 73 | }
|
---|
[2669] | 74 | }
|
---|
[8587] | 75 | #endregion
|
---|
[2669] | 76 | }
|
---|
[3054] | 77 | public virtual T this[int index] {
|
---|
[2669] | 78 | get { return array[index]; }
|
---|
| 79 | set {
|
---|
[3430] | 80 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
|
---|
[2669] | 81 | if (!value.Equals(array[index])) {
|
---|
| 82 | array[index] = value;
|
---|
[9308] | 83 | OnItemsChanged(new List<int>(1) { index });
|
---|
[2669] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[9308] | 88 | public virtual void SetValues(IEnumerable<ArrayValue<T>> arrayValues) {
|
---|
| 89 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
|
---|
| 90 | List<int> positions = new List<int>();
|
---|
| 91 | foreach (var arrayValue in arrayValues) {
|
---|
| 92 | if (!arrayValue.Value.Equals(array[arrayValue.Index])) {
|
---|
| 93 | array[arrayValue.Index] = arrayValue.Value;
|
---|
| 94 | positions.Add(arrayValue.Index);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | OnItemsChanged(positions);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[3430] | 100 | [Storable]
|
---|
| 101 | protected bool readOnly;
|
---|
| 102 | public virtual bool ReadOnly {
|
---|
| 103 | get { return readOnly; }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[9308] | 106 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 107 | private void AfterDeserialization() {
|
---|
| 108 | if (columnNames == null) {
|
---|
| 109 | columnNames = new List<string>(array.Length);
|
---|
| 110 | while (columnNames.Count < array.Length)
|
---|
| 111 | columnNames.Add("Column " + columnNames.Count);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[4722] | 115 | [StorableConstructor]
|
---|
| 116 | protected ValueTypeArray(bool deserializing) : base(deserializing) { }
|
---|
| 117 | protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
|
---|
| 118 | : base(original, cloner) {
|
---|
| 119 | this.array = (T[])original.array.Clone();
|
---|
[9308] | 120 | this.columnNames = new List<string>(original.columnNames);
|
---|
[4722] | 121 | this.readOnly = original.readOnly;
|
---|
| 122 | }
|
---|
[3054] | 123 | protected ValueTypeArray() {
|
---|
[2669] | 124 | array = new T[0];
|
---|
[9308] | 125 | columnNames = new List<string>();
|
---|
[3430] | 126 | readOnly = false;
|
---|
[2669] | 127 | }
|
---|
[3054] | 128 | protected ValueTypeArray(int length) {
|
---|
[2669] | 129 | array = new T[length];
|
---|
[9308] | 130 | columnNames = new List<string>(length);
|
---|
[3430] | 131 | readOnly = false;
|
---|
[2669] | 132 | }
|
---|
[3054] | 133 | protected ValueTypeArray(T[] elements) {
|
---|
[2669] | 134 | if (elements == null) throw new ArgumentNullException();
|
---|
| 135 | array = (T[])elements.Clone();
|
---|
[9308] | 136 | columnNames = new List<string>(elements.Length);
|
---|
[3430] | 137 | readOnly = false;
|
---|
[2669] | 138 | }
|
---|
| 139 |
|
---|
[3430] | 140 | public virtual ValueTypeArray<T> AsReadOnly() {
|
---|
| 141 | ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
|
---|
| 142 | readOnlyValueTypeArray.readOnly = true;
|
---|
| 143 | return readOnlyValueTypeArray;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[2669] | 146 | public override string ToString() {
|
---|
| 147 | StringBuilder sb = new StringBuilder();
|
---|
| 148 | sb.Append("[");
|
---|
| 149 | if (array.Length > 0) {
|
---|
| 150 | sb.Append(array[0].ToString());
|
---|
| 151 | for (int i = 1; i < array.Length; i++)
|
---|
| 152 | sb.Append(";").Append(array[i].ToString());
|
---|
| 153 | }
|
---|
| 154 | sb.Append("]");
|
---|
| 155 | return sb.ToString();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[3430] | 158 | public virtual IEnumerator<T> GetEnumerator() {
|
---|
| 159 | return array.Cast<T>().GetEnumerator();
|
---|
[2669] | 160 | }
|
---|
| 161 |
|
---|
[3430] | 162 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 163 | return GetEnumerator();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[9308] | 166 | public event EventHandler ColumnNamesChanged;
|
---|
| 167 | protected virtual void OnColumnNamesChanged() {
|
---|
| 168 | EventHandler handler = ColumnNamesChanged;
|
---|
| 169 | if (handler != null)
|
---|
| 170 | handler(this, EventArgs.Empty);
|
---|
| 171 | }
|
---|
| 172 | public event EventHandler<ArrayValuesChangedEventArgs> ItemsChanged;
|
---|
| 173 | protected virtual void OnItemsChanged(IEnumerable<int> indices) {
|
---|
| 174 | if (ItemsChanged != null)
|
---|
| 175 | ItemsChanged(this, new ArrayValuesChangedEventArgs(indices));
|
---|
[2932] | 176 | OnToStringChanged();
|
---|
[2669] | 177 | }
|
---|
[2973] | 178 | public event EventHandler Reset;
|
---|
[3054] | 179 | protected virtual void OnReset() {
|
---|
[2676] | 180 | if (Reset != null)
|
---|
| 181 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 182 | OnToStringChanged();
|
---|
[2676] | 183 | }
|
---|
[9308] | 184 |
|
---|
| 185 | protected abstract bool Validate(string value, out string errorMessage);
|
---|
| 186 | protected abstract string GetValue(int index);
|
---|
| 187 | protected abstract bool SetValue(string value, int index);
|
---|
| 188 | protected abstract bool SetValues(IEnumerable<ArrayValue<string>> arrayValues);
|
---|
| 189 |
|
---|
| 190 | #region IStringConvertibleArray Members
|
---|
| 191 | int IStringConvertibleArray.Length {
|
---|
| 192 | get { return Length; }
|
---|
| 193 | set { Length = value; }
|
---|
| 194 | }
|
---|
| 195 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
| 196 | return Validate(value, out errorMessage);
|
---|
| 197 | }
|
---|
| 198 | string IStringConvertibleArray.GetValue(int index) {
|
---|
| 199 | return GetValue(index);
|
---|
| 200 | }
|
---|
| 201 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
| 202 | return SetValue(value, index);
|
---|
| 203 | }
|
---|
| 204 | bool IStringConvertibleArray.SetValue(ArrayValue<string> arrayValue) {
|
---|
| 205 | return SetValue(arrayValue.Value, arrayValue.Index);
|
---|
| 206 | }
|
---|
| 207 | bool IStringConvertibleArray.SetValues(IEnumerable<ArrayValue<string>> arrayValues) {
|
---|
| 208 | return SetValues(arrayValues);
|
---|
| 209 | }
|
---|
| 210 | #endregion
|
---|
[2669] | 211 | }
|
---|
| 212 | }
|
---|