Skip to content

Commit

Permalink
Merge pull request #9 from chwyean/master
Browse files Browse the repository at this point in the history
EventList implementations: Remove static nodes and synchronization code
  • Loading branch information
chwyean authored Apr 11, 2017
2 parents 6d8475e + e07ca0b commit fed570a
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 146 deletions.
1 change: 0 additions & 1 deletion src/main/java/umontreal/ssj/simevents/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import umontreal.ssj.simevents.eventlist.EventList;
import umontreal.ssj.simevents.eventlist.SplayTree;
import java.util.ListIterator;

/**
* Represents the executive of a discrete-event simulator. This class
Expand Down
59 changes: 30 additions & 29 deletions src/main/java/umontreal/ssj/simevents/eventlist/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@
import java.util.ListIterator;
import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
import umontreal.ssj.util.PrintfFormat;
import umontreal.ssj.simevents.Event;
import umontreal.ssj.util.PrintfFormat;

/**
* Implantation de l'interface EventList ayant comme structure de
* donnees un arbre binaire.
*/

/**
* An implementation of @ref EventList using a binary search tree. Every
Expand All @@ -55,24 +50,25 @@ public class BinaryTree implements EventList {
// racine de l'arbre
private Entry root = null;

// liste d'objets qui peuvent etre reutilises
private Entry freeEntries = null;

// compteur de modifications sur l'iterateur.
private int modCount = 0;

@Override
public boolean isEmpty() {
return root == null;
}

@Override
public void clear() {
while (root != null)
remove (root);
}

@Override
public void add (Event ev) {
// fonction qui ajoute un evenement dans l'arbre
// note : si deux evenements ont le meme temps, alors il faut
// toujours faire en sorte que ces evenements se retrouvement
// toujours faire en sorte que ces evenements se retrouvent
// comme les fils droits les uns des autres

Entry cursor = root;
Expand Down Expand Up @@ -101,6 +97,7 @@ public void add (Event ev) {
++modCount;
}

@Override
public void addFirst (Event ev) {
/**
* Ajoute "ev" comme premier evenement dans l'arbre.
Expand Down Expand Up @@ -128,6 +125,7 @@ public void addFirst (Event ev) {
++modCount;
}

@Override
public void addBefore (Event ev, Event other) {
Entry otherEntry = findEntry (other);
Entry evEntry = add (ev , null);
Expand Down Expand Up @@ -164,6 +162,7 @@ public void addBefore (Event ev, Event other) {
++modCount;
}

@Override
public void addAfter (Event ev, Event other) {
// on va chercher le "Entry" de other
Entry otherEntry = findEntry (other);
Expand All @@ -183,6 +182,7 @@ public void addAfter (Event ev, Event other) {
++modCount;
}

@Override
public Event getFirst() {
if (root==null)
return null;
Expand All @@ -192,6 +192,7 @@ public Event getFirst() {
return cursor.event;
}

@Override
public Event getFirstOfClass (String cl) {
Entry cursor = root;
if (root != null)
Expand All @@ -207,6 +208,7 @@ public Event getFirstOfClass (String cl) {
}

@SuppressWarnings("unchecked")
@Override
public <E extends Event> E getFirstOfClass (Class<E> cl) {
Entry cursor = root;
if (root != null)
Expand All @@ -221,14 +223,17 @@ public <E extends Event> E getFirstOfClass (Class<E> cl) {
return null;
}

@Override
public Iterator<Event> iterator() {
return listIterator();
}

@Override
public ListIterator<Event> listIterator() {
return new BTItr();
}

@Override
public boolean remove (Event ev) {
Entry evEntry = findEntry(ev);
if (evEntry == null)
Expand All @@ -237,6 +242,7 @@ public boolean remove (Event ev) {
return remove(evEntry);
}

@Override
public Event removeFirst() {
if (root == null)
return null;
Expand All @@ -251,8 +257,9 @@ public Event removeFirst() {
return first;
}

@Override
public String toString() {
StringBuffer sb = new StringBuffer ("Contents of the event list BinaryTree:");
StringBuilder sb = new StringBuilder ("Contents of the event list BinaryTree:");
Entry cursor = root;

if (root != null)
Expand All @@ -271,19 +278,7 @@ public String toString() {
}

private Entry add (Event ev, Entry father) {
// On regarde la liste freeEntries
if (freeEntries != null) {
Entry tempo = freeEntries;
freeEntries = freeEntries.right;
tempo.event = ev;
tempo.left = null;
tempo.right = null;
tempo.father = father;
return tempo;
}
// on cree un nouvel objet
else
return new Entry(ev, null, null, father);
return new Entry(ev, null, null, father);
}

private boolean remove (Entry e) {
Expand Down Expand Up @@ -345,16 +340,14 @@ else if (filsGauche)
while (cursor.left != null)
cursor = cursor.left;

// echange entre e et cursor et elimination de e
cursor.father.left = cursor.right;

if (isRoot)
root = cursor;
else if (filsGauche)
e.father.left = cursor;
else
e.father.right = cursor;

// echange entre e et cursor et elimination de e
cursor.father.left = cursor.right;
if (cursor.right != null)
cursor.right.father = cursor.father;
Expand All @@ -369,11 +362,10 @@ else if (filsGauche)
}

// recupere l'espace du noeud
e.right = freeEntries;
e.right = null;
e.left = null;
e.event = null;
freeEntries = e;
e = null;

++modCount;
return true;
}
Expand Down Expand Up @@ -465,6 +457,7 @@ private class BTItr implements ListIterator<Event> {
nextIndex = 0;
}

@Override
public void add(Event ev) {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Expand Down Expand Up @@ -519,18 +512,21 @@ public void add(Event ev) {
++expectedModCount;
}

@Override
public boolean hasNext() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return next != null;
}

@Override
public boolean hasPrevious() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return prev != null;
}

@Override
public Event next() {
if (!hasNext())
throw new NoSuchElementException();
Expand All @@ -543,13 +539,15 @@ public Event next() {
return ev;
}

@Override
public int nextIndex() {
if (!hasNext())
throw new NoSuchElementException();

return nextIndex;
}

@Override
public Event previous() {
if (!hasPrevious())
throw new NoSuchElementException();
Expand All @@ -562,13 +560,15 @@ public Event previous() {
return ev;
}

@Override
public int previousIndex() {
if (!hasPrevious())
throw new NoSuchElementException();

return nextIndex - 1;
}

@Override
public void remove() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Expand All @@ -586,6 +586,7 @@ public void remove() {
++expectedModCount;
}

@Override
public void set (Event ev) {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Expand Down
Loading

0 comments on commit fed570a

Please sign in to comment.