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