using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Numerics; using CommunityToolkit.HighPerformance.Buffers; namespace Brutal.Enums; /// Represents a bit mask, ie a collection of bits that can be set/unset. [SuppressMessage("ReSharper", "StaticMemberInGenericType")] public struct BitMask : IEquatable>, IBitwiseOperators, BitMask, BitMask> where T : unmanaged, IBinaryInteger, IUnsignedNumber { public static readonly int BitCount = Unsafe.SizeOf() * 8; /// A mask with no bits set. public static readonly BitMask Empty = default; /// A mask with all the bits set. public static readonly BitMask Full = new(T.AllBitsSet); T _value; public T Value => _value; /// Create a mask from a bit mask, ie no shifting. public static BitMask FromMask(T mask) => new(mask); /// Create a mask from a bit index, ie shifting it. public static BitMask FromBit(byte bitIndex) => new(bitIndex); /// Create a mask from a value, ie no shifting. private BitMask(T mask) { _value = mask; } /// Create a mask from a bit index, ie shifting it. public BitMask(byte bitIndex) { _value = T.One << bitIndex; } /// Create a mask with the given range of bit indices set. public BitMask(Range bitRange) { this[bitRange] = true; } /// Create a mask with the given bit indices set. public BitMask(params ReadOnlySpan bitIndices) { foreach (var bitIndex in bitIndices) _value |= T.One << bitIndex; } #region this[index/range] accessors /// Get/set the value of a bit by its index. public bool this[byte bitIndex] { get => Contains(bitIndex); set => Set(bitIndex, value); } /// Get/set the value of a bit by its index. public bool this[Index bitIndex] { get => Contains((byte)bitIndex.GetOffset(BitCount)); set => Set((byte)bitIndex.GetOffset(BitCount), value); } /// Get/set the value of a range of bits. public bool this[Range bitRange] { get => ContainsAll(new BitMask(bitRange)); set { var (offset, length) = bitRange.GetOffsetAndLength(BitCount); var start = (byte)offset; var end = offset + (byte)length; for (var i = start; i < end; i++) this[i] = value; } } #endregion #region operators public static BitMask operator |(BitMask a, BitMask b) => FromMask(a._value | b._value); public static BitMask operator &(BitMask a, BitMask b) => FromMask(a._value & b._value); public static BitMask operator ^(BitMask a, BitMask b) => FromMask(a._value ^ b._value); public static BitMask operator ~(BitMask a) => FromMask(~a._value); #endregion #region Contains/Add/Remove/Set(bitIndex) /// [MethodImpl(MethodImplOptions.AggressiveInlining), Pure] public bool Contains(byte bitIndex) => ContainsAny(FromBit(bitIndex)); /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Add(byte bitIndex) => Add(FromBit(bitIndex)); /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Remove(byte bitIndex) => Remove(FromBit(bitIndex)); /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Set(byte bitIndex, bool enabled) => Set(FromBit(bitIndex), enabled); #endregion #region Contains/Add/Remove/Set(mask) /// /// Does this mask entirely contain ? /// Ie, does this mask contain all the bits in ? ///
/// Similarly to , this method returns true if the set of bits /// represented by is empty, even if the current mask is empty. ///
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure] public bool ContainsAll(BitMask other) { return (_value & other._value) == other._value; } /// Does this mask contain any of the bits in ? [MethodImpl(MethodImplOptions.AggressiveInlining), Pure] public bool ContainsAny(BitMask other) { return (_value & other._value) != T.Zero; } /// Add (set) the given bits to ours. [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Add(BitMask other) { _value |= other._value; return this; } /// Remove (unset) the other bits from ours. [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Remove(BitMask other) { _value &= ~other._value; return this; } /// Add/remove (set/unset) the other bits to/from ours. [MethodImpl(MethodImplOptions.AggressiveInlining)] public BitMask Set(BitMask other, bool enabled) => enabled ? Add(other) : Remove(other); #endregion /// Is the mask empty? ie no bits set. public bool IsEmpty => _value == default; /// Is the mask full? ie all bits set. public bool IsFull => _value == T.AllBitsSet; /// Does the mask have only a single bit set? public bool IsPow2 => T.IsPow2(_value); /// If the mask has only a single bit set, return its index. public bool TryGetBitIndex(out byte index) => BitOperations.TryGetBitIndex(_value, out index); /// Return the inverse of this mask, ie all bits flipped. public BitMask Inversed => new(~_value); #region ToString() - TODO: ISpanFormattable etc public static readonly string[] BitNames = ComputeBitNames(); static string[] ComputeBitNames() { var names = new string[BitCount]; for (var i = 0; i < BitCount; i++) { names[i] = i.ToString(); } return names; } static readonly ConcurrentDictionary ToStringCache = new(); public override string ToString() { // try to get string from cache if (ToStringCache.TryGetValue(_value, out var s)) { return s; } if (IsEmpty) return "(empty)"; if (IsFull) return "(full)"; // compute string and cache it using var tmpBitNames = SpanOwner.Allocate(BitCount); var tmpBitNameCount = 0; for (byte i = 0; i < BitCount; i++) if (Contains(i)) tmpBitNames.Span[tmpBitNameCount++] = BitNames[i]; ToStringCache[_value] = s = $"({string.Join(", ", tmpBitNames.Span[..tmpBitNameCount])})"; return s; } #endregion #region equality members public bool Equals(BitMask other) { return _value.Equals(other._value); } public override bool Equals(object? obj) { return obj is BitMask other && Equals(other); } public override int GetHashCode() { return _value.GetHashCode(); } public static bool operator ==(BitMask left, BitMask right) { return left.Equals(right); } public static bool operator !=(BitMask left, BitMask right) { return !left.Equals(right); } #endregion }