140 lines
2.6 KiB
C
140 lines
2.6 KiB
C
#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;
|
|
}
|
|
|
|
void ordinary_list_delete(struct ordinary_list *list) {
|
|
struct ordinary_node *node = list->head;
|
|
|
|
while(node) {
|
|
struct ordinary_node *next = node->next;
|
|
free(node); // R.I.P.
|
|
node = next;
|
|
}
|
|
}
|
|
|
|
uint8_t ordinary_list_empty(struct ordinary_list *list) {
|
|
if(!list->limit) {
|
|
return 1;
|
|
}
|
|
|
|
return list->count == 0;
|
|
}
|
|
|
|
uint8_t ordinary_list_full(struct ordinary_list *list) {
|
|
if(!list->limit) {
|
|
return 1;
|
|
}
|
|
|
|
return list->count == list->limit;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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) {
|
|
if(list->limit && list->count == list->limit) {
|
|
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;
|
|
}
|
|
|
|
void ordinary_list_mov(struct standard_list *dst, struct standard_list *src, struct standard_node *node) {
|
|
if(dst->limit &&)
|
|
|
|
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;
|
|
}
|
|
|
|
ordinary_list_insert(dst, node);
|
|
}
|