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);
25 * \param l poor victim
27 * Free the fifo structure. None of the objects that was in the fifo is however modified.
29 void xbt_fifo_free(xbt_fifo_t l)
31 xbt_fifo_item_t b, tmp;
33 for (b = xbt_fifo_get_first_item(l); b;
34 tmp = b, b = b->next, xbt_fifo_free_item(tmp));
42 * \return the bucket that was just added
44 * Add an object at the tail of the list
46 xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t)
50 new = xbt_fifo_new_item();
53 xbt_fifo_push_item(l,new);
59 * \returns the object stored at the tail of the list.
61 * Removes and returns the object stored at the tail of the list.
62 * Returns NULL if the list is empty.
64 void *xbt_fifo_pop(xbt_fifo_t l)
69 if(l==NULL) return NULL;
70 if(!(item = xbt_fifo_pop_item(l))) return NULL;
72 content = item->content;
73 xbt_fifo_free_item(item);
80 * \return the bucket that was just added
82 * Add an object at the head of the list
84 xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t)
88 new = xbt_fifo_new_item();
90 xbt_fifo_unshift_item(l,new);
96 * \returns the object stored at the head of the list.
98 * Removes and returns the object stored at the head of the list.
99 * Returns NULL if the list is empty.
101 void *xbt_fifo_shift(xbt_fifo_t l)
103 xbt_fifo_item_t item;
106 if(l==NULL) return NULL;
107 if(!(item = xbt_fifo_shift_item(l))) return NULL;
109 content = item->content;
110 xbt_fifo_free_item(item);
118 * Hook up this bucket at the tail of the list
120 void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new)
123 if (l->head == NULL) {
129 new->prev->next = new;
135 * \returns the bucket that was at the tail of the list.
137 * Returns NULL if the list was empty.
139 xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l)
141 xbt_fifo_item_t item;
148 l->tail = item->prev;
152 l->tail->next = NULL;
162 * Hook up this bucket at the head of the list
164 void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new)
167 if (l->head == NULL) {
173 new->next->prev = new;
180 * \returns the bucket that was at the head of the list.
182 * Returns NULL if the list was empty.
184 xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l)
186 xbt_fifo_item_t item;
193 l->head = item->next;
197 l->head->prev = NULL;
207 * removes the first occurence of \a t from \a l.
208 * \warning it will not remove duplicates
210 void xbt_fifo_remove(xbt_fifo_t l, void *t)
212 xbt_fifo_item_t current, current_next;
215 for (current = l->head; current; current = current_next) {
216 current_next = current->next;
217 if (current->content != t)
219 /* remove the item */
220 xbt_fifo_remove_item(l, current);
221 xbt_fifo_free_item(current);
222 /* WILL NOT REMOVE DUPLICATES */
230 * \param current a bucket
232 * removes a the bucket \a current from the list \a l
234 void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current)
236 if (l->head == l->tail) { /* special case */
243 if (current == l->head) { /* It's the head */
244 l->head = current->next;
245 l->head->prev = NULL;
246 } else if (current == l->tail) { /* It's the tail */
247 l->tail = current->prev;
248 l->tail->next = NULL;
249 } else { /* It's in the middle */
250 current->prev->next = current->next;
251 current->next->prev = current->prev;
258 * \param content an object
259 * \return 1 if \a content is in \a f.
261 int xbt_fifo_is_in(xbt_fifo_t f, void *content)
263 xbt_fifo_item_t item = xbt_fifo_get_first_item(f);
265 if (item->content == content)
274 * \return a table with the objects stored in \a f.
276 void **xbt_fifo_to_array(xbt_fifo_t f)
285 array = xbt_new0(void *, f->count);
287 for (i = 0, b = xbt_fifo_get_first_item(f); b; i++, b = b->next) {
288 array[i] = b->content;
295 * \return a copy of \a f.
297 xbt_fifo_t xbt_fifo_copy(xbt_fifo_t f)
299 xbt_fifo_t copy = NULL;
302 copy = xbt_fifo_new();
304 for (b = xbt_fifo_get_first_item(f); b; b = b->next) {
305 xbt_fifo_push(copy, b->content);
311 * \return a new bucket
313 xbt_fifo_item_t xbt_fifo_new_item(void)
315 return xbt_new0(struct xbt_fifo_item,1);
318 /** \deprecated Use #xbt_fifo_new_item instead.
320 xbt_fifo_item_t xbt_fifo_newitem(void)
322 WARN0("This function is deprecated. Use xbt_fifo_new_item.");
323 return xbt_fifo_new_item();
330 * stores \a v in \a i.
332 void xbt_fifo_set_item_content(xbt_fifo_item_t i , void *v)
334 xbt_fifo_setItemcontent(i,v);
339 * \return the object stored \a i.
341 void *xbt_fifo_get_item_content(xbt_fifo_item_t i)
343 return xbt_fifo_getItemcontent(i);
347 * \param b poor victim
349 * Free the bucket but does not modifies the object (if any) that was stored in it.
351 void xbt_fifo_free_item(xbt_fifo_item_t b)
358 * \deprecated Use #xbt_fifo_free_item instead.
360 void xbt_fifo_freeitem(xbt_fifo_item_t b)
362 WARN0("This function is deprecated. Use xbt_fifo_free_item.");
369 * \return the number of buckets in \a f.
371 int xbt_fifo_size(xbt_fifo_t f)
378 * \return the head of \a l.
380 xbt_fifo_item_t xbt_fifo_get_first_item(xbt_fifo_t l)
385 /** \deprecated Use #xbt_fifo_get_first_item instead.
387 xbt_fifo_item_t xbt_fifo_getFirstItem(xbt_fifo_t l)
389 WARN0("This function is deprecated. Use xbt_fifo_get_first_item.");
390 return xbt_fifo_get_first_item(l);
395 * \return the bucket that comes next
397 xbt_fifo_item_t xbt_fifo_get_next_item(xbt_fifo_item_t i)
399 if(i) return i->next;
403 /** \deprecated Use #xbt_fifo_get_next_item instead.
405 xbt_fifo_item_t xbt_fifo_getNextItem(xbt_fifo_item_t i)
407 WARN0("This function is deprecated. Use xbt_fifo_get_next_item.");
408 return xbt_fifo_get_next_item(i);
413 * \return the bucket that is just before \a i.
415 xbt_fifo_item_t xbt_fifo_get_prev_item(xbt_fifo_item_t i)
417 if(i) return i->prev;
421 /** \deprecated Use #xbt_fifo_get_prev_item instead.
423 xbt_fifo_item_t xbt_fifo_getPrevItem(xbt_fifo_item_t i)
425 WARN0("This function is deprecated. Use xbt_fifo_get_prev_item.");
426 return xbt_fifo_get_prev_item(i);