Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9274 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
34  [StorableClass]
35  public abstract class ValueTypeArray<T> : Item, IEnumerable<T> where T : struct {
36    public static new Image StaticItemImage {
37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
38    }
39
40    [Storable]
41    protected T[] array;
42
43    public virtual int Length {
44      get { return array.Length; }
45      #region Mono Compatibility
46      // this setter should be protected, but the Mono compiler couldn't handle it
47      set {
48        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
49        if (value != Length) {
50          Array.Resize<T>(ref array, value);
51          OnReset();
52        }
53      }
54      #endregion
55    }
56    public virtual T this[int index] {
57      get { return array[index]; }
58      set {
59        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
60        if (!value.Equals(array[index])) {
61          array[index] = value;
62          OnItemChanged(index);
63        }
64      }
65    }
66
67    [Storable]
68    protected bool readOnly;
69    public virtual bool ReadOnly {
70      get { return readOnly; }
71    }
72
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    }
80    protected ValueTypeArray() {
81      array = new T[0];
82      readOnly = false;
83    }
84    protected ValueTypeArray(int length) {
85      array = new T[length];
86      readOnly = false;
87    }
88    protected ValueTypeArray(T[] elements) {
89      if (elements == null) throw new ArgumentNullException();
90      array = (T[])elements.Clone();
91      readOnly = false;
92    }
93
94    public virtual ValueTypeArray<T> AsReadOnly() {
95      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
96      readOnlyValueTypeArray.readOnly = true;
97      return readOnlyValueTypeArray;
98    }
99
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
112    public virtual IEnumerator<T> GetEnumerator() {
113      return array.Cast<T>().GetEnumerator();
114    }
115
116    IEnumerator IEnumerable.GetEnumerator() {
117      return GetEnumerator();
118    }
119
120    public event EventHandler<EventArgs<int>> ItemChanged;
121    protected virtual void OnItemChanged(int index) {
122      if (ItemChanged != null)
123        ItemChanged(this, new EventArgs<int>(index));
124      OnToStringChanged();
125    }
126    public event EventHandler Reset;
127    protected virtual void OnReset() {
128      if (Reset != null)
129        Reset(this, EventArgs.Empty);
130      OnToStringChanged();
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.