1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "xbt/sysdep.h"
8 #include "fifo_private.h"
13 xbt_fifo_t xbt_fifo_new(void)
16 fifo = xbt_new0(struct xbt_fifo,1);
23 void xbt_fifo_free(xbt_fifo_t l)
25 xbt_fifo_item_t b, tmp;
27 for (b = xbt_fifo_getFirstitem(l); b;
28 tmp = b, b = b->next, xbt_fifo_freeitem(tmp));
37 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
41 new = xbt_fifo_newitem();
44 xbt_fifo_push_item(l,new);
52 void *xbt_fifo_pop(xbt_fifo_t l)
57 item = xbt_fifo_pop_item(l);
58 if(item==NULL) return NULL;
60 content = item->content;
61 xbt_fifo_freeitem(item);
69 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
73 new = xbt_fifo_newitem();
75 xbt_fifo_unshift_item(l,new);
83 void *xbt_fifo_shift(xbt_fifo_t l)
88 item = xbt_fifo_shift_item(l);
89 if(l==NULL) return NULL;
91 content = item->content;
92 xbt_fifo_freeitem(item);
97 * xbt_fifo_push_item()
100 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
103 if (l->head == NULL) {
109 new->prev->next = new;
114 * xbt_fifo_pop_item()
117 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
119 xbt_fifo_item_t item;
126 l->tail = item->prev;
130 l->tail->next = NULL;
137 * xbt_fifo_unshift_item()
140 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
143 if (l->head == NULL) {
149 new->next->prev = new;
155 * xbt_fifo_shift_item()
158 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
160 xbt_fifo_item_t item;
167 l->head = item->next;
171 l->head->prev = NULL;
179 * removes an xbt_fifo_item_t using its content from the xbt_fifo
181 void xbt_fifo_remove(xbt_fifo_t l, void *t)
183 xbt_fifo_item_t current, current_next;
186 for (current = l->head; current; current = current_next) {
187 current_next = current->next;
188 if (current->content != t)
190 /* remove the item */
191 xbt_fifo_remove_item(l, current);
192 xbt_fifo_freeitem(current);
193 /* WILL NOT REMOVE DUPLICATES */
200 * xbt_fifo_remove_item()
201 * removes a given xbt_fifo_item_t from the xbt_fifo
203 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
205 if (l->head == l->tail) { /* special case */
212 if (current == l->head) { /* It's the head */
213 l->head = current->next;
214 l->head->prev = NULL;
215 } else if (current == l->tail) { /* It's the tail */
216 l->tail = current->prev;
217 l->tail->next = NULL;
218 } else { /* It's in the middle */
219 current->prev->next = current->next;
220 current->next->prev = current->prev;
228 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
230 xbt_fifo_item_t item = xbt_fifo_getFirstitem(f);
232 if (item->content == content)
240 * xbt_fifo_to_array()
242 void **xbt_fifo_to_array(xbt_fifo_t f)
251 array = xbt_new0(void *, f->count);
253 for (i = 0, b = xbt_fifo_getFirstitem(f); b; i++, b = b->next) {
254 array[i] = b->content;
262 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
264 xbt_fifo_t copy = NULL;
267 copy = xbt_fifo_new();
269 for (b = xbt_fifo_getFirstitem(f); b; b = b->next) {
270 xbt_fifo_push(copy, b->content);
278 xbt_fifo_item_t xbt_fifo_newitem(void)
280 return xbt_new0(struct xbt_fifo_item,1);
283 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
285 xbt_fifo_setItemcontent(i,v);
288 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
290 return xbt_fifo_getItemcontent(i);
294 * xbt_fifo_freeitem()
296 void xbt_fifo_freeitem(xbt_fifo_item_t b)
302 int xbt_fifo_size(xbt_fifo_t f)