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 private static int QUEUE_GROWTH_FACTOR;
57 int pop; /* points before element to pop */
63 QUEUE_GROWTH_FACTOR = 2;
66 /* =============================================================================
68 * =============================================================================
70 public Queue_t queue_alloc (int initCapacity)
72 Queue_t queuePtr = new Queue_t();
74 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
75 queuePtr.elements = new Object[capacity];
76 if (queuePtr.elements == null) {
80 queuePtr.pop = capacity - 1;
82 queuePtr.capacity = capacity;
88 /* =============================================================================
90 * =============================================================================
93 Pqueue_alloc (int initCapacity)
95 Queue_t queuePtr = new Queue_t();
97 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
98 queuePtr.elements = new Object[capacity];
99 if (queuePtr.elements == null) {
103 queuePtr.pop = capacity - 1;
105 queuePtr.capacity = capacity;
110 /* =============================================================================
112 * =============================================================================
115 queue_free (Queue_t queuePtr)
117 queuePtr.elements = null;
122 /* =============================================================================
124 * =============================================================================
127 Pqueue_free (Queue_t queuePtr)
129 queuePtr.elements = null;
134 /* =============================================================================
136 * =============================================================================
139 TMqueue_free (TM_ARGDECL Queue* queuePtr)
141 queuePtr.elements = null;
146 /* =============================================================================
148 * =============================================================================
153 //int pop = queuePtr.pop;
154 //int push = queuePtr.push;
155 //int capacity = queuePtr.capacity;
157 return (((pop + 1) % capacity == push) ? true : false);
161 /* =============================================================================
163 * =============================================================================
173 /* =============================================================================
175 * =============================================================================
180 /* =============================================================================
182 * =============================================================================
185 queue_push (Object dataPtr)
190 System.out.println("push == pop in Queue.java");
196 int newPush = (push + 1) % capacity;
197 if (newPush == pop) {
199 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
200 Object[] newElements = new Object[newCapacity];
201 if (newElements == null) {
206 Object[] tmpelements = elements;
209 for (src = (pop + 1); src < push; src++, dst++) {
210 newElements[dst] = elements[src];
214 for (src = (pop + 1); src < capacity; src++, dst++) {
215 newElements[dst] = elements[src];
217 for (src = 0; src < push; src++, dst++) {
218 newElements[dst] = elements[src];
223 elements = newElements;
224 pop = newCapacity - 1;
225 capacity = newCapacity;
227 newPush = push + 1; /* no need modulo */
230 elements[push] = dataPtr;
237 /* =============================================================================
239 * =============================================================================
242 Pqueue_push (Queue_t queuePtr, Object dataPtr)
244 int pop = queuePtr.pop;
245 int push = queuePtr.push;
246 int capacity = queuePtr.capacity;
249 System.out.println("push == pop in Queue.java");
254 int newPush = (push + 1) % capacity;
255 if (newPush == pop) {
257 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
258 Object[] newElements = new Object[newCapacity];
259 if (newElements == null) {
264 Object[] elements = queuePtr.elements;
267 for (src = (pop + 1); src < push; src++, dst++) {
268 newElements[dst] = elements[src];
272 for (src = (pop + 1); src < capacity; src++, dst++) {
273 newElements[dst] = elements[src];
275 for (src = 0; src < push; src++, dst++) {
276 newElements[dst] = elements[src];
281 queuePtr.elements = newElements;
282 queuePtr.pop = newCapacity - 1;
283 queuePtr.capacity = newCapacity;
285 newPush = push + 1; /* no need modulo */
289 queuePtr.elements[push] = dataPtr;
290 queuePtr.push = newPush;
295 /* =============================================================================
297 * =============================================================================
300 //queue_pop (Queue queuePtr)
303 //int pop = queuePtr.pop;
304 //int push = queuePtr.push;
305 //int capacity = queuePtr.capacity;
307 int newPop = (pop + 1) % capacity;
308 if (newPop == push) {
312 //Object dataPtr = queuePtr.elements[newPop];
313 //queuePtr.pop = newPop;
314 Object dataPtr = elements[newPop];
320 /* =============================================================================
321 * pop a certain amount
322 * =============================================================================
325 //TODO optimize by taking advantage of underlying data structure
326 public Queue_t get(int n, Queue_t original)
328 Queue_t subset = original.queue_alloc(n);
330 for(int i = 0; i < n; i++)
332 Object o = original.queue_pop();
333 subset.queue_push(o);
340 /* =============================================================================
344 * =============================================================================