Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4203 was 3822, checked in by swagner, 14 years ago

Implemented reviewers' comments (#863)

  • adapted item names of generic items to reflect the type of their generic parameters
File size: 4.0 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 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 {
[3306]36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
[2694]40    [Storable]
[3054]41    protected T[] array;
[2669]42
[3054]43    public virtual int Length {
[2669]44      get { return array.Length; }
45      protected set {
[3430]46        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
[2677]47        if (value != Length) {
48          Array.Resize<T>(ref array, value);
49          OnReset();
50        }
[2669]51      }
52    }
[3054]53    public virtual T this[int index] {
[2669]54      get { return array[index]; }
55      set {
[3430]56        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
[2669]57        if (!value.Equals(array[index])) {
58          array[index] = value;
59          OnItemChanged(index);
60        }
61      }
62    }
63
[3430]64    [Storable]
65    protected bool readOnly;
66    public virtual bool ReadOnly {
67      get { return readOnly; }
68    }
69
[3054]70    protected ValueTypeArray() {
[2669]71      array = new T[0];
[3430]72      readOnly = false;
[2669]73    }
[3054]74    protected ValueTypeArray(int length) {
[2669]75      array = new T[length];
[3430]76      readOnly = false;
[2669]77    }
[3054]78    protected ValueTypeArray(T[] elements) {
[2669]79      if (elements == null) throw new ArgumentNullException();
80      array = (T[])elements.Clone();
[3430]81      readOnly = false;
[2669]82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
[3048]85      ValueTypeArray<T> clone = (ValueTypeArray<T>)base.Clone(cloner);
[2669]86      clone.array = (T[])array.Clone();
[3430]87      clone.readOnly = readOnly;
[2669]88      return clone;
89    }
90
[3430]91    public virtual ValueTypeArray<T> AsReadOnly() {
92      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
93      readOnlyValueTypeArray.readOnly = true;
94      return readOnlyValueTypeArray;
95    }
96
[2669]97    public override string ToString() {
98      StringBuilder sb = new StringBuilder();
99      sb.Append("[");
100      if (array.Length > 0) {
101        sb.Append(array[0].ToString());
102        for (int i = 1; i < array.Length; i++)
103          sb.Append(";").Append(array[i].ToString());
104      }
105      sb.Append("]");
106      return sb.ToString();
107    }
108
[3430]109    public virtual IEnumerator<T> GetEnumerator() {
110      return array.Cast<T>().GetEnumerator();
[2669]111    }
112
[3430]113    IEnumerator IEnumerable.GetEnumerator() {
114      return GetEnumerator();
115    }
116
[2973]117    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]118    protected virtual void OnItemChanged(int index) {
[2669]119      if (ItemChanged != null)
[2973]120        ItemChanged(this, new EventArgs<int>(index));
[2932]121      OnToStringChanged();
[2669]122    }
[2973]123    public event EventHandler Reset;
[3054]124    protected virtual void OnReset() {
[2676]125      if (Reset != null)
126        Reset(this, EventArgs.Empty);
[2932]127      OnToStringChanged();
[2676]128    }
[2669]129  }
130}
Note: See TracBrowser for help on using the repository browser.