-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathObservableSet.cs
More file actions
50 lines (39 loc) · 1.05 KB
/
ObservableSet.cs
File metadata and controls
50 lines (39 loc) · 1.05 KB
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
#nullable enable
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SharpGen.Platform;
public sealed class ObservableSet<T> : ObservableCollection<T>
{
private readonly HashSet<T> set;
public ObservableSet(IEqualityComparer<T> equalityComparer) => set = new HashSet<T>(equalityComparer);
public void AddRange(IEnumerable<T> items)
{
foreach (var item in items)
Add(item);
}
protected override void InsertItem(int index, T item)
{
if (!set.Add(item))
return;
base.InsertItem(index, item);
}
protected override void ClearItems()
{
base.ClearItems();
set.Clear();
}
protected override void RemoveItem(int index)
{
var item = this[index];
set.Remove(item);
base.RemoveItem(index);
}
protected override void SetItem(int index, T item)
{
if (!set.Add(item))
return;
var oldItem = this[index];
set.Remove(oldItem);
base.SetItem(index, item);
}
}