Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeArrayData.cs @ 2931

Last change on this file since 2931 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 3.2 KB
Line 
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Data {
32  [Item("ValueTypeArrayData<T>", "A base class for representing arrays of value types.")]
33  public class ValueTypeArrayData<T> : Item, IEnumerable where T : struct {
34    [Storable]
35    private T[] array;
36
37    public int Length {
38      get { return array.Length; }
39      protected set {
40        if (value != Length) {
41          Array.Resize<T>(ref array, value);
42          OnReset();
43        }
44      }
45    }
46    public T this[int index] {
47      get { return array[index]; }
48      set {
49        if (!value.Equals(array[index])) {
50          array[index] = value;
51          OnItemChanged(index);
52        }
53      }
54    }
55
56    public ValueTypeArrayData() {
57      array = new T[0];
58    }
59    public ValueTypeArrayData(int length) {
60      array = new T[length];
61    }
62    public ValueTypeArrayData(T[] elements) {
63      if (elements == null) throw new ArgumentNullException();
64      array = (T[])elements.Clone();
65    }
66    protected ValueTypeArrayData(ValueTypeArrayData<T> elements) {
67      if (elements == null) throw new ArgumentNullException();
68      array = (T[])elements.array.Clone();
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      ValueTypeArrayData<T> clone = (ValueTypeArrayData<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 IEnumerator GetEnumerator() {
90      return array.GetEnumerator();
91    }
92
93    protected event EventHandler<EventArgs<int, int>> ItemChanged;
94    private void OnItemChanged(int index) {
95      if (ItemChanged != null)
96        ItemChanged(this, new EventArgs<int, int>(index, 0));
97      OnChanged();
98    }
99    protected event EventHandler Reset;
100    private void OnReset() {
101      if (Reset != null)
102        Reset(this, EventArgs.Empty);
103      OnChanged();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.