ordinary/src/ordinary.c

182 lines
3.4 KiB
C
Raw Normal View History

#include <ordinary.h>
#include <stdlib.h>
void ordinary_list_new(struct ordinary_list *list, uint32_t limit) {
list->head = NULL;
list->tail = NULL;
list->count = 0;
list->limit = limit;
}
2022-11-17 16:05:44 +00:00
void ordinary_list_delete(struct ordinary_list *list) {
struct ordinary_node *node = list->head;
2022-11-17 16:05:44 +00:00
while(node) {
struct ordinary_node *next = node->next;
free(node); // R.I.P.
node = next;
}
2022-11-17 17:15:02 +00:00
list->head = NULL;
list->tail = NULL;
list->count = 0;
list->limit = 0;
}
2022-11-17 07:18:56 +00:00
2022-11-17 16:05:44 +00:00
uint8_t ordinary_list_empty(struct ordinary_list *list) {
2022-11-17 23:28:42 +00:00
return !list->head;
2022-11-17 16:05:44 +00:00
}
uint8_t ordinary_list_full(struct ordinary_list *list) {
2022-11-17 23:28:42 +00:00
return !list->limit || list->count == list->limit;
2022-11-17 16:05:44 +00:00
}
struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) {
struct ordinary_node *node = list->head;
for(; node; node = node->next) {
idx -= 1;
if(!idx) {
return node;
}
}
return NULL;
}
struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb) {
struct ordinary_node *node = list->head;
uint32_t idx = 0;
for(; node; node = node->next) {
if(cb(node, idx)) {
return node;
}
if(list->limit) {
idx += 1;
}
}
return NULL;
}
uint8_t ordinary_list_for(struct ordinary_list *list, callback cb) {
struct ordinary_node *node = list->head;
uint32_t idx = 0, res = 0;
for(; node; node = node->next) {
res |= cb(list, idx);
if(list->limit) {
idx += 1;
}
}
return res;
}
2022-11-17 07:18:56 +00:00
static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) {
if(!list->head) {
list->head = node;
list->tail = node;
} else if(list->head == list->tail) {
list->tail = node;
list->head->next = list->tail;
list->tail->prev = list->head;
} else {
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
}
if(list->limit) {
list->count += 1;
}
}
struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) {
2022-11-17 23:28:42 +00:00
if(ordinary_list_full(list)) {
2022-11-17 07:18:56 +00:00
return NULL;
}
struct ordinary_node *node = malloc(sizeof(struct ordinary_node));
if(!node) {
return NULL;
}
node->prev = NULL;
node->next = NULL;
node->val = val;
ordinary_list_insert(list, node);
return node;
}
2022-11-17 07:22:31 +00:00
2022-11-17 23:28:42 +00:00
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;
}
2022-11-17 16:05:44 +00:00
2022-11-17 07:22:31 +00:00
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;
}
2022-11-17 16:05:44 +00:00
ordinary_list_insert(dst, node);
2022-11-17 07:22:31 +00:00
}
2022-11-17 23:28:42 +00:00
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;
}
}