blob: 24319c27aec2d417671f667828d27e9796c32fa2 (
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
|
package bjc.utils.esodata;
import com.sun.rowset.internal.InsertRow;
/**
* Interface for something that acts like a tape.
*
* 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.
*
* @param <T>
* The element type of the tape.
*
* @author bjculkin
*/
public interface Tape<T> {
/**
* Get the item the tape is currently on.
*
* @return The item the tape is on.
*/
T item();
/**
* Set the item the tape is currently on.
*
* @param itm
* The new value for the tape item.
*/
void item(T itm);
/**
* Get the current number of elements in the tape.
*
* @return The current number of elements in the tape.
*/
int size();
/**
* Get the position of the current item.
*
* @return The position of the current item.
*/
int position();
/**
* Insert an element before the current item.
*
* @param itm
* The item to add.
*/
void insertBefore(T itm);
/**
* Insert an element after the current item.
*
* @param itm
* The item to insert.
*/
void insertAfter(T itm);
/**
* Remove the current element.
*
* Also moves the cursor back one step if possible to maintain relative
* position.
*
* @return The removed item.
*/
T remove();
/** Move the cursor to the left-most position. */
void first();
/** Move the cursor to the right-most position. */
void last();
/**
* Move the cursor one space left.
*
* The cursor can't go past zero.
*
* @return True if the cursor was moved left.
*/
boolean left();
/**
* 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.
*/
boolean left(int amt);
/**
* Move the cursor one space right.
*
* @return Whether the cursor was moved right.
*/
boolean right();
/**
* Move the cursor the specified amount right.
*
* @param amt
* The amount to move the cursor right by.
*
* @return Whether the cursor was moved right.
*/
boolean right(int amt);
/**
* Seek to an absolute position on the tape.
*
* @param pos
* The position to seek to.
* @return Whether or not the tape successfully seeked to that position.
*/
boolean seekTo(int pos);
/**
* Check if this tape is at its end.
*
* Equivalent to checking if position() == size().
*
* @return Whether or not the tape is at its end.
*/
default boolean atEnd() {
return position() == size();
}
/**
* Append an item to the tape.
*
* By default, uses a fairly non-performant implementation. Should be overidden in subclasses to be more performant.
* @param itm The item to append.
*/
default void append(T itm) {
int pos = position();
last();
insertAfter(itm);
seekTo(pos);
}
}
|