aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Ngc/Detail/BitVector32.cs
blob: f54562011b631d26078879a817b6f4f1493da408 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
        }
    }
}