Files
bluelib/ds/list.c

440 lines
8.9 KiB
C
Raw Normal View History

#include <blue/ds/list.h>
#include <stdlib.h>
#include <string.h>
2025-10-19 12:46:25 +01:00
/*** PRIVATE DATA *************************************************************/
2025-10-19 12:46:25 +01:00
struct b_list_entry {
struct b_queue_entry e_entry;
void *e_data;
};
2025-10-19 12:46:25 +01:00
struct b_list_p {
struct b_queue l_queue;
size_t l_len;
};
2025-10-19 12:46:25 +01:00
/*** PRIVATE FUNCTIONS ********************************************************/
2025-10-19 12:46:25 +01:00
static bool list_empty(struct b_list_p *q)
{
return q->l_len == 0;
}
2025-10-19 12:46:25 +01:00
static void *list_first_item(const struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_first(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
return list_entry->e_data;
}
2025-10-19 12:46:25 +01:00
static void *list_last_item(const struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_last(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
return list_entry->e_data;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_first_entry(const struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_first(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
return list_entry;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_last_entry(const struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_last(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
return list_entry;
}
2025-10-19 12:46:25 +01:00
static size_t list_length(const struct b_list_p *q)
{
return q->l_len;
}
static struct b_list_entry *make_entry(void *item)
{
struct b_list_entry *entry = malloc(sizeof *entry);
if (!entry) {
return NULL;
}
memset(entry, 0x0, sizeof *entry);
entry->e_data = item;
return entry;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_insert_before(
struct b_list_p *q, void *ptr, struct b_list_entry *before)
{
struct b_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
b_queue_insert_before(&q->l_queue, &entry->e_entry, &before->e_entry);
q->l_len++;
return entry;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_insert_after(
struct b_list_p *q, void *ptr, struct b_list_entry *after)
{
struct b_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
b_queue_insert_after(&q->l_queue, &entry->e_entry, &after->e_entry);
q->l_len++;
return entry;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_push_front(struct b_list_p *q, void *ptr)
{
struct b_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
b_queue_push_front(&q->l_queue, &entry->e_entry);
q->l_len++;
return entry;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *list_push_back(struct b_list_p *q, void *ptr)
{
struct b_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
b_queue_push_back(&q->l_queue, &entry->e_entry);
q->l_len++;
return entry;
}
2025-10-19 12:46:25 +01:00
static void *list_pop_front(struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_pop_front(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
void *item = list_entry->e_data;
free(list_entry);
q->l_len--;
return item;
}
2025-10-19 12:46:25 +01:00
static void *list_pop_back(struct b_list_p *q)
{
struct b_queue_entry *entry = b_queue_pop_back(&q->l_queue);
if (!entry) {
return NULL;
}
struct b_list_entry *list_entry
= b_unbox(struct b_list_entry, entry, e_entry);
void *item = list_entry->e_data;
free(list_entry);
q->l_len--;
return item;
}
2025-10-19 12:46:25 +01:00
static struct b_list_entry *find_item(struct b_list_p *list, void *item)
{
struct b_queue_iterator it;
b_queue_foreach (&it, &list->l_queue) {
struct b_list_entry *entry
= b_unbox(struct b_list_entry, it.entry, e_entry);
if (entry->e_data == item) {
return entry;
}
}
return NULL;
}
2025-10-19 12:46:25 +01:00
static b_status list_delete_item(struct b_list_p *q, void *ptr)
{
struct b_list_entry *entry = find_item(q, ptr);
if (!entry) {
return B_ERR_NO_ENTRY;
}
b_queue_delete(&q->l_queue, &entry->e_entry);
q->l_len--;
free(entry);
return B_SUCCESS;
}
2025-10-19 12:46:25 +01:00
static b_status list_delete_entry(struct b_list_p *q, struct b_list_entry *entry)
{
b_queue_delete(&q->l_queue, &entry->e_entry);
q->l_len--;
free(entry);
return B_SUCCESS;
}
2025-10-19 12:46:25 +01:00
static void list_delete_all(struct b_list_p *q)
{
struct b_queue_iterator it;
b_queue_iterator_begin(&q->l_queue, &it);
while (b_queue_iterator_is_valid(&it)) {
struct b_list_entry *entry
= b_unbox(struct b_list_entry, it.entry, e_entry);
b_queue_iterator_erase(&it);
free(entry);
}
q->l_len = 0;
}
2025-10-19 12:46:25 +01:00
/*** PUBLIC FUNCTIONS *********************************************************/
bool b_list_empty(b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_empty, q);
}
void *b_list_first_item(const b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_first_item, q);
}
void *b_list_last_item(const b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_last_item, q);
}
struct b_list_entry *b_list_first_entry(const b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_first_entry, q);
}
struct b_list_entry *b_list_last_entry(const b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, b_list_last_entry, q);
}
struct b_list_entry *b_list_next(const struct b_list_entry *entry)
{
if (!entry) {
return NULL;
}
struct b_queue_entry *next = b_queue_next(&entry->e_entry);
if (!next) {
return NULL;
}
return b_unbox(struct b_list_entry, next, e_entry);
}
struct b_list_entry *b_list_prev(const struct b_list_entry *entry)
{
if (!entry) {
return NULL;
}
struct b_queue_entry *next = b_queue_prev(&entry->e_entry);
if (!next) {
return NULL;
}
return b_unbox(struct b_list_entry, next, e_entry);
}
size_t b_list_length(const b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_length, q);
}
struct b_list_entry *b_list_insert_before(
b_list *q, void *ptr, struct b_list_entry *before)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_before, q, ptr, before);
}
struct b_list_entry *b_list_insert_after(
b_list *q, void *ptr, struct b_list_entry *after)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_after, q, ptr, after);
}
struct b_list_entry *b_list_push_front(b_list *q, void *ptr)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_front, q, ptr);
}
struct b_list_entry *b_list_push_back(b_list *q, void *ptr)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_back, q, ptr);
}
void *b_list_pop_front(b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_front, q);
}
void *b_list_pop_back(b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_back, q);
}
b_status b_list_delete_item(b_list *q, void *ptr)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_item, q, ptr);
}
b_status b_list_delete_entry(b_list *q, struct b_list_entry *entry)
{
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_entry, q, entry);
}
void b_list_delete_all(b_list *q)
{
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_delete_all, q);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void list_init(b_object *obj, void *priv)
{
struct b_list_p *list = priv;
}
static void list_fini(b_object *obj, void *priv)
{
struct b_list_p *list = priv;
list_delete_all(list);
}
/*** CLASS DEFINITION *********************************************************/
B_TYPE_CLASS_DEFINITION_BEGIN(b_list)
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
B_INTERFACE_ENTRY(to_string) = NULL;
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
B_TYPE_CLASS_DEFINITION_END(b_list)
B_TYPE_DEFINITION_BEGIN(b_list)
B_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb);
B_TYPE_CLASS(b_list_class);
B_TYPE_INSTANCE_PRIVATE(struct b_list_p);
2025-10-19 12:46:25 +01:00
B_TYPE_INSTANCE_INIT(list_init);
B_TYPE_INSTANCE_FINI(list_fini);
2025-10-19 12:46:25 +01:00
B_TYPE_DEFINITION_END(b_list)
/*** ITERATOR FUNCTIONS *******************************************************/
int b_list_iterator_begin(const b_list *q, struct b_list_iterator *it)
{
it->_q = q;
2025-10-19 12:46:25 +01:00
it->_q_p = b_object_get_private(q, B_TYPE_LIST);
b_queue_iterator_begin(&it->_q_p->l_queue, &it->_base);
it->i = 0;
it->entry = b_unbox(struct b_list_entry, it->_base.entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
bool b_list_iterator_next(struct b_list_iterator *it)
{
bool ok = b_queue_iterator_next(&it->_base);
if (!ok) {
it->entry = NULL;
it->item = NULL;
return false;
}
it->i++;
it->entry = b_unbox(struct b_list_entry, it->_base.entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return it->entry != NULL;
}
b_status b_list_iterator_erase(struct b_list_iterator *it)
{
if (!it->entry) {
return B_ERR_OUT_OF_BOUNDS;
}
b_queue_iterator_erase(&it->_base);
free(it->entry);
it->entry = b_unbox(struct b_list_entry, it->_base.entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return B_SUCCESS;
}
bool b_list_iterator_is_valid(const struct b_list_iterator *it)
{
return it->entry != NULL;
}
void *b_list_entry_value(const struct b_list_entry *entry)
{
return entry ? entry->e_data : NULL;
}