Files
fx/ds/list.c
2026-03-16 10:35:43 +00:00

530 lines
12 KiB
C

#include <fx/core/iterator.h>
#include <fx/core/queue.h>
#include <fx/ds/list.h>
#include <stdlib.h>
#include <string.h>
/*** PRIVATE DATA *************************************************************/
struct fx_list_entry {
struct fx_queue_entry e_entry;
void *e_data;
};
struct fx_list_p {
struct fx_queue l_queue;
size_t l_len;
};
struct fx_list_iterator_p {
fx_list *_q;
struct fx_list_p *_q_p;
struct fx_queue_entry *_q_entry;
size_t i;
void *item;
fx_list_entry *entry;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static bool list_empty(struct fx_list_p *q)
{
return q->l_len == 0;
}
static void *list_first_item(const struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
return list_entry->e_data;
}
static void *list_last_item(const struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_last(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
return list_entry->e_data;
}
static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
return list_entry;
}
static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_last(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
return list_entry;
}
static size_t list_length(const struct fx_list_p *q)
{
return q->l_len;
}
static struct fx_list_entry *make_entry(void *item)
{
struct fx_list_entry *entry = malloc(sizeof *entry);
if (!entry) {
return NULL;
}
memset(entry, 0x0, sizeof *entry);
entry->e_data = item;
return entry;
}
static struct fx_list_entry *list_insert_before(
struct fx_list_p *q, void *ptr, struct fx_list_entry *before)
{
struct fx_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
fx_queue_insert_before(&q->l_queue, &entry->e_entry, &before->e_entry);
q->l_len++;
return entry;
}
static struct fx_list_entry *list_insert_after(
struct fx_list_p *q, void *ptr, struct fx_list_entry *after)
{
struct fx_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
fx_queue_insert_after(&q->l_queue, &entry->e_entry, &after->e_entry);
q->l_len++;
return entry;
}
static struct fx_list_entry *list_push_front(struct fx_list_p *q, void *ptr)
{
struct fx_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
fx_queue_push_front(&q->l_queue, &entry->e_entry);
q->l_len++;
return entry;
}
static struct fx_list_entry *list_push_back(struct fx_list_p *q, void *ptr)
{
struct fx_list_entry *entry = make_entry(ptr);
if (!entry) {
return NULL;
}
fx_queue_push_back(&q->l_queue, &entry->e_entry);
q->l_len++;
return entry;
}
static void *list_pop_front(struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_pop_front(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
void *item = list_entry->e_data;
free(list_entry);
q->l_len--;
return item;
}
static void *list_pop_back(struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_pop_back(&q->l_queue);
if (!entry) {
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
void *item = list_entry->e_data;
free(list_entry);
q->l_len--;
return item;
}
static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
{
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
if (list_entry->e_data == item) {
return list_entry;
}
entry = fx_queue_next(entry);
}
return NULL;
}
static fx_status list_delete_item(struct fx_list_p *q, void *ptr)
{
struct fx_list_entry *entry = find_item(q, ptr);
if (!entry) {
return FX_ERR_NO_ENTRY;
}
fx_queue_delete(&q->l_queue, &entry->e_entry);
q->l_len--;
free(entry);
return FX_SUCCESS;
}
static fx_status list_delete_entry(struct fx_list_p *q, struct fx_list_entry *entry)
{
fx_queue_delete(&q->l_queue, &entry->e_entry);
q->l_len--;
free(entry);
return FX_SUCCESS;
}
static void list_delete_all(struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_queue_entry *next = fx_queue_next(entry);
free(list_entry);
entry = next;
}
q->l_len = 0;
}
/*** PUBLIC FUNCTIONS *********************************************************/
bool fx_list_empty(fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_empty, q);
}
void *fx_list_first_item(const fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_first_item, q);
}
void *fx_list_last_item(const fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_last_item, q);
}
struct fx_list_entry *fx_list_first_entry(const fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_first_entry, q);
}
struct fx_list_entry *fx_list_last_entry(const fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, fx_list_last_entry, q);
}
struct fx_list_entry *fx_list_next(const struct fx_list_entry *entry)
{
if (!entry) {
return NULL;
}
struct fx_queue_entry *next = fx_queue_next(&entry->e_entry);
if (!next) {
return NULL;
}
return fx_unbox(struct fx_list_entry, next, e_entry);
}
struct fx_list_entry *fx_list_prev(const struct fx_list_entry *entry)
{
if (!entry) {
return NULL;
}
struct fx_queue_entry *next = fx_queue_prev(&entry->e_entry);
if (!next) {
return NULL;
}
return fx_unbox(struct fx_list_entry, next, e_entry);
}
size_t fx_list_length(const fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_length, q);
}
struct fx_list_entry *fx_list_insert_before(
fx_list *q, void *ptr, struct fx_list_entry *before)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_insert_before, q, ptr, before);
}
struct fx_list_entry *fx_list_insert_after(
fx_list *q, void *ptr, struct fx_list_entry *after)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_insert_after, q, ptr, after);
}
struct fx_list_entry *fx_list_push_front(fx_list *q, void *ptr)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_push_front, q, ptr);
}
struct fx_list_entry *fx_list_push_back(fx_list *q, void *ptr)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_push_back, q, ptr);
}
void *fx_list_pop_front(fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_pop_front, q);
}
void *fx_list_pop_back(fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_pop_back, q);
}
fx_status fx_list_delete_item(fx_list *q, void *ptr)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_delete_item, q, ptr);
}
fx_status fx_list_delete_entry(fx_list *q, struct fx_list_entry *entry)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_LIST, list_delete_entry, q, entry);
}
void fx_list_delete_all(fx_list *q)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_LIST, list_delete_all, q);
}
void *fx_list_entry_value(const struct fx_list_entry *entry)
{
return entry ? entry->e_data : NULL;
}
fx_iterator *fx_list_begin(fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
const fx_iterator *fx_list_cbegin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
it->_q = (fx_list *)q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void list_init(fx_object *obj, void *priv)
{
struct fx_list_p *list = priv;
}
static void list_fini(fx_object *obj, void *priv)
{
struct fx_list_p *list = priv;
list_delete_all(list);
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
if (!it->_q_entry) {
it->entry = NULL;
it->item = NULL;
return FX_ERR_NO_DATA;
}
it->_q_entry = fx_queue_next(it->_q_entry);
if (!it->_q_entry) {
it->entry = NULL;
it->item = NULL;
return FX_ERR_NO_DATA;
}
it->i++;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return it->entry != NULL;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
if (!it->entry || !it->_q_entry) {
return FX_ERR_OUT_OF_BOUNDS;
}
struct fx_queue_entry *next = fx_queue_next(it->_q_entry);
fx_queue_delete(&it->_q_p->l_queue, it->_q_entry);
free(it->entry);
it->_q_entry = next;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_PTR(it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->item);
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_list DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_list)
FX_TYPE_DEFINITION_BEGIN(fx_list)
FX_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb);
FX_TYPE_CLASS(fx_list_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_list_p);
FX_TYPE_INSTANCE_INIT(list_init);
FX_TYPE_INSTANCE_FINI(list_fini);
FX_TYPE_DEFINITION_END(fx_list)
// ---- fx_list_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_list_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_list_iterator)
FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431);
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_list_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_list_iterator_p);
FX_TYPE_DEFINITION_END(fx_list_iterator)