| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* queue.c: Double-ended queues, also known as deque. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
#include "core/bootstrap_internal.h" |
| 26 |
|
| 27 |
static mowgli_heap_t *mowgli_queue_heap = NULL; |
| 28 |
|
| 29 |
void |
| 30 |
mowgli_queue_bootstrap(void) |
| 31 |
{ |
| 32 |
mowgli_queue_heap = mowgli_heap_create(sizeof(mowgli_queue_t), 256, BH_NOW); |
| 33 |
|
| 34 |
if (mowgli_queue_heap == NULL) |
| 35 |
mowgli_log("mowgli_queue_heap was not created, expect problems."); |
| 36 |
} |
| 37 |
|
| 38 |
mowgli_queue_t * |
| 39 |
mowgli_queue_shift(mowgli_queue_t *head, void *data) |
| 40 |
{ |
| 41 |
mowgli_queue_t *out = mowgli_heap_alloc(mowgli_queue_heap); |
| 42 |
|
| 43 |
return_val_if_fail(head != NULL, NULL); |
| 44 |
|
| 45 |
out->next = head; |
| 46 |
out->data = data; |
| 47 |
|
| 48 |
if (head != NULL) |
| 49 |
{ |
| 50 |
out->prev = head->prev; |
| 51 |
|
| 52 |
if (out->prev != NULL) |
| 53 |
out->prev->next = out; |
| 54 |
|
| 55 |
head->prev = out; |
| 56 |
} |
| 57 |
|
| 58 |
return out; |
| 59 |
} |
| 60 |
|
| 61 |
mowgli_queue_t * |
| 62 |
mowgli_queue_push(mowgli_queue_t *head, void *data) |
| 63 |
{ |
| 64 |
mowgli_queue_t *out = mowgli_heap_alloc(mowgli_queue_heap); |
| 65 |
|
| 66 |
return_val_if_fail(head != NULL, NULL); |
| 67 |
|
| 68 |
out->prev = head; |
| 69 |
out->data = data; |
| 70 |
|
| 71 |
if (head != NULL) |
| 72 |
head->next = out; |
| 73 |
|
| 74 |
return out; |
| 75 |
} |
| 76 |
|
| 77 |
mowgli_queue_t * |
| 78 |
mowgli_queue_remove(mowgli_queue_t *head) |
| 79 |
{ |
| 80 |
mowgli_queue_t *out; |
| 81 |
|
| 82 |
return_val_if_fail(head != NULL, NULL); |
| 83 |
|
| 84 |
if (head->prev != NULL) |
| 85 |
head->prev->next = head->next; |
| 86 |
|
| 87 |
if (head->next != NULL) |
| 88 |
head->next->prev = head->prev; |
| 89 |
|
| 90 |
out = head->prev != NULL ? head->prev : head->next; |
| 91 |
|
| 92 |
mowgli_heap_free(mowgli_queue_heap, head); |
| 93 |
|
| 94 |
return out; |
| 95 |
} |
| 96 |
|
| 97 |
mowgli_queue_t * |
| 98 |
mowgli_queue_find(mowgli_queue_t *head, void *data) |
| 99 |
{ |
| 100 |
mowgli_queue_t *n; |
| 101 |
|
| 102 |
return_val_if_fail(head != NULL, NULL); |
| 103 |
|
| 104 |
for (n = head; n != NULL; n = n->next) |
| 105 |
if (n->data == data) |
| 106 |
return n; |
| 107 |
|
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
mowgli_queue_t * |
| 112 |
mowgli_queue_remove_data(mowgli_queue_t *head, void *data) |
| 113 |
{ |
| 114 |
mowgli_queue_t *n = mowgli_queue_find(head, data); |
| 115 |
|
| 116 |
return_val_if_fail(head != NULL, NULL); |
| 117 |
|
| 118 |
if (n != NULL) |
| 119 |
return mowgli_queue_remove(n); |
| 120 |
|
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
mowgli_queue_destroy(mowgli_queue_t *head) |
| 126 |
{ |
| 127 |
mowgli_queue_t *n, *n2; |
| 128 |
|
| 129 |
return_if_fail(head != NULL); |
| 130 |
|
| 131 |
for (n = head, n2 = n ? n->next : NULL; n != NULL; n = n2, n2 = n ? n->next : NULL) |
| 132 |
mowgli_queue_remove(n); |
| 133 |
} |
| 134 |
|
| 135 |
mowgli_queue_t * |
| 136 |
mowgli_queue_skip(mowgli_queue_t *head, int nodes) |
| 137 |
{ |
| 138 |
mowgli_queue_t *n; |
| 139 |
int iter; |
| 140 |
|
| 141 |
return_val_if_fail(head != NULL, NULL); |
| 142 |
|
| 143 |
for (iter = 0, n = head; n != NULL && iter < nodes; n = n->next, iter++) |
| 144 |
; |
| 145 |
|
| 146 |
return n; |
| 147 |
} |
| 148 |
|
| 149 |
mowgli_queue_t * |
| 150 |
mowgli_queue_rewind(mowgli_queue_t *head, int nodes) |
| 151 |
{ |
| 152 |
mowgli_queue_t *n; |
| 153 |
int iter; |
| 154 |
|
| 155 |
return_val_if_fail(head != NULL, NULL); |
| 156 |
|
| 157 |
for (iter = 0, n = head; n != NULL && iter < nodes; n = n->prev, iter++) |
| 158 |
; |
| 159 |
|
| 160 |
return n; |
| 161 |
} |
| 162 |
|
| 163 |
mowgli_queue_t * |
| 164 |
mowgli_queue_head(mowgli_queue_t *n) |
| 165 |
{ |
| 166 |
mowgli_queue_t *tn; |
| 167 |
|
| 168 |
return_val_if_fail(n != NULL, NULL); |
| 169 |
|
| 170 |
for (tn = n; tn != NULL && tn->prev != NULL; tn = tn->prev) |
| 171 |
; |
| 172 |
|
| 173 |
return tn; |
| 174 |
} |
| 175 |
|
| 176 |
mowgli_queue_t * |
| 177 |
mowgli_queue_tail(mowgli_queue_t *n) |
| 178 |
{ |
| 179 |
mowgli_queue_t *tn; |
| 180 |
|
| 181 |
return_val_if_fail(n != NULL, NULL); |
| 182 |
|
| 183 |
for (tn = n; tn != NULL && tn->next != NULL; tn = tn->next) |
| 184 |
; |
| 185 |
|
| 186 |
return tn; |
| 187 |
} |
| 188 |
|
| 189 |
void * |
| 190 |
mowgli_queue_pop_head(mowgli_queue_t **n) |
| 191 |
{ |
| 192 |
mowgli_queue_t *tn; |
| 193 |
void *out; |
| 194 |
|
| 195 |
return_val_if_fail(n != NULL, NULL); |
| 196 |
return_val_if_fail(*n != NULL, NULL); |
| 197 |
|
| 198 |
tn = *n; |
| 199 |
out = tn->data; |
| 200 |
*n = tn->next; |
| 201 |
|
| 202 |
mowgli_queue_remove(tn); |
| 203 |
|
| 204 |
return out; |
| 205 |
} |
| 206 |
|
| 207 |
void * |
| 208 |
mowgli_queue_pop_tail(mowgli_queue_t **n) |
| 209 |
{ |
| 210 |
mowgli_queue_t *tn; |
| 211 |
void *out; |
| 212 |
|
| 213 |
return_val_if_fail(n != NULL, NULL); |
| 214 |
return_val_if_fail(*n != NULL, NULL); |
| 215 |
|
| 216 |
tn = *n; |
| 217 |
out = tn->data; |
| 218 |
*n = tn->prev; |
| 219 |
|
| 220 |
mowgli_queue_remove(tn); |
| 221 |
|
| 222 |
return out; |
| 223 |
} |
| 224 |
|
| 225 |
int |
| 226 |
mowgli_queue_length(mowgli_queue_t *head) |
| 227 |
{ |
| 228 |
int iter; |
| 229 |
mowgli_queue_t *n; |
| 230 |
|
| 231 |
return_val_if_fail(head != NULL, -1); |
| 232 |
|
| 233 |
for (n = head, iter = 0; n != NULL; n = n->next, iter++) |
| 234 |
; |
| 235 |
|
| 236 |
return iter; |
| 237 |
} |