Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 9.5 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 abstract class ObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
32    [Storable]
33    private Dictionary<TKey, TItem> dict;
34
35    #region Properties
36    public int Count {
37      get { return dict.Count; }
38    }
39    public IEqualityComparer<TKey> Comparer {
40      get { return dict.Comparer; }
41    }
42    bool ICollection<TItem>.IsReadOnly {
43      get { return ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly; }
44    }
45
46    public TItem this[TKey key] {
47      get {
48        return dict[key];
49      }
50    }
51    #endregion
52
53    #region Constructors
54    protected ObservableKeyedCollection() {
55      dict = new Dictionary<TKey, TItem>();
56    }
57    protected ObservableKeyedCollection(int capacity) {
58      dict = new Dictionary<TKey, TItem>(capacity);
59    }
60    protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer) {
61      dict = new Dictionary<TKey, TItem>(comparer);
62    }
63    protected ObservableKeyedCollection(IEnumerable<TItem> collection) {
64      if (collection == null) throw new ArgumentNullException();
65      dict = new Dictionary<TKey, TItem>();
66      foreach (TItem item in collection)
67        dict.Add(GetKeyForItem(item), item);
68    }
69    protected ObservableKeyedCollection(int capacity, IEqualityComparer<TKey> comparer) {
70      dict = new Dictionary<TKey, TItem>(capacity, comparer);
71    }
72    protected ObservableKeyedCollection(IEnumerable<TItem> collection, IEqualityComparer<TKey> comparer) {
73      if (collection == null) throw new ArgumentNullException();
74      dict = new Dictionary<TKey, TItem>(comparer);
75      foreach (TItem item in collection)
76        dict.Add(GetKeyForItem(item), item);
77    }
78    #endregion
79
80    protected abstract TKey GetKeyForItem(TItem item);
81    protected void UpdateItemKey(TItem item) {
82      if (item == null) throw new ArgumentNullException();
83      TKey oldKey = default(TKey);
84      bool oldKeyFound = false;
85      foreach (KeyValuePair<TKey, TItem> entry in dict) {
86        if (entry.Value.Equals(item)) {
87          oldKey = entry.Key;
88          oldKeyFound = true;
89          break;
90        }
91      }
92      if (!oldKeyFound) throw new ArgumentException("Item not found");
93      dict.Remove(oldKey);
94      dict.Add(GetKeyForItem(item), item);
95      OnPropertyChanged("Item[]");
96      OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
97    }
98
99    #region Access
100    public bool ContainsKey(TKey key) {
101      return dict.ContainsKey(key);
102    }
103    public bool Contains(TItem item) {
104      return dict.ContainsValue(item);
105    }
106
107    public bool TryGetValue(TKey key, out TItem item) {
108      return dict.TryGetValue(key, out item);
109    }
110
111    public bool Exists(Predicate<TItem> match) {
112      if (match == null) throw new ArgumentNullException();
113      foreach (TItem item in dict.Values) {
114        if (match(item)) return true;
115      }
116      return false;
117    }
118
119    public TItem Find(Predicate<TItem> match) {
120      if (match == null) throw new ArgumentNullException();
121      foreach (TItem item in dict.Values) {
122        if (match(item)) return item;
123      }
124      return default(TItem);
125    }
126    public ICollection<TItem> FindAll(Predicate<TItem> match) {
127      if (match == null) throw new ArgumentNullException();
128      List<TItem> result = new List<TItem>();
129      foreach (TItem item in dict.Values) {
130        if (match(item)) result.Add(item);
131      }
132      return result;
133    }
134    #endregion
135
136    #region Manipulation
137    public void Add(TItem item) {
138      dict.Add(GetKeyForItem(item), item);
139      OnPropertyChanged("Item[]");
140      OnPropertyChanged("Count");
141      OnItemsAdded(new TItem[] { item });
142    }
143    public void AddRange(IEnumerable<TItem> collection) {
144      if (collection == null) throw new ArgumentNullException();
145      bool empty = true;
146      foreach (TItem item in collection) {
147        dict.Add(GetKeyForItem(item), item);
148        empty = false;
149      }
150      if (!empty) {
151        OnPropertyChanged("Item[]");
152        OnPropertyChanged("Count");
153        OnItemsAdded(collection);
154      }
155    }
156
157    public bool Remove(TKey key) {
158      TItem item;
159      if (TryGetValue(key, out item)) {
160        dict.Remove(key);
161        OnPropertyChanged("Item[]");
162        OnPropertyChanged("Count");
163        OnItemsRemoved(new TItem[] { item });
164        return true;
165      }
166      return false;
167    }
168    public bool Remove(TItem item) {
169      if (dict.Remove(GetKeyForItem(item))) {
170        OnPropertyChanged("Item[]");
171        OnPropertyChanged("Count");
172        OnItemsRemoved(new TItem[] { item });
173        return true;
174      }
175      return false;
176    }
177    public void RemoveRange(IEnumerable<TItem> collection) {
178      if (collection == null) throw new ArgumentNullException();
179      List<TItem> items = new List<TItem>();
180      foreach (TItem item in collection) {
181        if (dict.Remove(GetKeyForItem(item)))
182          items.Add(item);
183      }
184      if (items.Count > 0) {
185        OnPropertyChanged("Item[]");
186        OnPropertyChanged("Count");
187        OnItemsRemoved(items);
188      }
189    }
190    public int RemoveAll(Predicate<TItem> match) {
191      ICollection<TItem> items = FindAll(match);
192      RemoveRange(items);
193      return items.Count;
194    }
195
196    public void Clear() {
197      if (dict.Count > 0) {
198        TItem[] items = dict.Values.ToArray();
199        dict.Clear();
200        OnPropertyChanged("Item[]");
201        OnPropertyChanged("Count");
202        OnCollectionReset(new TItem[0], items);
203      }
204    }
205    #endregion
206
207    #region Conversion
208    public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
209      return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
210    }
211    public TItem[] ToArray() {
212      return dict.Values.ToArray();
213    }
214    public void CopyTo(TItem[] array, int arrayIndex) {
215      dict.Values.CopyTo(array, arrayIndex);
216    }
217    public ICollection<TOutput> ConvertAll<TOutput>(Converter<TItem, TOutput> converter) {
218      if (converter == null) throw new ArgumentNullException();
219      List<TOutput> result = new List<TOutput>();
220      foreach (TItem item in dict.Values)
221        result.Add(converter(item));
222      return result;
223    }
224    #endregion
225
226    #region Processing
227    public void ForEach(Action<TItem> action) {
228      if (action == null) throw new ArgumentNullException();
229      foreach (TItem item in dict.Values)
230        action(item);
231    }
232    public bool TrueForAll(Predicate<TItem> match) {
233      if (match == null) throw new ArgumentNullException();
234      foreach (TItem item in dict.Values)
235        if (! match(item)) return false;
236      return true;
237    }
238    #endregion
239
240    #region Enumeration
241    public IEnumerator<TItem> GetEnumerator() {
242      return dict.Values.GetEnumerator();
243    }
244    IEnumerator IEnumerable.GetEnumerator() {
245      return dict.Values.GetEnumerator();
246    }
247    #endregion
248
249    #region Events
250    [field: NonSerialized]
251    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
252    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
253      if (ItemsAdded != null)
254        ItemsAdded(this, new CollectionItemsChangedEventArgs<TItem>(items));
255    }
256
257    [field: NonSerialized]
258    public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
259    protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
260      if (ItemsRemoved != null)
261        ItemsRemoved(this, new CollectionItemsChangedEventArgs<TItem>(items));
262    }
263
264    [field: NonSerialized]
265    public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
266    protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
267      if (ItemsReplaced != null)
268        ItemsReplaced(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
269    }
270
271    [field: NonSerialized]
272    public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
273    protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
274      if (CollectionReset != null)
275        CollectionReset(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
276    }
277
278    [field: NonSerialized]
279    public event PropertyChangedEventHandler PropertyChanged;
280    protected virtual void OnPropertyChanged(string propertyName) {
281      if (PropertyChanged != null)
282        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
283    }
284    #endregion
285  }
286}
Note: See TracBrowser for help on using the repository browser.