PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/windows-phone-8/ConcurrentHashtable/WeakValueDictionary.cs

https://github.com/Sfyler/c-sharp
C# | 208 lines | 164 code | 36 blank | 8 comment | 2 complexity | b7a4d0cd7053fd36fb2e156c42d92bb3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Copyright 2008 The 'A Concurrent Hashtable' development team
  3. (http://www.codeplex.com/CH/People/ProjectPeople.aspx)
  4. This library is licensed under the GNU Library General Public License (LGPL). You should
  5. have received a copy of the license along with the source code. If not, an online copy
  6. of the license can be found at http://www.codeplex.com/CH/license.
  7. */
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Collections;
  13. using System.Runtime.Serialization;
  14. using System.Security;
  15. namespace TvdP.Collections
  16. {
  17. #if !SILVERLIGHT
  18. [Serializable]
  19. #endif
  20. public class WeakValueDictionary<TStrongKey, TValue> : DictionaryBase<TStrongKey, TValue>
  21. #if !SILVERLIGHT
  22. , ISerializable
  23. #endif
  24. where TValue : class
  25. {
  26. class InternalWeakDictionary :
  27. InternalWeakDictionaryWeakValueBase<
  28. StrongKey<TStrongKey>,
  29. TStrongKey,
  30. TValue,
  31. Stacktype<TStrongKey>
  32. >
  33. {
  34. public InternalWeakDictionary(int concurrencyLevel, int capacity, KeyComparer<TStrongKey> keyComparer)
  35. : base(concurrencyLevel, capacity, keyComparer)
  36. { _comparer = keyComparer; }
  37. public InternalWeakDictionary(KeyComparer<TStrongKey> keyComparer)
  38. : base(keyComparer)
  39. { _comparer = keyComparer; }
  40. public KeyComparer<TStrongKey> _comparer;
  41. protected override StrongKey<TStrongKey> FromExternalKeyToSearchKey(TStrongKey externalKey)
  42. { return new StrongKey<TStrongKey>() { _element = externalKey }; }
  43. protected override StrongKey<TStrongKey> FromExternalKeyToStorageKey(TStrongKey externalKey)
  44. { return new StrongKey<TStrongKey>() { _element = externalKey }; }
  45. protected override StrongKey<TStrongKey> FromStackKeyToSearchKey(Stacktype<TStrongKey> externalKey)
  46. { return new StrongKey<TStrongKey>() { _element = externalKey.Item1 }; }
  47. protected override StrongKey<TStrongKey> FromStackKeyToStorageKey(Stacktype<TStrongKey> externalKey)
  48. { return new StrongKey<TStrongKey>() { _element = externalKey.Item1 }; }
  49. protected override bool FromInternalKeyToExternalKey(StrongKey<TStrongKey> internalKey, out TStrongKey externalKey)
  50. {
  51. externalKey = internalKey._element;
  52. return true;
  53. }
  54. protected override bool FromInternalKeyToStackKey(StrongKey<TStrongKey> internalKey, out Stacktype<TStrongKey> externalKey)
  55. {
  56. externalKey = Stacktype.Create(internalKey._element);
  57. return true;
  58. }
  59. }
  60. readonly InternalWeakDictionary _internalDictionary;
  61. protected override IDictionary<TStrongKey, TValue> InternalDictionary
  62. { get { return _internalDictionary; } }
  63. #if !SILVERLIGHT
  64. WeakValueDictionary(SerializationInfo serializationInfo, StreamingContext streamingContext)
  65. {
  66. var comparer = (KeyComparer<TStrongKey>)serializationInfo.GetValue("Comparer", typeof(KeyComparer<TStrongKey>));
  67. var items = (List<KeyValuePair<TStrongKey, TValue>>)serializationInfo.GetValue("Items", typeof(List<KeyValuePair<TStrongKey, TValue>>));
  68. _internalDictionary = new InternalWeakDictionary(comparer);
  69. _internalDictionary.InsertContents(items);
  70. }
  71. #region ISerializable Members
  72. [SecurityCritical]
  73. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  74. {
  75. info.AddValue("Comparer", _internalDictionary._comparer);
  76. info.AddValue("Items", _internalDictionary.GetContents());
  77. }
  78. #endregion
  79. #endif
  80. public WeakValueDictionary()
  81. : this(EqualityComparer<TStrongKey>.Default)
  82. {}
  83. public WeakValueDictionary(IEqualityComparer<TStrongKey> strongKeyComparer)
  84. : this(Enumerable.Empty<KeyValuePair<TStrongKey,TValue>>(), strongKeyComparer)
  85. {}
  86. public WeakValueDictionary(IEnumerable<KeyValuePair<TStrongKey,TValue>> collection)
  87. : this(collection, EqualityComparer<TStrongKey>.Default)
  88. {}
  89. public WeakValueDictionary(IEnumerable<KeyValuePair<TStrongKey, TValue>> collection, IEqualityComparer<TStrongKey> strongKeyComparer)
  90. {
  91. _internalDictionary =
  92. new InternalWeakDictionary(
  93. new KeyComparer<TStrongKey>(strongKeyComparer)
  94. )
  95. ;
  96. _internalDictionary.InsertContents(collection);
  97. }
  98. public WeakValueDictionary(int concurrencyLevel, int capacity)
  99. : this(concurrencyLevel, capacity, EqualityComparer<TStrongKey>.Default)
  100. {}
  101. public WeakValueDictionary(int concurrencyLevel, IEnumerable<KeyValuePair<TStrongKey, TValue>> collection, IEqualityComparer<TStrongKey> strongKeyComparer)
  102. {
  103. var contentsList = collection.ToList();
  104. _internalDictionary =
  105. new InternalWeakDictionary(
  106. concurrencyLevel,
  107. contentsList.Count,
  108. new KeyComparer<TStrongKey>(strongKeyComparer)
  109. )
  110. ;
  111. _internalDictionary.InsertContents(contentsList);
  112. }
  113. public WeakValueDictionary(int concurrencyLevel, int capacity, IEqualityComparer<TStrongKey> strongKeyComparer)
  114. {
  115. _internalDictionary =
  116. new InternalWeakDictionary(
  117. concurrencyLevel,
  118. capacity,
  119. new KeyComparer<TStrongKey>(strongKeyComparer)
  120. )
  121. ;
  122. }
  123. public bool ContainsKey(TStrongKey strongKey)
  124. { return _internalDictionary.ContainsKey(Stacktype.Create(strongKey)); }
  125. public bool TryGetValue(TStrongKey strongKey, out TValue value)
  126. { return _internalDictionary.TryGetValue(Stacktype.Create(strongKey), out value); }
  127. public TValue this[TStrongKey strongKey]
  128. {
  129. get { return _internalDictionary.GetItem(Stacktype.Create(strongKey)); }
  130. set { _internalDictionary.SetItem(Stacktype.Create(strongKey), value); }
  131. }
  132. public bool IsEmpty
  133. { get { return _internalDictionary.IsEmpty; } }
  134. public TValue AddOrUpdate(TStrongKey strongKey, Func<TStrongKey, TValue> addValueFactory, Func<TStrongKey, TValue, TValue> updateValueFactory)
  135. {
  136. return
  137. _internalDictionary.AddOrUpdate(
  138. Stacktype.Create(strongKey),
  139. ht => addValueFactory(ht.Item1),
  140. (ht, v) => updateValueFactory(ht.Item1, v)
  141. )
  142. ;
  143. }
  144. public TValue AddOrUpdate(TStrongKey strongKey, TValue addValue, Func<TStrongKey, TValue, TValue> updateValueFactory)
  145. {
  146. return
  147. _internalDictionary.AddOrUpdate(
  148. Stacktype.Create(strongKey),
  149. addValue,
  150. (ht, v) => updateValueFactory(ht.Item1, v)
  151. )
  152. ;
  153. }
  154. public TValue GetOrAdd(TStrongKey strongKey, TValue value)
  155. { return _internalDictionary.GetOrAdd(Stacktype.Create(strongKey), value); }
  156. public TValue GetOrAdd(TStrongKey strongKey, Func<TStrongKey, TValue> valueFactory)
  157. {
  158. if (null == valueFactory)
  159. throw new ArgumentNullException("valueFactory");
  160. return _internalDictionary.GetOrAdd(Stacktype.Create(strongKey), ht => valueFactory(ht.Item1));
  161. }
  162. public KeyValuePair<TStrongKey, TValue>[] ToArray()
  163. { return _internalDictionary.ToArray(); }
  164. public bool TryAdd(TStrongKey strongKey, TValue value)
  165. { return _internalDictionary.TryAdd(Stacktype.Create(strongKey), value); }
  166. public bool TryRemove(TStrongKey strongKey, out TValue value)
  167. { return _internalDictionary.TryRemove(Stacktype.Create(strongKey), out value); }
  168. public bool TryUpdate(TStrongKey strongKey, TValue newValue, TValue comparisonValue)
  169. { return _internalDictionary.TryUpdate(Stacktype.Create(strongKey), newValue, comparisonValue); }
  170. }
  171. }