aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs')
-rw-r--r--src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs b/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs
new file mode 100644
index 00000000..f5456201
--- /dev/null
+++ b/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs
@@ -0,0 +1,78 @@
+namespace Ryujinx.Horizon.Sdk.Ngc.Detail
+{
+ class BitVector32
+ {
+ private const int BitsPerWord = Set.BitsPerWord;
+
+ private int _bitLength;
+ private uint[] _array;
+
+ public int BitLength => _bitLength;
+ public uint[] Array => _array;
+
+ public BitVector32()
+ {
+ _bitLength = 0;
+ _array = null;
+ }
+
+ public BitVector32(int length)
+ {
+ _bitLength = length;
+ _array = new uint[(length + BitsPerWord - 1) / BitsPerWord];
+ }
+
+ public bool Has(int index)
+ {
+ if ((uint)index < (uint)_bitLength)
+ {
+ int wordIndex = index / BitsPerWord;
+ int wordBitOffset = index % BitsPerWord;
+
+ return ((_array[wordIndex] >> wordBitOffset) & 1u) != 0;
+ }
+
+ return false;
+ }
+
+ public bool TurnOn(int index, int count)
+ {
+ for (int bit = 0; bit < count; bit++)
+ {
+ if (!TurnOn(index + bit))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public bool TurnOn(int index)
+ {
+ if ((uint)index < (uint)_bitLength)
+ {
+ int wordIndex = index / BitsPerWord;
+ int wordBitOffset = index % BitsPerWord;
+
+ _array[wordIndex] |= 1u << wordBitOffset;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public bool Import(ref BinaryReader reader)
+ {
+ if (!reader.Read(out _bitLength))
+ {
+ return false;
+ }
+
+ int arrayLength = (_bitLength + BitsPerWord - 1) / BitsPerWord;
+
+ return reader.AllocateAndReadArray(ref _array, arrayLength) == arrayLength;
+ }
+ }
+}