1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
10 * Ported to Java June 2009 Alokika Dash
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
55 int pop; /* points before element to pop */
63 /* =============================================================================
65 * =============================================================================
67 public static Queue queue_alloc (int initCapacity)
69 Queue queuePtr = new Queue();
71 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
72 queuePtr.elements = new int[capacity];
73 if (queuePtr.elements == null) {
77 queuePtr.pop = capacity - 1;
79 queuePtr.capacity = capacity;
85 /* =============================================================================
87 * =============================================================================
90 Pqueue_alloc (int initCapacity)
92 Queue queuePtr = new Queue();
94 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
95 queuePtr.elements = new int[capacity];
96 if (queuePtr.elements == null) {
100 queuePtr.pop = capacity - 1;
102 queuePtr.capacity = capacity;
108 /* =============================================================================
110 * =============================================================================
112 public Queue TMqueue_alloc (int initCapacity)
114 Queue queuePtr = new Queue();
116 int capacity = ((initCapacity < 2) ? 2 : initCapacity);
117 queuePtr.elements = new int[capacity];
118 if (queuePtr.elements == null) {
122 queuePtr.pop = capacity - 1;
124 queuePtr.capacity = capacity;
130 /* =============================================================================
132 * =============================================================================
141 /* =============================================================================
143 * =============================================================================
152 /* =============================================================================
154 * =============================================================================
163 /* =============================================================================
165 * =============================================================================
170 return (((pop + 1) % capacity == push) ? true : false);
174 /* =============================================================================
176 * =============================================================================
186 /* =============================================================================
188 * =============================================================================
191 TMqueue_isEmpty (Queue queuePtr)
193 int pop = queuePtr.pop;
194 int push = queuePtr.push;
195 int capacity = queuePtr.capacity;
197 return (((pop + 1) % capacity == push) ? true : false);
201 /* =============================================================================
203 * =============================================================================
206 queue_shuffle (Queue queuePtr, Random randomPtr)
208 int pop = queuePtr.pop;
209 int push = queuePtr.push;
210 int capacity = queuePtr.capacity;
214 numElement = push - (pop + 1);
216 numElement = capacity - (pop - push + 1);
219 int[] elements = queuePtr.elements;
222 for (i = 0; i < numElement; i++) {
223 int r1 = (int) (randomPtr.random_generate() % numElement);
224 int r2 = (int) (randomPtr.random_generate() % numElement);
225 int i1 = (base + r1) % capacity;
226 int i2 = (base + r2) % capacity;
227 int tmp = elements[i1];
228 elements[i1] = elements[i2];
234 /* =============================================================================
236 * =============================================================================
239 queue_push (int dataPtr)
242 System.out.println("push == pop in Queue.java");
247 int newPush = (push + 1) % capacity;
248 if (newPush == pop) {
250 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
251 int[] newElements = new int[newCapacity];
252 if (newElements == null) {
257 int[] tmpelements = elements;
260 for (src = (pop + 1); src < push; src++, dst++) {
261 newElements[dst] = elements[src];
265 for (src = (pop + 1); src < capacity; src++, dst++) {
266 newElements[dst] = elements[src];
268 for (src = 0; src < push; src++, dst++) {
269 newElements[dst] = elements[src];
273 elements = newElements;
274 pop = newCapacity - 1;
275 capacity = newCapacity;
277 newPush = push + 1; /* no need modulo */
280 elements[push] = dataPtr;
287 /* =============================================================================
289 * =============================================================================
292 Pqueue_push (Queue queuePtr, int dataPtr)
294 int pop = queuePtr.pop;
295 int push = queuePtr.push;
296 int capacity = queuePtr.capacity;
299 System.out.println("push == pop in Queue.java");
304 int newPush = (push + 1) % capacity;
305 if (newPush == pop) {
307 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
308 int[] newElements = new int[newCapacity];
309 if (newElements == null) {
314 int[] elements = queuePtr.elements;
317 for (src = (pop + 1); src < push; src++, dst++) {
318 newElements[dst] = elements[src];
322 for (src = (pop + 1); src < capacity; src++, dst++) {
323 newElements[dst] = elements[src];
325 for (src = 0; src < push; src++, dst++) {
326 newElements[dst] = elements[src];
331 queuePtr.elements = newElements;
332 queuePtr.pop = newCapacity - 1;
333 queuePtr.capacity = newCapacity;
335 newPush = push + 1; /* no need modulo */
339 queuePtr.elements[push] = dataPtr;
340 queuePtr.push = newPush;
346 /* =============================================================================
348 * =============================================================================
351 TMqueue_push (Queue queuePtr, int dataPtr)
353 int pop = (queuePtr.pop);
354 int push = (queuePtr.push);
355 int capacity = (queuePtr.capacity);
358 System.out.println("push == pop in Queue.java");
363 int newPush = (push + 1) % capacity;
364 if (newPush == pop) {
365 int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
366 int[] newElements = new int[newCapacity];
367 if (newElements == null) {
372 int[] elements = queuePtr.elements;
375 for (src = (pop + 1); src < push; src++, dst++) {
376 newElements[dst] = (elements[src]);
380 for (src = (pop + 1); src < capacity; src++, dst++) {
381 newElements[dst] = (elements[src]);
383 for (src = 0; src < push; src++, dst++) {
384 newElements[dst] = (elements[src]);
389 queuePtr.elements = newElements;
390 queuePtr.pop = newCapacity - 1;
391 queuePtr.capacity = newCapacity;
393 newPush = push + 1; /* no need modulo */
397 int[] elements = queuePtr.elements;
398 elements[push] = dataPtr;
399 queuePtr.push = newPush;
405 /* =============================================================================
407 * =============================================================================
412 int newPop = (pop + 1) % capacity;
413 if (newPop == push) {
417 int dataPtr = elements[newPop];
424 /* =============================================================================
426 * =============================================================================
429 TMqueue_pop (Queue queuePtr)
431 int pop = queuePtr.pop;
432 int push = queuePtr.push;
433 int capacity = queuePtr.capacity;
435 int newPop = (pop + 1) % capacity;
436 if (newPop == push) {
440 int[] elements = queuePtr.elements;
441 int dataPtr = elements[newPop];
442 queuePtr.pop = newPop;
448 * main method for testing
451 public static void main(String[] args) {
452 testQueue queuePtr = testQueue.queue_alloc(-1);
454 if(queuePtr.queue_isEmpty())
455 System.out.println("Queue is empty");
457 for(int i = 0; i<numData; i++) {
458 System.out.println("Inserting " + i);
459 queuePtr.queue_push(i);
462 for(int i = 0; i<numData; i++) {
463 int val = queuePtr.queue_pop();
464 System.out.println("Removing " + val);
467 if(queuePtr.queue_isEmpty())
468 System.out.println("Queue is empty");
472 /* =============================================================================
476 * =============================================================================