BitMap.cs 5.0 KB

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