Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 14080

Last change on this file since 14080 was 13695, checked in by mkommend, 8 years ago

#2589: Added resizable option in ValueTypeArray and refactored the array implementations and interfaces.

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
34  [StorableClass]
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    [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
91    public virtual T this[int index] {
92      get { return array[index]; }
93      set {
94        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
95        if (!value.Equals(array[index])) {
96          array[index] = value;
97          OnItemChanged(index);
98        }
99      }
100    }
101
102    [Storable]
103    protected bool readOnly;
104    public virtual bool ReadOnly {
105      get { return readOnly; }
106    }
107
108    [StorableHook(HookType.AfterDeserialization)]
109    private void AfterDeserialization() {
110      if (elementNames == null) { elementNames = new List<string>(); }
111    }
112
113    [StorableConstructor]
114    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
115    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
116      : base(original, cloner) {
117      this.array = (T[])original.array.Clone();
118      this.readOnly = original.readOnly;
119      this.resizable = original.resizable;
120      this.elementNames = new List<string>(original.elementNames);
121    }
122    protected ValueTypeArray() {
123      array = new T[0];
124      readOnly = false;
125      resizable = true;
126      elementNames = new List<string>();
127    }
128    protected ValueTypeArray(int length) {
129      array = new T[length];
130      readOnly = false;
131      resizable = true;
132      elementNames = new List<string>();
133    }
134    protected ValueTypeArray(T[] elements) {
135      if (elements == null) throw new ArgumentNullException();
136      array = (T[])elements.Clone();
137      readOnly = false;
138      resizable = true;
139      elementNames = new List<string>();
140    }
141
142    public virtual IValueTypeArray AsReadOnly() {
143      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
144      readOnlyValueTypeArray.readOnly = true;
145      return readOnlyValueTypeArray;
146    }
147
148    public override string ToString() {
149      if (array.Length == 0) return "[]";
150
151      StringBuilder sb = new StringBuilder();
152      sb.Append("[");
153      sb.Append(array[0].ToString());
154      for (int i = 1; i < array.Length; i++) {
155        sb.Append(";").Append(array[i].ToString());
156        if (sb.Length > maximumToStringLength) {
157          sb.Append("...");
158          break;
159        }
160      }
161      sb.Append("]");
162      return sb.ToString();
163    }
164
165    public virtual IEnumerator<T> GetEnumerator() {
166      return array.Cast<T>().GetEnumerator();
167    }
168
169    IEnumerator IEnumerable.GetEnumerator() {
170      return GetEnumerator();
171    }
172
173    public event EventHandler ResizableChanged;
174    protected virtual void OnResizableChanged() {
175      EventHandler handler = ResizableChanged;
176      if (handler != null)
177        handler(this, EventArgs.Empty);
178    }
179    public event EventHandler ElementNamesChanged;
180    protected virtual void OnElementNamesChanged() {
181      EventHandler handler = ElementNamesChanged;
182      if (handler != null)
183        handler(this, EventArgs.Empty);
184    }
185
186    public event EventHandler<EventArgs<int>> ItemChanged;
187    protected virtual void OnItemChanged(int index) {
188      if (ItemChanged != null)
189        ItemChanged(this, new EventArgs<int>(index));
190      if (index < maximumToStringLength)
191        OnToStringChanged();
192    }
193    public event EventHandler Reset;
194    protected virtual void OnReset() {
195      if (Reset != null)
196        Reset(this, EventArgs.Empty);
197      OnToStringChanged();
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.