BitMap.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. namespace ARMeilleure.Common
  5. {
  6. class BitMap : IEnumerator<int>
  7. {
  8. private const int IntSize = 64;
  9. private const int IntMask = IntSize - 1;
  10. private 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 bool Set(BitMap map)
  71. {
  72. EnsureCapacity(map._masks.Count * IntSize);
  73. bool modified = false;
  74. for (int index = 0; index < _masks.Count; index++)
  75. {
  76. long newValue = _masks[index] | map._masks[index];
  77. if (_masks[index] != newValue)
  78. {
  79. _masks[index] = newValue;
  80. modified = true;
  81. }
  82. }
  83. return modified;
  84. }
  85. public bool Clear(BitMap map)
  86. {
  87. EnsureCapacity(map._masks.Count * IntSize);
  88. bool modified = false;
  89. for (int index = 0; index < _masks.Count; index++)
  90. {
  91. long newValue = _masks[index] & ~map._masks[index];
  92. if (_masks[index] != newValue)
  93. {
  94. _masks[index] = newValue;
  95. modified = true;
  96. }
  97. }
  98. return modified;
  99. }
  100. #region IEnumerable<long> Methods
  101. // Note: The bit enumerator is embedded in this class to avoid creating garbage when enumerating.
  102. private void EnsureCapacity(int size)
  103. {
  104. while (_masks.Count * IntSize < size)
  105. {
  106. _masks.Add(0);
  107. }
  108. }
  109. public IEnumerator<int> GetEnumerator()
  110. {
  111. Reset();
  112. return this;
  113. }
  114. public bool MoveNext()
  115. {
  116. if (_enumMask != 0)
  117. {
  118. _enumMask &= ~(1L << _enumBit);
  119. }
  120. while (_enumMask == 0)
  121. {
  122. if (++_enumIndex >= _masks.Count)
  123. {
  124. return false;
  125. }
  126. _enumMask = _masks[_enumIndex];
  127. }
  128. _enumBit = BitOperations.TrailingZeroCount(_enumMask);
  129. return true;
  130. }
  131. public void Reset()
  132. {
  133. _enumIndex = -1;
  134. _enumMask = 0;
  135. _enumBit = 0;
  136. }
  137. public void Dispose() { }
  138. #endregion
  139. }
  140. }