Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8622 was 8587, checked in by ascheibe, 12 years ago

#1937

  • fixed wrong assembly references in project files
  • fixed cases of file names
  • removed an InvokeRequired check that Mono couldn't handle
  • changed visibility of inherited setters so that the Mono compiler doesn't crash
  • specified the namespace of the Random class so that Mono doesn't confuses it with our Random class
File size: 4.2 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]
[3430]35  public abstract class ValueTypeArray<T> : Item, IEnumerable<T> 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
[3054]43    public virtual int Length {
[2669]44      get { return array.Length; }
[8587]45      #region Mono Compatibility
46      // this setter should be protected, but the Mono compiler couldn't handle it
47      set {
[3430]48        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
[2677]49        if (value != Length) {
50          Array.Resize<T>(ref array, value);
51          OnReset();
52        }
[2669]53      }
[8587]54      #endregion
[2669]55    }
[3054]56    public virtual T this[int index] {
[2669]57      get { return array[index]; }
58      set {
[3430]59        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
[2669]60        if (!value.Equals(array[index])) {
61          array[index] = value;
62          OnItemChanged(index);
63        }
64      }
65    }
66
[3430]67    [Storable]
68    protected bool readOnly;
69    public virtual bool ReadOnly {
70      get { return readOnly; }
71    }
72
[4722]73    [StorableConstructor]
74    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
75    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
76      : base(original, cloner) {
77      this.array = (T[])original.array.Clone();
78      this.readOnly = original.readOnly;
79    }
[3054]80    protected ValueTypeArray() {
[2669]81      array = new T[0];
[3430]82      readOnly = false;
[2669]83    }
[3054]84    protected ValueTypeArray(int length) {
[2669]85      array = new T[length];
[3430]86      readOnly = false;
[2669]87    }
[3054]88    protected ValueTypeArray(T[] elements) {
[2669]89      if (elements == null) throw new ArgumentNullException();
90      array = (T[])elements.Clone();
[3430]91      readOnly = false;
[2669]92    }
93
[3430]94    public virtual ValueTypeArray<T> AsReadOnly() {
95      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
96      readOnlyValueTypeArray.readOnly = true;
97      return readOnlyValueTypeArray;
98    }
99
[2669]100    public override string ToString() {
101      StringBuilder sb = new StringBuilder();
102      sb.Append("[");
103      if (array.Length > 0) {
104        sb.Append(array[0].ToString());
105        for (int i = 1; i < array.Length; i++)
106          sb.Append(";").Append(array[i].ToString());
107      }
108      sb.Append("]");
109      return sb.ToString();
110    }
111
[3430]112    public virtual IEnumerator<T> GetEnumerator() {
113      return array.Cast<T>().GetEnumerator();
[2669]114    }
115
[3430]116    IEnumerator IEnumerable.GetEnumerator() {
117      return GetEnumerator();
118    }
119
[2973]120    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]121    protected virtual void OnItemChanged(int index) {
[2669]122      if (ItemChanged != null)
[2973]123        ItemChanged(this, new EventArgs<int>(index));
[2932]124      OnToStringChanged();
[2669]125    }
[2973]126    public event EventHandler Reset;
[3054]127    protected virtual void OnReset() {
[2676]128      if (Reset != null)
129        Reset(this, EventArgs.Empty);
[2932]130      OnToStringChanged();
[2676]131    }
[2669]132  }
133}
Note: See TracBrowser for help on using the repository browser.