|
| 1 | +#region Licensing information |
| 2 | +/* |
| 3 | + * Copyright(c) 2020 Vadim Zhukov<zhuk@openbsd.org> |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and distribute this software for any |
| 6 | + * purpose with or without fee is hereby granted, provided that the above |
| 7 | + * copyright notice and this permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | + * MERCHANTABILITY AND FITNESS.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | + */ |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Collections; |
| 21 | +using System.Collections.Generic; |
| 22 | +using System.Collections.Specialized; |
| 23 | +using System.Linq; |
| 24 | +using System.Reactive; |
| 25 | +using System.Reactive.Linq; |
| 26 | +using IX.Observable; |
| 27 | +using MahApps.Metro.Controls; |
| 28 | + |
| 29 | +namespace PipeExplorer.Services |
| 30 | +{ |
| 31 | + class ObservableSet<T> : ISet<T>, INotifyCollectionChanged |
| 32 | + { |
| 33 | + private readonly ObservableDictionary<T, Unit> impl; |
| 34 | + |
| 35 | + public event NotifyCollectionChangedEventHandler CollectionChanged; |
| 36 | + |
| 37 | + public ObservableSet() |
| 38 | + { |
| 39 | + impl = new ObservableDictionary<T, Unit>(); |
| 40 | + impl.CollectionChanged += Impl_CollectionChanged; |
| 41 | + } |
| 42 | + |
| 43 | + public ObservableSet(int capacity) |
| 44 | + { |
| 45 | + impl = new ObservableDictionary<T, Unit>(capacity); |
| 46 | + } |
| 47 | + |
| 48 | + private void Impl_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
| 49 | + { |
| 50 | + var oldItems = (e.OldItems as IList<KeyValuePair<T, Unit>>)?.Select(kv => kv.Key).ToList(); |
| 51 | + var newItems = (e.NewItems as IList<KeyValuePair<T, Unit>>)?.Select(kv => kv.Key).ToList(); |
| 52 | + NotifyCollectionChangedEventArgs newArgs; |
| 53 | + switch (e.Action) |
| 54 | + { |
| 55 | + case NotifyCollectionChangedAction.Add: |
| 56 | + newArgs = new NotifyCollectionChangedEventArgs(e.Action, newItems, e.NewStartingIndex); |
| 57 | + break; |
| 58 | + case NotifyCollectionChangedAction.Remove: |
| 59 | + newArgs = new NotifyCollectionChangedEventArgs(e.Action, oldItems, e.OldStartingIndex); |
| 60 | + break; |
| 61 | + case NotifyCollectionChangedAction.Replace: |
| 62 | + newArgs = new NotifyCollectionChangedEventArgs(e.Action, oldItems, newItems, e.OldStartingIndex); |
| 63 | + break; |
| 64 | + case NotifyCollectionChangedAction.Move: |
| 65 | + newArgs = new NotifyCollectionChangedEventArgs(e.Action, oldItems, e.NewStartingIndex, e.OldStartingIndex); |
| 66 | + break; |
| 67 | + case NotifyCollectionChangedAction.Reset: |
| 68 | + newArgs = new NotifyCollectionChangedEventArgs(e.Action); |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new ArgumentOutOfRangeException(nameof(e), e, "unsupported action"); |
| 72 | + } |
| 73 | + CollectionChanged?.Invoke(this, newArgs); |
| 74 | + } |
| 75 | + |
| 76 | + // Trivial |
| 77 | + |
| 78 | + public void Clear() => impl.Clear(); |
| 79 | + public bool Contains(T item) => impl.ContainsKey(item); |
| 80 | + public void CopyTo(T[] array, int arrayIndex) => impl.Keys.CopyTo(array, arrayIndex); |
| 81 | + public int Count => impl.Count; |
| 82 | + public IEnumerator<T> GetEnumerator() => impl.Keys.GetEnumerator(); |
| 83 | + public bool IsReadOnly => impl.IsReadOnly; |
| 84 | + public bool Remove(T item) => impl.Remove(item); |
| 85 | + |
| 86 | + void ICollection<T>.Add(T item) => Add(item); |
| 87 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 88 | + |
| 89 | + // Non-trivial |
| 90 | + |
| 91 | + public bool Add(T item) |
| 92 | + { |
| 93 | + if (!impl.TryGetValue(item, out _)) |
| 94 | + { |
| 95 | + impl.Add(item, Unit.Default); |
| 96 | + return true; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public int AddRange(IEnumerable<T> items) |
| 105 | + { |
| 106 | + int cnt = 0; |
| 107 | + foreach (var item in items) |
| 108 | + if (Add(item)) |
| 109 | + cnt++; |
| 110 | + return cnt; |
| 111 | + } |
| 112 | + |
| 113 | + public void UnionWith(IEnumerable<T> other) |
| 114 | + { |
| 115 | + foreach (var item in other) |
| 116 | + Add(item); |
| 117 | + } |
| 118 | + |
| 119 | + public void IntersectWith(IEnumerable<T> other) |
| 120 | + { |
| 121 | + foreach (var item in other) |
| 122 | + if (!Contains(item)) |
| 123 | + Remove(item); |
| 124 | + } |
| 125 | + |
| 126 | + public void ExceptWith(IEnumerable<T> other) |
| 127 | + { |
| 128 | + throw new NotImplementedException(); |
| 129 | + } |
| 130 | + |
| 131 | + public void SymmetricExceptWith(IEnumerable<T> other) |
| 132 | + { |
| 133 | + throw new NotImplementedException(); |
| 134 | + } |
| 135 | + |
| 136 | + public bool IsSubsetOf(IEnumerable<T> other) |
| 137 | + { |
| 138 | + foreach (var item in this) |
| 139 | + if (!other.Contains(item)) |
| 140 | + return false; |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + public bool IsSupersetOf(IEnumerable<T> other) |
| 145 | + { |
| 146 | + foreach (var item in other) |
| 147 | + if (!Contains(item)) |
| 148 | + return false; |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + public bool IsProperSupersetOf(IEnumerable<T> other) |
| 153 | + { |
| 154 | + if (Count <= other.Count()) |
| 155 | + return false; |
| 156 | + return IsSupersetOf(other); |
| 157 | + } |
| 158 | + |
| 159 | + public bool IsProperSubsetOf(IEnumerable<T> other) |
| 160 | + { |
| 161 | + if (other.Count() <= Count) |
| 162 | + return false; |
| 163 | + return IsSubsetOf(other); |
| 164 | + } |
| 165 | + |
| 166 | + public bool Overlaps(IEnumerable<T> other) |
| 167 | + { |
| 168 | + foreach (var item in other) |
| 169 | + if (Contains(item)) |
| 170 | + return true; |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + public bool SetEquals(IEnumerable<T> other) |
| 175 | + { |
| 176 | + if (Count != other.Count()) |
| 177 | + return false; |
| 178 | + foreach (var item in other) |
| 179 | + if (!Contains(item)) |
| 180 | + return false; |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments