BitMap.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. namespace ARMeilleure.Common
  7. {
  8. unsafe class BitMap : IEnumerable<int>, IDisposable
  9. {
  10. private const int IntSize = 64;
  11. private const int IntMask = IntSize - 1;
  12. private int _count;
  13. private long* _masks;
  14. private readonly Allocator _allocator;
  15. public BitMap(Allocator allocator)
  16. {
  17. _allocator = allocator;
  18. }
  19. public BitMap(Allocator allocator, int capacity) : this(allocator)
  20. {
  21. EnsureCapacity(capacity);
  22. }
  23. public bool Set(int bit)
  24. {
  25. EnsureCapacity(bit + 1);
  26. int wordIndex = bit / IntSize;
  27. int wordBit = bit & IntMask;
  28. long wordMask = 1L << wordBit;
  29. if ((_masks[wordIndex] & wordMask) != 0)
  30. {
  31. return false;
  32. }
  33. _masks[wordIndex] |= wordMask;
  34. return true;
  35. }
  36. public void Clear(int bit)
  37. {
  38. EnsureCapacity(bit + 1);
  39. int wordIndex = bit / IntSize;
  40. int wordBit = bit & IntMask;
  41. long wordMask = 1L << wordBit;
  42. _masks[wordIndex] &= ~wordMask;
  43. }
  44. public bool IsSet(int bit)
  45. {
  46. EnsureCapacity(bit + 1);
  47. int wordIndex = bit / IntSize;
  48. int wordBit = bit & IntMask;
  49. return (_masks[wordIndex] & (1L << wordBit)) != 0;
  50. }
  51. public int FindFirstUnset()
  52. {
  53. for (int index = 0; index < _count; index++)
  54. {
  55. long mask = _masks[index];
  56. if (mask != -1L)
  57. {
  58. return BitOperations.TrailingZeroCount(~mask) + index * IntSize;
  59. }
  60. }
  61. return _count * IntSize;
  62. }
  63. public bool Set(BitMap map)
  64. {
  65. EnsureCapacity(map._count * IntSize);
  66. bool modified = false;
  67. for (int index = 0; index < _count; index++)
  68. {
  69. long newValue = _masks[index] | map._masks[index];
  70. if (_masks[index] != newValue)
  71. {
  72. _masks[index] = newValue;
  73. modified = true;
  74. }
  75. }
  76. return modified;
  77. }
  78. public bool Clear(BitMap map)
  79. {
  80. EnsureCapacity(map._count * IntSize);
  81. bool modified = false;
  82. for (int index = 0; index < _count; index++)
  83. {
  84. long newValue = _masks[index] & ~map._masks[index];
  85. if (_masks[index] != newValue)
  86. {
  87. _masks[index] = newValue;
  88. modified = true;
  89. }
  90. }
  91. return modified;
  92. }
  93. private void EnsureCapacity(int size)
  94. {
  95. int count = (size + IntMask) / IntSize;
  96. if (count > _count)
  97. {
  98. var oldMask = _masks;
  99. var oldSpan = new Span<long>(_masks, _count);
  100. _masks = _allocator.Allocate<long>((uint)count);
  101. _count = count;
  102. var newSpan = new Span<long>(_masks, _count);
  103. oldSpan.CopyTo(newSpan);
  104. newSpan.Slice(oldSpan.Length).Clear();
  105. _allocator.Free(oldMask);
  106. }
  107. }
  108. public void Dispose()
  109. {
  110. if (_masks != null)
  111. {
  112. _allocator.Free(_masks);
  113. _masks = null;
  114. }
  115. }
  116. IEnumerator IEnumerable.GetEnumerator()
  117. {
  118. return GetEnumerator();
  119. }
  120. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  121. {
  122. return GetEnumerator();
  123. }
  124. public Enumerator GetEnumerator()
  125. {
  126. return new Enumerator(this);
  127. }
  128. public struct Enumerator : IEnumerator<int>
  129. {
  130. private long _index;
  131. private long _mask;
  132. private int _bit;
  133. private readonly BitMap _map;
  134. public int Current => (int)_index * IntSize + _bit;
  135. object IEnumerator.Current => Current;
  136. public Enumerator(BitMap map)
  137. {
  138. _index = -1;
  139. _mask = 0;
  140. _bit = 0;
  141. _map = map;
  142. }
  143. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  144. public bool MoveNext()
  145. {
  146. if (_mask != 0)
  147. {
  148. _mask &= ~(1L << _bit);
  149. }
  150. // Manually hoist these loads, because RyuJIT does not.
  151. long count = (uint)_map._count;
  152. long* masks = _map._masks;
  153. while (_mask == 0)
  154. {
  155. if (++_index >= count)
  156. {
  157. return false;
  158. }
  159. _mask = masks[_index];
  160. }
  161. _bit = BitOperations.TrailingZeroCount(_mask);
  162. return true;
  163. }
  164. public void Reset() { }
  165. public void Dispose() { }
  166. }
  167. }
  168. }