dblqueue.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include "dblqueue.hpp"
42 #include "regexp.hpp"
43 
44 // constructor: initialize the double queue
45 doubleQueue::doubleQueue(int n) : memory(NULL), size(n), head(n/2), tail((n/2)+1)
46 {
47  memory = (int*) malloc(sizeof(int)*size);
48  put(SCAN); // insert delimiter
49 }
50 
51 // destructor: free memory
53 {
54  if (memory) free(memory);
55 }
56 
57 /**************************************************/
58 /* doubleQueue::push */
59 /* */
60 /* push an element at the beginning of the queue. */
61 /**************************************************/
62 void doubleQueue::push(int value)
63 {
64  memory[head] = value;
65  if (head == 0) enlarge();
66  head--;
67 }
68 
69 /*******************************************/
70 /* doubleQueue::put */
71 /* */
72 /* put an element at the end of the queue. */
73 /*******************************************/
74 void doubleQueue::put(int value)
75 {
76  memory[tail] = value;
77  tail++;
78  if (tail == size) enlarge();
79 }
80 
81 /****************************************************/
82 /* doubleQueue::pop */
83 /* */
84 /* get the first element of the queue and remove it */
85 /* from the queue. */
86 /****************************************************/
88 {
89  int value = 0;
90 
91  if (head < tail) {
92  head++;
93  value = memory[head];
94  }
95 
96  return value;
97 }
98 
99 /*******************************************************/
100 /* doubleQueue::enlarge */
101 /* */
102 /* the queue is an array holding the elements. if the */
103 /* head or tail pointer pass the limits of the array, */
104 /* it needs to be enlarged. reallocation is used here. */
105 /*******************************************************/
107 {
108  int *newmemory = (int*) malloc(sizeof(int)*2*size);
109  int *oldmemory = memory;
110  int offset = size>>2;
111 
112  if (newmemory) {
113 // memset(newmemory,0x00,sizeof(int)*2*size);
114  memcpy(newmemory+offset,memory,size*sizeof(int));
115  head+=offset;
116  tail+=offset;
117  size<<=1;
118  memory = newmemory;
119  free(oldmemory);
120  }
121 }
doubleQueue(int n)
Definition: dblqueue.cpp:45
void put(int value)
Definition: dblqueue.cpp:74
int pop()
Definition: dblqueue.cpp:87
int * memory
Definition: dblqueue.hpp:72
void enlarge()
Definition: dblqueue.cpp:106
void push(int value)
Definition: dblqueue.cpp:62
#define SCAN
Definition: regexp.hpp:44