forked from s-wagner/HeuristicLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableKeyedCollection.cs
289 lines (268 loc) · 9.88 KB
/
ObservableKeyedCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Collections {
[StorableClass]
[Serializable]
public abstract class ObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
[Storable]
protected Dictionary<TKey, TItem> dict;
#region Properties
public int Count {
get { return dict.Count; }
}
public IEqualityComparer<TKey> Comparer {
get { return dict.Comparer; }
}
bool ICollection<TItem>.IsReadOnly {
get { return ((ICollection<KeyValuePair<TKey, TItem>>)dict).IsReadOnly; }
}
public TItem this[TKey key] {
get {
return dict[key];
}
}
#endregion
#region Constructors
protected ObservableKeyedCollection() {
dict = new Dictionary<TKey, TItem>();
}
protected ObservableKeyedCollection(int capacity) {
dict = new Dictionary<TKey, TItem>(capacity);
}
protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer) {
dict = new Dictionary<TKey, TItem>(comparer);
}
protected ObservableKeyedCollection(IEnumerable<TItem> collection) {
if (collection == null) throw new ArgumentNullException();
dict = new Dictionary<TKey, TItem>();
foreach (TItem item in collection)
dict.Add(GetKeyForItem(item), item);
}
protected ObservableKeyedCollection(int capacity, IEqualityComparer<TKey> comparer) {
dict = new Dictionary<TKey, TItem>(capacity, comparer);
}
protected ObservableKeyedCollection(IEnumerable<TItem> collection, IEqualityComparer<TKey> comparer) {
if (collection == null) throw new ArgumentNullException();
dict = new Dictionary<TKey, TItem>(comparer);
foreach (TItem item in collection)
dict.Add(GetKeyForItem(item), item);
}
[StorableConstructor]
protected ObservableKeyedCollection(bool deserializing) { }
#endregion
protected abstract TKey GetKeyForItem(TItem item);
protected void UpdateItemKey(TItem item) {
if (item == null) throw new ArgumentNullException();
TKey oldKey = default(TKey);
bool oldKeyFound = false;
foreach (KeyValuePair<TKey, TItem> entry in dict) {
if (entry.Value.Equals(item)) {
oldKey = entry.Key;
oldKeyFound = true;
break;
}
}
if (!oldKeyFound) throw new ArgumentException("Item not found");
dict.Remove(oldKey);
dict.Add(GetKeyForItem(item), item);
OnPropertyChanged("Item[]");
OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
}
#region Access
public bool ContainsKey(TKey key) {
return dict.ContainsKey(key);
}
public bool Contains(TItem item) {
return dict.ContainsValue(item);
}
public bool TryGetValue(TKey key, out TItem item) {
return dict.TryGetValue(key, out item);
}
public bool Exists(Predicate<TItem> match) {
if (match == null) throw new ArgumentNullException();
foreach (TItem item in dict.Values) {
if (match(item)) return true;
}
return false;
}
public TItem Find(Predicate<TItem> match) {
if (match == null) throw new ArgumentNullException();
foreach (TItem item in dict.Values) {
if (match(item)) return item;
}
return default(TItem);
}
public ICollection<TItem> FindAll(Predicate<TItem> match) {
if (match == null) throw new ArgumentNullException();
List<TItem> result = new List<TItem>();
foreach (TItem item in dict.Values) {
if (match(item)) result.Add(item);
}
return result;
}
#endregion
#region Manipulation
public void Add(TItem item) {
dict.Add(GetKeyForItem(item), item);
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnItemsAdded(new TItem[] { item });
}
public void AddRange(IEnumerable<TItem> collection) {
if (collection == null) throw new ArgumentNullException();
bool empty = true;
foreach (TItem item in collection) {
dict.Add(GetKeyForItem(item), item);
empty = false;
}
if (!empty) {
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnItemsAdded(collection);
}
}
public bool Remove(TKey key) {
TItem item;
if (TryGetValue(key, out item)) {
dict.Remove(key);
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnItemsRemoved(new TItem[] { item });
return true;
}
return false;
}
public bool Remove(TItem item) {
if (dict.Remove(GetKeyForItem(item))) {
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnItemsRemoved(new TItem[] { item });
return true;
}
return false;
}
public void RemoveRange(IEnumerable<TItem> collection) {
if (collection == null) throw new ArgumentNullException();
List<TItem> items = new List<TItem>();
foreach (TItem item in collection) {
if (dict.Remove(GetKeyForItem(item)))
items.Add(item);
}
if (items.Count > 0) {
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnItemsRemoved(items);
}
}
public int RemoveAll(Predicate<TItem> match) {
ICollection<TItem> items = FindAll(match);
RemoveRange(items);
return items.Count;
}
public void Clear() {
if (dict.Count > 0) {
TItem[] items = dict.Values.ToArray();
dict.Clear();
OnPropertyChanged("Item[]");
OnPropertyChanged("Count");
OnCollectionReset(new TItem[0], items);
}
}
#endregion
#region Conversion
public ReadOnlyObservableKeyedCollection<TKey, TItem> AsReadOnly() {
return new ReadOnlyObservableKeyedCollection<TKey, TItem>(this);
}
public TItem[] ToArray() {
return dict.Values.ToArray();
}
public void CopyTo(TItem[] array, int arrayIndex) {
dict.Values.CopyTo(array, arrayIndex);
}
public ICollection<TOutput> ConvertAll<TOutput>(Converter<TItem, TOutput> converter) {
if (converter == null) throw new ArgumentNullException();
List<TOutput> result = new List<TOutput>();
foreach (TItem item in dict.Values)
result.Add(converter(item));
return result;
}
#endregion
#region Processing
public void ForEach(Action<TItem> action) {
if (action == null) throw new ArgumentNullException();
foreach (TItem item in dict.Values)
action(item);
}
public bool TrueForAll(Predicate<TItem> match) {
if (match == null) throw new ArgumentNullException();
foreach (TItem item in dict.Values)
if (! match(item)) return false;
return true;
}
#endregion
#region Enumeration
public IEnumerator<TItem> GetEnumerator() {
return dict.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return dict.Values.GetEnumerator();
}
#endregion
#region Events
[field: NonSerialized]
public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}