-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitVector.java
326 lines (296 loc) · 10.6 KB
/
BitVector.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* @author Clément Petit (282626)
* @author Yanis Berkani (271348)
*/
package ch.epfl.gameboj.bits;
import java.util.Arrays;
import java.util.Objects;
import ch.epfl.gameboj.Preconditions;
import static ch.epfl.gameboj.Preconditions.checkArgument;
/**
* represents a bit vector whose size is a strictly positive multiple of 32.
*/
public final class BitVector {
private static final int nbOfBytesInInt = Integer.SIZE / Byte.SIZE;
private static final int BYTE_MASK = 0b1111_1111;
private final int[] vector;
private enum extensionType {
zero, wrapped
};
/**
* builds a bit vector of the given size and of which all bits have the
* value 0 if initValue is false and 1 otherwise.
*
* @param sizeInBits
* the size in bits (must be a strictly positive multiple of 32)
*
* @param initValue
* the initial value
* @throws IllegalArgumentException
* if sizeInBits is invalid
*/
public BitVector(int sizeInBits, boolean initValue) {
checkArgument(sizeInBits > 0 && (sizeInBits % Integer.SIZE == 0));
vector = new int[sizeInBits / Integer.SIZE];
if (initValue)
Arrays.fill(vector, ~0);
}
/**
* builds a bit vector of the given size and of which all bits have the
* value 0.
*
* @param sizeInBits
* the size in bits (must be a multiple of 32 strictly positive)
* @throws IllegalArgumentException
* if sizeInBits is invalid
*/
public BitVector(int sizeInBits) {
this(sizeInBits, false);
}
private BitVector(int[] vector) {
this.vector = vector;
}
/**
* @return the size of the vector in bits.
*/
public int size() {
return 32 * vector.length;
}
/**
* determines if the bit of given index is true or false.
*
* @param index
* the index (must be positive and strictly inferior to the size
* of the vector)
* @throws IndexOutOfBoundsException
* if the index is invalid
* @return true if the bit of given index is 1 and false otherwise
*/
public boolean testBit(int index) {
Objects.checkIndex(index, size());
return Bits.test(vector[index / Integer.SIZE], index % Integer.SIZE);
}
/**
* computes the complement of this bit vector.
*
* @return a new bit vector which is the complement of this bit vector
*/
public BitVector not() {
int[] not = new int[vector.length];
for (int i = 0; i < not.length; i++) {
not[i] = ~vector[i];
}
return new BitVector(not);
}
/**
* computes the bitwise conjunction with another vector of the same size.
*
* @param that
* the other vector (must have the same length as this vector)
* @throws IllegalArgumentException
* if the other vector is invalid
* @return a new bit vector which is the bitwise conjunction between this
* vector and the other vector
*/
public BitVector and(BitVector that) {
int[] vector2 = that.vector;
checkArgument(vector2.length == vector.length);
int[] and = new int[vector.length];
for (int i = 0; i < and.length; i++) {
and[i] = vector[i] & vector2[i];
}
return new BitVector(and);
}
/**
* computes the bitwise disjunction with another vector of the same size.
*
* @param that
* the other vector (must have the same length as this vector)
* @throws IllegalArgumentException
* if the other vector is invalid
* @return a new bit vector which is the bitwise disjunction between this
* vector and another vector
*/
public BitVector or(BitVector that) {
int[] vector2 = that.vector;
checkArgument(vector2.length == vector.length);
int[] or = new int[vector.length];
for (int i = 0; i < or.length; i++) {
or[i] = vector[i] | vector2[i];
}
return new BitVector(or);
}
private int extensionElement(int index, extensionType type) {
if (index >= 0 && index < vector.length) {
return vector[index];
} else {
if (type == extensionType.zero) {
return 0;
} else {
return vector[Math.floorMod(index, vector.length)];
}
}
}
private BitVector extract(int sizeInBits, int start, extensionType type) {
checkArgument(sizeInBits % Integer.SIZE == 0 && sizeInBits > 0);
int[] extracted = new int[sizeInBits / Integer.SIZE];
int startMod32 = Math.floorMod(start, Integer.SIZE);
int startDiv32 = Math.floorDiv(start, Integer.SIZE);
if (startMod32 == 0) {
for (int i = 0; i < extracted.length; i++) {
extracted[i] = extensionElement(startDiv32 + i, type);
}
} else {
for (int i = 0; i < extracted.length; i++) {
extracted[i] = extensionElement(startDiv32 + i, type) >>> start
| extensionElement(startDiv32 + i + 1,
type) << Integer.SIZE - start;
}
}
return new BitVector(extracted);
}
/**
* extracts a vector of given size from the extension by 0 of this vector.
*
* @param start
* the start bit
* @param sizeInBits
* the size in bits (must be a strictly positive multiple of 32)
* @throws IllegalArgumentException
* if sizeInBits is invalid
* @return a new bit vector which is the extracted vector of given size from
* the extension by 0 of this vector
*/
public BitVector extractZeroExtended(int sizeInBits, int start) {
return extract(sizeInBits, start, extensionType.zero);
}
/**
* extracts a vector of given size from the extension by wrapping of this
* vector.
*
* @param start
* the start bit
* @param sizeInBits
* the size in bits (must be a strictly positive multiple of 32)
* @throws IllegalArgumentException
* if sizeInBits is invalid
* @return a new bit vector which is the extracted vector of given size from
* the extension by wrapping of this vector
*/
public BitVector extractWrapped(int sizeInBits, int start) {
return extract(sizeInBits, start, extensionType.wrapped);
}
/**
* shifts the vector by the given distance, using the usual convention that
* a positive distance represents a shift to the left and a negative
* distance a shift to the right.
*
* @param distance
* the distance
* @return a new bit vector which is this vector shifted by the given
* distance using the usual convention
*/
public BitVector shift(int distance) {
return extractZeroExtended(size(), -distance);
}
/**
* return the hash code of the vector.
*
* @return the hash code of the vector
*/
public int hashcode() {
return Arrays.hashCode(vector);
}
/**
* checks if the given object is a BitVector and if it is the same size and
* has the same bits as this vector.
*
* @param that
* the object
* @return true if the given object is a BitVector and is equal to this
* vector and false otherwise
*/
public boolean equals(Object that) {
return (that instanceof BitVector)
&& Arrays.equals(vector, ((BitVector) that).vector);
}
/**
* returns a representation of the vector as a string consisting only of
* characters 0 and 1.
*
* @return a representation of the vector as a string consisting only of
* characters 0 and 1
*/
public String toString() {
StringBuilder b = new StringBuilder(size());
for (int i = 0; i < size(); i++) {
b.append(testBit(i) ? 1 : 0);
}
return b.reverse().toString();
}
/**
* represents a bit vector builder.
*/
public final static class Builder {
private int[] vector;
/**
* creates a bit vector builder of given size and of which all bits have the
* value 0.
*
* @param sizeInBits
* the size in bits (must be a strictly positive multiple of
* 32)
* @throws IllegalArgumentException
* if sizeInBits is invalid
*/
public Builder(int sizeInBits) {
checkArgument(sizeInBits > 0 && sizeInBits % Integer.SIZE == 0);
vector = new int[sizeInBits / Integer.SIZE];
}
/**
* sets the value of a byte designated by its index.
*
* @param index
* the index (must be positive and strictly inferior to the
* number of bytes in an integer (4) times the length of the
* vector)
* @param newValue
* the new value (must be an 8 bits value)
* @throws IllegalStateException
* if the vector is null
* @throws IllegalArgumentException
* if newValue is invalid
* @throws IndexOutOfBoundsException
* if the index is invalid
* @return the builder
*/
public Builder setByte(int index, int newValue) {
if (vector == null) {
throw new IllegalStateException();
}
Preconditions.checkBits8(newValue);
Objects.checkIndex(index, nbOfBytesInInt * vector.length);
int indexInInt = index % nbOfBytesInInt;
int indexInTab = index / nbOfBytesInInt;
int mask = ~(BYTE_MASK << (Byte.SIZE * indexInInt));
vector[indexInTab] = (vector[indexInTab] & mask)
| newValue << (Byte.SIZE * indexInInt);
return this;
}
/**
* builds the bit vector and makes the attribute vector as null so the builder cannot be used again.
*
* @throws IllegalStateException
* if the vector is null
* @return the built bit vector
*/
public BitVector build() {
if (vector == null) {
throw new IllegalStateException();
}
BitVector bitVector = new BitVector(vector);
vector = null;
return bitVector;
}
}
}