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