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. */
7 #include "fifo_private.h"
12 xbt_fifo_t xbt_fifo_new(void)
15 fifo = (xbt_fifo_t) calloc(1, sizeof(struct xbt_fifo));
22 void xbt_fifo_free(xbt_fifo_t l)
24 xbt_fifo_item_t b, tmp;
26 for (b = xbt_fifo_getFirstitem(l); b;
27 tmp = b, b = b->next, xbt_fifo_freeitem(tmp));
36 void xbt_fifo_push(xbt_fifo_t l, void *t)
41 new = xbt_fifo_newitem();
43 if (l->head == NULL) {
49 new->prev->next = new;
58 void *xbt_fifo_pop(xbt_fifo_t l)
67 content = item->content;
75 xbt_fifo_freeitem(item);
84 void xbt_fifo_unshift(xbt_fifo_t l, void *t)
89 new = xbt_fifo_newitem();
91 if (l->head == NULL) {
97 new->next->prev = new;
106 void *xbt_fifo_shift(xbt_fifo_t l)
108 xbt_fifo_item_t item;
115 content = item->content;
117 l->head = item->next;
121 l->head->prev = NULL;
123 xbt_fifo_freeitem(item);
130 * removes an xbt_fifo_item_t using its content from the xbt_fifo
132 void xbt_fifo_remove(xbt_fifo_t l, void *t)
134 xbt_fifo_item_t current, current_next;
137 for (current = l->head; current; current = current_next) {
138 current_next = current->next;
139 if (current->content != t)
141 /* remove the item */
142 xbt_fifo_remove_item(l, current);
143 /* WILL NOT REMOVE DUPLICATES */
150 * xbt_fifo_remove_item()
151 * removes a given xbt_fifo_item_t from the xbt_fifo
153 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
155 if (l->head == l->tail) { /* special case */
158 xbt_fifo_freeitem(current);
163 if (current == l->head) { /* It's the head */
164 l->head = current->next;
165 l->head->prev = NULL;
166 xbt_fifo_freeitem(current);
167 } else if (current == l->tail) { /* It's the tail */
168 l->tail = current->prev;
169 l->tail->next = NULL;
170 xbt_fifo_freeitem(current);
171 } else { /* It's in the middle */
172 current->prev->next = current->next;
173 current->next->prev = current->prev;
174 xbt_fifo_freeitem(current);
182 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
184 xbt_fifo_item_t item = xbt_fifo_getFirstitem(f);
186 if (item->content == content)
194 * xbt_fifo_to_array()
196 void **xbt_fifo_to_array(xbt_fifo_t f)
205 array = (void **) calloc(f->count, sizeof(void *));
207 for (i = 0, b = xbt_fifo_getFirstitem(f); b; i++, b = b->next) {
208 array[i] = b->content;
216 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
218 xbt_fifo_t copy = NULL;
221 copy = xbt_fifo_new();
223 for (b = xbt_fifo_getFirstitem(f); b; b = b->next) {
224 xbt_fifo_push(copy, b->content);
232 xbt_fifo_item_t xbt_fifo_newitem(void)
234 return (xbt_fifo_item_t) calloc(1, sizeof(struct xbt_fifo_item));
237 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
239 xbt_fifo_setItemcontent(i,v);
242 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
244 return xbt_fifo_getItemcontent(i);
248 * xbt_fifo_freeitem()
250 void xbt_fifo_freeitem(xbt_fifo_item_t b)
256 int xbt_fifo_size(xbt_fifo_t f)