[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[14650] | 30 | using HeuristicLab.Data.PersistentDataStructures.Adaptations;
|
---|
[2669] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Data {
|
---|
[3822] | 34 | [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
|
---|
[3017] | 35 | [StorableClass]
|
---|
[13979] | 36 | public abstract class ValueTypeArray<T> : Item, IValueTypeArray<T> where T : struct {
|
---|
[9432] | 37 | private const int maximumToStringLength = 100;
|
---|
| 38 |
|
---|
[7201] | 39 | public static new Image StaticItemImage {
|
---|
[5287] | 40 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[3306] | 41 | }
|
---|
| 42 |
|
---|
[14650] | 43 | [Storable(AllowOneWay = true, Name = "array")]
|
---|
| 44 | protected T[] oldArrayValuePersistence { set { historyArray = new HistoryArray<T>(value); } }
|
---|
| 45 |
|
---|
[2694] | 46 | [Storable]
|
---|
[14650] | 47 | protected HistoryArray<T> historyArray;
|
---|
[2669] | 48 |
|
---|
[9737] | 49 | [Storable]
|
---|
| 50 | protected List<string> elementNames;
|
---|
| 51 | public virtual IEnumerable<string> ElementNames {
|
---|
| 52 | get { return this.elementNames; }
|
---|
| 53 | set {
|
---|
| 54 | if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
|
---|
| 55 | if (value == null || !value.Any())
|
---|
| 56 | elementNames = new List<string>();
|
---|
| 57 | else if (value.Count() > Length)
|
---|
| 58 | throw new ArgumentException("The number of element names must not exceed the array length.");
|
---|
| 59 | else
|
---|
| 60 | elementNames = new List<string>(value);
|
---|
| 61 | OnElementNamesChanged();
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[3054] | 65 | public virtual int Length {
|
---|
[14650] | 66 | get { return historyArray.Length; }
|
---|
[8587] | 67 | #region Mono Compatibility
|
---|
| 68 | // this setter should be protected, but the Mono compiler couldn't handle it
|
---|
| 69 | set {
|
---|
[3430] | 70 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
|
---|
[2677] | 71 | if (value != Length) {
|
---|
[14650] | 72 | historyArray.Resize(value);
|
---|
[9737] | 73 | while (elementNames.Count > value)
|
---|
| 74 | elementNames.RemoveAt(elementNames.Count - 1);
|
---|
| 75 | OnElementNamesChanged();
|
---|
[2677] | 76 | OnReset();
|
---|
| 77 | }
|
---|
[2669] | 78 | }
|
---|
[8587] | 79 | #endregion
|
---|
[2669] | 80 | }
|
---|
[13979] | 81 |
|
---|
| 82 | [Storable]
|
---|
| 83 | protected bool resizable = true;
|
---|
| 84 | public bool Resizable {
|
---|
| 85 | get { return resizable; }
|
---|
| 86 | set {
|
---|
| 87 | if (resizable != value) {
|
---|
| 88 | resizable = value;
|
---|
| 89 | OnResizableChanged();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
[3054] | 95 | public virtual T this[int index] {
|
---|
[14650] | 96 | get { return historyArray[index]; }
|
---|
[2669] | 97 | set {
|
---|
[3430] | 98 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
|
---|
[14650] | 99 | if (!value.Equals(historyArray[index])) {
|
---|
| 100 | historyArray[index] = value;
|
---|
[2669] | 101 | OnItemChanged(index);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[3430] | 106 | [Storable]
|
---|
| 107 | protected bool readOnly;
|
---|
| 108 | public virtual bool ReadOnly {
|
---|
| 109 | get { return readOnly; }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[9737] | 112 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 113 | private void AfterDeserialization() {
|
---|
| 114 | if (elementNames == null) { elementNames = new List<string>(); }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[4722] | 117 | [StorableConstructor]
|
---|
| 118 | protected ValueTypeArray(bool deserializing) : base(deserializing) { }
|
---|
| 119 | protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
|
---|
| 120 | : base(original, cloner) {
|
---|
[14650] | 121 | this.historyArray = (HistoryArray<T>)original.historyArray.Clone();
|
---|
[4722] | 122 | this.readOnly = original.readOnly;
|
---|
[13979] | 123 | this.resizable = original.resizable;
|
---|
[9737] | 124 | this.elementNames = new List<string>(original.elementNames);
|
---|
[4722] | 125 | }
|
---|
[3054] | 126 | protected ValueTypeArray() {
|
---|
[14650] | 127 | historyArray = new HistoryArray<T>(0);
|
---|
[3430] | 128 | readOnly = false;
|
---|
[13979] | 129 | resizable = true;
|
---|
[9737] | 130 | elementNames = new List<string>();
|
---|
[2669] | 131 | }
|
---|
[3054] | 132 | protected ValueTypeArray(int length) {
|
---|
[14650] | 133 | historyArray = new HistoryArray<T>(length);
|
---|
[3430] | 134 | readOnly = false;
|
---|
[13979] | 135 | resizable = true;
|
---|
[9737] | 136 | elementNames = new List<string>();
|
---|
[2669] | 137 | }
|
---|
[3054] | 138 | protected ValueTypeArray(T[] elements) {
|
---|
[2669] | 139 | if (elements == null) throw new ArgumentNullException();
|
---|
[14650] | 140 | historyArray = new HistoryArray<T>(elements);
|
---|
[3430] | 141 | readOnly = false;
|
---|
[13979] | 142 | resizable = true;
|
---|
[9737] | 143 | elementNames = new List<string>();
|
---|
[2669] | 144 | }
|
---|
| 145 |
|
---|
[13979] | 146 | public virtual IValueTypeArray AsReadOnly() {
|
---|
[3430] | 147 | ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
|
---|
| 148 | readOnlyValueTypeArray.readOnly = true;
|
---|
| 149 | return readOnlyValueTypeArray;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[14113] | 152 | public T[] CloneAsArray() {
|
---|
[14650] | 153 | return historyArray.ToArray();
|
---|
[14113] | 154 | }
|
---|
| 155 |
|
---|
[2669] | 156 | public override string ToString() {
|
---|
[14650] | 157 | if (historyArray.Length == 0) return "[]";
|
---|
[9432] | 158 |
|
---|
[2669] | 159 | StringBuilder sb = new StringBuilder();
|
---|
| 160 | sb.Append("[");
|
---|
[14650] | 161 | sb.Append(historyArray[0].ToString());
|
---|
| 162 | for (int i = 1; i < historyArray.Length; i++) {
|
---|
| 163 | sb.Append(";").Append(historyArray[i].ToString());
|
---|
[9432] | 164 | if (sb.Length > maximumToStringLength) {
|
---|
| 165 | sb.Append("...");
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
[2669] | 168 | }
|
---|
| 169 | sb.Append("]");
|
---|
| 170 | return sb.ToString();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[3430] | 173 | public virtual IEnumerator<T> GetEnumerator() {
|
---|
[14650] | 174 | return historyArray.Cast<T>().GetEnumerator();
|
---|
[2669] | 175 | }
|
---|
| 176 |
|
---|
[3430] | 177 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 178 | return GetEnumerator();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[13979] | 181 | public event EventHandler ResizableChanged;
|
---|
| 182 | protected virtual void OnResizableChanged() {
|
---|
| 183 | EventHandler handler = ResizableChanged;
|
---|
| 184 | if (handler != null)
|
---|
| 185 | handler(this, EventArgs.Empty);
|
---|
| 186 | }
|
---|
[9737] | 187 | public event EventHandler ElementNamesChanged;
|
---|
| 188 | protected virtual void OnElementNamesChanged() {
|
---|
| 189 | EventHandler handler = ElementNamesChanged;
|
---|
| 190 | if (handler != null)
|
---|
| 191 | handler(this, EventArgs.Empty);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[2973] | 194 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
[3054] | 195 | protected virtual void OnItemChanged(int index) {
|
---|
[2669] | 196 | if (ItemChanged != null)
|
---|
[2973] | 197 | ItemChanged(this, new EventArgs<int>(index));
|
---|
[9432] | 198 | if (index < maximumToStringLength)
|
---|
| 199 | OnToStringChanged();
|
---|
[2669] | 200 | }
|
---|
[2973] | 201 | public event EventHandler Reset;
|
---|
[3054] | 202 | protected virtual void OnReset() {
|
---|
[2676] | 203 | if (Reset != null)
|
---|
| 204 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 205 | OnToStringChanged();
|
---|
[2676] | 206 | }
|
---|
[2669] | 207 | }
|
---|
| 208 | }
|
---|