[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
[3430] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3430] | 26 | using System.Linq;
|
---|
[2669] | 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace 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 {
|
---|
[7201] | 36 | public static new Image StaticItemImage {
|
---|
[5287] | 37 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[3306] | 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 |
|
---|
[4722] | 70 | [StorableConstructor]
|
---|
| 71 | protected ValueTypeArray(bool deserializing) : base(deserializing) { }
|
---|
| 72 | protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
|
---|
| 73 | : base(original, cloner) {
|
---|
| 74 | this.array = (T[])original.array.Clone();
|
---|
| 75 | this.readOnly = original.readOnly;
|
---|
| 76 | }
|
---|
[3054] | 77 | protected ValueTypeArray() {
|
---|
[2669] | 78 | array = new T[0];
|
---|
[3430] | 79 | readOnly = false;
|
---|
[2669] | 80 | }
|
---|
[3054] | 81 | protected ValueTypeArray(int length) {
|
---|
[2669] | 82 | array = new T[length];
|
---|
[3430] | 83 | readOnly = false;
|
---|
[2669] | 84 | }
|
---|
[3054] | 85 | protected ValueTypeArray(T[] elements) {
|
---|
[2669] | 86 | if (elements == null) throw new ArgumentNullException();
|
---|
| 87 | array = (T[])elements.Clone();
|
---|
[3430] | 88 | readOnly = false;
|
---|
[2669] | 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 | }
|
---|