HashTableSlim.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. interface IRefEquatable<T>
  6. {
  7. bool Equals(ref T other);
  8. }
  9. class HashTableSlim<K, V> where K : IRefEquatable<K>
  10. {
  11. private const int TotalBuckets = 16; // Must be power of 2
  12. private const int TotalBucketsMask = TotalBuckets - 1;
  13. private struct Entry
  14. {
  15. public K Key;
  16. public V Value;
  17. }
  18. private readonly Entry[][] _hashTable = new Entry[TotalBuckets][];
  19. public IEnumerable<K> Keys
  20. {
  21. get
  22. {
  23. foreach (Entry[] bucket in _hashTable)
  24. {
  25. if (bucket != null)
  26. {
  27. foreach (Entry entry in bucket)
  28. {
  29. yield return entry.Key;
  30. }
  31. }
  32. }
  33. }
  34. }
  35. public IEnumerable<V> Values
  36. {
  37. get
  38. {
  39. foreach (Entry[] bucket in _hashTable)
  40. {
  41. if (bucket != null)
  42. {
  43. foreach (Entry entry in bucket)
  44. {
  45. yield return entry.Value;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. public void Add(ref K key, V value)
  52. {
  53. var entry = new Entry()
  54. {
  55. Key = key,
  56. Value = value
  57. };
  58. int hashCode = key.GetHashCode();
  59. int bucketIndex = hashCode & TotalBucketsMask;
  60. var bucket = _hashTable[bucketIndex];
  61. if (bucket != null)
  62. {
  63. int index = bucket.Length;
  64. Array.Resize(ref _hashTable[bucketIndex], index + 1);
  65. _hashTable[bucketIndex][index] = entry;
  66. }
  67. else
  68. {
  69. _hashTable[bucketIndex] = new Entry[]
  70. {
  71. entry
  72. };
  73. }
  74. }
  75. public bool TryGetValue(ref K key, out V value)
  76. {
  77. int hashCode = key.GetHashCode();
  78. var bucket = _hashTable[hashCode & TotalBucketsMask];
  79. if (bucket != null)
  80. {
  81. for (int i = 0; i < bucket.Length; i++)
  82. {
  83. ref var entry = ref bucket[i];
  84. if (entry.Key.Equals(ref key))
  85. {
  86. value = entry.Value;
  87. return true;
  88. }
  89. }
  90. }
  91. value = default;
  92. return false;
  93. }
  94. }
  95. }