| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Runtime.Collections |
| | | 10 | | { |
| | | 11 | | internal class GenericHashtable<TKey, TValue> : IDictionary<TKey, TValue> |
| | | 12 | | { |
| | | 13 | | private Hashtable _hashtable; |
| | | 14 | | |
| | 0 | 15 | | public GenericHashtable() |
| | | 16 | | { |
| | 0 | 17 | | _hashtable = Hashtable.Synchronized(new Hashtable()); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public TValue this[TKey key] |
| | | 21 | | { |
| | | 22 | | get |
| | | 23 | | { |
| | 0 | 24 | | if (key == null) throw new ArgumentNullException(nameof(key)); |
| | 0 | 25 | | if (!_hashtable.ContainsKey(key)) |
| | | 26 | | { |
| | 0 | 27 | | throw new KeyNotFoundException(); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | return (TValue)_hashtable[key]; |
| | | 31 | | } |
| | 0 | 32 | | set { _hashtable[key] = value; } |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | public ICollection<TKey> Keys => _hashtable.Keys.Cast<TKey>().ToList(); |
| | 0 | 36 | | public ICollection<TValue> Values => _hashtable.Values.Cast<TValue>().ToList(); |
| | 0 | 37 | | public int Count => _hashtable.Count; |
| | 0 | 38 | | public bool IsReadOnly => _hashtable.IsReadOnly; |
| | 0 | 39 | | public void Add(TKey key, TValue value) => _hashtable.Add(key, value); |
| | 0 | 40 | | public void Add(KeyValuePair<TKey, TValue> item) => _hashtable.Add(item.Key, item.Value); |
| | 0 | 41 | | public void Clear() => _hashtable.Clear(); |
| | | 42 | | |
| | | 43 | | public bool Contains(KeyValuePair<TKey, TValue> item) |
| | | 44 | | { |
| | | 45 | | // Two reads from table need to be treated atomic otherwise Contains can return true |
| | | 46 | | // and this this[item.Key] might not find the key if a remove was in progress. |
| | 0 | 47 | | lock (_hashtable.SyncRoot) |
| | | 48 | | { |
| | 0 | 49 | | return _hashtable.Contains(item.Key) && this[item.Key].Equals(item.Value); |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | public bool ContainsKey(TKey key) => _hashtable.ContainsKey(key); |
| | | 54 | | |
| | | 55 | | public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) |
| | | 56 | | { |
| | 0 | 57 | | if (array == null) throw new ArgumentNullException(nameof(array)); |
| | 0 | 58 | | if ((uint)arrayIndex > (uint)array.Length || array.Length - arrayIndex < Count) |
| | 0 | 59 | | throw new ArgumentOutOfRangeException(nameof(arrayIndex)); |
| | | 60 | | |
| | 0 | 61 | | foreach (DictionaryEntry entry in _hashtable) |
| | | 62 | | { |
| | 0 | 63 | | var keyValuePair = new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value); |
| | 0 | 64 | | array[arrayIndex++] = keyValuePair; |
| | | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| | | 69 | | { |
| | 0 | 70 | | foreach (DictionaryEntry entry in _hashtable) |
| | | 71 | | { |
| | 0 | 72 | | yield return new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value); |
| | | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public bool Remove(TKey key) |
| | | 77 | | { |
| | 0 | 78 | | lock (_hashtable.SyncRoot) |
| | | 79 | | { |
| | 0 | 80 | | if (!ContainsKey(key)) |
| | 0 | 81 | | return false; |
| | | 82 | | |
| | 0 | 83 | | _hashtable.Remove(key); |
| | 0 | 84 | | return true; |
| | | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | public bool Remove(KeyValuePair<TKey, TValue> item) |
| | | 89 | | { |
| | | 90 | | // Need to wrap in lock otherwise the value could change between call to Contains(item) and |
| | | 91 | | // the remove call |
| | 0 | 92 | | lock (_hashtable.SyncRoot) |
| | | 93 | | { |
| | 0 | 94 | | if (!Contains(item)) |
| | 0 | 95 | | return false; |
| | | 96 | | |
| | 0 | 97 | | _hashtable.Remove(item.Key); |
| | 0 | 98 | | return true; |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public bool TryGetValue(TKey key, out TValue value) |
| | | 103 | | { |
| | | 104 | | // Lock free optimization when TValue is a value type |
| | 0 | 105 | | if (typeof(TValue).IsValueType) |
| | | 106 | | { |
| | 0 | 107 | | object obj = _hashtable[key]; |
| | | 108 | | // Can only be null if not found, otherwise if found will always be a boxed value type |
| | 0 | 109 | | if (obj != null) |
| | | 110 | | { |
| | 0 | 111 | | value = (TValue)obj; |
| | 0 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | value = default; |
| | 0 | 116 | | return false; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | // When TValue is nullable, it might be in the Hashtable with a value of null so need to do |
| | | 120 | | // 2 lookups, one to see if it's there, another to get the (possibly null) value. Needs to |
| | | 121 | | // be atomic otherwise table could get modified between the 2 reads so using lock. |
| | 0 | 122 | | lock (_hashtable.SyncRoot) |
| | | 123 | | { |
| | | 124 | | // Need to call ContainsKey as value might be null so getting value and null checking |
| | | 125 | | // is insufficient. |
| | 0 | 126 | | if (!_hashtable.ContainsKey(key)) |
| | | 127 | | { |
| | 0 | 128 | | value = default; |
| | 0 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | value = this[key]; |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 138 | | { |
| | 0 | 139 | | return GetEnumerator(); |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |