ConcurrentHashMap.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. _table = new Dictionary<T, TU> ();
  19. }
  20. }
  21. public override bool ContainsKey (object name)
  22. {
  23. return _table.ContainsKey ((T)name);
  24. }
  25. public override ICollection<KeyValuePair<T, TU>> EntrySet ()
  26. {
  27. return this;
  28. }
  29. public override TU Get (object key)
  30. {
  31. TU local;
  32. _table.TryGetValue ((T)key, out local);
  33. return local;
  34. }
  35. protected override IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
  36. {
  37. return _table.GetEnumerator ();
  38. }
  39. public override bool IsEmpty ()
  40. {
  41. return _table.Count == 0;
  42. }
  43. public override TU Put (T key, TU value)
  44. {
  45. lock (_table) {
  46. TU old = Get (key);
  47. Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
  48. newTable[key] = value;
  49. _table = newTable;
  50. return old;
  51. }
  52. }
  53. public TU PutIfAbsent (T key, TU value)
  54. {
  55. lock (_table) {
  56. if (!ContainsKey (key)) {
  57. Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
  58. newTable[key] = value;
  59. _table = newTable;
  60. return value;
  61. }
  62. return Get (key);
  63. }
  64. }
  65. public override TU Remove (object key)
  66. {
  67. lock (_table) {
  68. TU old = Get ((T)key);
  69. Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
  70. newTable.Remove ((T)key);
  71. _table = newTable;
  72. return old;
  73. }
  74. }
  75. public bool Remove (object key, object value)
  76. {
  77. lock (_table) {
  78. if (ContainsKey (key) && value.Equals (Get (key))) {
  79. Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
  80. newTable.Remove ((T)key);
  81. _table = newTable;
  82. return true;
  83. }
  84. return false;
  85. }
  86. }
  87. public bool Replace (T key, TU oldValue, TU newValue)
  88. {
  89. lock (_table) {
  90. if (ContainsKey (key) && oldValue.Equals (Get (key))) {
  91. Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
  92. newTable[key] = newValue;
  93. _table = newTable;
  94. return true;
  95. }
  96. return false;
  97. }
  98. }
  99. public override IEnumerable<T> Keys {
  100. get { return _table.Keys; }
  101. }
  102. public override IEnumerable<TU> Values {
  103. get { return _table.Values; }
  104. }
  105. }
  106. }