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