1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
12 * University of California, Irvine
14 * =============================================================================
16 * Unless otherwise noted, the following license applies to STAMP files:
18 * Copyright (c) 2007, Stanford University
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions are
25 * * Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in
30 * the documentation and/or other materials provided with the
33 * * Neither the name of Stanford University nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
38 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
47 * THE POSSIBILITY OF SUCH DAMAGE.
49 * =============================================================================
52 #define QUEUE_GROWTH_FACTOR 2
54 public class Queue_t {
55 int pop; /* points before element to pop */
60 /* =============================================================================
62 * =============================================================================
64 public Queue_t(int initCapacity) {
65 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
66 elements = new Object[capacity];
69 this.capacity = capacity;
72 /* =============================================================================
74 * =============================================================================
76 public boolean queue_isEmpty () {
77 //int pop = queuePtr.pop;
78 //int push = queuePtr.push;
79 //int capacity = queuePtr.capacity;
81 return (pop + 1) % capacity == push;
85 /* =============================================================================
87 * =============================================================================
97 /* =============================================================================
99 * =============================================================================
104 /* =============================================================================
106 * =============================================================================
108 public boolean queue_push (Object dataPtr) {
111 System.out.println("push == pop in Queue.java");
117 int newPush = (push + 1) % capacity;
118 if (newPush == pop) {
120 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
121 Object[] newElements = new Object[newCapacity];
122 if (newElements == null) {
127 Object[] tmpelements = elements;
130 for (src = (pop + 1); src < push; src++, dst++) {
131 newElements[dst] = elements[src];
135 for (src = (pop + 1); src < capacity; src++, dst++) {
136 newElements[dst] = elements[src];
138 for (src = 0; src < push; src++, dst++) {
139 newElements[dst] = elements[src];
144 elements = newElements;
145 pop = newCapacity - 1;
146 capacity = newCapacity;
148 newPush = push + 1; /* no need modulo */
151 elements[push] = dataPtr;
158 /* =============================================================================
160 * =============================================================================
163 Pqueue_push (Queue_t queuePtr, Object dataPtr)
165 int pop = queuePtr.pop;
166 int push = queuePtr.push;
167 int capacity = queuePtr.capacity;
170 System.out.println("push == pop in Queue.java");
175 int newPush = (push + 1) % capacity;
176 if (newPush == pop) {
178 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
179 Object[] newElements = new Object[newCapacity];
180 if (newElements == null) {
185 Object[] elements = queuePtr.elements;
188 for (src = (pop + 1); src < push; src++, dst++) {
189 newElements[dst] = elements[src];
193 for (src = (pop + 1); src < capacity; src++, dst++) {
194 newElements[dst] = elements[src];
196 for (src = 0; src < push; src++, dst++) {
197 newElements[dst] = elements[src];
202 queuePtr.elements = newElements;
203 queuePtr.pop = newCapacity - 1;
204 queuePtr.capacity = newCapacity;
206 newPush = push + 1; /* no need modulo */
210 queuePtr.elements[push] = dataPtr;
211 queuePtr.push = newPush;
216 /* =============================================================================
218 * =============================================================================
220 public Object queue_pop () {
221 int newPop = (pop + 1) % capacity;
222 if (newPop == push) {
226 //Object dataPtr = queuePtr.elements[newPop];
227 //queuePtr.pop = newPop;
228 Object dataPtr = elements[newPop];
236 /* =============================================================================
240 * =============================================================================