-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList3.java
More file actions
377 lines (321 loc) · 12 KB
/
List3.java
File metadata and controls
377 lines (321 loc) · 12 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.util.Iterator;
import java.util.NoSuchElementException;
import components.list.List;
import components.list.ListSecondary;
/**
* {@code List} represented as a doubly linked list, done "bare-handed", with
* implementations of primary methods and {@code retreat} secondary method.
*
* <p>
* Execution-time performance of all methods implemented in this class is O(1).
* </p>
*
* @param <T>
* type of {@code List} entries
* @convention <pre>
* $this.leftLength >= 0 and
* [$this.rightLength >= 0] and
* [$this.preStart is not null] and
* [$this.lastLeft is not null] and
* [$this.postFinish is not null] and
* [$this.preStart points to the first node of a doubly linked list
* containing ($this.leftLength + $this.rightLength + 2) nodes] and
* [$this.lastLeft points to the ($this.leftLength + 1)-th node in
* that doubly linked list] and
* [$this.postFinish points to the last node in that doubly linked list] and
* [for every node n in the doubly linked list of nodes, except the one
* pointed to by $this.preStart, n.previous.next = n] and
* [for every node n in the doubly linked list of nodes, except the one
* pointed to by $this.postFinish, n.next.previous = n]
* </pre>
* @correspondence <pre>
* this =
* ([data in nodes starting at $this.preStart.next and running through
* $this.lastLeft],
* [data in nodes starting at $this.lastLeft.next and running through
* $this.postFinish.previous])
* </pre>
*
* @author P.Shrestha
* @author S. Rajesh
*
*/
public class List3<T> extends ListSecondary<T> {
/**
* Node class for doubly linked list nodes.
*/
private final class Node {
/**
* Data in node, or, if this is a "smart" Node, irrelevant.
*/
private T data;
/**
* Next node in doubly linked list, or, if this is a trailing "smart"
* Node, irrelevant.
*/
private Node next;
/**
* Previous node in doubly linked list, or, if this is a leading "smart"
* Node, irrelevant.
*/
private Node previous;
}
/**
* "Smart node" before start node of doubly linked list.
*/
private Node preStart;
/**
* Last node of doubly linked list in this.left.
*/
private Node lastLeft;
/**
* "Smart node" after finish node of linked list.
*/
private Node postFinish;
/**
* Length of this.left.
*/
private int leftLength;
/**
* Length of this.right.
*/
private int rightLength;
/**
* Checks that the part of the convention repeated below holds for the
* current representation.
*
* @return true if the convention holds (or if assertion checking is off);
* otherwise reports a violated assertion
* @convention <pre>
* $this.leftLength >= 0 and
* [$this.rightLength >= 0] and
* [$this.preStart is not null] and
* [$this.lastLeft is not null] and
* [$this.postFinish is not null] and
* [$this.preStart points to the first node of a doubly linked list
* containing ($this.leftLength + $this.rightLength + 2) nodes] and
* [$this.lastLeft points to the ($this.leftLength + 1)-th node in
* that doubly linked list] and
* [$this.postFinish points to the last node in that doubly linked list] and
* [for every node n in the doubly linked list of nodes, except the one
* pointed to by $this.preStart, n.previous.next = n] and
* [for every node n in the doubly linked list of nodes, except the one
* pointed to by $this.postFinish, n.next.previous = n]
* </pre>
*/
private boolean conventionHolds() {
assert this.leftLength >= 0 : "Violation of: $this.leftLength >= 0";
assert this.rightLength >= 0 : "Violation of: $this.rightLength >= 0";
assert this.preStart != null : "Violation of: $this.preStart is not null";
assert this.lastLeft != null : "Violation of: $this.lastLeft is not null";
assert this.postFinish != null : "Violation of: $this.postFinish is not null";
int count = 0;
boolean lastLeftFound = false;
Node n = this.preStart;
while ((count < this.leftLength + this.rightLength + 1)
&& (n != this.postFinish)) {
count++;
if (n == this.lastLeft) {
/*
* Check $this.lastLeft points to the ($this.leftLength + 1)-th
* node in that doubly linked list
*/
assert count == this.leftLength + 1 : ""
+ "Violation of: [$this.lastLeft points to the"
+ " ($this.leftLength + 1)-th node in that doubly linked list]";
lastLeftFound = true;
}
/*
* Check for every node n in the doubly linked list of nodes, except
* the one pointed to by $this.postFinish, n.next.previous = n
*/
assert (n.next != null) && (n.next.previous == n)
: "" + "Violation of: [for every node n in the doubly linked"
+ " list of nodes, except the one pointed to by"
+ " $this.postFinish, n.next.previous = n]";
n = n.next;
/*
* Check for every node n in the doubly linked list of nodes, except
* the one pointed to by $this.preStart, n.previous.next = n
*/
assert n.previous.next == n
: "" + "Violation of: [for every node n in the doubly linked"
+ " list of nodes, except the one pointed to by"
+ " $this.preStart, n.previous.next = n]";
}
count++;
assert count == this.leftLength + this.rightLength + 2
: "" + "Violation of: [$this.preStart points to the first node of"
+ " a doubly linked list containing"
+ " ($this.leftLength + $this.rightLength + 2) nodes]";
assert lastLeftFound : "" + "Violation of: [$this.lastLeft points to the"
+ " ($this.leftLength + 1)-th node in that doubly linked list]";
assert n == this.postFinish
: "" + "Violation of: [$this.postFinish points to the last"
+ " node in that doubly linked list]";
return true;
}
/**
* Creator of initial representation.
*/
private void createNewRep() {
this.preStart = new Node();
this.postFinish = new Node();
this.preStart.next = this.postFinish;
this.postFinish.previous = this.preStart;
this.lastLeft = this.preStart;
this.leftLength = 0;
this.rightLength = 0;
}
/**
* No-argument constructor.
*/
public List3() {
this.createNewRep();
assert this.conventionHolds();
}
@SuppressWarnings("unchecked")
@Override
public final List3<T> newInstance() {
try {
return this.getClass().getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new AssertionError(
"Cannot construct object of type " + this.getClass());
}
}
@Override
public final void clear() {
this.createNewRep();
assert this.conventionHolds();
}
@Override
public final void transferFrom(List<T> source) {
assert source instanceof List3<?>
: "" + "Violation of: source is of dynamic type List3<?>";
/*
* This cast cannot fail since the assert above would have stopped
* execution in that case: source must be of dynamic type List3<?>, and
* the ? must be T or the call would not have compiled.
*/
List3<T> localSource = (List3<T>) source;
this.preStart = localSource.preStart;
this.lastLeft = localSource.lastLeft;
this.postFinish = localSource.postFinish;
this.leftLength = localSource.leftLength;
this.rightLength = localSource.rightLength;
localSource.createNewRep();
assert this.conventionHolds();
assert localSource.conventionHolds();
}
@Override
public final void addRightFront(T x) {
assert x != null : "Violation of: x is not null";
Node p = new Node();
p.data = x;
p.next = this.lastLeft.next;
p.previous = this.lastLeft;
this.lastLeft.next.previous = p;
this.lastLeft.next = p;
this.rightLength++;
assert this.conventionHolds();
}
@Override
public final T removeRightFront() {
assert this.rightLength() > 0 : "Violation of: this.right /= <>";
T data = this.lastLeft.next.data;
this.lastLeft.next = this.lastLeft.next.next;
this.lastLeft.next.previous = this.lastLeft;
this.rightLength--;
assert this.conventionHolds();
return data;
}
@Override
public final void advance() {
assert this.rightLength() > 0 : "Violation of: this.right /= <>";
this.lastLeft = this.lastLeft.next;
this.leftLength++;
this.rightLength--;
assert this.conventionHolds();
}
@Override
public final void moveToStart() {
this.lastLeft = this.preStart;
this.rightLength += this.leftLength;
this.leftLength = 0;
assert this.conventionHolds();
}
@Override
public final int leftLength() {
assert this.conventionHolds();
return this.leftLength;
}
@Override
public final int rightLength() {
assert this.conventionHolds();
return this.rightLength;
}
@Override
public final Iterator<T> iterator() {
assert this.conventionHolds();
return new List3Iterator();
}
/**
* Implementation of {@code Iterator} interface for {@code List3}.
*/
private final class List3Iterator implements Iterator<T> {
/**
* Current node in the linked list.
*/
private Node current;
/**
* No-argument constructor.
*/
private List3Iterator() {
this.current = List3.this.preStart.next;
assert List3.this.conventionHolds();
}
@Override
public boolean hasNext() {
return this.current != List3.this.postFinish;
}
@Override
public T next() {
assert this.hasNext() : "Violation of: ~this.unseen /= <>";
if (!this.hasNext()) {
/*
* Exception is supposed to be thrown in this case, but with
* assertion-checking enabled it cannot happen because of assert
* above.
*/
throw new NoSuchElementException();
}
T x = this.current.data;
this.current = this.current.next;
assert List3.this.conventionHolds();
return x;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove operation not supported");
}
}
/*
* Other methods (overridden for performance reasons) ---------------------
*/
@Override
public final void moveToFinish() {
this.lastLeft = this.postFinish.previous;
this.leftLength += this.rightLength;
this.rightLength = 0;
assert this.conventionHolds();
}
@Override
public final void retreat() {
assert this.leftLength() > 0 : "Violation of: this.left /= <>";
this.lastLeft = this.lastLeft.previous;
this.rightLength++;
this.leftLength--;
assert this.conventionHolds();
}
}