2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
20 #define EXTRA_SPACE_HEAD 8
25 q->alloc = q->elements = 0;
26 q->count = q->left = 0;
30 queue_init_clone(Queue *t, Queue *s)
34 t->alloc = t->elements = 0;
35 t->count = t->left = 0;
38 t->alloc = t->elements = solv_malloc2(s->count + EXTRA_SPACE, sizeof(Id));
40 memcpy(t->alloc, s->elements, s->count * sizeof(Id));
42 t->left = EXTRA_SPACE;
46 queue_init_buffer(Queue *q, Id *buf, int size)
59 q->alloc = q->elements = 0;
60 q->count = q->left = 0;
64 queue_alloc_one(Queue *q)
68 q->alloc = solv_malloc2(q->count + EXTRA_SPACE, sizeof(Id));
70 memcpy(q->alloc, q->elements, q->count * sizeof(Id));
71 q->elements = q->alloc;
72 q->left = EXTRA_SPACE;
74 else if (q->alloc != q->elements)
76 int l = q->elements - q->alloc;
78 memmove(q->alloc, q->elements, q->count * sizeof(Id));
84 q->elements = q->alloc = solv_realloc2(q->alloc, q->count + EXTRA_SPACE, sizeof(Id));
85 q->left = EXTRA_SPACE;
89 /* make room for an element in front of queue */
91 queue_alloc_one_head(Queue *q)
94 if (!q->alloc || !q->left)
96 l = q->left > EXTRA_SPACE_HEAD ? EXTRA_SPACE_HEAD : q->left;
98 memmove(q->elements + l, q->elements, q->count * sizeof(Id));
104 queue_insert(Queue *q, int pos, Id id)
106 queue_push(q, id); /* make room */
107 if (pos < q->count - 1)
109 memmove(q->elements + pos + 1, q->elements + pos, (q->count - 1 - pos) * sizeof(Id));
110 q->elements[pos] = id;
115 queue_delete(Queue *q, int pos)
119 if (pos < q->count - 1)
120 memmove(q->elements + pos, q->elements + pos + 1, (q->count - 1 - pos) * sizeof(Id));
126 queue_insert2(Queue *q, int pos, Id id1, Id id2)
128 queue_push(q, id1); /* make room */
129 queue_push(q, id2); /* make room */
130 if (pos < q->count - 2)
132 memmove(q->elements + pos + 2, q->elements + pos, (q->count - 2 - pos) * sizeof(Id));
133 q->elements[pos] = id1;
134 q->elements[pos + 1] = id2;
139 queue_delete2(Queue *q, int pos)
143 if (pos == q->count - 1)
149 if (pos < q->count - 2)
150 memmove(q->elements + pos, q->elements + pos + 2, (q->count - 2 - pos) * sizeof(Id));
156 queue_insertn(Queue *q, int pos, int n, Id *elements)
167 off = q->elements - q->alloc;
168 q->alloc = solv_realloc2(q->alloc, off + q->count + n + EXTRA_SPACE, sizeof(Id));
169 q->elements = q->alloc + off;
170 q->left = n + EXTRA_SPACE;
173 memmove(q->elements + pos + n, q->elements + pos, (q->count - pos) * sizeof(Id));
175 memcpy(q->elements + pos, elements, n * sizeof(Id));
177 memset(q->elements + pos, 0, n * sizeof(Id));
183 queue_deleten(Queue *q, int pos, int n)
185 if (n <= 0 || pos >= q->count)
187 if (pos + n >= q->count)
190 memmove(q->elements + pos, q->elements + pos + n, (q->count - n - pos) * sizeof(Id));
195 /* allocate room for n more elements */
197 queue_prealloc(Queue *q, int n)
200 if (n <= 0 || q->left >= n)
204 off = q->elements - q->alloc;
205 q->alloc = solv_realloc2(q->alloc, off + q->count + n + EXTRA_SPACE, sizeof(Id));
206 q->elements = q->alloc + off;
207 q->left = n + EXTRA_SPACE;