Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ObservableList.cs @ 2572

Last change on this file since 2572 was 2572, checked in by swagner, 15 years ago

Added new plugin HeuristicLab.Collections which contains generic observable collections (#819)

File size: 9.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.ObjectModel;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Collections {
31  [Serializable]
32  public class ObservableList<T> : IndexedCollectionChangedEventsBase<IndexedItem<T>>, IList<T> {
33    [Storable]
34    private List<T> list;
35
36    #region Properties
37    public int Capacity {
38      get { return list.Capacity; }
39      set { list.Capacity = value; }
40    }
41    public int Count {
42      get { return list.Count; }
43    }
44    bool ICollection<T>.IsReadOnly {
45      get { return ((ICollection<T>)list).IsReadOnly; }
46    }
47
48    public T this[int index] {
49      get {
50        return list[index];
51      }
52      set {
53        T item = list[index];
54        list[index] = value;
55        OnItemsReplaced(new IndexedItem<T>[] { new IndexedItem<T>(index, value) }, new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
56      }
57    }
58    #endregion
59
60    #region Constructors
61    public ObservableList() {
62      list = new List<T>();
63    }
64    public ObservableList(int capacity) {
65      list = new List<T>(capacity);
66    }
67    public ObservableList(IEnumerable<T> collection) {
68      list = new List<T>(collection);
69    }
70    #endregion
71
72    #region Access
73    public List<T> GetRange(int index, int count) {
74      return list.GetRange(index, count);
75    }
76
77    public bool Contains(T item) {
78      return list.Contains(item);
79    }
80
81    public int IndexOf(T item) {
82      return list.IndexOf(item);
83    }
84    public int IndexOf(T item, int index) {
85      return list.IndexOf(item, index);
86    }
87    public int IndexOf(T item, int index, int count) {
88      return list.IndexOf(item, index, count);
89    }
90
91    public int LastIndexOf(T item) {
92      return list.LastIndexOf(item);
93    }
94    public int LastIndexOf(T item, int index) {
95      return list.LastIndexOf(item, index);
96    }
97    public int LastIndexOf(T item, int index, int count) {
98      return list.LastIndexOf(item, index, count);
99    }
100
101    public int BinarySearch(T item) {
102      return list.BinarySearch(item);
103    }
104    public int BinarySearch(T item, IComparer<T> comparer) {
105      return list.BinarySearch(item, comparer);
106    }
107    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
108      return list.BinarySearch(index, count, item, comparer);
109    }
110
111    public bool Exists(Predicate<T> match) {
112      return list.Exists(match);
113    }
114
115    public T Find(Predicate<T> match) {
116      return list.Find(match);
117    }
118    public List<T> FindAll(Predicate<T> match) {
119      return list.FindAll(match);
120    }
121    public T FindLast(Predicate<T> match) {
122      return list.FindLast(match);
123    }
124
125    public int FindIndex(Predicate<T> match) {
126      return list.FindIndex(match);
127    }
128    public int FindIndex(int startIndex, Predicate<T> match) {
129      return list.FindIndex(startIndex, match);
130    }
131    public int FindIndex(int startIndex, int count, Predicate<T> match) {
132      return list.FindIndex(startIndex, count, match);
133    }
134
135    public int FindLastIndex(Predicate<T> match) {
136      return list.FindLastIndex(match);
137    }
138    public int FindLastIndex(int startIndex, Predicate<T> match) {
139      return list.FindLastIndex(startIndex, match);
140    }
141    public int FindLastIndex(int startIndex, int count, Predicate<T> match) {
142      return list.FindLastIndex(startIndex, count, match);
143    }
144    #endregion
145
146    #region Manipulation
147    public void Add(T item) {
148      list.Add(item);
149      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(list.Count - 1, item) });
150    }
151    public void AddRange(IEnumerable<T> collection) {
152      int index = list.Count;
153      list.AddRange(collection);
154      List<IndexedItem<T>> items = new List<IndexedItem<T>>();
155      foreach (T item in collection) {
156        items.Add(new IndexedItem<T>(index, item));
157        index++;
158      }
159      OnItemsAdded(items);
160    }
161
162    public void Insert(int index, T item) {
163      list.Insert(index, item);
164      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
165    }
166    public void InsertRange(int index, IEnumerable<T> collection) {
167      list.InsertRange(index, collection);
168      List<IndexedItem<T>> items = new List<IndexedItem<T>>();
169      foreach (T item in collection) {
170        items.Add(new IndexedItem<T>(index, item));
171        index++;
172      }
173      OnItemsAdded(items);
174    }
175
176    public bool Remove(T item) {
177      int index = list.IndexOf(item);
178      if (index != -1) {
179        list.RemoveAt(index);
180        OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
181        return true;
182      }
183      return false;
184    }
185    public int RemoveAll(Predicate<T> match) {
186      List<IndexedItem<T>> items = new List<IndexedItem<T>>();
187      for (int i = 0; i < list.Count; i++) {
188        if (match(list[i]))
189          items.Add(new IndexedItem<T>(i, list[i]));
190      }
191      int result = list.RemoveAll(match);
192      OnItemsRemoved(items);
193      return result;
194    }
195    public void RemoveAt(int index) {
196      T item = list[index];
197      list.RemoveAt(index);
198      OnItemsRemoved(new IndexedItem<T>[] { new IndexedItem<T>(index, item) });
199    }
200    public void RemoveRange(int index, int count) {
201      IndexedItem<T>[] items = GetIndexedItems(index, count);
202      list.RemoveRange(index, count);
203      OnItemsRemoved(items);
204    }
205
206    public void Clear() {
207      IndexedItem<T>[] items = GetIndexedItems();
208      list.Clear();
209      OnCollectionReset(new IndexedItem<T>[0], items);
210    }
211
212    public void Reverse() {
213      IndexedItem<T>[] oldItems = GetIndexedItems();
214      list.Reverse();
215      OnItemsMoved(GetIndexedItems(), oldItems);
216    }
217    public void Reverse(int index, int count) {
218      IndexedItem<T>[] oldItems = GetIndexedItems(index, count);
219      list.Reverse(index, count);
220      OnItemsMoved(GetIndexedItems(index, count), oldItems);
221    }
222
223    public void Sort() {
224      IndexedItem<T>[] oldItems = GetIndexedItems();
225      list.Sort();
226      OnItemsMoved(GetIndexedItems(), oldItems);
227    }
228    public void Sort(Comparison<T> comparison) {
229      IndexedItem<T>[] oldItems = GetIndexedItems();
230      list.Sort(comparison);
231      OnItemsMoved(GetIndexedItems(), oldItems);
232    }
233    public void Sort(IComparer<T> comparer) {
234      IndexedItem<T>[] oldItems = GetIndexedItems();
235      list.Sort(comparer);
236      OnItemsMoved(GetIndexedItems(), oldItems);
237    }
238    public void Sort(int index, int count, IComparer<T> comparer) {
239      IndexedItem<T>[] oldItems = GetIndexedItems(index, count);
240      list.Sort(index, count, comparer);
241      OnItemsMoved(GetIndexedItems(index, count), oldItems);
242    }
243    #endregion
244
245    #region Conversion
246    public ReadOnlyCollection<T> AsReadOnly() {
247      return list.AsReadOnly();
248    }
249    public T[] ToArray() {
250      return list.ToArray();
251    }
252    void ICollection<T>.CopyTo(T[] array, int arrayIndex) {
253      list.CopyTo(array, arrayIndex);
254    }
255    public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) {
256      return list.ConvertAll<TOutput>(converter);
257    }
258    #endregion
259
260    #region Processing
261    public void ForEach(Action<T> action) {
262      list.ForEach(action);
263    }
264    public bool TrueForAll(Predicate<T> match) {
265      return list.TrueForAll(match);
266    }
267    #endregion
268
269    #region Enumeration
270    public List<T>.Enumerator GetEnumerator() {
271      return list.GetEnumerator();
272    }
273    IEnumerator<T> IEnumerable<T>.GetEnumerator() {
274      return ((IEnumerable<T>)list).GetEnumerator();
275    }
276    IEnumerator IEnumerable.GetEnumerator() {
277      return ((IEnumerable)list).GetEnumerator();
278    }
279    #endregion
280
281    #region Helpers
282    public void TrimExcess() {
283      list.TrimExcess();
284    }
285    #endregion
286
287    #region Private helpers
288    private IndexedItem<T>[] GetIndexedItems() {
289      IndexedItem<T>[] items = new IndexedItem<T>[list.Count];
290      for (int i = 0; i < list.Count; i++)
291        items[i] = new IndexedItem<T>(i, list[i]);
292      return items;
293    }
294    private IndexedItem<T>[] GetIndexedItems(int index, int count) {
295      IndexedItem<T>[] items = new IndexedItem<T>[count];
296      for (int i = 0; i < count; i++)
297        items[i] = new IndexedItem<T>(index + i, list[index + i]);
298      return items;
299    }
300    #endregion
301  }
302}
Note: See TracBrowser for help on using the repository browser.