BitMap.cs 4.7 KB

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