diff --git a/include/ordinary.h b/include/ordinary.h index 771d94e..8046b17 100644 --- a/include/ordinary.h +++ b/include/ordinary.h @@ -30,5 +30,6 @@ uint8_t ordinary_list_for(struct ordinary_list *list, callback cb); struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val); void ordinary_list_move(struct standard_list *dst, struct standard_list *src, struct standard_node *node); struct ordinary_node *ordinary_list_pop(struct ordinary_list *list); +void ordinary_list_drop(struct ordinary_list *list, struct ordinary_node *node); #endif diff --git a/src/ordinary.c b/src/ordinary.c index d2fa5b7..7918829 100644 --- a/src/ordinary.c +++ b/src/ordinary.c @@ -24,19 +24,11 @@ void ordinary_list_delete(struct ordinary_list *list) { } uint8_t ordinary_list_empty(struct ordinary_list *list) { - if(!list->limit) { - return 1; - } - - return list->count == 0; + return !list->head; } uint8_t ordinary_list_full(struct ordinary_list *list) { - if(!list->limit) { - return 1; - } - - return list->count == list->limit; + return !list->limit || list->count == list->limit; } struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) { @@ -105,7 +97,7 @@ static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_nod } struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) { - if(list->limit && list->count == list->limit) { + if(ordinary_list_full(list)) { return NULL; } @@ -124,8 +116,10 @@ struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) { return node; } -void ordinary_list_mov(struct standard_list *dst, struct standard_list *src, struct standard_node *node) { - if(dst->limit &&) +uint8_t ordinary_list_mov(struct standard_list *dst, struct standard_list *src, struct standard_node *node) { + if(ordinary_list_full(dst) || ordinary_list_empty(src)) { + return 0; + } struct ordinary_node *prev = node->prev; struct ordinary_node *next = node->next; @@ -142,3 +136,46 @@ void ordinary_list_mov(struct standard_list *dst, struct standard_list *src, str ordinary_list_insert(dst, node); } + +struct ordinary_node *ordinary_list_pop(struct ordinary_list *list) { + if(ordinary_list_empty(list)) { + return NULL; + } + + struct ordinary_node *node = list->tail; + + if(list->head == list->tail) { + list->head = NULL; + list->tail = NULL; + } else { + list->tail = list->tail->prev; + list->tail->next = NULL; + } + + if(list->limit) { + list->count -= 1; + } + + return node; +} + +void ordinary_list_rem(struct ordinary_list *list, struct ordinary_node *node) { + struct ordinary_node *prev = node->prev; + struct ordinary_node *next = node->next; + + if(prev) { + prev->next = next; + node->prev = NULL; + } + + if(next) { + next->prev = prev; + node->next = NULL; + } + + free(node); + + if(list->limit) { + list->count -= 1; + } +}