File: dqueue.c

package info (click to toggle)
magic 7.5.241-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,772 kB
  • ctags: 12,301
  • sloc: ansic: 170,465; sh: 10,446; perl: 4,105; lisp: 2,554; tcl: 2,469; makefile: 1,887; cpp: 587; csh: 148; awk: 140
file content (260 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download | duplicates (2)
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
255
256
257
258
259
260
/*
 * dqueue.c --
 *
 *	Routines for double ended queues.  See 'dqueue.h'.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/dqueue.c,v 1.1.1.1 2006/04/10 22:03:14 tim Exp $";
#endif  /* not lint */

#include <stdio.h>

#include "utils/magic.h"
#include "utils/dqueue.h"
#include "utils/malloc.h"

/*
 * ----------------------------------------------------------------------------
 *
 * DQInit --
 *
 *	Initialize a new queue to have a certain capacity.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

void
DQInit(q, capacity)
    DQueue *q;
    int capacity;
{
    if (capacity < 1) capacity = 1;
    q->dq_data = (ClientData *) mallocMagic((unsigned)((capacity+1) * sizeof (ClientData)));
    q->dq_size = 0;
    q->dq_maxSize = capacity;
    q->dq_front = 0;	/* next slot in front is loc 0 */
    q->dq_rear = 1;	/* next slot in rear is loc 1 */
}


/*
 * ----------------------------------------------------------------------------
 *
 * DQFree --
 *
 *	Free up a queue.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

void
DQFree(q)
    DQueue *q;
{
    freeMagic((char *) q->dq_data); 
}

/*
 * ----------------------------------------------------------------------------
 *
 * DQPushFront & DQPushRear --
 *
 *	Push a new element onto one end of the DQueue.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Puts an element in the queue.
 *
 * ----------------------------------------------------------------------------
 */

void
DQPushFront(q, elem)
    DQueue *q;
    ClientData elem;
{
    if (q->dq_size == q->dq_maxSize)  DQChangeSize(q, 2 * q->dq_maxSize);
    q->dq_data[q->dq_front] = elem; 
    q->dq_front--;
    if (q->dq_front < 0) q->dq_front = q->dq_maxSize;
    q->dq_size++;
}

void
DQPushRear(q, elem)
    DQueue *q;
    ClientData elem;
{
    if (q->dq_size == q->dq_maxSize)  DQChangeSize(q, 2 * q->dq_maxSize);
    q->dq_data[q->dq_rear] = elem; 
    q->dq_rear++;
    if (q->dq_rear > q->dq_maxSize) q->dq_rear = 0;
    q->dq_size++;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DQPopFront & DQPopRear --
 *
 *	Pop an element from one end of the queue.
 *
 * Results:
 *	The element, or NULL if there is none.
 *
 * Side Effects:
 *	Removes the element from the queue.
 *
 * ----------------------------------------------------------------------------
 */

ClientData
DQPopFront(q)
    DQueue *q;
{
    if (q->dq_size == 0) return (ClientData) NULL;
    q->dq_size--;
    q->dq_front++;
    if (q->dq_front > q->dq_maxSize) q->dq_front = 0;
    return q->dq_data[q->dq_front];
}

ClientData
DQPopRear(q)
    DQueue *q;
{
    if (q->dq_size == 0) return (ClientData) NULL;
    q->dq_size--;
    q->dq_rear--;
    if (q->dq_rear < 0) q->dq_rear = q->dq_maxSize;
    return q->dq_data[q->dq_rear];
}


/*
 * ----------------------------------------------------------------------------
 *
 * DQChangeSize --
 *
 *	Change the size of a DQueue -- either increase or decrease.
 *
 * Results:
 *	None
 *
 * Side Effects:
 *	The DQueue changes size.
 *
 * ----------------------------------------------------------------------------
 */

void
DQChangeSize(q, newSize)
    DQueue *q;
    int newSize;
{
    DQueue newq;

    if (newSize < q->dq_size) newSize = q->dq_size;
    DQInit(&newq, newSize);
    DQCopy(&newq, q);
    freeMagic((char *) q->dq_data); 
    q->dq_data = newq.dq_data;
    q->dq_maxSize = newq.dq_maxSize;
    q->dq_front = newq.dq_front;
    q->dq_rear = newq.dq_rear;
}


/*
 * ----------------------------------------------------------------------------
 *
 * DQCopy --
 *
 *	Copy one DQueue into another.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Elements (ClientData pointers) are copied.
 *
 * ----------------------------------------------------------------------------
 */

void
DQCopy(dst, src)
    DQueue *dst;	/* The destination queue */
    DQueue *src;	/* The source queue */
{
    int i;
    dst->dq_size = 0;
    i = src->dq_front;
    while (dst->dq_size != src->dq_size) {
	i = i + 1;
	if (i > src->dq_maxSize) i = 0;
	DQPushRear(dst, src->dq_data[i]);
    }
}


/*
 * ----------------------------------------------------------------------------
 *
 * Main --
 *
 *	Test out this module.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Stuff on the screen.
 *
 * ----------------------------------------------------------------------------
 */

/****
void
main()
{
    int i;
    DQueue q;
    DQInit(&q, 0);
    for (i = 0; i < 10; i++) DQPushFront(&q, (ClientData) i);
    while (!DQIsEmpty(&q)) printf("got %d\n", (int) DQPopRear(&q));
    DQFree(&q);
    printf("-------\n");
    DQInit(&q, 0);
    for (i = 0; i < 10; i++) DQPushFront(&q, (ClientData) i);
    for (i = 0; i < 10000; i++) DQPushRear(&q, DQPopFront(&q));
    for (i = 0; i < 10000; i++) DQPushFront(&q, DQPopRear(&q));
    while (!DQIsEmpty(&q)) printf("got %d\n", (int) DQPopFront(&q));
}
****/