-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitArr.java
More file actions
70 lines (57 loc) · 1.77 KB
/
BitArr.java
File metadata and controls
70 lines (57 loc) · 1.77 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
//Class that can have a big array of bytes
public class BitArr{
final int MAXARRAYSIZE = Integer.MAX_VALUE-5; // Maximum length of a array
byte[][] bitArr;
long size;
// The constructor finds out how many arrays of bytes, and how big they have
// to be, depending on the size.
BitArr(long size){
this.size = size;
long localSize = size;
int maxOuterArraySize = 1;
while (localSize > MAXARRAYSIZE){
localSize -= MAXARRAYSIZE;
maxOuterArraySize++;
}
localSize = size;
bitArr = new byte[maxOuterArraySize][];
for (int i = 0; i < MAXARRAYSIZE; i++){
if (localSize < MAXARRAYSIZE){
bitArr[i] = new byte[(int)localSize];
break;
}
bitArr[i] = new byte[(int)MAXARRAYSIZE];
localSize -= MAXARRAYSIZE;
}
}
// Returns the length of the array
public long getLength(){
return size;
}
// Stores a byte at a index
public void set(long index, byte value){
long localIndex = index;
for (int i = 0; i < MAXARRAYSIZE; i++){
if (localIndex < MAXARRAYSIZE){
bitArr[i][(int)localIndex] = value;
break;
}
localIndex -= MAXARRAYSIZE;
}
}
// Returns the byte on a given index
public byte get(long index){
if (index < 0){
System.out.println("Negative index: " + index);
System.exit(0);
}
long localIndex = index;
for (int i = 0; i < MAXARRAYSIZE; i++){
if (localIndex < MAXARRAYSIZE){
return bitArr[i][(int)localIndex];
}
localIndex -= MAXARRAYSIZE;
}
return 0;
}
}