blob: 253c15f2d9f45e582817d13ba882f86f32219ed7 (
plain)
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
|
package bjc.utils.esodata;
import java.util.ArrayList;
/**
* A tape is a one-dimensional array that can only be accessed in one position
* at a time.
*
* A tape is essentially a 1D array with a cursor attached to it, and you can
* only affect elements at that cursor. The size of the array is theoretically
* unbounded to the right, but in practice bounded by available memory.
*
* You can choose whether or not you want the tape to automatically extend
* itself to the right with null elements by specifying its auto-extension
* policy.
*
* @param <T>
* The element type of the tape.
*
* @author bjculkin
*/
public class SingleTape<T> implements Tape<T> {
protected ArrayList<T> backing;
protected int pos;
protected boolean autoExtend;
/**
* Create a new empty tape that doesn't autoextend.
*/
public SingleTape() {
this(false);
}
/**
* Create a new empty tape that follows the specified auto-extension
* policy.
*
* @param autoExtnd
* Whether or not to auto-extend the tape to the right w/
* nulls.
*/
public SingleTape(boolean autoExtnd) {
autoExtend = autoExtnd;
backing = new ArrayList<>();
}
/**
* Get the item the tape is currently on.
*
* @return The item the tape is on.
*/
@Override
public T item() {
return backing.get(pos);
}
/**
* Set the item the tape is currently on.
*
* @param itm
* The new value for the tape item.
*/
@Override
public void item(T itm) {
backing.set(pos, itm);
}
/**
* Get the current number of elements in the tape.
*
* @return The current number of elements in the tape.
*/
@Override
public int size() {
return backing.size();
}
/**
* Insert an element before the current item.
*
* @param itm
* The item to add.
*/
@Override
public void insertBefore(T itm) {
backing.add(pos, itm);
}
/**
* Insert an element after the current item.
*/
@Override
public void insertAfter(T itm) {
if (pos == backing.size() - 1) {
backing.add(itm);
} else {
backing.add(pos + 1, itm);
}
}
/**
* Remove the current element.
*
* Also moves the cursor back one step if possible to maintain relative
* position.
*
* @return The removed item.
*/
@Override
public T remove() {
T res = backing.remove(pos);
if (pos != 0) {
pos -= 1;
}
return res;
}
/**
* Move the cursor to the left-most position.
*/
@Override
public void first() {
pos = 0;
}
/**
* Move the cursor the right-most position.
*/
@Override
public void last() {
pos = backing.size() - 1;
}
/**
* Move the cursor one space left.
*
* The cursor can't go past zero.
*
* @return True if the cursor was moved left.
*/
@Override
public boolean left() {
return left(1);
}
/**
* Move the cursor the specified amount left.
*
* The cursor can't go past zero. Attempts to move the cursor by amounts
* that would exceed zero don't move the cursor at all.
*
* @param amt
* The amount to attempt to move the cursor left.
*
* @return True if the cursor was moved left.
*/
@Override
public boolean left(int amt) {
if (pos - amt < 0)
return false;
pos -= amt;
return true;
}
/**
* Move the cursor one space right.
*
* Moving the cursor right will auto-extend the tape if that is enabled.
*
* @return Whether the cursor was moved right.
*/
@Override
public boolean right() {
return right(1);
}
/**
* Move the cursor the specified amount right.
*
* Moving the cursor right will auto-extend the tape if that is enabled.
*
* @param amt
* The amount to move the cursor right by.
*
* @return Whether the cursor was moved right.
*/
@Override
public boolean right(int amt) {
if (pos + amt >= backing.size() - 1) {
if (autoExtend) {
while (pos + amt >= backing.size() - 1) {
backing.add(null);
}
} else
return false;
}
pos += amt;
return true;
}
@Override
public boolean isDoubleSided() {
return false;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((backing == null) ? 0 : backing.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof SingleTape<?>))
return false;
SingleTape<?> other = (SingleTape<?>) obj;
if (backing == null) {
if (other.backing != null)
return false;
} else if (!backing.equals(other.backing))
return false;
return true;
}
@Override
public String toString() {
return String.format("SingleTape [backing=%s, pos=%s, autoExtend=%s]", backing, pos, autoExtend);
}
}
|