File: stack.c

package info (click to toggle)
grpn 1.4.1-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 2,020 kB
  • sloc: ansic: 6,616; makefile: 72
file content (254 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*

Copyright (C) 2002  Paul Wilkins

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
/* stack.c  by Paul Wilkins */

#include <stdio.h>
#include <stdlib.h>

#include "stack.h"
#include "number.h"

#define STACK_CHUNK_SIZE 20

/* a stack element */
struct StackElem {
   struct StackElem *t;   /* the node above us */
   struct StackElem *b;   /* the node below us */
   Number *data;          /* the data */
};


/* we build the stack in these arrays */
struct StackChunk {
   int freeIndx;
   struct StackElem *freeList[STACK_CHUNK_SIZE];
   struct StackChunk *next;
   struct StackElem ary[STACK_CHUNK_SIZE];
};

/* The main stack of GRPN.  This is where we store the numbers */
struct Stack stack;

struct StackChunk *stackChunkHead = NULL;

struct Stack *getStack(){
   return &stack;
}

/* get the length of the stack */
int stackLen(){
   return stack.length;
}

/* malloc a new stack chunk */
struct StackChunk * newStackChunk(){
   int i;
   struct StackChunk *c;

   /* malloc a new chunk */
   if(NULL == (c=(struct StackChunk *)malloc(sizeof(struct StackChunk)))){
      perror("Malloc");
      exit(0);
   }

   /* initalize stuff */
   c->freeIndx = STACK_CHUNK_SIZE - 1;
   c->next = NULL;

   for(i=0; i<STACK_CHUNK_SIZE; i++){
      c->freeList[i] = &((c->ary)[i]);
      c->ary[i].data = NULL;
   }

   return c;
}

/* malloc a new stack element */
struct StackElem * newStackEle(){
   struct StackElem *s;
   struct StackChunk *c;

   /* find the first stack chunk with free elements */
   for(c=stackChunkHead; c->freeIndx==-1; c=c->next);

   /* get the StackEle */
   s = c->freeList[c->freeIndx];

   c->freeIndx--;

   /* if there are no more free elements */
   if(c->freeIndx < 0){

      c->freeIndx = -1;

      if(c->next == NULL) c->next = newStackChunk();
   }
 
   return s;
}

void stackAddToFreeList(struct StackElem *s){
   struct StackChunk *c;

   for(c=stackChunkHead; c!=NULL; c=c->next)
      if(s >= c->ary && s <= &((c->ary)[STACK_CHUNK_SIZE]))
         c->freeList[++(c->freeIndx)] = s;
}


int copyStack(struct Stack *srcStack, struct Stack *dstStack, int nelts){
   int i;
   struct StackElem *dst, *src;

   for(i=0, src=srcStack->head; 
       i<nelts && src;
       i++, src=src->t){

      /* set the stuff in the newly created stack element */
      dst = newStackEle();
      dst->data = src->data;
      dst->t = dstStack->head;
      dst->b = NULL;

      incRefcntNumber(dst->data);

      /* update the stuff in the ele above us */
      if(dst->t) dst->t->b = dst;

      /* update the dest stack */
      dstStack->head = dst;
      dstStack->length++;

   }

   if(i != nelts){
      fprintf(stderr, "copyStack: Error copying stack.\n");
      return 0;
   }
   return 1;
}


int setup_stack(){

   stack.head = NULL;
   stack.length = 0;

   stackChunkHead = newStackChunk();
   
   return 1;
}

void clearNamedStack(struct Stack *stk){
   struct StackElem *s;

   for(s=stk->head; s; s=s->t){
      decRefcntNumber(s->data);
      freeNumber(s->data);
      stackAddToFreeList(s);
   }
   stk->head = NULL;
   stk->length = 0;

}

void clearStack(){
   clearNamedStack(&stack);
}

void printStack(){
   char *c;
   struct StackElem *s = NULL;
   struct StackElem *p = NULL;

   /* find the top of the stack */
   for(s=stack.head; s; s=s->t) p = s;

   /* print the numbers starting from the top */
   for(s=p; p; p=p->b){
      c = printNumber(p->data);
      printf("%s\n", c);
      free(c);
   }
}

Number * getStackEle(int which){
   int i;
   struct StackElem *s = NULL;

   if(which < 0 || which >= stack.length) return NULL;

   s = stack.head;
   for(i=0; i<which; i++) s = s->t;

   return s->data;
}

int Push(Number *data){

   struct StackElem *s;

   /* set the stuff in the newly created stack element */
   s = newStackEle();
   s->data = data;
   s->t = stack.head;
   s->b = NULL;

   /* increment the ref count of the number */
   incRefcntNumber(data);

   /* update the stuff in the ele above us */
   if(s->t) s->t->b = s;

   /* update the head */
   stack.head = s;

   stack.length++;

   return 1;  /* success */
}

Number * Pop(){
   Number *ptr;
   struct StackElem *s;

   s = stack.head;

   /* nothing to pop */
   if(s == NULL) return NULL;

   /* update the stuff in the ele above us */
   if(s->t){
      s->t->b = NULL;
      stack.head = s->t;
   } 
   else stack.head = NULL;
   
   ptr = s->data;
   
   /* decrement the ref count of the number */
   decRefcntNumber(ptr);

   stackAddToFreeList(s);

   stack.length--;

   return ptr;
}