ds: list: convert to new object system
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
#ifndef BLUE_OBJECT_LIST_H_
|
||||
#define BLUE_OBJECT_LIST_H_
|
||||
#ifndef BLUE_DS_LIST_H_
|
||||
#define BLUE_DS_LIST_H_
|
||||
|
||||
#include <blue/core/macros.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/ds/object.h>
|
||||
|
||||
#define B_LIST(p) ((b_list *)(p))
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
#define B_TYPE_LIST (b_list_get_type())
|
||||
|
||||
struct b_list_p;
|
||||
|
||||
B_DECLARE_TYPE(b_list);
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_list)
|
||||
B_TYPE_CLASS_DECLARATION_END(b_list)
|
||||
|
||||
typedef struct b_list b_list;
|
||||
typedef struct b_list_entry b_list_entry;
|
||||
|
||||
#define b_list_foreach(it, q) \
|
||||
@@ -16,21 +24,16 @@ typedef struct b_list_entry b_list_entry;
|
||||
typedef struct b_list_iterator {
|
||||
b_queue_iterator _base;
|
||||
const b_list *_q;
|
||||
const struct b_list_p *_q_p;
|
||||
|
||||
size_t i;
|
||||
void *item;
|
||||
b_list_entry *entry;
|
||||
} b_list_iterator;
|
||||
|
||||
BLUE_API b_list *b_list_create(void);
|
||||
BLUE_API b_type b_list_get_type(void);
|
||||
|
||||
static inline b_list *b_list_retain(b_list *str)
|
||||
{
|
||||
return B_LIST(b_retain(B_DSREF(str)));
|
||||
}
|
||||
static inline void b_list_release(b_list *str)
|
||||
{
|
||||
b_release(B_DSREF(str));
|
||||
}
|
||||
B_TYPE_DEFAULT_CONSTRUCTOR(b_list, B_TYPE_LIST);
|
||||
|
||||
BLUE_API bool b_list_empty(b_list *q);
|
||||
BLUE_API void *b_list_first_item(const b_list *q);
|
||||
@@ -64,4 +67,6 @@ BLUE_API bool b_list_iterator_is_valid(const b_list_iterator *it);
|
||||
|
||||
BLUE_API void *b_list_entry_value(const b_list_entry *entry);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
410
ds/list.c
410
ds/list.c
@@ -1,36 +1,27 @@
|
||||
#include "list.h"
|
||||
|
||||
#include <blue/ds/list.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void list_release(struct b_dsref *obj);
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
static struct b_dsref_type list_type = {
|
||||
.t_name = "corelib::list",
|
||||
.t_flags = B_DSREF_FUNDAMENTAL,
|
||||
.t_id = B_DSREF_TYPE_LIST,
|
||||
.t_instance_size = sizeof(struct b_list),
|
||||
.t_release = list_release,
|
||||
struct b_list_entry {
|
||||
struct b_queue_entry e_entry;
|
||||
void *e_data;
|
||||
};
|
||||
|
||||
struct b_list *b_list_create(void)
|
||||
{
|
||||
struct b_list *list
|
||||
= (struct b_list *)b_dsref_type_instantiate(&list_type);
|
||||
if (!list) {
|
||||
return NULL;
|
||||
}
|
||||
struct b_list_p {
|
||||
struct b_queue l_queue;
|
||||
size_t l_len;
|
||||
};
|
||||
|
||||
return list;
|
||||
}
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
bool b_list_empty(struct b_list *q)
|
||||
static bool list_empty(struct b_list_p *q)
|
||||
{
|
||||
return q->l_len == 0;
|
||||
}
|
||||
|
||||
void *b_list_first_item(const struct b_list *q)
|
||||
static void *list_first_item(const struct b_list_p *q)
|
||||
{
|
||||
struct b_queue_entry *entry = b_queue_first(&q->l_queue);
|
||||
|
||||
@@ -44,7 +35,7 @@ void *b_list_first_item(const struct b_list *q)
|
||||
return list_entry->e_data;
|
||||
}
|
||||
|
||||
void *b_list_last_item(const struct b_list *q)
|
||||
static void *list_last_item(const struct b_list_p *q)
|
||||
{
|
||||
struct b_queue_entry *entry = b_queue_last(&q->l_queue);
|
||||
|
||||
@@ -58,7 +49,7 @@ void *b_list_last_item(const struct b_list *q)
|
||||
return list_entry->e_data;
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_first_entry(const struct b_list *q)
|
||||
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);
|
||||
|
||||
@@ -72,7 +63,7 @@ struct b_list_entry *b_list_first_entry(const struct b_list *q)
|
||||
return list_entry;
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_last_entry(const struct b_list *q)
|
||||
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);
|
||||
|
||||
@@ -86,6 +77,194 @@ struct b_list_entry *b_list_last_entry(const struct b_list *q)
|
||||
return list_entry;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*** 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) {
|
||||
@@ -116,171 +295,94 @@ struct b_list_entry *b_list_prev(const struct b_list_entry *entry)
|
||||
return b_unbox(struct b_list_entry, next, e_entry);
|
||||
}
|
||||
|
||||
size_t b_list_length(const struct b_list *q)
|
||||
size_t b_list_length(const b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_length, q);
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_insert_before(
|
||||
struct b_list *q, void *ptr, struct b_list_entry *before)
|
||||
b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_before, q, ptr, before);
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_insert_after(
|
||||
struct b_list *q, void *ptr, struct b_list_entry *after)
|
||||
b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_insert_after, q, ptr, after);
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_push_front(struct b_list *q, void *ptr)
|
||||
struct b_list_entry *b_list_push_front(b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_front, q, ptr);
|
||||
}
|
||||
|
||||
struct b_list_entry *b_list_push_back(struct b_list *q, void *ptr)
|
||||
struct b_list_entry *b_list_push_back(b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_push_back, q, ptr);
|
||||
}
|
||||
|
||||
void *b_list_pop_front(struct b_list *q)
|
||||
void *b_list_pop_front(b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_front, q);
|
||||
}
|
||||
|
||||
void *b_list_pop_back(struct b_list *q)
|
||||
void *b_list_pop_back(b_list *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;
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_pop_back, q);
|
||||
}
|
||||
|
||||
static struct b_list_entry *find_item(struct b_list *list, void *item)
|
||||
b_status b_list_delete_item(b_list *q, void *ptr)
|
||||
{
|
||||
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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_item, q, ptr);
|
||||
}
|
||||
|
||||
b_status b_list_delete_item(struct b_list *q, void *ptr)
|
||||
b_status b_list_delete_entry(b_list *q, struct b_list_entry *entry)
|
||||
{
|
||||
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;
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_LIST, list_delete_entry, q, entry);
|
||||
}
|
||||
|
||||
b_status b_list_delete_entry(struct b_list *q, struct b_list_entry *entry)
|
||||
void b_list_delete_all(b_list *q)
|
||||
{
|
||||
b_queue_delete(&q->l_queue, &entry->e_entry);
|
||||
q->l_len--;
|
||||
|
||||
free(entry);
|
||||
|
||||
return B_SUCCESS;
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_LIST, list_delete_all, q);
|
||||
}
|
||||
|
||||
void b_list_delete_all(struct b_list *q)
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void list_init(b_object *obj, void *priv)
|
||||
{
|
||||
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;
|
||||
struct b_list_p *list = priv;
|
||||
}
|
||||
|
||||
int b_list_iterator_begin(const struct b_list *q, struct b_list_iterator *it)
|
||||
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_INIT(list_init);
|
||||
B_TYPE_INSTANCE_INIT(list_fini);
|
||||
B_TYPE_DEFINITION_END(b_list)
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
int b_list_iterator_begin(const b_list *q, struct b_list_iterator *it)
|
||||
{
|
||||
b_queue_iterator_begin(&q->l_queue, &it->_base);
|
||||
it->_q = q;
|
||||
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) {
|
||||
@@ -334,9 +436,3 @@ void *b_list_entry_value(const struct b_list_entry *entry)
|
||||
{
|
||||
return entry ? entry->e_data : NULL;
|
||||
}
|
||||
|
||||
static void list_release(struct b_dsref *obj)
|
||||
{
|
||||
struct b_list *list = B_LIST(obj);
|
||||
b_list_delete_all(list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user