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"
9 #include "fifo_private.h"
11 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(fifo,xbt,"FIFO"); UNUSED SO FAR */
16 xbt_fifo_t xbt_fifo_new(void)
19 fifo = xbt_new0(struct xbt_fifo,1);
24 * \param l poor victim
26 * Free the fifo structure. None of the objects that was in the fifo is however modified.
28 void xbt_fifo_free(xbt_fifo_t l)
30 xbt_fifo_item_t b, tmp;
32 for (b = xbt_fifo_getFirstitem(l); b;
33 tmp = b, b = b->next, xbt_fifo_freeitem(tmp));
41 * \return the bucket that was just added
43 * Add an object at the tail of the list
45 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
49 new = xbt_fifo_newitem();
52 xbt_fifo_push_item(l,new);
58 * \returns the object stored at the tail of the list.
60 * Removes and returns the object stored at the tail of the list.
61 * Returns NULL if the list is empty.
63 void *xbt_fifo_pop(xbt_fifo_t l)
68 if(l==NULL) return NULL;
69 if(!(item = xbt_fifo_pop_item(l))) return NULL;
71 content = item->content;
72 xbt_fifo_freeitem(item);
79 * \return the bucket that was just added
81 * Add an object at the head of the list
83 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
87 new = xbt_fifo_newitem();
89 xbt_fifo_unshift_item(l,new);
95 * \returns the object stored at the head of the list.
97 * Removes and returns the object stored at the head of the list.
98 * Returns NULL if the list is empty.
100 void *xbt_fifo_shift(xbt_fifo_t l)
102 xbt_fifo_item_t item;
105 if(l==NULL) return NULL;
106 if(!(item = xbt_fifo_shift_item(l))) return NULL;
108 content = item->content;
109 xbt_fifo_freeitem(item);
117 * Hook up this bucket at the tail of the list
119 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
122 if (l->head == NULL) {
128 new->prev->next = new;
134 * \returns the bucket that was at the tail of the list.
136 * Returns NULL if the list was empty.
138 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
140 xbt_fifo_item_t item;
147 l->tail = item->prev;
151 l->tail->next = NULL;
161 * Hook up this bucket at the head of the list
163 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
166 if (l->head == NULL) {
172 new->next->prev = new;
179 * \returns the bucket that was at the head of the list.
181 * Returns NULL if the list was empty.
183 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
185 xbt_fifo_item_t item;
192 l->head = item->next;
196 l->head->prev = NULL;
206 * removes the first occurence of \a t from \a l.
207 * \warning it will not remove duplicates
209 void xbt_fifo_remove(xbt_fifo_t l, void *t)
211 xbt_fifo_item_t current, current_next;
214 for (current = l->head; current; current = current_next) {
215 current_next = current->next;
216 if (current->content != t)
218 /* remove the item */
219 xbt_fifo_remove_item(l, current);
220 xbt_fifo_freeitem(current);
221 /* WILL NOT REMOVE DUPLICATES */
229 * \param current a bucket
231 * removes a the bucket \a current from the list \a l
233 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
235 if (l->head == l->tail) { /* special case */
242 if (current == l->head) { /* It's the head */
243 l->head = current->next;
244 l->head->prev = NULL;
245 } else if (current == l->tail) { /* It's the tail */
246 l->tail = current->prev;
247 l->tail->next = NULL;
248 } else { /* It's in the middle */
249 current->prev->next = current->next;
250 current->next->prev = current->prev;
257 * \param content an object
258 * \return 1 if \a content is in \a f.
260 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
262 xbt_fifo_item_t item = xbt_fifo_getFirstitem(f);
264 if (item->content == content)
273 * \return a table with the objects stored in \a f.
275 void **xbt_fifo_to_array(xbt_fifo_t f)
284 array = xbt_new0(void *, f->count);
286 for (i = 0, b = xbt_fifo_getFirstitem(f); b; i++, b = b->next) {
287 array[i] = b->content;
294 * \return a copy of \a f.
296 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
298 xbt_fifo_t copy = NULL;
301 copy = xbt_fifo_new();
303 for (b = xbt_fifo_getFirstitem(f); b; b = b->next) {
304 xbt_fifo_push(copy, b->content);
310 * \return a new bucket
312 xbt_fifo_item_t xbt_fifo_newitem(void)
314 return xbt_new0(struct xbt_fifo_item,1);
321 * stores \a v in \a i.
323 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
325 xbt_fifo_setItemcontent(i,v);
330 * \return the object stored \a i.
332 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
334 return xbt_fifo_getItemcontent(i);
338 * \param b poor victim
340 * Free the bucket but does not modifies the object (if any) that was stored in it.
342 void xbt_fifo_freeitem(xbt_fifo_item_t b)
350 * \return the number of buckets in \a f.
352 int xbt_fifo_size(xbt_fifo_t f)
359 * \return the head of \a l.
361 xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
368 * \return the bucket that comes next
370 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
372 if(i) return i->next;
378 * \return the bucket that is just before \a i.
380 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
382 if(i) return i->prev;