Guest User

Untitled

a guest
Nov 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. [DebuggerTypeProxy(typeof(HashSetDebugViewInt<>))]
  2. [DebuggerDisplay("Count = {Count}")]
  3. [Serializable]
  4. public class BigDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
  5. {
  6. public BigSet<TKey> Keys;
  7. public int Resizes;
  8. public BigArray<TValue> Values;
  9. public BigDictionary() : this(BigArray<TKey>.Granularity, new BigComparer<TKey>())
  10. {
  11. }
  12. public BigDictionary(ulong size) : this(size, new BigComparer<TKey>())
  13. {
  14. }
  15. public BigDictionary(ulong size, IBigEqualityComparer<TKey> comparer)
  16. {
  17. if(comparer == null) comparer = new BigComparer<TKey>();
  18. Keys = new BigSet<TKey>(size);
  19. Values = new BigArray<TValue>(size);
  20. Keys._comparer = comparer;
  21. }
  22. public BigDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IBigEqualityComparer<TKey> comparer = null)
  23. {
  24. if(comparer == null) comparer = new BigComparer<TKey>();
  25. Keys._comparer = comparer;
  26. foreach(var kp in collection)
  27. Add(kp.Key, kp.Value);
  28. }
  29. public ulong Count => Keys.Count;
  30. public TValue this[TKey key]
  31. {
  32. get
  33. {
  34. var pos = Keys.FindEntry(key);
  35. return pos == ulong.MaxValue ? default : Values[pos];
  36. }
  37. set => Add(key, value);
  38. }
  39. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  40. {
  41. return new Enumerator(this);
  42. }
  43. IEnumerator IEnumerable.GetEnumerator()
  44. {
  45. return new Enumerator(this);
  46. }
  47. public Enumerator GetEnumerator()
  48. {
  49. return new Enumerator(this);
  50. }
  51. public bool Add(TKey key, TValue value)
  52. {
  53. if(!Keys.Insert(key, true))
  54. {
  55. if(Values.Length != Keys._slots.Length)
  56. {
  57. Values = Values.Copy(Keys._slots.Length);
  58. Resizes++;
  59. }
  60. Values[Keys.Position] = value;
  61. return false;
  62. }
  63. Values[Keys.Position] = value;
  64. return true;
  65. }
  66. public bool ContainsKey(TKey key)
  67. {
  68. return Keys.FindEntry(key) != ulong.MaxValue;
  69. }
  70. [Serializable]
  71. public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDictionaryEnumerator
  72. {
  73. private readonly BigDictionary<TKey, TValue> dictionary;
  74. private ulong index;
  75. internal Enumerator(BigDictionary<TKey, TValue> dictionary)
  76. {
  77. this.dictionary = dictionary;
  78. index = 0;
  79. Current = new KeyValuePair<TKey, TValue>();
  80. }
  81. public bool MoveNext()
  82. {
  83. if((uint) index < (uint) dictionary.Keys.Count)
  84. {
  85. Current = new KeyValuePair<TKey, TValue>(dictionary.Keys._slots[index].value, dictionary.Values[index]);
  86. index++;
  87. return true;
  88. }
  89. index = dictionary.Keys.Count + 1;
  90. Current = new KeyValuePair<TKey, TValue>();
  91. return false;
  92. }
  93. public KeyValuePair<TKey, TValue> Current{get; private set;}
  94. public void Dispose()
  95. {
  96. }
  97. object IEnumerator.Current
  98. {
  99. get
  100. {
  101. if(index == 0 || index == dictionary.Keys.Count + 1)
  102. throw new Exception("Invalid Index.");
  103. return new KeyValuePair<TKey, TValue>(Current.Key, Current.Value);
  104. }
  105. }
  106. void IEnumerator.Reset()
  107. {
  108. index = 0;
  109. Current = new KeyValuePair<TKey, TValue>();
  110. }
  111. DictionaryEntry IDictionaryEnumerator.Entry
  112. {
  113. get
  114. {
  115. if(index == 0 || index == dictionary.Keys.Count + 1)
  116. throw new Exception("Invalid Index.");
  117. return new DictionaryEntry(Current.Key, Current.Value);
  118. }
  119. }
  120. object IDictionaryEnumerator.Key
  121. {
  122. get
  123. {
  124. if(index == 0 || index == dictionary.Keys.Count + 1)
  125. throw new Exception("Invalid Index.");
  126. return Current.Key;
  127. }
  128. }
  129. object IDictionaryEnumerator.Value
  130. {
  131. get
  132. {
  133. if(index == 0 || index == dictionary.Keys.Count + 1)
  134. throw new Exception("Invalid Index.");
  135. return Current.Value;
  136. }
  137. }
  138. }
  139. }
Add Comment
Please, Sign In to add comment