/
BitSet.java
231 lines (196 loc) · 5.96 KB
/
BitSet.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Copyright (c) 2008-2009, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
import java.io.Serializable;
/**
* @author zsombor
*
*/
public class BitSet implements Serializable, Cloneable {
final static int BITS_PER_LONG = 64;
final static int BITS_PER_LONG_SHIFT = 6;
final static long MASK = 0xFFFFFFFFFFFFFFFFL;
private long[] bits;
private static int longPosition(int index) {
return index >> BITS_PER_LONG_SHIFT;
}
private static long bitPosition(int index) {
return 1L << (index % BITS_PER_LONG);
}
public BitSet(int bitLength) {
if (bitLength % BITS_PER_LONG == 0) {
enlarge(longPosition(bitLength));
} else {
enlarge(longPosition(bitLength) + 1);
}
}
public BitSet() {
enlarge(1);
}
public void and(BitSet otherBits) {
int min = Math.min(bits.length, otherBits.bits.length);
for (int i = 0; i < min; i++) {
bits[i] &= otherBits.bits[i];
}
for (int i = min; i < bits.length; i++) {
bits[i] = 0;
}
}
public void andNot(BitSet otherBits) {
int max = Math.max(bits.length, otherBits.bits.length);
enlarge(max);
int min = Math.min(bits.length, otherBits.bits.length);
for (int i = 0; i < min; i++) {
bits[i] &= ~otherBits.bits[i];
}
}
public void or(BitSet otherBits) {
int max = Math.max(bits.length, otherBits.bits.length);
enlarge(max);
int min = Math.min(bits.length, otherBits.bits.length);
for (int i = 0; i < min; i++) {
bits[i] |= otherBits.bits[i];
}
}
public void xor(BitSet otherBits) {
int max = Math.max(bits.length, otherBits.bits.length);
enlarge(max);
int min = Math.min(bits.length, otherBits.bits.length);
for (int i = 0; i < min; i++) {
bits[i] ^= otherBits.bits[i];
}
}
public void flip(int index) {
flip(index, index+1);
}
public void flip(int fromIndex, int toIndex) {
if (fromIndex > toIndex || fromIndex < 0 || toIndex < 0) {
throw new IndexOutOfBoundsException();
} else if (fromIndex != toIndex) {
int basePartition = longPosition(fromIndex);
int lastPartition = longPosition(toIndex - 1); //range is [fromIndex, toIndex)
int numPartitionsToTraverse = lastPartition - basePartition + 1;
enlarge(lastPartition);
int currentFirstIndex = fromIndex;
for (int i = 0; i < numPartitionsToTraverse; ++i) {
int currentToIndex = Math.min(toIndex, (basePartition + i + 1) * BITS_PER_LONG);
long mask = getTrueMask(currentFirstIndex, currentToIndex);
bits[i + basePartition] ^= mask;
currentFirstIndex = currentToIndex;
}
}
}
private void enlarge(int newPartition) {
if (bits == null || bits.length < (newPartition + 1)) {
long[] newBits = new long[newPartition + 1];
if (bits != null) {
System.arraycopy(bits, 0, newBits, 0, bits.length);
}
bits = newBits;
}
}
private long getTrueMask(int fromIndex, int toIndex) {
int currentRange = toIndex - fromIndex;
return (((1L << currentRange) - 1L) << (fromIndex % BITS_PER_LONG));
}
public void clear(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
bits[pos] &= (MASK ^ bitPosition(index));
}
}
public boolean get(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
return (bits[pos] & bitPosition(index)) != 0;
}
return false;
}
public void set(int index) {
int pos = longPosition(index);
enlarge(pos);
bits[pos] |= bitPosition(index);
}
public void set(int start, int end) {
//TODO remove copypasta
int basePartition = longPosition(start);
int lastPartition = longPosition(end - 1); //range is [fromIndex, toIndex)
int numPartitionsToTraverse = lastPartition - basePartition + 1;
enlarge(lastPartition);
int currentFirstIndex = start;
for (int i = 0; i < numPartitionsToTraverse; ++i) {
int currentToIndex = Math.min(end, (basePartition + i + 1) * BITS_PER_LONG);
long mask = getTrueMask(currentFirstIndex, currentToIndex);
bits[i + basePartition] |= mask;
currentFirstIndex = currentToIndex;
}
}
public void clear(int start, int end) {
for (int i = start; i < end; i++) {
clear(i);
}
}
public boolean isEmpty() {
for (int i = 0; i < bits.length; i++) {
if (bits[i] != 0) {
return false;
}
}
return true;
}
public boolean intersects(BitSet otherBits) {
int max = Math.max(bits.length, otherBits.bits.length);
for (int i = 0; i < max; i++) {
if ((bits[i] & otherBits.bits[i]) != 0) {
return true;
}
}
return false;
}
public int length() {
return bits.length << BITS_PER_LONG_SHIFT;
}
public int nextSetBit(int fromIndex) {
return nextBit(fromIndex, false);
}
private int nextBit(int fromIndex, boolean bitClear) {
int pos = longPosition(fromIndex);
if (pos >= bits.length) {
return -1;
}
int current = fromIndex;
do {
long currValue = bits[pos];
if (currValue == 0) {
pos++;
current = pos << BITS_PER_LONG_SHIFT;
} else {
do {
long bitPos = bitPosition(current);
if (((currValue & bitPos) != 0) ^ bitClear) {
return current;
} else {
current++;
}
} while (current % BITS_PER_LONG != 0);
}
pos++;
} while (pos < bits.length);
return -1;
}
public int nextClearBit(int fromIndex) {
return nextBit(fromIndex, true);
}
public int cardinality() {
int numSetBits = 0;
for (int i = nextSetBit(0); i >= 0; i = nextSetBit(i+1)) {
++numSetBits;
}
return numSetBits;
}
}