From: alegrand Date: Fri, 5 Nov 2004 19:38:41 +0000 (+0000) Subject: Add the possibility to remove an item in the middle of the list. X-Git-Tag: v3.3~4864 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/3e8755b31f40edd05eb6679b4c5571083020545a?hp=8e7b6da73f9b4393ba523e947a9dc5837e654df9 Add the possibility to remove an item in the middle of the list. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@486 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/include/xbt/fifo.h b/include/xbt/fifo.h index b59e0b1af2..33fa27af1e 100644 --- a/include/xbt/fifo.h +++ b/include/xbt/fifo.h @@ -16,11 +16,16 @@ typedef struct xbt_fifo *xbt_fifo_t; xbt_fifo_t xbt_fifo_new(void); void xbt_fifo_free(xbt_fifo_t); -void xbt_fifo_push(xbt_fifo_t, void *); +xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t, void *); void *xbt_fifo_pop(xbt_fifo_t); -void xbt_fifo_unshift(xbt_fifo_t, void *); +xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t, void *); void *xbt_fifo_shift(xbt_fifo_t); +void xbt_fifo_push_item(xbt_fifo_t, xbt_fifo_item_t); +xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t); +void xbt_fifo_unshift_item(xbt_fifo_t, xbt_fifo_item_t); +xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t); + void xbt_fifo_remove(xbt_fifo_t, void *); void xbt_fifo_remove_item(xbt_fifo_t, xbt_fifo_item_t); diff --git a/src/xbt/fifo.c b/src/xbt/fifo.c index a2e952f1fe..2f191af958 100644 --- a/src/xbt/fifo.c +++ b/src/xbt/fifo.c @@ -4,6 +4,7 @@ under the terms of the license (GNU LGPL) which comes with this package. */ #include "xbt/sysdep.h" +#include "xbt/error.h" #include "fifo_private.h" /* @@ -33,13 +34,72 @@ void xbt_fifo_free(xbt_fifo_t l) * xbt_fifo_push() * at the tail */ -void xbt_fifo_push(xbt_fifo_t l, void *t) +xbt_fifo_item_t xbt_fifo_push(xbt_fifo_t l, void *t) +{ + xbt_fifo_item_t new; + + new = xbt_fifo_newitem(); + new->content = t; + + xbt_fifo_push_item(l,new); + return new; +} + +/* + * xbt_fifo_pop() + * from the tail + */ +void *xbt_fifo_pop(xbt_fifo_t l) +{ + xbt_fifo_item_t item; + void *content; + + item = xbt_fifo_pop_item(l); + if(item==NULL) return NULL; + + content = item->content; + xbt_fifo_freeitem(item); + return content; +} + +/* + * xbt_fifo_unshift() + * at the head + */ +xbt_fifo_item_t xbt_fifo_unshift(xbt_fifo_t l, void *t) { xbt_fifo_item_t new; - (l->count)++; new = xbt_fifo_newitem(); new->content = t; + xbt_fifo_unshift_item(l,new); + return new; +} + +/* + * xbt_fifo_shift() + * from the head + */ +void *xbt_fifo_shift(xbt_fifo_t l) +{ + xbt_fifo_item_t item; + void *content; + + item = xbt_fifo_shift_item(l); + if(l==NULL) return NULL; + + content = item->content; + xbt_fifo_freeitem(item); + return content; +} + +/* + * xbt_fifo_push_item() + * at the tail + */ +void xbt_fifo_push_item(xbt_fifo_t l, xbt_fifo_item_t new) +{ + (l->count)++; if (l->head == NULL) { l->head = new; l->tail = new; @@ -48,23 +108,20 @@ void xbt_fifo_push(xbt_fifo_t l, void *t) new->prev = l->tail; new->prev->next = new; l->tail = new; - return; } /* - * xbt_fifo_pop() + * xbt_fifo_pop_item() * from the tail */ -void *xbt_fifo_pop(xbt_fifo_t l) +xbt_fifo_item_t xbt_fifo_pop_item(xbt_fifo_t l) { xbt_fifo_item_t item; - void *content; if (l->tail == NULL) return NULL; item = l->tail; - content = item->content; l->tail = item->prev; if (l->tail == NULL) @@ -72,22 +129,17 @@ void *xbt_fifo_pop(xbt_fifo_t l) else l->tail->next = NULL; - xbt_fifo_freeitem(item); (l->count)--; - return content; + return item; } /* - * xbt_fifo_unshift() + * xbt_fifo_unshift_item() * at the head */ -void xbt_fifo_unshift(xbt_fifo_t l, void *t) +void xbt_fifo_unshift_item(xbt_fifo_t l, xbt_fifo_item_t new) { - xbt_fifo_item_t new; - (l->count)++; - new = xbt_fifo_newitem(); - new->content = t; if (l->head == NULL) { l->head = new; l->tail = new; @@ -100,19 +152,17 @@ void xbt_fifo_unshift(xbt_fifo_t l, void *t) } /* - * xbt_fifo_shift() + * xbt_fifo_shift_item() * from the head */ -void *xbt_fifo_shift(xbt_fifo_t l) +xbt_fifo_item_t xbt_fifo_shift_item(xbt_fifo_t l) { xbt_fifo_item_t item; - void *content; if (l->head == NULL) return NULL; item = l->head; - content = item->content; l->head = item->next; if (l->head == NULL) @@ -120,9 +170,8 @@ void *xbt_fifo_shift(xbt_fifo_t l) else l->head->prev = NULL; - xbt_fifo_freeitem(item); (l->count)--; - return content; + return item; } /* @@ -140,6 +189,7 @@ void xbt_fifo_remove(xbt_fifo_t l, void *t) continue; /* remove the item */ xbt_fifo_remove_item(l, current); + xbt_fifo_freeitem(current); /* WILL NOT REMOVE DUPLICATES */ break; } @@ -155,7 +205,6 @@ void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current) if (l->head == l->tail) { /* special case */ l->head = NULL; l->tail = NULL; - xbt_fifo_freeitem(current); (l->count)--; return; } @@ -163,15 +212,12 @@ void xbt_fifo_remove_item(xbt_fifo_t l, xbt_fifo_item_t current) if (current == l->head) { /* It's the head */ l->head = current->next; l->head->prev = NULL; - xbt_fifo_freeitem(current); } else if (current == l->tail) { /* It's the tail */ l->tail = current->prev; l->tail->next = NULL; - xbt_fifo_freeitem(current); } else { /* It's in the middle */ current->prev->next = current->next; current->next->prev = current->prev; - xbt_fifo_freeitem(current); } (l->count)--; } diff --git a/src/xbt/fifo_private.h b/src/xbt/fifo_private.h index ba3adb6566..19d5fe1000 100644 --- a/src/xbt/fifo_private.h +++ b/src/xbt/fifo_private.h @@ -26,6 +26,7 @@ typedef struct xbt_fifo { #define xbt_fifo_getNextitem(i) ((i)?(i)->next:NULL) #define xbt_fifo_getPrevitem(i) ((i)?(i)->prev:NULL) #define xbt_fifo_getItemcontent(i) ((i)?(i)->content:NULL) +#define xbt_fifo_Itemcontent(i) ((i)->content) #define xbt_fifo_setItemcontent(i,v) (i->content=v)