diff options
Diffstat (limited to 'ARMeilleure/Common/BitMap.cs')
-rw-r--r-- | ARMeilleure/Common/BitMap.cs | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/ARMeilleure/Common/BitMap.cs b/ARMeilleure/Common/BitMap.cs index 35100536..b1c9a673 100644 --- a/ARMeilleure/Common/BitMap.cs +++ b/ARMeilleure/Common/BitMap.cs @@ -4,12 +4,12 @@ using System.Numerics; namespace ARMeilleure.Common { - class BitMap : IEnumerator<int> + class BitMap : IEnumerator<int>, IEnumerable<int> { private const int IntSize = 64; private const int IntMask = IntSize - 1; - private List<long> _masks; + private readonly List<long> _masks; private int _enumIndex; private long _enumMask; @@ -57,7 +57,7 @@ namespace ARMeilleure.Common EnsureCapacity(bit + 1); int wordIndex = bit / IntSize; - int wordBit = bit & IntMask; + int wordBit = bit & IntMask; long wordMask = 1L << wordBit; @@ -76,7 +76,7 @@ namespace ARMeilleure.Common EnsureCapacity(bit + 1); int wordIndex = bit / IntSize; - int wordBit = bit & IntMask; + int wordBit = bit & IntMask; long wordMask = 1L << wordBit; @@ -88,11 +88,26 @@ namespace ARMeilleure.Common EnsureCapacity(bit + 1); int wordIndex = bit / IntSize; - int wordBit = bit & IntMask; + int wordBit = bit & IntMask; return (_masks[wordIndex] & (1L << wordBit)) != 0; } + public int FindFirstUnset() + { + for (int index = 0; index < _masks.Count; index++) + { + long mask = _masks[index]; + + if (mask != -1L) + { + return BitOperations.TrailingZeroCount(~mask) + index * IntSize; + } + } + + return _masks.Count * IntSize; + } + public bool Set(BitMap map) { EnsureCapacity(map._masks.Count * IntSize); @@ -135,7 +150,7 @@ namespace ARMeilleure.Common return modified; } - #region IEnumerable<long> Methods + #region IEnumerable<long> Methods // Note: The bit enumerator is embedded in this class to avoid creating garbage when enumerating. @@ -147,6 +162,11 @@ namespace ARMeilleure.Common } } + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + public IEnumerator<int> GetEnumerator() { Reset(); @@ -180,6 +200,6 @@ namespace ARMeilleure.Common public void Dispose() { } -#endregion + #endregion } }
\ No newline at end of file |