3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
10 #include "fifo_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(fifo,xbt,"FIFO");
17 xbt_fifo_t xbt_fifo_new(void)
20 fifo = xbt_new0(struct xbt_fifo,1);
27 void xbt_fifo_free(xbt_fifo_t l)
29 xbt_fifo_item_t b, tmp;
31 for (b = xbt_fifo_getFirstitem(l); b;
32 tmp = b, b = b->next, xbt_fifo_freeitem(tmp));
41 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
45 new = xbt_fifo_newitem();
48 xbt_fifo_push_item(l,new);
56 void *xbt_fifo_pop(xbt_fifo_t l)
61 if(l==NULL) return NULL;
62 if(!(item = xbt_fifo_pop_item(l))) return NULL;
64 content = item->content;
65 xbt_fifo_freeitem(item);
73 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
77 new = xbt_fifo_newitem();
79 xbt_fifo_unshift_item(l,new);
87 void *xbt_fifo_shift(xbt_fifo_t l)
92 if(l==NULL) return NULL;
93 if(!(item = xbt_fifo_shift_item(l))) return NULL;
95 content = item->content;
96 xbt_fifo_freeitem(item);
101 * xbt_fifo_push_item()
104 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
107 if (l->head == NULL) {
113 new->prev->next = new;
118 * xbt_fifo_pop_item()
121 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
123 xbt_fifo_item_t item;
130 l->tail = item->prev;
134 l->tail->next = NULL;
141 * xbt_fifo_unshift_item()
144 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
147 if (l->head == NULL) {
153 new->next->prev = new;
159 * xbt_fifo_shift_item()
162 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
164 xbt_fifo_item_t item;
171 l->head = item->next;
175 l->head->prev = NULL;
183 * removes an xbt_fifo_item_t using its content from the xbt_fifo
185 void xbt_fifo_remove(xbt_fifo_t l, void *t)
187 xbt_fifo_item_t current, current_next;
190 for (current = l->head; current; current = current_next) {
191 current_next = current->next;
192 if (current->content != t)
194 /* remove the item */
195 xbt_fifo_remove_item(l, current);
196 xbt_fifo_freeitem(current);
197 /* WILL NOT REMOVE DUPLICATES */
204 * xbt_fifo_remove_item()
205 * removes a given xbt_fifo_item_t from the xbt_fifo
207 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
209 if (l->head == l->tail) { /* special case */
216 if (current == l->head) { /* It's the head */
217 l->head = current->next;
218 l->head->prev = NULL;
219 } else if (current == l->tail) { /* It's the tail */
220 l->tail = current->prev;
221 l->tail->next = NULL;
222 } else { /* It's in the middle */
223 current->prev->next = current->next;
224 current->next->prev = current->prev;
232 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
234 xbt_fifo_item_t item = xbt_fifo_getFirstitem(f);
236 if (item->content == content)
244 * xbt_fifo_to_array()
246 void **xbt_fifo_to_array(xbt_fifo_t f)
255 array = xbt_new0(void *, f->count);
257 for (i = 0, b = xbt_fifo_getFirstitem(f); b; i++, b = b->next) {
258 array[i] = b->content;
266 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
268 xbt_fifo_t copy = NULL;
271 copy = xbt_fifo_new();
273 for (b = xbt_fifo_getFirstitem(f); b; b = b->next) {
274 xbt_fifo_push(copy, b->content);
282 xbt_fifo_item_t xbt_fifo_newitem(void)
284 return xbt_new0(struct xbt_fifo_item,1);
287 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
289 xbt_fifo_setItemcontent(i,v);
292 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
294 return xbt_fifo_getItemcontent(i);
298 * xbt_fifo_freeitem()
300 void xbt_fifo_freeitem(xbt_fifo_item_t b)
306 int xbt_fifo_size(xbt_fifo_t f)
311 xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
316 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
318 if(i) return i->next;
322 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
324 if(i) return i->prev;