1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Data {
|
---|
31 | [Item("ValueTypeArray<T>", "An abstract base class for representing arrays of value types.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class ValueTypeArray<T> : Item, IEnumerable where T : struct {
|
---|
34 | public override Image ItemImage {
|
---|
35 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | protected T[] array;
|
---|
40 |
|
---|
41 | public virtual int Length {
|
---|
42 | get { return array.Length; }
|
---|
43 | protected set {
|
---|
44 | if (value != Length) {
|
---|
45 | Array.Resize<T>(ref array, value);
|
---|
46 | OnReset();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | public virtual T this[int index] {
|
---|
51 | get { return array[index]; }
|
---|
52 | set {
|
---|
53 | if (!value.Equals(array[index])) {
|
---|
54 | array[index] = value;
|
---|
55 | OnItemChanged(index);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected ValueTypeArray() {
|
---|
61 | array = new T[0];
|
---|
62 | }
|
---|
63 | protected ValueTypeArray(int length) {
|
---|
64 | array = new T[length];
|
---|
65 | }
|
---|
66 | protected ValueTypeArray(T[] elements) {
|
---|
67 | if (elements == null) throw new ArgumentNullException();
|
---|
68 | array = (T[])elements.Clone();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | ValueTypeArray<T> clone = (ValueTypeArray<T>)base.Clone(cloner);
|
---|
73 | clone.array = (T[])array.Clone();
|
---|
74 | return clone;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override string ToString() {
|
---|
78 | StringBuilder sb = new StringBuilder();
|
---|
79 | sb.Append("[");
|
---|
80 | if (array.Length > 0) {
|
---|
81 | sb.Append(array[0].ToString());
|
---|
82 | for (int i = 1; i < array.Length; i++)
|
---|
83 | sb.Append(";").Append(array[i].ToString());
|
---|
84 | }
|
---|
85 | sb.Append("]");
|
---|
86 | return sb.ToString();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public virtual IEnumerator GetEnumerator() {
|
---|
90 | return array.GetEnumerator();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
94 | protected virtual void OnItemChanged(int index) {
|
---|
95 | if (ItemChanged != null)
|
---|
96 | ItemChanged(this, new EventArgs<int>(index));
|
---|
97 | OnToStringChanged();
|
---|
98 | }
|
---|
99 | public event EventHandler Reset;
|
---|
100 | protected virtual void OnReset() {
|
---|
101 | if (Reset != null)
|
---|
102 | Reset(this, EventArgs.Empty);
|
---|
103 | OnToStringChanged();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|