00001
package com.quadcap.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.*;
00042
00043
import java.util.Enumeration;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 public class DList {
00057
00058
00059 int size;
00060
00061
00062
00063 DListItem head;
00064
00065
00066
00067
00068 public DList() {
00069
head = null;
00070
size = 0;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public void resize(
int newsize) {
00080
while (
size < newsize)
addBack((Object)null);
00081
try {
00082
while (
size > newsize)
popBack();
00083 }
catch (
ListException e) {
00084
00085
throw new RuntimeException(e.toString());
00086 }
00087 }
00088
00089
00090
00091
00092
00093
00094 public int size() {
00095
return this.size;
00096 }
00097
00098
00099
00100
00101
00102
00103 public DListItem addFront(Object obj) {
00104
DListItem d =
new DListItem(obj);
00105 addFront(d);
00106
return d;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 public void addAfter(
DListItem d, Object obj) {
00116
DListItem e =
new DListItem(obj);
00117 e.
next = d.
next;
00118 e.
next.
prev = e;
00119 e.
prev = d;
00120 d.
next = e;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 public void addBefore(
DListItem d, Object obj) {
00130
DListItem e =
new DListItem(obj);
00131 e.
next = d;
00132 e.
prev = d.
prev;
00133 e.
prev.
next = e;
00134 d.
prev = e;
00135 }
00136
00137
00138
00139
00140
00141 public DListItem addBack(Object obj) {
00142
DListItem d =
new DListItem(obj);
00143 addBack(d);
00144
return d;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 public DListItem head() throws
ListException {
00154
if (head == null)
throw new ListException(
"head() of empty list");
00155
return head;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 public DListItem tail() throws
ListException {
00165
if (head == null)
throw new ListException(
"tail() of empty list");
00166
return head.
prev;
00167 }
00168
00169
00170
00171
00172
00173
00174 public DListItem popFront() throws
ListException {
00175
if (head == null)
throw new ListException(
"popFront() of empty list");
00176
DListItem d = head;
00177
unlink(d);
00178
if (d == d.
next) head = null;
00179
else head = d.
next;
00180
return d;
00181 }
00182
00183
00184
00185
00186
00187
00188 public DListItem popBack() throws
ListException {
00189
if (head == null)
throw new ListException(
"popBack() of empty list");
00190
DListItem d = head.
prev;
00191
unlink(d);
00192
if (d == d.
next) head = null;
00193
return d;
00194 }
00195
00196
00197
00198
00199
00200 public String
toString() {
00201 ByteArrayOutputStream bos =
new ByteArrayOutputStream();
00202
show(
new PrintStream(bos));
00203
return bos.toString();
00204 }
00205
00206
00207
00208
00209 public void show(PrintWriter os, String delim) {
00210 String delimiter =
"";
00211 os.print(
"(");
00212
if (head != null) {
00213
boolean started =
false;
00214
for (
DListItem d = head; !started || d != head; d = d.
next) {
00215
if (d.
obj == null)
continue;
00216 started =
true;
00217 os.print(delimiter + d.
obj);
00218 delimiter = delim;
00219 }
00220 }
00221 os.println(
")");
00222 }
00223
00224
00225
00226
00227
00228
00229 public void show(PrintStream os) {
00230 show(
new PrintWriter(os),
", ");
00231 }
00232
00233
00234
00235
00236
00237
00238 public void show(PrintWriter os) {
00239 show(os,
", ");
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 public final void moveFront(
DListItem d) {
00253
if (d != head) {
00254 d.
next.
prev = d.
prev;
00255 d.
prev.
next = d.
next;
00256 d.
next = head != null ? head : d;
00257 d.
prev = head != null ? head.
prev : d;
00258 d.
prev.
next = d;
00259 d.
next.
prev = d;
00260 head = d;
00261 }
00262 }
00263
00264
00265 final void addFront(
DListItem d) {
00266 addBack(d);
00267 head = d;
00268 }
00269
00270
00271 final void addBack(
DListItem d) {
00272 size++;
00273 d.
next = head != null ? head : d;
00274 d.
prev = head != null ? head.
prev : d;
00275 d.
prev.
next = d;
00276 d.
next.
prev = d;
00277
if (head == null) head = d;
00278 }
00279
00280
00281 public final void unlink(
DListItem d) {
00282
if (d != null) {
00283 d.
next.
prev = d.
prev;
00284 d.
prev.
next = d.
next;
00285
if (d == d.
next) {
00286 head = null;
00287 }
else if (head == d) {
00288 head = d.
next;
00289 }
00290 size--;
00291 }
00292 }
00293
00294
00295
00296
00297 public Enumeration
elements() {
00298
final DList thislist =
this;
00299
final DListItem thisd = head;
00300
00301
return new Enumeration() {
00302
DList dlist = thislist;
00303
DListItem d = thisd;
00304
boolean first =
true;
00305
public boolean hasMoreElements() {
00306
return d != null && first || d != dlist.
head;
00307 }
00308
public Object nextElement() {
00309 Object obj = d.
obj;
00310 d = d.
next;
00311 first =
false;
00312
return obj;
00313 }
00314 };
00315 }
00316 }