47 private Entry root =
null;
48 private int modCount = 0;
50 private int myCompareTo(
Event ev,
Event other) {
56 if (ev.getRa() < other.getRa())
58 if (ev.getRa() > other.getRa())
94 Entry e =
add(ev,
null);
100 boolean end_splay =
false;
102 if (myCompareTo(ev, next.event) > 0) {
110 }
else if (myCompareTo(ev, temp.event) < 0) {
118 next.right = temp.left;
119 if (temp.left !=
null)
120 temp.left.father = next;
141 }
else if (myCompareTo(ev, temp.event) > 0) {
149 next.left = temp.right;
150 if (temp.right !=
null)
151 temp.right.father = next;
175 root =
add(ev,
null);
178 while (cursor.left !=
null)
179 cursor = cursor.left;
180 cursor.left =
add(ev, cursor);
186 Entry otherEntry = findEntry(other);
187 if (otherEntry ==
null)
188 throw new IllegalArgumentException(
"Event not in list.");
190 Entry e =
add(ev,
null);
193 if (otherEntry != root) {
194 if (otherEntry == otherEntry.father.left)
195 otherEntry.father.left = e;
197 otherEntry.father.right = e;
200 e.father = otherEntry.father;
201 otherEntry.father = e;
202 e.right = otherEntry;
207 e.left = otherEntry.left;
210 otherEntry.left =
null;
215 Entry otherEntry = findEntry(other);
216 if (otherEntry ==
null)
217 throw new IllegalArgumentException(
"Event not in list.");
218 Entry e =
add(ev, otherEntry);
220 e.right = otherEntry.right;
221 otherEntry.right = e;
231 while (cursor.left !=
null)
232 cursor = cursor.left;
239 while (cursor.left !=
null)
240 cursor = cursor.left;
241 while (cursor !=
null) {
242 if (cursor.event.getClass().getName().equals(cl))
244 cursor = successor(cursor);
249 @SuppressWarnings(
"unchecked")
254 while (cursor.left !=
null)
255 cursor = cursor.left;
256 while (cursor !=
null) {
257 if (cursor.event.getClass() == cl)
258 return (E) cursor.event;
259 cursor = successor(cursor);
264 public Iterator<Event> iterator() {
276 Entry e = findEntry(ev);
286 while (cursor.left !=
null)
287 cursor = cursor.left;
288 Event first = cursor.event;
293 public String toString() {
294 StringBuilder sb =
new StringBuilder(
"Contents of the event list SplayTree:");
297 while (cursor.left !=
null)
298 cursor = cursor.left;
299 while (cursor !=
null) {
301 +
PrintfFormat.
g(8, 4, cursor.event.priority()) +
" : " + cursor.event.toString());
302 cursor = successor(cursor);
304 return sb.toString();
307 private static class Entry {
313 Entry(Event event, Entry left, Entry right, Entry father) {
317 this.father = father;
321 private class SPItr
implements ListIterator<Event> {
324 private Entry lastRet;
325 private int expectedModCount;
326 private int nextIndex;
332 while (next.left !=
null)
335 expectedModCount = modCount;
340 public void add(Event ev) {
341 if (modCount != expectedModCount)
342 throw new ConcurrentModificationException();
345 if (next !=
null && ev.compareTo(next.event) > 0) {
346 ev.setTime(next.event.time());
347 ev.setPriority(next.event.priority());
349 if (prev !=
null && ev.compareTo(prev.event) < 0) {
350 ev.setTime(prev.event.time());
351 ev.setPriority(prev.event.priority());
354 Entry e = SplayTree.this.add(ev,
null);
359 e.right = prev.right;
368 if (next == next.father.left)
369 next.father.left = e;
371 next.father.right = e;
374 e.father = next.father;
394 public boolean hasNext() {
395 if (modCount != expectedModCount)
396 throw new ConcurrentModificationException();
400 public boolean hasPrevious() {
401 if (modCount != expectedModCount)
402 throw new ConcurrentModificationException();
406 public Event next() {
408 throw new NoSuchElementException();
411 Event ev = next.event;
414 next = successor(next);
418 public int nextIndex() {
420 throw new NoSuchElementException();
425 public Event previous() {
427 throw new NoSuchElementException();
430 Event ev = prev.event;
433 prev = predecessor(prev);
437 public int previousIndex() {
439 throw new NoSuchElementException();
441 return nextIndex - 1;
444 public void remove() {
445 if (modCount != expectedModCount)
446 throw new ConcurrentModificationException();
448 throw new IllegalStateException();
451 next = successor(next);
453 prev = predecessor(prev);
456 SplayTree.this.remove(lastRet);
461 public void set(Event ev) {
462 if (modCount != expectedModCount)
463 throw new ConcurrentModificationException();
465 throw new IllegalStateException();
467 Entry pred = predecessor(lastRet);
468 Entry succ = successor(lastRet);
470 if (pred !=
null && ev.compareTo(pred.event) < 0) {
471 ev.setTime(pred.event.time());
472 ev.setPriority(pred.event.priority());
474 if (succ !=
null && ev.compareTo(succ.event) > 0) {
475 ev.setTime(succ.event.time());
476 ev.setPriority(succ.event.priority());
486 private Entry
add(Event ev, Entry father) {
487 return new Entry(ev,
null,
null, father);
503 private void splay(Entry e) {
510 while (e.father !=
null) {
525 }
else if (gf.right == f) {
571 }
else if (gf.left == f) {
587 if (gf.right !=
null)
588 gf.right.father = gf;
624 private boolean remove(Entry e) {
625 if (root ==
null || e ==
null)
631 Entry leftTree = e.left;
632 Entry rightTree = e.right;
634 if (leftTree !=
null)
635 leftTree.father =
null;
636 if (rightTree !=
null)
637 rightTree.father =
null;
643 if (rightTree ==
null)
645 else if (leftTree ==
null)
649 Entry newRoot = rightTree;
650 while (newRoot.left !=
null)
651 newRoot = newRoot.left;
657 newRoot.left = leftTree;
658 leftTree.father = newRoot;
670 private Entry findEntry(Event ev) {
672 while (cursor !=
null) {
673 if (cursor.event == ev)
675 else if (ev.compareTo(cursor.event) > 0)
676 cursor = cursor.right;
677 else if (ev.compareTo(cursor.event) < 0)
678 cursor = cursor.left;
684 Entry center = cursor;
687 while (cursor !=
null && ev.compareTo(cursor.event) == 0)
688 if (cursor.event == ev)
691 cursor = predecessor(cursor);
696 while (cursor !=
null && ev.compareTo(cursor.event) == 0)
697 if (cursor.event == ev)
700 cursor = successor(cursor);
708 private Entry successor(Entry cursor) {
712 if (cursor.right !=
null) {
713 cursor = cursor.right;
714 while (cursor.left !=
null)
715 cursor = cursor.left;
717 while (cursor.father !=
null && cursor.father.right == cursor)
718 cursor = cursor.father;
719 cursor = cursor.father;
724 private Entry predecessor(Entry cursor) {
728 if (cursor.left !=
null) {
729 cursor = cursor.left;
730 while (cursor.right !=
null)
731 cursor = cursor.right;
734 while (cursor.father !=
null && cursor.father.left == cursor)
735 cursor = cursor.father;
736 cursor = cursor.father;