summaryrefslogtreecommitdiffstats
path: root/contrib/cvs/src/stack.c
blob: 22a10888c350fb067efd9228de2e3d0feb99cb61 (plain)
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
/*
 * Copyright (C) 2004-2005 The Free Software Foundation, Inc.
 *
 * Portions Copyright (C) 2004-2005 Derek Price, Ximbiot <http://ximbiot.com>,
 *                                  and others.
 * 
 * You may distribute under the terms of the GNU General Public License as
 * specified in the README file that comes with the CVS source distribution.
 *
 * This module uses the hash.c module to implement a stack.
 */

#include "cvs.h"
#include <assert.h>



static void
do_push (stack, elem, isstring)
    List *stack;
    void *elem;
    int isstring;
{
    Node *p = getnode();

    if (isstring)
	p->key = elem;
    else
	p->data = elem;

    addnode(stack, p);
}



void
push (stack, elem)
    List *stack;
    void *elem;
{
    do_push (stack, elem, 0);
}



void
push_string (stack, elem)
    List *stack;
    char *elem;
{
    do_push (stack, elem, 1);
}



static void *
do_pop (stack, isstring)
    List *stack;
    int isstring;
{
    void *elem;

    if (isempty (stack)) return NULL;

    if (isstring)
    {
	elem = stack->list->prev->key;
	stack->list->prev->key = NULL;
    }
    else
    {
	elem = stack->list->prev->data;
	stack->list->prev->data = NULL;
    }

    delnode (stack->list->prev);
    return elem;
}



void *
pop (stack)
    List *stack;
{
    return do_pop (stack, 0);
}



char *
pop_string (stack)
    List *stack;
{
    return do_pop (stack, 1);
}



static void
do_unshift (stack, elem, isstring)
    List *stack;
    void *elem;
    int isstring;
{
    Node *p = getnode();

    if (isstring)
	p->key = elem;
    else
	p->data = elem;

    addnode_at_front(stack, p);
}



void
unshift (stack, elem)
    List *stack;
    void *elem;
{
    do_unshift (stack, elem, 0);
}



void
unshift_string (stack, elem)
    List *stack;
    char *elem;
{
    do_unshift (stack, elem, 1);
}



static void *
do_shift (stack, isstring)
    List *stack;
    int isstring;
{
    void *elem;

    if (isempty (stack)) return NULL;

    if (isstring)
    {
	elem = stack->list->next->key;
	stack->list->next->key = NULL;
    }
    else
    {
	elem = stack->list->next->data;
	stack->list->next->data = NULL;
    }
    delnode (stack->list->next);
    return elem;
}



void *
shift (stack)
    List *stack;
{
    return do_shift (stack, 0);
}



char *
shift_string (stack)
    List *stack;
{
    return do_shift (stack, 1);
}



int
isempty (stack)
    List *stack;
{
    if (stack->list == stack->list->next)
	return 1;
    return 0;
}
OpenPOWER on IntegriCloud