| 1 | // An implementation of queues, based on arrays. |
|---|
| 2 | // (c) 1998, 2001 duane a. bailey |
|---|
| 3 | |
|---|
| 4 | package planet.util; |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * An implementation of queues based on arrays. |
|---|
| 9 | * The head of the queue starts out at the head of the array, allowing the queue to |
|---|
| 10 | * grow and shrink in constant time. |
|---|
| 11 | * This queue implementation is ideal for |
|---|
| 12 | * applications that require a queue with a known maximum size that expands |
|---|
| 13 | * in constant time. |
|---|
| 14 | * <P> |
|---|
| 15 | * Example usage: |
|---|
| 16 | * <P> |
|---|
| 17 | * To compute the sum of the unicode value of every character in the standard input |
|---|
| 18 | * we could use the following: |
|---|
| 19 | * <P> |
|---|
| 20 | * <pre> |
|---|
| 21 | * public static void main(String[] arguments) |
|---|
| 22 | * { |
|---|
| 23 | * int charsInInput = QueueExample.countChars(argument); |
|---|
| 24 | * {@link Queue} q = new {@link #Queue(int) Queue(charsInInput)}; |
|---|
| 25 | * int unicodeSum = 0; |
|---|
| 26 | * |
|---|
| 27 | * if(arguments.length > 0){ |
|---|
| 28 | * for(int i=0; i < arguments.length; i++){ |
|---|
| 29 | * for(int j=0; j < arguments[i].length(); j++){ |
|---|
| 30 | * q.{@link #add(Object) #remove(new Character(arguments[i].charAt(j)))}; |
|---|
| 31 | * } |
|---|
| 32 | * } |
|---|
| 33 | * } |
|---|
| 34 | * |
|---|
| 35 | * while(!q.{@link #isEmpty()}){ |
|---|
| 36 | * char c = ((Character)q.{@link #remove()}).charValue(); |
|---|
| 37 | * unicodeSum+=Character.getNumericValue(c); |
|---|
| 38 | * } |
|---|
| 39 | * |
|---|
| 40 | * System.out.println("Total Value: " + unicodeSum); |
|---|
| 41 | * } |
|---|
| 42 | * </pre> |
|---|
| 43 | * @version $Id: Queue.java,v 1.1 2004/03/11 11:22:31 jordi Exp $ |
|---|
| 44 | * @author 2001 duane a. bailey |
|---|
| 45 | */ |
|---|
| 46 | public class Queue implements java.io.Serializable |
|---|
| 47 | { |
|---|
| 48 | /** |
|---|
| 49 | * The references to values stored within the queue. |
|---|
| 50 | */ |
|---|
| 51 | protected Object data[]; // an array of data |
|---|
| 52 | /** |
|---|
| 53 | * index of the head of queue. |
|---|
| 54 | */ |
|---|
| 55 | protected int head; // next dequeue-able value |
|---|
| 56 | /** |
|---|
| 57 | * current size of queue |
|---|
| 58 | */ |
|---|
| 59 | protected int count; // current size of queue |
|---|
| 60 | protected long total_count; //Total number in Queue |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Construct a queue holding at most size elements. |
|---|
| 64 | * |
|---|
| 65 | * Postconditions: create a queue capable of holding at most size values |
|---|
| 66 | * |
|---|
| 67 | * @param size The maximum size of the queue. |
|---|
| 68 | */ |
|---|
| 69 | public Queue(int size) |
|---|
| 70 | { |
|---|
| 71 | data = new Object[size]; |
|---|
| 72 | head = 0; |
|---|
| 73 | count = 0; |
|---|
| 74 | total_count = 0; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Add a value to the tail of the queue. |
|---|
| 79 | * Preconditions: the queue is not full |
|---|
| 80 | * Postconditions: the value is added to the tail of the structure |
|---|
| 81 | * |
|---|
| 82 | * @param value The value added. |
|---|
| 83 | */ |
|---|
| 84 | public void add(Object value) throws QueueFull |
|---|
| 85 | { |
|---|
| 86 | if (isFull()) { |
|---|
| 87 | throw new QueueFull(); |
|---|
| 88 | |
|---|
| 89 | } |
|---|
| 90 | int tail = (head + count) % data.length; |
|---|
| 91 | data[tail] = value; |
|---|
| 92 | count++; |
|---|
| 93 | total_count++; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Remove a value from the head of the queue. |
|---|
| 98 | * |
|---|
| 99 | * Preconditions: the queue is not empty |
|---|
| 100 | * Postconditions: the head of the queue is removed and returned |
|---|
| 101 | * |
|---|
| 102 | * @return The value actually removed. |
|---|
| 103 | */ |
|---|
| 104 | public Object remove() |
|---|
| 105 | { |
|---|
| 106 | Object value = data[head]; |
|---|
| 107 | data[head] = null; //free reference |
|---|
| 108 | head = (head + 1) % data.length; |
|---|
| 109 | count--; |
|---|
| 110 | return value; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Fetch the value at the head of the queue. |
|---|
| 115 | * |
|---|
| 116 | * Preconditions: the queue is not empty |
|---|
| 117 | * Postconditions: the element at the head of the queue is returned |
|---|
| 118 | * |
|---|
| 119 | * @return Reference to the first value of the queue. |
|---|
| 120 | */ |
|---|
| 121 | public Object get() |
|---|
| 122 | { |
|---|
| 123 | return data[head]; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Determine the number of elements within the queue |
|---|
| 128 | * |
|---|
| 129 | * Postconditions: returns the number of elements in the queue |
|---|
| 130 | * |
|---|
| 131 | * @return The number of elements within the queue. |
|---|
| 132 | */ |
|---|
| 133 | public int size() |
|---|
| 134 | { |
|---|
| 135 | return count; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Remove all the values from the queue. |
|---|
| 140 | * |
|---|
| 141 | * Postconditions: removes all elements from the queue |
|---|
| 142 | */ |
|---|
| 143 | public void clear() |
|---|
| 144 | { |
|---|
| 145 | // we could remove all the elements from the queue |
|---|
| 146 | count = 0; |
|---|
| 147 | head = 0; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Determines if the queue is not able to accept any new values. |
|---|
| 152 | * |
|---|
| 153 | * Postconditions: returns true if the queue is at its capacity |
|---|
| 154 | * |
|---|
| 155 | * @return True iff the queue is full. |
|---|
| 156 | */ |
|---|
| 157 | public boolean isFull() |
|---|
| 158 | { |
|---|
| 159 | return count == data.length; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Determine if the queue is empty. |
|---|
| 164 | * |
|---|
| 165 | * Postconditions: returns true iff the queue is empty |
|---|
| 166 | * |
|---|
| 167 | * @return True iff the queue is empty. |
|---|
| 168 | */ |
|---|
| 169 | public boolean isEmpty() |
|---|
| 170 | { |
|---|
| 171 | return count == 0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * Construct a string representation of the queue. |
|---|
| 177 | * |
|---|
| 178 | * Postconditions: returns string representation of queue |
|---|
| 179 | * |
|---|
| 180 | * @return String representing the queue. |
|---|
| 181 | */ |
|---|
| 182 | public String toString() |
|---|
| 183 | { |
|---|
| 184 | StringBuffer s = new StringBuffer(); |
|---|
| 185 | int i,l; |
|---|
| 186 | |
|---|
| 187 | s.append("<QueueArray:"); |
|---|
| 188 | for (i = 0, l = head; i < count; i++, l = (l+1)%data.length) |
|---|
| 189 | { |
|---|
| 190 | s.append("[" + l + "]"+data[l]); |
|---|
| 191 | } |
|---|
| 192 | s.append(">"); |
|---|
| 193 | return s.toString(); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * get the totalCount in Queue since the Queue be created |
|---|
| 198 | */ |
|---|
| 199 | public long getTotalCount(){ |
|---|
| 200 | return total_count; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|