summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Entities/BitArrayExtensions.cs
blob: 2ca0737c5f7f10927ba39954d842e30c4dbe5b23 (plain)
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
using System;
using System.Collections;

namespace MonoGame.Extended.Entities
{
    public static class BitArrayExtensions
    {
        public static bool IsEmpty(this BitArray bitArray)
        {
            for (var i = 0; i < bitArray.Length; i++)
            {
                if (bitArray[i])
                    return false;
            }

            return true;
        }

        public static bool ContainsAll(this BitArray bitArray, BitArray other)
        {
            var otherBitsLength = other.Length;
            var bitsLength = bitArray.Length;

            for (var i = bitsLength; i < otherBitsLength; i++)
            {
                if (other[i])
                    return false;
            }

            var s = Math.Min(bitsLength, otherBitsLength);

            for (var i = 0; s > i; i++)
            {
                if ((bitArray[i] & other[i]) != other[i])
                    return false;
            }

            return true;
        }

        public static bool Intersects(this BitArray bitArray, BitArray other)
        {
            var s = Math.Min(bitArray.Length, other.Length);

            for (var i = 0; s > i; i++)
            {
                if (bitArray[i] & other[i])
                    return true;
            }

            return false;
        }
    }
}