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