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
|
/*
* queue.c
*
*/
#include <stdlib.h>
#include <string.h>
#include "queue.h"
void
clonequeue(Queue *t, Queue *s)
{
t->alloc = t->elements = malloc((s->count + 8) * sizeof(Id));
if (s->count)
memcpy(t->alloc, s->elements, s->count * sizeof(Id));
t->count = s->count;
t->left = 8;
}
void
queueinit(Queue *q)
{
q->alloc = q->elements = 0;
q->count = q->left = 0;
}
void
queueinit_buffer(Queue *q, Id *buf, int size)
{
q->alloc = 0;
q->elements = buf;
q->count = 0;
q->left = size;
}
void
queuefree(Queue *q)
{
if (q->alloc)
free(q->alloc);
q->alloc = q->elements = 0;
q->count = q->left = 0;
}
Id
queueshift(Queue *q)
{
if (!q->count)
return 0;
q->count--;
return *q->elements++;
}
void
queuepush(Queue *q, Id id)
{
if (!q->left)
{
if (q->alloc && q->alloc != q->elements)
{
memmove(q->alloc, q->elements, q->count * sizeof(Id));
q->left += q->elements - q->alloc;
q->elements = q->alloc;
}
else if (q->alloc)
{
q->elements = q->alloc = realloc(q->alloc, (q->count + 8) * sizeof(Id));
q->left += 8;
}
else
{
q->alloc = malloc((q->count + 8) * sizeof(Id));
if (q->count)
memcpy(q->alloc, q->elements, q->count * sizeof(Id));
q->elements = q->alloc;
q->left += 8;
}
}
q->elements[q->count++] = id;
q->left--;
}
void
queuepushunique(Queue *q, Id id)
{
int i;
for (i = q->count; i > 0; )
if (q->elements[--i] == id)
return;
queuepush(q, id);
}
|