wasSharp – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections; |
||
9 | using System.Collections.Generic; |
||
10 | using System.Collections.Specialized; |
||
11 | using System.Linq; |
||
12 | |||
13 | namespace wasSharp.Collections.Specialized |
||
14 | { |
||
15 | /////////////////////////////////////////////////////////////////////////// |
||
16 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
17 | /////////////////////////////////////////////////////////////////////////// |
||
18 | /// <summary> |
||
19 | /// An implementation of an observable HashSet. |
||
20 | /// </summary> |
||
21 | /// <typeparam name="T">the object type</typeparam> |
||
23 | office | 22 | public class ObservableHashSet<T> : ICollection<T>, INotifyCollectionChanged, IEnumerable<T> |
10 | office | 23 | { |
24 | private readonly HashSet<T> store = new HashSet<T>(); |
||
25 | |||
26 | public ObservableHashSet(HashSet<T> set) |
||
27 | { |
||
28 | UnionWith(set); |
||
29 | } |
||
30 | |||
31 | public ObservableHashSet() |
||
32 | { |
||
33 | } |
||
34 | |||
35 | public ObservableHashSet(T item) |
||
36 | { |
||
37 | Add(item); |
||
38 | } |
||
39 | |||
40 | public ObservableHashSet(ObservableHashSet<T> other) |
||
41 | { |
||
42 | UnionWith(other); |
||
43 | } |
||
44 | |||
45 | public ObservableHashSet(IEnumerable<T> list) |
||
46 | { |
||
47 | UnionWith(list); |
||
48 | } |
||
49 | |||
50 | public bool IsVirgin { get; private set; } = true; |
||
51 | |||
52 | public IEnumerator<T> GetEnumerator() |
||
53 | { |
||
54 | return store.GetEnumerator(); |
||
55 | } |
||
56 | |||
57 | IEnumerator IEnumerable.GetEnumerator() |
||
58 | { |
||
59 | return GetEnumerator(); |
||
60 | } |
||
61 | |||
62 | public void Add(T item) |
||
63 | { |
||
64 | store.Add(item); |
||
65 | IsVirgin = false; |
||
66 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); |
||
67 | } |
||
68 | |||
69 | public void Clear() |
||
70 | { |
||
71 | store.Clear(); |
||
72 | if (!IsVirgin) |
||
73 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
||
74 | IsVirgin = false; |
||
75 | } |
||
76 | |||
77 | public bool Contains(T item) |
||
78 | { |
||
79 | return store.Contains(item); |
||
80 | } |
||
81 | |||
82 | public void CopyTo(T[] array, int arrayIndex) |
||
83 | { |
||
84 | store.CopyTo(array, arrayIndex); |
||
85 | } |
||
86 | |||
87 | public bool Remove(T item) |
||
88 | { |
||
89 | var removed = store.Remove(item); |
||
90 | IsVirgin = false; |
||
91 | if (removed) |
||
92 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); |
||
93 | return removed; |
||
94 | } |
||
95 | |||
96 | public int Count => store.Count; |
||
97 | |||
98 | public bool IsReadOnly => false; |
||
99 | |||
100 | public event NotifyCollectionChangedEventHandler CollectionChanged; |
||
101 | |||
102 | public void UnionWith(IEnumerable<T> list) |
||
103 | { |
||
104 | var added = new List<T>(list.Except(store)); |
||
105 | store.UnionWith(added); |
||
106 | if (!IsVirgin && added.Any()) |
||
107 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added)); |
||
108 | IsVirgin = false; |
||
109 | } |
||
110 | |||
111 | private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) |
||
112 | { |
||
113 | CollectionChanged?.Invoke(this, args); |
||
114 | } |
||
115 | |||
116 | public void ExceptWith(IEnumerable<T> list) |
||
117 | { |
||
118 | var removed = new List<T>(list.Intersect(store)); |
||
119 | store.ExceptWith(removed); |
||
120 | if (!IsVirgin && removed.Any()) |
||
121 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
||
122 | removed)); |
||
123 | IsVirgin = false; |
||
124 | } |
||
125 | |||
126 | public void RemoveWhere(Func<T, bool> func) |
||
127 | { |
||
128 | var removed = new List<T>(store.Where(func)); |
||
129 | store.ExceptWith(removed); |
||
130 | if (!IsVirgin && removed.Any()) |
||
131 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, |
||
132 | removed)); |
||
133 | IsVirgin = false; |
||
134 | } |
||
23 | office | 135 | |
136 | public IEnumerable<T> AsEnumerable() |
||
137 | { |
||
138 | return store.AsEnumerable(); |
||
139 | } |
||
10 | office | 140 | } |
27 | office | 141 | } |