wasSharp – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
22 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2016 - 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.Collections; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Collections.Specialized; |
||
10 | |||
11 | namespace wasSharp.Collections.Specialized |
||
12 | { |
||
13 | /////////////////////////////////////////////////////////////////////////// |
||
14 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
15 | /////////////////////////////////////////////////////////////////////////// |
||
16 | /// <summary> |
||
17 | /// An implementation of an observable Dictionary. |
||
18 | /// </summary> |
||
19 | /// <typeparam name="K">the key type</typeparam> |
||
20 | /// <typeparam name="V">the value type</typeparam> |
||
21 | public class ObservableDictionary<K, V> : IDictionary<K, V>, INotifyCollectionChanged |
||
22 | { |
||
23 | private readonly Dictionary<K, V> store = new Dictionary<K, V>(); |
||
24 | |||
25 | public bool IsVirgin { get; private set; } = true; |
||
26 | |||
27 | public V this[K key] |
||
28 | { |
||
29 | get { return store[key]; } |
||
30 | |||
31 | set { store[key] = value; } |
||
32 | } |
||
33 | |||
34 | public int Count => store.Count; |
||
35 | |||
36 | public bool IsReadOnly => false; |
||
37 | |||
38 | public ICollection<K> Keys => store.Keys; |
||
39 | |||
40 | public ICollection<V> Values => store.Values; |
||
41 | |||
42 | public void Add(KeyValuePair<K, V> item) |
||
43 | { |
||
27 | office | 44 | ((IDictionary<K, V>)store).Add(item); |
22 | office | 45 | IsVirgin = false; |
46 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); |
||
47 | } |
||
48 | |||
49 | public void Add(K key, V value) |
||
50 | { |
||
51 | store.Add(key, value); |
||
52 | IsVirgin = false; |
||
53 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, |
||
54 | new KeyValuePair<K, V>(key, value))); |
||
55 | } |
||
56 | |||
57 | public void Clear() |
||
58 | { |
||
59 | store.Clear(); |
||
60 | if (!IsVirgin) |
||
61 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
||
62 | IsVirgin = false; |
||
63 | } |
||
64 | |||
65 | public bool Contains(KeyValuePair<K, V> item) |
||
66 | { |
||
27 | office | 67 | return ((IDictionary<K, V>)store).Contains(item); |
22 | office | 68 | } |
69 | |||
70 | public bool ContainsKey(K key) |
||
71 | { |
||
72 | return store.ContainsKey(key); |
||
73 | } |
||
74 | |||
75 | public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) |
||
76 | { |
||
27 | office | 77 | ((IDictionary<K, V>)store).CopyTo(array, arrayIndex); |
22 | office | 78 | } |
79 | |||
80 | public IEnumerator<KeyValuePair<K, V>> GetEnumerator() |
||
81 | { |
||
27 | office | 82 | return ((IDictionary<K, V>)store).GetEnumerator(); |
22 | office | 83 | } |
84 | |||
85 | public bool Remove(KeyValuePair<K, V> item) |
||
86 | { |
||
27 | office | 87 | var removed = ((IDictionary<K, V>)store).Remove(item); |
22 | office | 88 | IsVirgin = false; |
89 | if (removed) |
||
90 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); |
||
91 | return removed; |
||
92 | } |
||
93 | |||
94 | public bool Remove(K key) |
||
95 | { |
||
96 | KeyValuePair<K, V> item; |
||
97 | if (store.ContainsKey(key)) |
||
98 | item = new KeyValuePair<K, V>(key, store[key]); |
||
99 | |||
100 | var removed = store.Remove(key); |
||
101 | IsVirgin = false; |
||
102 | if (removed) |
||
103 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); |
||
104 | return removed; |
||
105 | } |
||
106 | |||
107 | public bool TryGetValue(K key, out V value) |
||
108 | { |
||
109 | return store.TryGetValue(key, out value); |
||
110 | } |
||
111 | |||
112 | IEnumerator IEnumerable.GetEnumerator() |
||
113 | { |
||
27 | office | 114 | return ((IDictionary<K, V>)store).GetEnumerator(); |
22 | office | 115 | } |
116 | |||
117 | public event NotifyCollectionChangedEventHandler CollectionChanged; |
||
118 | |||
119 | private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) |
||
120 | { |
||
121 | CollectionChanged?.Invoke(this, args); |
||
122 | } |
||
123 | } |
||
27 | office | 124 | } |