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