Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9480 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.4 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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
[3054]45    public virtual int Length {
[2669]46      get { return array.Length; }
[8587]47      #region Mono Compatibility
48      // this setter should be protected, but the Mono compiler couldn't handle it
49      set {
[3430]50        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
[2677]51        if (value != Length) {
52          Array.Resize<T>(ref array, value);
53          OnReset();
54        }
[2669]55      }
[8587]56      #endregion
[2669]57    }
[3054]58    public virtual T this[int index] {
[2669]59      get { return array[index]; }
60      set {
[3430]61        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
[2669]62        if (!value.Equals(array[index])) {
63          array[index] = value;
64          OnItemChanged(index);
65        }
66      }
67    }
68
[3430]69    [Storable]
70    protected bool readOnly;
71    public virtual bool ReadOnly {
72      get { return readOnly; }
73    }
74
[4722]75    [StorableConstructor]
76    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
77    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
78      : base(original, cloner) {
79      this.array = (T[])original.array.Clone();
80      this.readOnly = original.readOnly;
81    }
[3054]82    protected ValueTypeArray() {
[2669]83      array = new T[0];
[3430]84      readOnly = false;
[2669]85    }
[3054]86    protected ValueTypeArray(int length) {
[2669]87      array = new T[length];
[3430]88      readOnly = false;
[2669]89    }
[3054]90    protected ValueTypeArray(T[] elements) {
[2669]91      if (elements == null) throw new ArgumentNullException();
92      array = (T[])elements.Clone();
[3430]93      readOnly = false;
[2669]94    }
95
[3430]96    public virtual ValueTypeArray<T> AsReadOnly() {
97      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
98      readOnlyValueTypeArray.readOnly = true;
99      return readOnlyValueTypeArray;
100    }
101
[2669]102    public override string ToString() {
[9432]103      if (array.Length == 0) return "[]";
104
[2669]105      StringBuilder sb = new StringBuilder();
106      sb.Append("[");
[9432]107      sb.Append(array[0].ToString());
108      for (int i = 1; i < array.Length; i++) {
109        sb.Append(";").Append(array[i].ToString());
110        if (sb.Length > maximumToStringLength) {
111          sb.Append("...");
112          break;
113        }
[2669]114      }
115      sb.Append("]");
116      return sb.ToString();
117    }
118
[3430]119    public virtual IEnumerator<T> GetEnumerator() {
120      return array.Cast<T>().GetEnumerator();
[2669]121    }
122
[3430]123    IEnumerator IEnumerable.GetEnumerator() {
124      return GetEnumerator();
125    }
126
[2973]127    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]128    protected virtual void OnItemChanged(int index) {
[2669]129      if (ItemChanged != null)
[2973]130        ItemChanged(this, new EventArgs<int>(index));
[9432]131      if (index < maximumToStringLength)
132        OnToStringChanged();
[2669]133    }
[2973]134    public event EventHandler Reset;
[3054]135    protected virtual void OnReset() {
[2676]136      if (Reset != null)
137        Reset(this, EventArgs.Empty);
[2932]138      OnToStringChanged();
[2676]139    }
[2669]140  }
141}
Note: See TracBrowser for help on using the repository browser.