Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored HeuristicLab.Collections (#977)

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