-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitArray.java
More file actions
86 lines (69 loc) · 2.17 KB
/
BitArray.java
File metadata and controls
86 lines (69 loc) · 2.17 KB
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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
public class BitArray implements Iterable<Boolean> {
private int _length;
private long[] _data = null;
private static final short SIZE_OF_LONG = 64;
public BitArray(int length){
this._data = new long[length / SIZE_OF_LONG + 1];
this._length = length;
}
public BitArray(int length, boolean $default){
this._data = new long[length / SIZE_OF_LONG + 1];
this._length = length;
if($default) for(int i = 0; i < _data.length; i++) _data[i] = -1L;
}
public int length(){
return this._length;
}
public void set(int index, boolean value) throws IndexOutOfBoundsException {
if(index >= _length || index < 0) throw new IndexOutOfBoundsException("index was outside the length of the array");
int integerIndex = index / SIZE_OF_LONG;
int bitIndex = index % SIZE_OF_LONG;
long currentValue = _data[integerIndex];
if(value){
currentValue |= MASKS.get(bitIndex);
} else {
currentValue &= ~MASKS.get(bitIndex);
}
_data[integerIndex] = currentValue;
}
public boolean get(int index) throws IndexOutOfBoundsException {
if(index >= _length || index < 0) throw new IndexOutOfBoundsException("index was outside the length of the array");
int integerIndex = index / SIZE_OF_LONG;
int bitIndex = index % SIZE_OF_LONG;
long currentValue = _data[integerIndex];
return (currentValue & MASKS.get(bitIndex)) == MASKS.get(bitIndex);
}
private static final Map<Integer, Long> MASKS = new HashMap<Integer, Long>();
static {
for (int i = 0; i < SIZE_OF_LONG; i++){
MASKS.put(i, 1L << i);
}
}
@Override
public Iterator<Boolean> iterator() {
return new BitArrayIterator(this);
}
class BitArrayIterator implements Iterator<Boolean> {
private int index = 0;
private BitArray array;
BitArrayIterator(BitArray iterable){
this.array = iterable;
}
@Override
public boolean hasNext() {
return index < this.array._length;
}
@Override
public Boolean next() {
try{
return this.array.get(index++);
} catch (IndexOutOfBoundsException ex){
throw new NoSuchElementException(ex.getMessage());
}
}
}
}