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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
//
// MutableLookup.cs
//
// Author:
// Eric Maupin <me@ermau.com>
//
// Copyright (c) 2011 Eric Maupin (http://www.ermau.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Cadenza.Collections;
namespace Xamarin.PropertyEditing
{
/// <summary>
/// A mutable lookup implementing <see cref="ILookup{TKey,TElement}"/>
/// </summary>
/// <typeparam name="TKey">The lookup key.</typeparam>
/// <typeparam name="TElement">The elements under each <typeparamref name="TKey"/>.</typeparam>
internal class ObservableLookup<TKey, TElement>
: IMutableLookup<TKey, TElement>, INotifyCollectionChanged, IReadOnlyList<IGroupingList<TKey, TElement>>
{
public ObservableLookup ()
: this (EqualityComparer<TKey>.Default)
{
}
public ObservableLookup (IEqualityComparer<TKey> comparer)
{
if (comparer == null)
throw new ArgumentNullException ("comparer");
this.groupings = new OrderedDictionary<TKey, ObservableGrouping<TKey, TElement>> (comparer);
if (!typeof (TKey).IsValueType)
this.nullGrouping = new ObservableGrouping<TKey, TElement> (default (TKey));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public bool ReuseGroups
{
get { return this.reuseGroups; }
set
{
if (this.reuseGroups == value)
return;
this.reuseGroups = value;
if (value)
this.oldGroups = new Dictionary<TKey, ObservableGrouping<TKey, TElement>> ();
else
this.oldGroups = null;
}
}
/// <summary>
/// Adds <paramref name="element"/> under the specified <paramref name="key"/>. <paramref name="key"/> does not need to exist.
/// </summary>
/// <param name="key">The key to add <paramref name="element"/> under.</param>
/// <param name="element">The element to add.</param>
public void Add (TKey key, TElement element)
{
ObservableGrouping<TKey, TElement> grouping;
if (key == null) {
grouping = nullGrouping;
if (grouping.Count == 0)
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count));
} else if (!this.groupings.TryGetValue (key, out grouping)) {
if (!ReuseGroups || !this.oldGroups.TryRemove (key, out grouping))
grouping = new ObservableGrouping<TKey, TElement> (key);
this.groupings.Add (key, grouping);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count - 1));
}
grouping.Add (element);
}
public void Add (TKey key, IEnumerable<TElement> elements)
{
if (elements == null)
throw new ArgumentNullException (nameof(elements));
ObservableGrouping<TKey, TElement> grouping;
if (key == null) {
bool wasEmpty = this.nullGrouping.Count == 0;
grouping = nullGrouping;
grouping.AddRange (elements);
if (wasEmpty && this.nullGrouping.Count > 0)
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count));
} else if (!this.groupings.TryGetValue (key, out grouping)) {
if (!ReuseGroups || !this.oldGroups.TryRemove (key, out grouping))
grouping = new ObservableGrouping<TKey, TElement> (key);
grouping.AddRange (elements);
if (grouping.Count == 0) {
if (ReuseGroups)
this.oldGroups.Add (grouping.Key, grouping);
return;
}
this.groupings.Add (key, grouping);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count - 1));
}
}
public void Add (IGrouping<TKey, TElement> grouping)
{
if (grouping == null)
throw new ArgumentNullException (nameof(grouping));
if (grouping.Key == null) {
bool wasEmpty = this.nullGrouping.Count == 0;
this.nullGrouping.AddRange (grouping);
if (wasEmpty && this.nullGrouping.Count > 0)
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count));
return;
}
ObservableGrouping<TKey, TElement> og;
if (!ReuseGroups || !this.oldGroups.TryRemove (grouping.Key, out og)) {
og = new ObservableGrouping<TKey, TElement> (grouping.Key);
}
og.AddRange (grouping);
if (og.Count == 0) {
if (ReuseGroups)
this.oldGroups.Add (grouping.Key, og);
return;
}
this.groupings.Add (grouping.Key, og);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)og, this.groupings.Count - 1));
}
public void Insert (int index, IGrouping<TKey, TElement> grouping)
{
ObservableGrouping<TKey, TElement> og;
if (!ReuseGroups || !this.oldGroups.TryRemove (grouping.Key, out og)) {
og = new ObservableGrouping<TKey, TElement> (grouping.Key);
}
og.AddRange (grouping);
if (og.Count == 0) {
if (ReuseGroups)
this.oldGroups.Add (grouping.Key, og);
return;
}
this.groupings.Insert (index, og.Key, og);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)og, index));
}
/// <summary>
/// Removes <paramref name="element"/> from the <paramref name="key"/>.
/// </summary>
/// <param name="key">The key that <paramref name="element"/> is located under.</param>
/// <param name="element">The element to remove from <paramref name="key"/>. </param>
/// <returns><c>true</c> if <paramref name="key"/> and <paramref name="element"/> existed, <c>false</c> if not.</returns>
public bool Remove (TKey key, TElement element)
{
ObservableGrouping<TKey, TElement> group;
if (key == null) {
group = this.nullGrouping;
} else {
if (!this.groupings.TryGetValue (key, out group))
return false;
}
if (group.Remove (element)) {
if (group.Count == 0) {
if (!Remove (key) && key == null)
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, (object)this.nullGrouping, this.groupings.Count));
}
return true;
}
return false;
}
/// <summary>
/// Removes <paramref name="key"/> from the lookup.
/// </summary>
/// <param name="key">They to remove.</param>
/// <returns><c>true</c> if <paramref name="key"/> existed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
public bool Remove (TKey key)
{
if (key == null) {
bool removed = (this.nullGrouping.Count > 0);
if (removed) {
this.nullGrouping.Clear();
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, (object) this.nullGrouping, this.groupings.Count));
}
return removed;
}
int index = this.groupings.IndexOf (key);
if (index >= 0) {
var g = this.groupings[index];
this.groupings.Remove (key);
if (ReuseGroups) {
g.Clear ();
this.oldGroups.Add (key, g);
}
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, (object)g, index));
return true;
}
return false;
}
public void Clear ()
{
this.nullGrouping?.Clear();
if (ReuseGroups) {
foreach (var g in this.groupings.Values) {
this.oldGroups.Add (g.Key, g);
g.Clear ();
}
}
this.groupings.Clear ();
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
}
public bool TryGetValues (TKey key, out IEnumerable<TElement> values)
{
values = null;
if (key == null) {
if (this.nullGrouping.Count != 0) {
values = this.nullGrouping;
return true;
} else
return false;
}
ObservableGrouping<TKey, TElement> grouping;
if (!this.groupings.TryGetValue (key, out grouping))
return false;
values = grouping;
return true;
}
#region ILookup Members
/// <summary>
/// Gets the number of groupings.
/// </summary>
public int Count
{
get { return this.groupings.Count + ((this.nullGrouping != null && this.nullGrouping.Count > 0) ? 1 : 0); }
}
IGroupingList<TKey, TElement> IReadOnlyList<IGroupingList<TKey, TElement>>.this [int index]
{
get
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException (nameof(index));
return (IGroupingList<TKey, TElement>)this[index];
}
}
/// <summary>
/// Gets the elements for <paramref name="key"/>.
/// </summary>
/// <param name="key">The key to get the elements for.</param>
/// <returns>The elements under <paramref name="key"/>.</returns>
public IEnumerable<TElement> this[TKey key]
{
get
{
if (key == null)
return this.nullGrouping;
ObservableGrouping<TKey, TElement> grouping;
if (this.groupings.TryGetValue (key, out grouping))
return grouping;
return Enumerable.Empty<TElement>();
}
}
public IEnumerable<TElement> this[int index]
{
get
{
if (index == this.groupings.Count) {
if (this.nullGrouping.Count > 0)
return this.nullGrouping;
else
return Enumerable.Empty<TElement>();
}
return this.groupings[index];
}
}
/// <summary>
/// Gets whether or not there's a grouping for <paramref name="key"/>.
/// </summary>
/// <param name="key">The key to check for.</param>
/// <returns><c>true</c> if <paramref name="key"/> is present.</returns>
public bool Contains (TKey key)
{
if (key == null)
return (this.nullGrouping.Count > 0);
return this.groupings.ContainsKey (key);
}
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator ()
{
foreach (var g in this.groupings.Values)
yield return g;
if (this.nullGrouping != null && this.nullGrouping.Count > 0)
yield return this.nullGrouping;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return this.GetEnumerator ();
}
#endregion
private readonly OrderedDictionary<TKey, ObservableGrouping<TKey, TElement>> groupings;
private readonly ObservableGrouping<TKey, TElement> nullGrouping;
private bool reuseGroups;
private Dictionary<TKey, ObservableGrouping<TKey, TElement>> oldGroups;
private void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
{
CollectionChanged?.Invoke (this, e);
}
IEnumerator<IGroupingList<TKey, TElement>> IEnumerable<IGroupingList<TKey, TElement>>.GetEnumerator ()
{
foreach (var g in this.groupings.Values)
yield return g;
if (this.nullGrouping != null && this.nullGrouping.Count > 0)
yield return this.nullGrouping;
}
}
}
|