ConcurrentHashMap.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections.Generic;
  2. namespace SharpCifs.Util.Sharpen
  3. {
  4. internal class ConcurrentHashMap<T, TU> : AbstractMap<T, TU>, IConcurrentMap<T, TU>
  5. {
  6. private Dictionary<T, TU> _table;
  7. public ConcurrentHashMap()
  8. {
  9. _table = new Dictionary<T, TU>();
  10. }
  11. public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
  12. {
  13. _table = new Dictionary<T, TU>(initialCapacity);
  14. }
  15. public override void Clear()
  16. {
  17. lock (_table)
  18. {
  19. _table = new Dictionary<T, TU>();
  20. }
  21. }
  22. public override bool ContainsKey(object name)
  23. {
  24. return _table.ContainsKey((T)name);
  25. }
  26. public override ICollection<KeyValuePair<T, TU>> EntrySet()
  27. {
  28. return this;
  29. }
  30. public override TU Get(object key)
  31. {
  32. TU local;
  33. _table.TryGetValue((T)key, out local);
  34. return local;
  35. }
  36. protected override IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator()
  37. {
  38. return _table.GetEnumerator();
  39. }
  40. public override bool IsEmpty()
  41. {
  42. return _table.Count == 0;
  43. }
  44. public override TU Put(T key, TU value)
  45. {
  46. lock (_table)
  47. {
  48. TU old = Get(key);
  49. Dictionary<T, TU> newTable = new Dictionary<T, TU>(_table);
  50. newTable[key] = value;
  51. _table = newTable;
  52. return old;
  53. }
  54. }
  55. public TU PutIfAbsent(T key, TU value)
  56. {
  57. lock (_table)
  58. {
  59. if (!ContainsKey(key))
  60. {
  61. Dictionary<T, TU> newTable = new Dictionary<T, TU>(_table);
  62. newTable[key] = value;
  63. _table = newTable;
  64. return value;
  65. }
  66. return Get(key);
  67. }
  68. }
  69. public override TU Remove(object key)
  70. {
  71. lock (_table)
  72. {
  73. TU old = Get((T)key);
  74. Dictionary<T, TU> newTable = new Dictionary<T, TU>(_table);
  75. newTable.Remove((T)key);
  76. _table = newTable;
  77. return old;
  78. }
  79. }
  80. public bool Remove(object key, object value)
  81. {
  82. lock (_table)
  83. {
  84. if (ContainsKey(key) && value.Equals(Get(key)))
  85. {
  86. Dictionary<T, TU> newTable = new Dictionary<T, TU>(_table);
  87. newTable.Remove((T)key);
  88. _table = newTable;
  89. return true;
  90. }
  91. return false;
  92. }
  93. }
  94. public bool Replace(T key, TU oldValue, TU newValue)
  95. {
  96. lock (_table)
  97. {
  98. if (ContainsKey(key) && oldValue.Equals(Get(key)))
  99. {
  100. Dictionary<T, TU> newTable = new Dictionary<T, TU>(_table);
  101. newTable[key] = newValue;
  102. _table = newTable;
  103. return true;
  104. }
  105. return false;
  106. }
  107. }
  108. public override IEnumerable<T> Keys
  109. {
  110. get { return _table.Keys; }
  111. }
  112. public override IEnumerable<TU> Values
  113. {
  114. get { return _table.Values; }
  115. }
  116. }
  117. }