BitMap.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 void 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. }
  42. public bool Set(int bit)
  43. {
  44. EnsureCapacity(bit + 1);
  45. int wordIndex = bit / IntSize;
  46. int wordBit = bit & IntMask;
  47. long wordMask = 1L << wordBit;
  48. if ((_masks[wordIndex] & wordMask) != 0)
  49. {
  50. return false;
  51. }
  52. _masks[wordIndex] |= wordMask;
  53. return true;
  54. }
  55. public void Clear(int bit)
  56. {
  57. EnsureCapacity(bit + 1);
  58. int wordIndex = bit / IntSize;
  59. int wordBit = bit & IntMask;
  60. long wordMask = 1L << wordBit;
  61. _masks[wordIndex] &= ~wordMask;
  62. }
  63. public bool IsSet(int bit)
  64. {
  65. EnsureCapacity(bit + 1);
  66. int wordIndex = bit / IntSize;
  67. int wordBit = bit & IntMask;
  68. return (_masks[wordIndex] & (1L << wordBit)) != 0;
  69. }
  70. public int FindFirstUnset()
  71. {
  72. for (int index = 0; index < _masks.Count; index++)
  73. {
  74. long mask = _masks[index];
  75. if (mask != -1L)
  76. {
  77. return BitOperations.TrailingZeroCount(~mask) + index * IntSize;
  78. }
  79. }
  80. return _masks.Count * IntSize;
  81. }
  82. public bool Set(BitMap map)
  83. {
  84. EnsureCapacity(map._masks.Count * IntSize);
  85. bool modified = false;
  86. for (int index = 0; index < _masks.Count; index++)
  87. {
  88. long newValue = _masks[index] | map._masks[index];
  89. if (_masks[index] != newValue)
  90. {
  91. _masks[index] = newValue;
  92. modified = true;
  93. }
  94. }
  95. return modified;
  96. }
  97. public bool Clear(BitMap map)
  98. {
  99. EnsureCapacity(map._masks.Count * IntSize);
  100. bool modified = false;
  101. for (int index = 0; index < _masks.Count; index++)
  102. {
  103. long newValue = _masks[index] & ~map._masks[index];
  104. if (_masks[index] != newValue)
  105. {
  106. _masks[index] = newValue;
  107. modified = true;
  108. }
  109. }
  110. return modified;
  111. }
  112. #region IEnumerable<long> Methods
  113. // Note: The bit enumerator is embedded in this class to avoid creating garbage when enumerating.
  114. private void EnsureCapacity(int size)
  115. {
  116. while (_masks.Count * IntSize < size)
  117. {
  118. _masks.Add(0);
  119. }
  120. }
  121. IEnumerator IEnumerable.GetEnumerator()
  122. {
  123. return GetEnumerator();
  124. }
  125. public IEnumerator<int> GetEnumerator()
  126. {
  127. Reset();
  128. return this;
  129. }
  130. public bool MoveNext()
  131. {
  132. if (_enumMask != 0)
  133. {
  134. _enumMask &= ~(1L << _enumBit);
  135. }
  136. while (_enumMask == 0)
  137. {
  138. if (++_enumIndex >= _masks.Count)
  139. {
  140. return false;
  141. }
  142. _enumMask = _masks[_enumIndex];
  143. }
  144. _enumBit = BitOperations.TrailingZeroCount(_enumMask);
  145. return true;
  146. }
  147. public void Reset()
  148. {
  149. _enumIndex = -1;
  150. _enumMask = 0;
  151. _enumBit = 0;
  152. }
  153. public void Dispose() { }
  154. #endregion
  155. }
  156. }