Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ObservableArray.cs @ 2830

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

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 10.9 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.ComponentModel;
26using System.Linq;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Collections {
30  [Serializable]
31  public class ObservableArray<T> : IObservableArray<T> {
32    [Storable]
33    private T[] array;
34
35    #region Properties
36    public int Length {
37      get { return array.Length; }
38    }
39    int ICollection<T>.Count {
40      get { return array.Length; }
41    }
42    bool ICollection<T>.IsReadOnly {
43      get { return array.IsReadOnly; }
44    }
45
46    public T this[int index] {
47      get {
48        return array[index];
49      }
50      set {
51        T item = array[index];
52        if (!((item == null) && (value == null)) && ((item == null) || (!item.Equals(value)))) {
53          array[index] = value;
54          OnItemsReplaced(new IndexedItem<T>[] { new IndexedItem<T>(index, value) }, new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
55          OnPropertyChanged("Item[]");
56        }
57      }
58    }
59    #endregion
60
61    #region Constructors
62    public ObservableArray() {
63      array = new T[0];
64    }
65    public ObservableArray(int length) {
66      array = new T[length];
67    }
68    public ObservableArray(T[] array) {
69      this.array = (T[])array.Clone();
70    }
71    public ObservableArray(IEnumerable<T> collection) {
72      array = collection.ToArray();
73    }
74    #endregion
75
76    #region Access
77    public bool Contains(T item) {
78      return IndexOf(item) != -1;
79    }
80    public int IndexOf(T item) {
81      return Array.IndexOf<T>(array, item);
82    }
83    public int IndexOf(T item, int startIndex) {
84      return Array.IndexOf<T>(array, item, startIndex);
85    }
86    public int IndexOf(T item, int startIndex, int count) {
87      return Array.IndexOf<T>(array, item, startIndex, count);
88    }
89
90    public int LastIndexOf(T item) {
91      return Array.LastIndexOf<T>(array, item);
92    }
93    public int LastIndexOf(T item, int startIndex) {
94      return Array.LastIndexOf<T>(array, item, startIndex);
95    }
96    public int LastIndexOf(T item, int startIndex, int count) {
97      return Array.LastIndexOf<T>(array, item, startIndex, count);
98    }
99
100    public int BinarySearch(T item) {
101      return Array.BinarySearch<T>(array, item);
102    }
103    public int BinarySearch(T item, IComparer<T> comparer) {
104      return Array.BinarySearch<T>(array, item, comparer);
105    }
106    public int BinarySearch(int index, int count, T item) {
107      return Array.BinarySearch<T>(array, index, count, item);
108    }
109    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
110      return Array.BinarySearch<T>(array, index, count, item, comparer);
111    }
112
113    public bool Exists(Predicate<T> match) {
114      return Array.Exists<T>(array, match);
115    }
116
117    public T Find(Predicate<T> match) {
118      return Array.Find<T>(array, match);
119    }
120    public T[] FindAll(Predicate<T> match) {
121      return Array.FindAll<T>(array, match);
122    }
123    public T FindLast(Predicate<T> match) {
124      return Array.FindLast<T>(array, match);
125    }
126
127    public int FindIndex(Predicate<T> match) {
128      return Array.FindIndex<T>(array, match);
129    }
130    public int FindIndex(int startIndex, Predicate<T> match) {
131      return Array.FindIndex<T>(array, startIndex, match);
132    }
133    public int FindIndex(int startIndex, int count, Predicate<T> match) {
134      return Array.FindIndex<T>(array, startIndex, count, match);
135    }
136
137    public int FindLastIndex(Predicate<T> match) {
138      return Array.FindLastIndex<T>(array, match);
139    }
140    public int FindLastIndex(int startIndex, Predicate<T> match) {
141      return Array.FindLastIndex<T>(array, startIndex, match);
142    }
143    public int FindLastIndex(int startIndex, int count, Predicate<T> match) {
144      return Array.FindLastIndex<T>(array, startIndex, count, match);
145    }
146    #endregion
147
148    #region Manipulation
149    void ICollection<T>.Add(T item) {
150      throw new NotSupportedException();
151    }
152    void IList<T>.Insert(int index, T item) {
153      throw new NotSupportedException();
154    }
155    bool ICollection<T>.Remove(T item) {
156      throw new NotSupportedException();
157    }
158    void IList<T>.RemoveAt(int index) {
159      throw new NotSupportedException();
160    }
161
162    public void Clear(int index, int length) {
163      if (length > 0) {
164        IndexedItem<T>[] oldItems = GetIndexedItems(index, length);
165        Array.Clear(array, index, length);
166        OnPropertyChanged("Item[]");
167        OnItemsReplaced(GetIndexedItems(index, length), oldItems);
168      }
169    }
170    void ICollection<T>.Clear() {
171      Clear(0, array.Length);
172    }
173
174    public void Resize(int newSize) {
175      if (newSize != array.Length) {
176        IndexedItem<T>[] oldItems = GetIndexedItems();
177        Array.Resize<T>(ref array, newSize);
178        OnPropertyChanged("Length");
179        OnPropertyChanged("Item[]");
180        OnCollectionReset(GetIndexedItems(), oldItems);
181      }
182    }
183
184    public void Reverse() {
185      if (array.Length > 1) {
186        IndexedItem<T>[] oldItems = GetIndexedItems();
187        Array.Reverse(array);
188        OnPropertyChanged("Item[]");
189        OnItemsMoved(GetIndexedItems(), oldItems);
190      }
191    }
192    public void Reverse(int index, int length) {
193      if (length > 1) {
194        IndexedItem<T>[] oldItems = GetIndexedItems(index, length);
195        Array.Reverse(array, index, length);
196        OnPropertyChanged("Item[]");
197        OnItemsMoved(GetIndexedItems(index, length), oldItems);
198      }
199    }
200
201    public void Sort() {
202      if (array.Length > 1) {
203        IndexedItem<T>[] oldItems = GetIndexedItems();
204        Array.Sort<T>(array);
205        OnPropertyChanged("Item[]");
206        OnItemsMoved(GetIndexedItems(), oldItems);
207      }
208    }
209    public void Sort(Comparison<T> comparison) {
210      if (array.Length > 1) {
211        IndexedItem<T>[] oldItems = GetIndexedItems();
212        Array.Sort<T>(array, comparison);
213        OnPropertyChanged("Item[]");
214        OnItemsMoved(GetIndexedItems(), oldItems);
215      }
216    }
217    public void Sort(IComparer<T> comparer) {
218      if (array.Length > 1) {
219        IndexedItem<T>[] oldItems = GetIndexedItems();
220        Array.Sort<T>(array, comparer);
221        OnPropertyChanged("Item[]");
222        OnItemsMoved(GetIndexedItems(), oldItems);
223      }
224    }
225    public void Sort(int index, int length) {
226      if (length > 1) {
227        IndexedItem<T>[] oldItems = GetIndexedItems(index, length);
228        Array.Sort<T>(array, index, length);
229        OnPropertyChanged("Item[]");
230        OnItemsMoved(GetIndexedItems(index, length), oldItems);
231      }
232    }
233    public void Sort(int index, int length, IComparer<T> comparer) {
234      if (length > 1) {
235        IndexedItem<T>[] oldItems = GetIndexedItems(index, length);
236        Array.Sort<T>(array, index, length, comparer);
237        OnPropertyChanged("Item[]");
238        OnItemsMoved(GetIndexedItems(index, length), oldItems);
239      }
240    }
241    #endregion
242
243    #region Conversion
244    public ReadOnlyObservableArray<T> AsReadOnly() {
245      return new ReadOnlyObservableArray<T>(this);
246    }
247    public void CopyTo(T[] array) {
248      Array.Copy(this.array, array, this.array.Length);
249    }
250    public void CopyTo(T[] array, int arrayIndex) {
251      Array.Copy(this.array, 0, array, arrayIndex, this.array.Length);
252    }
253    public void CopyTo(int index, T[] array, int arrayIndex, int count) {
254      Array.Copy(this.array, index, array, arrayIndex, count);
255    }
256    public TOutput[] ConvertAll<TOutput>(Converter<T, TOutput> converter) {
257      return Array.ConvertAll<T, TOutput>(array, converter);
258    }
259    #endregion
260
261    #region Processing
262    public void ForEach(Action<T> action) {
263      Array.ForEach<T>(array, action);
264    }
265    public bool TrueForAll(Predicate<T> match) {
266      return Array.TrueForAll<T>(array, match);
267    }
268    #endregion
269
270    #region Enumeration
271    public IEnumerator<T> GetEnumerator() {
272      foreach (object o in ((IEnumerable)this))
273        yield return (T)o;
274    }
275    IEnumerator IEnumerable.GetEnumerator() {
276      return array.GetEnumerator();
277    }
278    #endregion
279
280    #region Events
281    [field: NonSerialized]
282    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
283    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
284      if (ItemsReplaced != null)
285        ItemsReplaced(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
286    }
287
288    [field: NonSerialized]
289    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
290    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
291      if (ItemsMoved != null)
292        ItemsMoved(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
293    }
294
295    [field: NonSerialized]
296    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
297    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
298      if (CollectionReset != null)
299        CollectionReset(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
300    }
301
302    [field: NonSerialized]
303    public event PropertyChangedEventHandler PropertyChanged;
304    protected virtual void OnPropertyChanged(string propertyName) {
305      if (PropertyChanged != null)
306        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
307    }
308    #endregion
309
310    #region Private helpers
311    private IndexedItem<T>[] GetIndexedItems() {
312      IndexedItem<T>[] items = new IndexedItem<T>[array.Length];
313      for (int i = 0; i < array.Length; i++)
314        items[i] = new IndexedItem<T>(i, array[i]);
315      return items;
316    }
317    private IndexedItem<T>[] GetIndexedItems(int index, int count) {
318      IndexedItem<T>[] items = new IndexedItem<T>[count];
319      for (int i = 0; i < count; i++)
320        items[i] = new IndexedItem<T>(index + i, array[index + i]);
321      return items;
322    }
323    #endregion
324  }
325}
Note: See TracBrowser for help on using the repository browser.