52 private Entry root =
null;
55 private int modCount = 0;
73 boolean found =
false;
80 if (cursor.left ==
null) {
81 cursor.left =
add(ev, cursor);
87 if (cursor.right ==
null) {
88 cursor.right =
add(ev, cursor);
91 cursor = cursor.right;
106 if (cursor !=
null) {
107 while (cursor.left !=
null)
108 cursor = cursor.left;
110 Entry e =
add(ev, cursor.father);
115 cursor.father.left = e;
118 root =
add(ev,
null);
124 Entry otherEntry = findEntry(other);
125 Entry evEntry =
add(ev,
null);
127 if (otherEntry ==
null)
128 throw new IllegalArgumentException(
"other not in the tree");
132 if (otherEntry != root) {
133 if (otherEntry == otherEntry.father.right)
134 otherEntry.father.right = evEntry;
136 otherEntry.father.left = evEntry;
140 evEntry.father = otherEntry.father;
141 otherEntry.father = evEntry;
142 evEntry.right = otherEntry;
148 evEntry.left = otherEntry.left;
150 if (evEntry.left !=
null)
151 evEntry.left.father = evEntry;
153 otherEntry.left =
null;
160 Entry otherEntry = findEntry(other);
162 if (otherEntry ==
null)
163 throw new IllegalArgumentException(
"other not in the tree");
166 Entry evEntry =
add(ev, otherEntry);
168 evEntry.right = otherEntry.right;
169 otherEntry.right = evEntry;
171 if (evEntry.right !=
null)
172 evEntry.right.father = evEntry;
181 while (cursor.left !=
null)
182 cursor = cursor.left;
189 while (cursor.left !=
null)
190 cursor = cursor.left;
192 while (cursor !=
null) {
193 if (cursor.event.getClass().getName().equals(cl))
195 cursor = successor(cursor);
200 @SuppressWarnings(
"unchecked")
204 while (cursor.left !=
null)
205 cursor = cursor.left;
207 while (cursor !=
null) {
208 if (cursor.event.getClass() == cl)
209 return (E) cursor.event;
210 cursor = successor(cursor);
215 public Iterator<Event> iterator() {
224 Entry evEntry = findEntry(ev);
228 return remove(evEntry);
236 while (cursor.left !=
null)
237 cursor = cursor.left;
239 Event first = cursor.event;
246 public String toString() {
247 StringBuilder sb =
new StringBuilder(
"Contents of the event list BinaryTree:");
251 while (cursor.left !=
null)
252 cursor = cursor.left;
254 while (cursor !=
null) {
256 +
PrintfFormat.
g(8, 4, cursor.event.priority()) +
" : " + cursor.event.toString());
257 cursor = successor(cursor);
260 return sb.toString();
263 private Entry
add(Event ev, Entry father) {
264 return new Entry(ev,
null,
null, father);
267 private boolean remove(Entry e) {
268 boolean filsGauche =
false;
269 boolean isRoot =
false;
275 if (e == e.father.left)
282 if (e.left ==
null) {
286 e.father.left = e.right;
288 e.father.right = e.right;
291 e.right.father = e.father;
292 }
else if (e.right ==
null) {
297 e.father.left = e.left;
299 e.father.right = e.left;
300 e.left.father = e.father;
307 if (cursor.left ==
null) {
313 e.father.left = cursor;
315 e.father.right = cursor;
317 cursor.left = e.left;
320 while (cursor.left !=
null)
321 cursor = cursor.left;
326 e.father.left = cursor;
328 e.father.right = cursor;
331 cursor.father.left = cursor.right;
332 if (cursor.right !=
null)
333 cursor.right.father = cursor.father;
335 cursor.right = e.right;
336 cursor.left = e.left;
337 e.right.father = cursor;
340 cursor.father = e.father;
341 e.left.father = cursor;
353 private Entry successor(Entry cursor) {
357 if (cursor.right !=
null) {
358 cursor = cursor.right;
359 while (cursor.left !=
null)
360 cursor = cursor.left;
362 while (cursor.father !=
null && cursor.father.right == cursor)
363 cursor = cursor.father;
364 cursor = cursor.father;
372 private Entry findEntry(Event ev) {
374 while (cursor !=
null) {
375 if (cursor.event == ev)
377 else if (ev.compareTo(cursor.event) < 0)
378 cursor = cursor.left;
380 cursor = cursor.right;
385 private Entry predecessor(Entry cursor) {
389 if (cursor.left !=
null) {
390 cursor = cursor.left;
391 while (cursor.right !=
null)
392 cursor = cursor.right;
394 while (cursor.father !=
null && cursor.father.left == cursor)
395 cursor = cursor.father;
396 cursor = cursor.father;
404 private static class Entry {
410 Entry(Event event, Entry left, Entry right, Entry father) {
414 this.father = father;
418 private class BTItr
implements ListIterator<Event> {
421 private Entry lastRet;
422 private int expectedModCount;
423 private int nextIndex;
429 while (next.left !=
null)
432 expectedModCount = modCount;
437 public void add(Event ev) {
438 if (modCount != expectedModCount)
439 throw new ConcurrentModificationException();
442 if (next !=
null && ev.compareTo(next.event) > 0) {
443 ev.setTime(next.event.time());
444 ev.setPriority(next.event.priority());
446 if (prev !=
null && ev.compareTo(prev.event) < 0) {
447 ev.setTime(prev.event.time());
448 ev.setPriority(prev.event.priority());
451 Entry e = BinaryTree.this.add(ev, next);
456 e.right = prev.right;
464 if (next == next.father.left)
465 next.father.left = e;
467 next.father.right = e;
470 e.father = prev.father;
476 e.right = prev.right;
489 public boolean hasNext() {
490 if (modCount != expectedModCount)
491 throw new ConcurrentModificationException();
495 public boolean hasPrevious() {
496 if (modCount != expectedModCount)
497 throw new ConcurrentModificationException();
501 public Event next() {
503 throw new NoSuchElementException();
506 Event ev = next.event;
509 next = successor(next);
513 public int nextIndex() {
515 throw new NoSuchElementException();
520 public Event previous() {
522 throw new NoSuchElementException();
525 Event ev = prev.event;
528 prev = predecessor(prev);
532 public int previousIndex() {
534 throw new NoSuchElementException();
536 return nextIndex - 1;
539 public void remove() {
540 if (modCount != expectedModCount)
541 throw new ConcurrentModificationException();
543 throw new IllegalStateException();
546 next = successor(next);
548 prev = predecessor(prev);
551 BinaryTree.this.remove(lastRet);
556 public void set(Event ev) {
557 if (modCount != expectedModCount)
558 throw new ConcurrentModificationException();
560 throw new IllegalStateException();
562 Entry pred = predecessor(lastRet);
563 Entry succ = successor(lastRet);
564 if (pred !=
null && ev.compareTo(pred.event) < 0) {
565 ev.setTime(pred.event.time());
566 ev.setPriority(pred.event.priority());
568 if (succ !=
null && ev.compareTo(succ.event) > 0) {
569 ev.setTime(succ.event.time());
570 ev.setPriority(succ.event.priority());