blob: 9dff271b4cd98b472d224630f9d642a6975f8635 (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
using System.Collections;
using System.Collections.Generic;
namespace ARMeilleure.Common
{
class BitMap : IEnumerable<int>
{
private const int IntSize = 32;
private const int IntMask = IntSize - 1;
private List<int> _masks;
public BitMap(int initialCapacity)
{
int count = (initialCapacity + IntMask) / IntSize;
_masks = new List<int>(count);
while (count-- > 0)
{
_masks.Add(0);
}
}
public bool Set(int bit)
{
EnsureCapacity(bit + 1);
int wordIndex = bit / IntSize;
int wordBit = bit & IntMask;
int wordMask = 1 << wordBit;
if ((_masks[wordIndex] & wordMask) != 0)
{
return false;
}
_masks[wordIndex] |= wordMask;
return true;
}
public void Clear(int bit)
{
EnsureCapacity(bit + 1);
int wordIndex = bit / IntSize;
int wordBit = bit & IntMask;
int wordMask = 1 << wordBit;
_masks[wordIndex] &= ~wordMask;
}
public bool IsSet(int bit)
{
EnsureCapacity(bit + 1);
int wordIndex = bit / IntSize;
int wordBit = bit & IntMask;
return (_masks[wordIndex] & (1 << wordBit)) != 0;
}
public bool Set(BitMap map)
{
EnsureCapacity(map._masks.Count * IntSize);
bool modified = false;
for (int index = 0; index < _masks.Count; index++)
{
int newValue = _masks[index] | map._masks[index];
if (_masks[index] != newValue)
{
_masks[index] = newValue;
modified = true;
}
}
return modified;
}
public bool Clear(BitMap map)
{
EnsureCapacity(map._masks.Count * IntSize);
bool modified = false;
for (int index = 0; index < _masks.Count; index++)
{
int newValue = _masks[index] & ~map._masks[index];
if (_masks[index] != newValue)
{
_masks[index] = newValue;
modified = true;
}
}
return modified;
}
private void EnsureCapacity(int size)
{
while (_masks.Count * IntSize < size)
{
_masks.Add(0);
}
}
public IEnumerator<int> GetEnumerator()
{
for (int index = 0; index < _masks.Count; index++)
{
int mask = _masks[index];
while (mask != 0)
{
int bit = BitUtils.LowestBitSet(mask);
mask &= ~(1 << bit);
yield return index * IntSize + bit;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
|