19 using System.Collections;
20 using System.Collections.Generic;
22 using System.Threading;
26 namespace Lucene.Net.Support.Compatibility
36 public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>
38 private readonly
object _lockObj =
new object();
39 private readonly Dictionary<TKey, TValue> _dictInst;
41 public ConcurrentDictionary()
45 public ConcurrentDictionary(
int capacity)
46 : this(capacity, EqualityComparer<TKey>.Default)
49 public ConcurrentDictionary(
int capacity, IEqualityComparer<TKey> comparer)
51 _dictInst =
new Dictionary<TKey, TValue>(capacity, comparer);
54 public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> keyValuePairs)
57 foreach(var value
in keyValuePairs)
59 _dictInst.Add(value.Key, value.Value);
63 #region Concurrent Dictionary Special Methods
65 public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
69 if(_dictInst.ContainsKey(key))
71 _dictInst[key] = updateValueFactory(key, _dictInst[key]);
75 _dictInst[key] = addValueFactory(key);
78 return _dictInst[key];
82 public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
86 if (_dictInst.ContainsKey(key))
88 _dictInst[key] = updateValueFactory(key, _dictInst[key]);
92 _dictInst[key] = addValue;
95 return _dictInst[key];
99 public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
103 if (!_dictInst.ContainsKey(key))
105 _dictInst[key] = valueFactory(key);
108 return _dictInst[key];
112 public TValue GetOrAdd(TKey key, TValue value)
116 if (!_dictInst.ContainsKey(key))
118 _dictInst[key] = value;
121 return _dictInst[key];
125 public bool TryAdd(TKey key, TValue value)
129 if (_dictInst.ContainsKey(key))
134 _dictInst[key] = value;
139 public bool TryRemove(TKey key, out TValue value)
143 if (_dictInst.ContainsKey(key))
145 value = _dictInst[key];
146 _dictInst.Remove(key);
150 value =
default(TValue);
155 public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
159 if (_dictInst.ContainsKey(key) && _dictInst[key].Equals(comparisonValue))
161 _dictInst[key] = newValue;
171 #region IDictionary Methods
175 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
179 return _dictInst.ToList().GetEnumerator();
183 public bool TryGetValue(TKey key, out TValue value)
187 return _dictInst.TryGetValue(key, out value);
191 IEnumerator IEnumerable.GetEnumerator()
193 return GetEnumerator();
210 return _dictInst.Count;
215 public bool ContainsKey(TKey key)
219 return _dictInst.ContainsKey(key);
223 public TValue
this[TKey key]
229 return _dictInst[key];
236 _dictInst[key] = value;
241 public ICollection<TKey> Keys
243 get {
return _dictInst.Keys.ToArray(); }
246 public ICollection<TValue> Values
248 get {
return _dictInst.Values.ToArray(); }
253 #region Explicit Interface Definitions
255 bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
257 get {
return ((ICollection<KeyValuePair<TKey, TValue>>) _dictInst).IsReadOnly; }
260 void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
264 _dictInst.Add(key, value);
268 bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
272 return _dictInst.Contains(item);
276 bool IDictionary<TKey, TValue>.Remove(TKey key)
280 return _dictInst.Remove(key);
284 void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
288 ((ICollection<KeyValuePair<TKey, TValue>>)_dictInst).Add(item);
292 void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array,
int arrayIndex)
296 ((ICollection<KeyValuePair<TKey, TValue>>)_dictInst).CopyTo(array, arrayIndex);
300 bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
304 return ((ICollection<KeyValuePair<TKey, TValue>>)_dictInst).Remove(item);