Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 12009

Last change on this file since 12009 was 12009, checked in by ascheibe, 9 years ago

#2212 updated copyright year

File size: 5.8 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections;
[3430]24using System.Collections.Generic;
[3306]25using System.Drawing;
[3430]26using System.Linq;
[2669]27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
[3822]33  [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
[3017]34  [StorableClass]
[3430]35  public abstract class ValueTypeArray<T> : Item, IEnumerable<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
[9737]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
[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);
[9737]69          while (elementNames.Count > value)
70            elementNames.RemoveAt(elementNames.Count - 1);
71          OnElementNamesChanged();
[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;
83          OnItemChanged(index);
84        }
85      }
86    }
87
[3430]88    [Storable]
89    protected bool readOnly;
90    public virtual bool ReadOnly {
91      get { return readOnly; }
92    }
93
[9737]94    [StorableHook(HookType.AfterDeserialization)]
95    private void AfterDeserialization() {
96      if (elementNames == null) { elementNames = new List<string>(); }
97    }
98
[4722]99    [StorableConstructor]
100    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
101    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
102      : base(original, cloner) {
103      this.array = (T[])original.array.Clone();
104      this.readOnly = original.readOnly;
[9737]105      this.elementNames = new List<string>(original.elementNames);
[4722]106    }
[3054]107    protected ValueTypeArray() {
[2669]108      array = new T[0];
[3430]109      readOnly = false;
[9737]110      elementNames = new List<string>();
[2669]111    }
[3054]112    protected ValueTypeArray(int length) {
[2669]113      array = new T[length];
[3430]114      readOnly = false;
[9737]115      elementNames = new List<string>();
[2669]116    }
[3054]117    protected ValueTypeArray(T[] elements) {
[2669]118      if (elements == null) throw new ArgumentNullException();
119      array = (T[])elements.Clone();
[3430]120      readOnly = false;
[9737]121      elementNames = new List<string>();
[2669]122    }
123
[3430]124    public virtual ValueTypeArray<T> AsReadOnly() {
125      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
126      readOnlyValueTypeArray.readOnly = true;
127      return readOnlyValueTypeArray;
128    }
129
[2669]130    public override string ToString() {
[9432]131      if (array.Length == 0) return "[]";
132
[2669]133      StringBuilder sb = new StringBuilder();
134      sb.Append("[");
[9432]135      sb.Append(array[0].ToString());
136      for (int i = 1; i < array.Length; i++) {
137        sb.Append(";").Append(array[i].ToString());
138        if (sb.Length > maximumToStringLength) {
139          sb.Append("...");
140          break;
141        }
[2669]142      }
143      sb.Append("]");
144      return sb.ToString();
145    }
146
[3430]147    public virtual IEnumerator<T> GetEnumerator() {
148      return array.Cast<T>().GetEnumerator();
[2669]149    }
150
[3430]151    IEnumerator IEnumerable.GetEnumerator() {
152      return GetEnumerator();
153    }
154
[9737]155    public event EventHandler ElementNamesChanged;
156    protected virtual void OnElementNamesChanged() {
157      EventHandler handler = ElementNamesChanged;
158      if (handler != null)
159        handler(this, EventArgs.Empty);
160    }
161
[2973]162    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]163    protected virtual void OnItemChanged(int index) {
[2669]164      if (ItemChanged != null)
[2973]165        ItemChanged(this, new EventArgs<int>(index));
[9432]166      if (index < maximumToStringLength)
167        OnToStringChanged();
[2669]168    }
[2973]169    public event EventHandler Reset;
[3054]170    protected virtual void OnReset() {
[2676]171      if (Reset != null)
172        Reset(this, EventArgs.Empty);
[2932]173      OnToStringChanged();
[2676]174    }
[2669]175  }
176}
Note: See TracBrowser for help on using the repository browser.