Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 9401

Last change on this file since 9401 was 9401, checked in by sforsten, 11 years ago

#2018: renamed "column" to "element" in array types

File size: 7.5 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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]
[9308]35  public abstract class ValueTypeArray<T> : Item, IEnumerable<T>, IStringConvertibleArray where T : struct {
[7201]36    public static new Image StaticItemImage {
[5287]37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
[3306]38    }
39
[2694]40    [Storable]
[3054]41    protected T[] array;
[2669]42
[9308]43    [Storable]
[9401]44    protected List<string> elementNames;
45    public virtual IEnumerable<string> ElementNames {
46      get { return this.elementNames; }
[9308]47      set {
[9401]48        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
[9308]49        if (value == null || value.Count() == 0)
[9401]50          elementNames = new List<string>();
[9308]51        else if (value.Count() != Length)
[9401]52          throw new ArgumentException("An element name must be specified for each element.");
[9308]53        else
[9401]54          elementNames = new List<string>(value);
55        OnElementNamesChanged();
[9308]56      }
57    }
58
[3054]59    public virtual int Length {
[2669]60      get { return array.Length; }
[8587]61      #region Mono Compatibility
62      // this setter should be protected, but the Mono compiler couldn't handle it
63      set {
[3430]64        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
[2677]65        if (value != Length) {
66          Array.Resize<T>(ref array, value);
[9401]67          while (elementNames.Count > value)
68            elementNames.RemoveAt(elementNames.Count - 1);
69          while (elementNames.Count < value)
70            elementNames.Add("Element " + elementNames.Count);
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;
[9308]83          OnItemsChanged(new List<int>(1) { index });
[2669]84        }
85      }
86    }
87
[9308]88    public virtual void SetValues(IEnumerable<ArrayValue<T>> arrayValues) {
89      if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeMatrix is read-only.");
90      List<int> positions = new List<int>();
91      foreach (var arrayValue in arrayValues) {
92        if (!arrayValue.Value.Equals(array[arrayValue.Index])) {
93          array[arrayValue.Index] = arrayValue.Value;
94          positions.Add(arrayValue.Index);
95        }
96      }
97      OnItemsChanged(positions);
98    }
99
[3430]100    [Storable]
101    protected bool readOnly;
102    public virtual bool ReadOnly {
103      get { return readOnly; }
104    }
105
[9308]106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
[9401]108      if (elementNames == null) {
109        elementNames = new List<string>(array.Length);
110        while (elementNames.Count < array.Length)
111          elementNames.Add("Element " + elementNames.Count);
[9308]112      }
113    }
114
[4722]115    [StorableConstructor]
116    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
117    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
118      : base(original, cloner) {
119      this.array = (T[])original.array.Clone();
[9401]120      this.elementNames = new List<string>(original.elementNames);
[4722]121      this.readOnly = original.readOnly;
122    }
[3054]123    protected ValueTypeArray() {
[2669]124      array = new T[0];
[9401]125      elementNames = new List<string>();
[3430]126      readOnly = false;
[2669]127    }
[3054]128    protected ValueTypeArray(int length) {
[2669]129      array = new T[length];
[9401]130      elementNames = new List<string>(length);
[3430]131      readOnly = false;
[2669]132    }
[3054]133    protected ValueTypeArray(T[] elements) {
[2669]134      if (elements == null) throw new ArgumentNullException();
135      array = (T[])elements.Clone();
[9401]136      elementNames = new List<string>(elements.Length);
[3430]137      readOnly = false;
[2669]138    }
139
[3430]140    public virtual ValueTypeArray<T> AsReadOnly() {
141      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
142      readOnlyValueTypeArray.readOnly = true;
143      return readOnlyValueTypeArray;
144    }
145
[2669]146    public override string ToString() {
147      StringBuilder sb = new StringBuilder();
148      sb.Append("[");
149      if (array.Length > 0) {
150        sb.Append(array[0].ToString());
151        for (int i = 1; i < array.Length; i++)
152          sb.Append(";").Append(array[i].ToString());
153      }
154      sb.Append("]");
155      return sb.ToString();
156    }
157
[3430]158    public virtual IEnumerator<T> GetEnumerator() {
159      return array.Cast<T>().GetEnumerator();
[2669]160    }
161
[3430]162    IEnumerator IEnumerable.GetEnumerator() {
163      return GetEnumerator();
164    }
165
[9401]166    public event EventHandler ElementNamesChanged;
167    protected virtual void OnElementNamesChanged() {
168      EventHandler handler = ElementNamesChanged;
[9308]169      if (handler != null)
170        handler(this, EventArgs.Empty);
171    }
172    public event EventHandler<ArrayValuesChangedEventArgs> ItemsChanged;
173    protected virtual void OnItemsChanged(IEnumerable<int> indices) {
174      if (ItemsChanged != null)
175        ItemsChanged(this, new ArrayValuesChangedEventArgs(indices));
[2932]176      OnToStringChanged();
[2669]177    }
[2973]178    public event EventHandler Reset;
[3054]179    protected virtual void OnReset() {
[2676]180      if (Reset != null)
181        Reset(this, EventArgs.Empty);
[2932]182      OnToStringChanged();
[2676]183    }
[9308]184
185    protected abstract bool Validate(string value, out string errorMessage);
186    protected abstract string GetValue(int index);
187    protected abstract bool SetValue(string value, int index);
188    protected abstract bool SetValues(IEnumerable<ArrayValue<string>> arrayValues);
189
190    #region IStringConvertibleArray Members
191    int IStringConvertibleArray.Length {
192      get { return Length; }
193      set { Length = value; }
194    }
195    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
196      return Validate(value, out errorMessage);
197    }
198    string IStringConvertibleArray.GetValue(int index) {
199      return GetValue(index);
200    }
201    bool IStringConvertibleArray.SetValue(string value, int index) {
202      return SetValue(value, index);
203    }
204    bool IStringConvertibleArray.SetValue(ArrayValue<string> arrayValue) {
205      return SetValue(arrayValue.Value, arrayValue.Index);
206    }
207    bool IStringConvertibleArray.SetValues(IEnumerable<ArrayValue<string>> arrayValues) {
208      return SetValues(arrayValues);
209    }
210    #endregion
[2669]211  }
212}
Note: See TracBrowser for help on using the repository browser.