summaryrefslogtreecommitdiff
path: root/java/EAC/Queue.java
blob: 20c4483a17a2d4386a48fe28db5cbc13009a8d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * Title:        Queue
 * Description:  The primary event queue for the Event Analysis Configurator
 */
package EAC;

public class Queue {

  private class Node {

    public Primitive primitive;
    public long time;
    public Node next;

    public Node(Primitive p, long t, Node n) {
      primitive = p;
      time = t;
      next = n;
    } /* constructor */

  } /* Node */

  private Node head;

  private int count;

  public Queue() {
    count = 0;
  } /* constructor */

  public boolean empty() {
    return (count == 0);
  } /* empty */

  public void clear() {
    while (!empty())
    try {
      dequeue();
    } catch (EmptyQueueException eqe) {
    // can't happen
    }
  } /* clear */

  public void enqueue(Primitive p, long t) {
    if (head == null) {
      head = new Node(p,t,null);
      count = 1;
    } else { // non-empty
      if ((head.time == t) && (head.primitive == p))
        return; // no duplicates
      else if (head.time >= t) { // need new head
        Node temp = new Node(p,t,head);
        head = temp;
      } else { // find insertion point
        Node ptr = head;

        while (ptr.next != null)
          if (ptr.next.time < t)
            ptr = ptr.next;
          else
            break;

        if (ptr.next == null)
          ptr.next = new Node(p,t,null);
        else if ((ptr.next.time == t) && (ptr.next.primitive == p))
          return; // no duplicates
        else {
          Node temp = new Node(p,t,ptr.next);
          ptr.next = temp;
        }
      }

      count++;
    } /* else */
  } /* enqueue */

  public Primitive dequeue () throws EmptyQueueException {
    if (count == 0)
      throw new EmptyQueueException("ERROR: dequeue called when queue empty");

    Primitive temp = head.primitive;
    head = head.next;
    --count;
    return temp;
  } /* dequeue */

  public long frontTime () throws EmptyQueueException {
    if (count == 0)
      throw new EmptyQueueException("ERROR: frontTime called when queue empty");

    return head.time;
  } /* frontTime */

}