66 private static final double MIN_VALUE = -10E38;
67 private static final double MAX_VALUE = 10E38;
69 private static final int ARRAY_LENGTH_INIT = 256;
71 private int modCount = 0;
73 private Entry firstEntry;
76 private Entry[] entryVec;
79 private int arrayLength;
83 Entry lastEntry =
new Entry(
null,
null,
null, MAX_VALUE);
84 firstEntry =
new Entry(
null,
null, lastEntry, MIN_VALUE);
85 lastEntry.left = firstEntry;
87 arrayLength = ARRAY_LENGTH_INIT;
88 entryVec =
new Entry[arrayLength];
91 entryVec[0] = lastEntry;
95 return firstEntry.right == entryVec[0];
102 firstEntry.right = entryVec[0];
103 entryVec[0].left = firstEntry;
107 for (
int i = 1; i < arrayLength; i++)
114 Entry prec = findEntry(ev,
false);
116 Entry e =
new Entry(ev, prec, prec.right, ev.
time());
123 Entry e =
new Entry(ev, firstEntry, firstEntry.right, ev.
time());
124 firstEntry.right.left = e;
125 firstEntry.right = e;
131 Entry otherEntry = findEntry(other,
true);
132 if (otherEntry ==
null)
133 throw new IllegalArgumentException(
"Event not in list.");
134 Entry e =
new Entry(ev, otherEntry.left, otherEntry, ev.
time());
135 otherEntry.left.right = e;
142 Entry otherEntry = findEntry(other,
true);
143 if (otherEntry ==
null)
144 throw new IllegalArgumentException(
"Event not in list.");
145 Entry e =
new Entry(ev, otherEntry, otherEntry.right, ev.
time());
146 otherEntry.right.left = e;
147 otherEntry.right = e;
153 return firstEntry.right.event;
157 Entry e = firstEntry.right;
158 while (e.right !=
null) {
159 if (e.event.getClass().getName().equals(cl))
166 @SuppressWarnings(
"unchecked")
169 Entry e = firstEntry.right;
170 while (e.right !=
null) {
171 if (e.event.getClass() == cl)
178 public Iterator<Event> iterator() {
187 Entry e = findEntry(ev,
true);
192 int i = findIndex(ev.time());
195 while (i < vectSize && entryVec[i].event !=
null && ev.compareTo(entryVec[i].event) == 0) {
196 if (entryVec[i].event == ev)
197 entryVec[i] = e.left;
203 e.left.right = e.right;
204 e.right.left = e.left;
217 if (entryVec[vectSize / 2].time <= firstEntry.right.time && vectSize > 1)
218 changeSize(vectSize / 2);
220 Entry e = firstEntry.right;
223 if (e == entryVec[0])
226 firstEntry.right = e.right;
227 e.right.left = firstEntry;
240 public String toString() {
241 StringBuilder sb =
new StringBuilder(
"Contents of the event list Henriksen:");
242 Entry e = firstEntry.right;
243 while (e.right !=
null) {
245 +
PrintfFormat.
g(8, 4, e.event.priority()) +
" : " + e.event.toString());
248 return sb.toString();
251 private static class Entry {
257 Entry(Event event, Entry left, Entry right,
double time) {
264 public String toString() {
265 return "[" +
event +
" |" + time +
"|]";
269 private class HItr
implements ListIterator<Event> {
272 private Entry lastRet;
273 private int expectedModCount;
274 private int nextIndex;
278 next = firstEntry.right;
279 expectedModCount = modCount;
284 public void add(Event ev) {
285 if (modCount != expectedModCount)
286 throw new ConcurrentModificationException();
289 if (ev.time() > next.time) {
290 ev.setTime(next.time);
291 ev.setPriority(next.event.priority());
293 if (ev.time() < prev.time) {
294 ev.setTime(prev.time);
295 ev.setPriority(prev.event.priority());
298 Entry e =
new Entry(ev, prev, next, ev.time());
309 public boolean hasNext() {
310 if (modCount != expectedModCount)
311 throw new ConcurrentModificationException();
312 return next != entryVec[0];
315 public boolean hasPrevious() {
316 if (modCount != expectedModCount)
317 throw new ConcurrentModificationException();
318 return next != firstEntry;
321 public Event next() {
323 throw new NoSuchElementException();
326 Event ev = next.event;
333 public int nextIndex() {
335 throw new NoSuchElementException();
340 public Event previous() {
342 throw new NoSuchElementException();
345 Event ev = prev.event;
352 public int previousIndex() {
354 throw new NoSuchElementException();
356 return nextIndex - 1;
359 public void remove() {
360 if (modCount != expectedModCount)
361 throw new ConcurrentModificationException();
363 throw new IllegalStateException();
365 if (lastRet == next) {
366 if (next != entryVec[0])
369 if (prev != firstEntry) {
376 double evtime = lastRet.time;
377 int i = findIndex(evtime);
380 while (i < vectSize && entryVec[i].time == evtime) {
381 if (entryVec[i].event == lastRet.event)
382 entryVec[i] = lastRet.left;
386 lastRet.event =
null;
387 lastRet.left.right = lastRet.right;
388 lastRet.right.left = lastRet.left;
390 lastRet.right =
null;
396 public void set(Event ev) {
397 if (modCount != expectedModCount)
398 throw new ConcurrentModificationException();
400 throw new IllegalStateException();
403 if (ev.time() < lastRet.left.time) {
404 ev.setTime(lastRet.left.time);
405 ev.setPriority(lastRet.left.event.priority());
407 if (ev.time() > lastRet.right.time) {
408 ev.setTime(lastRet.right.time);
409 ev.setPriority(lastRet.right.event.priority());
421 private void changeSize(
int newSize) {
423 if (newSize > arrayLength) {
424 Entry[] newVec =
new Entry[newSize];
425 for (
int i = 0; i < vectSize; i++)
426 newVec[i] = entryVec[i];
428 arrayLength = newSize;
432 for (
int i = vectSize; i < newSize; i++)
433 entryVec[i] = firstEntry;
444 private int findIndex(
double evtime) {
445 int i = vectSize / 2;
446 int j = vectSize / 4;
451 if (evtime >= entryVec[i].time)
458 if (evtime >= entryVec[i].time)
469 private Entry findEntry(Event ev,
boolean findEvent) {
470 double evtime = ev.time();
471 int i = findIndex(evtime);
473 Entry e = entryVec[i].left;
478 while (e.time >= evtime && ev.compareTo(e.event) < 0) {
482 if (i + 1 >= vectSize)
483 changeSize(vectSize * 2);
496 while (e != firstEntry && e.time == evtime && e.event != ev)