forked from InsideEmpire/CS61B-PathwayToSuccess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitSequence.java
More file actions
executable file
·218 lines (188 loc) · 6.66 KB
/
Copy pathBitSequence.java
File metadata and controls
executable file
·218 lines (188 loc) · 6.66 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
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
/** Utility class for manipulating sequences of bits. DO NOT MODIFY THIS FILE!!!
*
* BitSequences are immutable.
*
*
* DO NOT MODIFY THIS FILE.
*
*
*
* Josh Hug: April 30, 2016 */
import java.util.BitSet;
import java.util.List;
import java.io.Serializable;
public class BitSequence implements Serializable {
private static final long serialVersionUID = 157816335349L;
private BitSet bits;
private int numBits;
/* lastBitNum is exclusive. */
private int firstBitNum;
private int lastBitNum;
/** Creates a new empty BitSequence. */
public BitSequence() {
bits = new BitSet();
numBits = 0;
firstBitNum = 0;
lastBitNum = 0;
}
/* Makes a memory efficient copy of a bit sequence. Unlikely to be useful for HW7. */
public BitSequence(BitSequence b) {
bits = new BitSet();
for (int i = 0; i < b.length(); i += 1) {
append(b.bitAt(i));
}
}
/** Creates a bit sequence from a series of 1s and 0s. */
public BitSequence(String s) {
BitSequence bs = new BitSequence();
for (char b : s.toCharArray()) {
if (b == '0') {
bs.append(0);
} else if (b == '1') {
bs.append(1);
} else {
throw new IllegalArgumentException("Bit Sequence creation string contains: " + b);
}
}
this.bits = bs.bits;
this.numBits = bs.numBits;
this.firstBitNum = 0;
this.lastBitNum = bs.numBits;
}
/** Number of bits in this sequence. */
public int length() {
return lastBitNum - firstBitNum;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null) {
return false;
}
if (other.getClass() != this.getClass()) {
return false;
}
BitSequence that = (BitSequence) other;
if (this.length() != that.length()) {
return false;
}
for (int i = 0; i < length(); i += 1) {
if (this.bitAt(i) != that.bitAt(i)) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int hashCode = 1;
for (int i = 0; i < length(); i += 1) {
hashCode *= 31;
}
return hashCode;
}
/** Returns the bit in the given position. */
public int bitAt(int position) {
boolean bit = bits.get(position + firstBitNum);
if (!bit) {
return 0;
} else {
return 1;
}
}
/** Creates a new BitSequence with the given bit appended. */
public BitSequence appended(int bit) {
BitSequence bscopy = new BitSequence(this);
bscopy.append(bit);
return bscopy;
}
/** Returns a new bit sequence corresponding to the first N bits.
* Runs in constant time. New bit sequence contains a pointer to this
* sequence's bits, so use the copy constructor on the returned BitSequence if
* memory is more important than speed. Don't do this in HW7. */
public BitSequence firstNBits(int N) {
if (N > numBits) {
throw new IllegalArgumentException("More bits requested than present in sequence, "
+ "requested " + N + ", but have " + numBits + ".");
}
BitSequence firstN = new BitSequence();
firstN.bits = this.bits;
firstN.numBits = N;
firstN.firstBitNum = this.firstBitNum;
firstN.lastBitNum = this.firstBitNum + N;
return firstN;
}
/** Returns a new bit sequence corresponding to all but the first N bits.
* Runs in constant time. New bit sequence contains a pointer to this
* sequence's bits, so use the copy constructor on the returned BitSequence if
* memory is more important than speed. Don't do this in HW7. */
public BitSequence allButFirstNBits(int N) {
if (N > numBits) {
throw new IllegalArgumentException("Requested allButFirstNBits, where N is equal to "
+ N + ", but have " + numBits + ".");
}
BitSequence allButFirstN = new BitSequence();
allButFirstN.bits = this.bits;
allButFirstN.numBits = this.numBits - N;
allButFirstN.firstBitNum = this.firstBitNum + N;
allButFirstN.lastBitNum = this.lastBitNum;
return allButFirstN;
}
/** Returns a new bit sequence corresponding to the last N bits.
* Runs in constant time. New bit sequence contains a pointer to this
* sequence's bits, so use the copy constructor on the returned BitSequence if
* memory is more important than speed. Don't do this in HW7. */
public BitSequence lastNBits(int N) {
if (N > numBits) {
throw new IllegalArgumentException("More bits requested than present in sequence, "
+ "requested " + N + ", but have " + numBits + ".");
}
BitSequence lastN = new BitSequence();
lastN.bits = this.bits;
lastN.numBits = N;
lastN.lastBitNum = this.lastBitNum;
lastN.firstBitNum = this.lastBitNum - N;
return lastN;
}
/** Combines a bunch of bit sequences into one huge bit sequence. */
public static BitSequence assemble(List<BitSequence> sequences) {
BitSequence masterSequence = new BitSequence();
for (BitSequence bs : sequences) {
masterSequence.append(bs);
}
return masterSequence;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length(); i += 1) {
sb.append(String.valueOf(bitAt(i)));
}
return sb.toString();
}
/** Do not make this method public. This is a utility method for
* implementers only. BitSequence is, and should remain, immutable! */
private void append(int bit) {
if (bit == 1) {
bits.set(numBits, true);
}
numBits += 1;
lastBitNum += 1;
}
/** Do not make this method public. This is a utility method for
* implementers only. BitSequence is, and should remain, immutable! */
private void append(BitSequence bs) {
for (int i = 0; i < bs.numBits; i += 1) {
append(bs.bitAt(i));
}
}
public static void main(String[] args) {
BitSequence bs = new BitSequence();
bs = bs.appended(1);
System.out.println(bs.toString());
bs = bs.appended(1);
System.out.println(bs.toString());
}
}