]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/cvs/src/stack.c
This commit was generated by cvs2svn to compensate for changes in r169693,
[FreeBSD/FreeBSD.git] / contrib / cvs / src / stack.c
1 /*
2  * Copyright (c) 2004, Free Software Foundation,
3  *                     Derek Price,
4  *                     & Ximbiot <http://ximbiot.com>.
5  * 
6  * You may distribute under the terms of the GNU General Public License as
7  * specified in the README file that comes with the CVS source distribution.
8  *
9  * This module uses the hash.c module to implement a stack.
10  */
11
12 #include "cvs.h"
13 #include <assert.h>
14
15
16
17 static void
18 do_push (stack, elem, isstring)
19     List *stack;
20     void *elem;
21     int isstring;
22 {
23     Node *p = getnode();
24
25     if (isstring)
26         p->key = elem;
27     else
28         p->data = elem;
29
30     addnode(stack, p);
31 }
32
33
34
35 void
36 push (stack, elem)
37     List *stack;
38     void *elem;
39 {
40     do_push (stack, elem, 0);
41 }
42
43
44
45 void
46 push_string (stack, elem)
47     List *stack;
48     char *elem;
49 {
50     do_push (stack, elem, 1);
51 }
52
53
54
55 static void *
56 do_pop (stack, isstring)
57     List *stack;
58     int isstring;
59 {
60     void *elem;
61
62     if (isempty (stack)) return NULL;
63
64     if (isstring)
65     {
66         elem = stack->list->prev->key;
67         stack->list->prev->key = NULL;
68     }
69     else
70     {
71         elem = stack->list->prev->data;
72         stack->list->prev->data = NULL;
73     }
74
75     delnode (stack->list->prev);
76     return elem;
77 }
78
79
80
81 void *
82 pop (stack)
83     List *stack;
84 {
85     return do_pop (stack, 0);
86 }
87
88
89
90 char *
91 pop_string (stack)
92     List *stack;
93 {
94     return do_pop (stack, 1);
95 }
96
97
98
99 static void
100 do_unshift (stack, elem, isstring)
101     List *stack;
102     void *elem;
103     int isstring;
104 {
105     Node *p = getnode();
106
107     if (isstring)
108         p->key = elem;
109     else
110         p->data = elem;
111
112     addnode_at_front(stack, p);
113 }
114
115
116
117 void
118 unshift (stack, elem)
119     List *stack;
120     void *elem;
121 {
122     do_unshift (stack, elem, 0);
123 }
124
125
126
127 void
128 unshift_string (stack, elem)
129     List *stack;
130     char *elem;
131 {
132     do_unshift (stack, elem, 1);
133 }
134
135
136
137 static void *
138 do_shift (stack, isstring)
139     List *stack;
140     int isstring;
141 {
142     void *elem;
143
144     if (isempty (stack)) return NULL;
145
146     if (isstring)
147     {
148         elem = stack->list->next->key;
149         stack->list->next->key = NULL;
150     }
151     else
152     {
153         elem = stack->list->next->data;
154         stack->list->next->data = NULL;
155     }
156     delnode (stack->list->next);
157     return elem;
158 }
159
160
161
162 void *
163 shift (stack)
164     List *stack;
165 {
166     return do_shift (stack, 0);
167 }
168
169
170
171 char *
172 shift_string (stack)
173     List *stack;
174 {
175     return do_shift (stack, 1);
176 }
177
178
179
180 int
181 isempty (stack)
182     List *stack;
183 {
184     if (stack->list == stack->list->next)
185         return 1;
186     return 0;
187 }