ds: array: convert to new object system
This commit is contained in:
334
ds/array.c
334
ds/array.c
@@ -1,67 +1,22 @@
|
||||
#include "array.h"
|
||||
|
||||
#include <blue/core/iterator.h>
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/ds/array.h>
|
||||
#include <blue/ds/type.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void array_release(struct b_dsref *obj);
|
||||
static void array_to_string(const struct b_dsref *obj, struct b_stream *out);
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
static struct b_dsref_type array_type = {
|
||||
.t_flags = B_DSREF_FUNDAMENTAL,
|
||||
.t_id = B_DSREF_TYPE_ARRAY,
|
||||
.t_name = "corelib::array",
|
||||
.t_instance_size = sizeof(struct b_array),
|
||||
.t_release = array_release,
|
||||
.t_to_string = array_to_string,
|
||||
struct b_array_p {
|
||||
/* number of items in array */
|
||||
size_t ar_len;
|
||||
/* maximum number of items that can currently be stored in array */
|
||||
size_t ar_cap;
|
||||
b_object **ar_data;
|
||||
};
|
||||
|
||||
struct b_array *b_array_create(void)
|
||||
{
|
||||
struct b_array *array
|
||||
= (struct b_array *)b_dsref_type_instantiate(&array_type);
|
||||
if (!array) {
|
||||
return NULL;
|
||||
}
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
struct b_array *b_array_create_with_values(
|
||||
struct b_dsref *const *values, size_t nr_values)
|
||||
{
|
||||
struct b_array *array = b_array_create();
|
||||
if (!array) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t real_nr_values = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (values[i]) {
|
||||
real_nr_values++;
|
||||
}
|
||||
}
|
||||
|
||||
array->ar_len = real_nr_values;
|
||||
array->ar_cap = real_nr_values;
|
||||
array->ar_data = calloc(real_nr_values, sizeof(struct b_dsref *));
|
||||
if (!array->ar_data) {
|
||||
b_array_release(array);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
array->ar_data[index++] = b_retain(values[i]);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
static b_status resize_array(struct b_array *array, size_t new_capacity)
|
||||
static b_status resize_array(struct b_array_p *array, size_t new_capacity)
|
||||
{
|
||||
if (array->ar_cap < new_capacity) {
|
||||
void *new_data = realloc(
|
||||
@@ -73,7 +28,7 @@ static b_status resize_array(struct b_array *array, size_t new_capacity)
|
||||
array->ar_data = new_data;
|
||||
} else {
|
||||
for (size_t i = new_capacity; i < array->ar_len; i++) {
|
||||
b_release(array->ar_data[i]);
|
||||
b_object_unref(array->ar_data[i]);
|
||||
}
|
||||
|
||||
void *new_data = realloc(
|
||||
@@ -93,17 +48,7 @@ static b_status resize_array(struct b_array *array, size_t new_capacity)
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_array_append(struct b_array *array, struct b_dsref *value)
|
||||
{
|
||||
return b_array_insert(array, value, B_NPOS);
|
||||
}
|
||||
|
||||
b_status b_array_prepend(struct b_array *array, struct b_dsref *value)
|
||||
{
|
||||
return b_array_insert(array, value, 0);
|
||||
}
|
||||
|
||||
b_status b_array_insert(struct b_array *array, struct b_dsref *value, size_t at)
|
||||
static b_status array_insert(struct b_array_p *array, b_object *value, size_t at)
|
||||
{
|
||||
if (at == B_NPOS) {
|
||||
at = array->ar_len;
|
||||
@@ -123,29 +68,29 @@ b_status b_array_insert(struct b_array *array, struct b_dsref *value, size_t at)
|
||||
}
|
||||
}
|
||||
|
||||
struct b_dsref **src = array->ar_data + at;
|
||||
struct b_dsref **dest = src + 1;
|
||||
b_object **src = array->ar_data + at;
|
||||
b_object **dest = src + 1;
|
||||
size_t move_len = (array->ar_len - at) * sizeof(struct b_dsref *);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
array->ar_data[at] = b_retain(value);
|
||||
array->ar_data[at] = b_object_ref(value);
|
||||
array->ar_len++;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_array_remove(struct b_array *array, size_t at)
|
||||
static b_status array_remove(struct b_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return B_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
struct b_dsref **src = array->ar_data + at;
|
||||
struct b_dsref **dest = src + 1;
|
||||
b_object **src = array->ar_data + at;
|
||||
b_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct b_dsref *);
|
||||
|
||||
b_release(array->ar_data[at]);
|
||||
b_object_unref(array->ar_data[at]);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -154,27 +99,27 @@ b_status b_array_remove(struct b_array *array, size_t at)
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
b_status b_array_remove_front(struct b_array *array)
|
||||
static b_status array_remove_front(struct b_array_p *array)
|
||||
{
|
||||
return b_array_remove(array, 0);
|
||||
return array_remove(array, 0);
|
||||
}
|
||||
|
||||
b_status b_array_remove_back(struct b_array *array)
|
||||
static b_status array_remove_back(struct b_array_p *array)
|
||||
{
|
||||
return b_array_remove(array, array->ar_len - 1);
|
||||
return array_remove(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
struct b_dsref *b_array_pop(b_array *array, size_t at)
|
||||
static b_object *array_pop(struct b_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct b_dsref **src = array->ar_data + at;
|
||||
struct b_dsref **dest = src + 1;
|
||||
b_object **src = array->ar_data + at;
|
||||
b_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct b_dsref *);
|
||||
|
||||
struct b_dsref *out = array->ar_data[at];
|
||||
b_object *out = array->ar_data[at];
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -183,17 +128,17 @@ struct b_dsref *b_array_pop(b_array *array, size_t at)
|
||||
return out;
|
||||
}
|
||||
|
||||
struct b_dsref *b_array_pop_front(struct b_array *array)
|
||||
static b_object *array_pop_front(struct b_array_p *array)
|
||||
{
|
||||
return b_array_pop(array, 0);
|
||||
return array_pop(array, 0);
|
||||
}
|
||||
|
||||
struct b_dsref *b_array_pop_back(struct b_array *array)
|
||||
static b_object *array_pop_back(struct b_array_p *array)
|
||||
{
|
||||
return b_array_pop(array, array->ar_len - 1);
|
||||
return array_pop(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
struct b_dsref *b_array_at(const struct b_array *array, size_t at)
|
||||
static b_object *array_at(const struct b_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
@@ -202,28 +147,169 @@ struct b_dsref *b_array_at(const struct b_array *array, size_t at)
|
||||
return array->ar_data[at];
|
||||
}
|
||||
|
||||
struct b_dsref *b_array_get(struct b_array *array, size_t at)
|
||||
static b_object *array_get(struct b_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b_retain(array->ar_data[at]);
|
||||
return b_object_ref(array->ar_data[at]);
|
||||
}
|
||||
|
||||
size_t b_array_size(const struct b_array *array)
|
||||
static size_t array_size(const struct b_array_p *array)
|
||||
{
|
||||
return array->ar_len;
|
||||
}
|
||||
|
||||
size_t b_array_capacity(const struct b_array *array)
|
||||
static size_t array_capacity(const struct b_array_p *array)
|
||||
{
|
||||
return array->ar_cap;
|
||||
}
|
||||
|
||||
static void array_to_string(const struct b_dsref *obj, struct b_stream *out)
|
||||
static void array_clear(struct b_array_p *array)
|
||||
{
|
||||
struct b_array *array = B_ARRAY(obj);
|
||||
if (!array->ar_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
b_object_unref(array->ar_data[i]);
|
||||
}
|
||||
|
||||
memset(array->ar_data, 0x0, array->ar_cap * sizeof(b_object *));
|
||||
array->ar_len = 0;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
b_array *b_array_create_with_values(b_object *const *values, size_t nr_values)
|
||||
{
|
||||
b_array *array = b_array_create();
|
||||
if (!array) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct b_array_p *p = b_object_get_private(array, B_TYPE_ARRAY);
|
||||
|
||||
size_t real_nr_values = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (values[i]) {
|
||||
real_nr_values++;
|
||||
}
|
||||
}
|
||||
|
||||
p->ar_len = real_nr_values;
|
||||
p->ar_cap = real_nr_values;
|
||||
p->ar_data = calloc(real_nr_values, sizeof(struct b_dsref *));
|
||||
if (!p->ar_data) {
|
||||
b_array_unref(array);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
p->ar_data[index++] = b_object_ref(values[i]);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
b_status b_array_insert(b_array *array, b_object *value, size_t at)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_insert, array, value, at);
|
||||
}
|
||||
|
||||
b_status b_array_remove(b_array *array, size_t at)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_remove, array, at);
|
||||
}
|
||||
|
||||
b_status b_array_remove_front(b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_remove_front, array);
|
||||
}
|
||||
|
||||
b_status b_array_remove_back(b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_remove_back, array);
|
||||
}
|
||||
|
||||
b_object *b_array_pop(b_array *array, size_t at)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_pop, array, at);
|
||||
}
|
||||
|
||||
b_object *b_array_pop_front(b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_pop_front, array);
|
||||
}
|
||||
|
||||
b_object *b_array_pop_back(b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_pop_back, array);
|
||||
}
|
||||
|
||||
b_object *b_array_at(const b_array *array, size_t at)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_at, array, at);
|
||||
}
|
||||
|
||||
b_object *b_array_get(b_array *array, size_t at)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ARRAY, array_get, array, at);
|
||||
}
|
||||
|
||||
size_t b_array_size(const b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_size, array);
|
||||
}
|
||||
|
||||
size_t b_array_capacity(const b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_capacity, array);
|
||||
}
|
||||
|
||||
void b_array_clear(b_array *array)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ARRAY, array_clear, array);
|
||||
}
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
|
||||
b_status b_array_append(b_array *array, b_object *value)
|
||||
{
|
||||
return b_array_insert(array, value, B_NPOS);
|
||||
}
|
||||
|
||||
b_status b_array_prepend(b_array *array, b_object *value)
|
||||
{
|
||||
return b_array_insert(array, value, 0);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void array_init(b_object *obj, void *priv)
|
||||
{
|
||||
struct b_array_p *array = priv;
|
||||
}
|
||||
|
||||
static void array_fini(b_object *obj, void *priv)
|
||||
{
|
||||
struct b_array_p *array = priv;
|
||||
|
||||
if (array->ar_data) {
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
b_object_unref(array->ar_data[i]);
|
||||
}
|
||||
|
||||
free(array->ar_data);
|
||||
array->ar_data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void array_to_string(const b_object *obj, struct b_stream *out)
|
||||
{
|
||||
struct b_array_p *array = b_object_get_private(obj, B_TYPE_ARRAY);
|
||||
|
||||
if (!array->ar_len) {
|
||||
b_stream_write_string(out, "[]", NULL);
|
||||
@@ -233,24 +319,24 @@ static void array_to_string(const struct b_dsref *obj, struct b_stream *out)
|
||||
b_stream_write_string(out, "[\n", NULL);
|
||||
|
||||
b_stream_push_indent(out, 1);
|
||||
size_t len = b_array_size(array);
|
||||
size_t len = array_size(array);
|
||||
|
||||
b_array_iterator it;
|
||||
b_array_foreach(&it, array)
|
||||
{
|
||||
bool is_string = b_typeid(it.value) == B_DSREF_TYPE_STRING;
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
b_object *value = array->ar_data[i];
|
||||
bool is_string
|
||||
= false; // b_object_is_type(value, B_TYPE_STRING);
|
||||
|
||||
if (is_string) {
|
||||
b_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
b_to_string(it.value, out);
|
||||
b_object_to_string(value, out);
|
||||
|
||||
if (is_string) {
|
||||
b_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
if (it.i < len - 1) {
|
||||
if (i < len - 1) {
|
||||
b_stream_write_string(out, ",", NULL);
|
||||
}
|
||||
|
||||
@@ -261,26 +347,22 @@ static void array_to_string(const struct b_dsref *obj, struct b_stream *out)
|
||||
b_stream_write_char(out, ']');
|
||||
}
|
||||
|
||||
static void array_release(struct b_dsref *obj)
|
||||
{
|
||||
struct b_array *array = B_ARRAY(obj);
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
if (array->ar_data) {
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
b_release(array->ar_data[i]);
|
||||
}
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_array)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = array_to_string;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_array)
|
||||
|
||||
free(array->ar_data);
|
||||
array->ar_data = NULL;
|
||||
}
|
||||
}
|
||||
B_TYPE_DEFINITION_BEGIN(b_array)
|
||||
B_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b);
|
||||
B_TYPE_CLASS(b_array_class);
|
||||
B_TYPE_INSTANCE_INIT(array_init);
|
||||
B_TYPE_INSTANCE_INIT(array_fini);
|
||||
B_TYPE_DEFINITION_END(b_array)
|
||||
|
||||
void b_array_clear(struct b_array *array)
|
||||
{
|
||||
while (array->ar_len) {
|
||||
b_array_remove_back(array);
|
||||
}
|
||||
}
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static bool array_iterator_next(struct b_iterator *it)
|
||||
{
|
||||
@@ -304,14 +386,15 @@ static b_iterator_ops it_ops = {
|
||||
.it_is_valid = array_iterator_is_valid,
|
||||
};
|
||||
|
||||
int b_array_iterator_begin(struct b_array *array, struct b_array_iterator *it)
|
||||
int b_array_iterator_begin(b_array *array, struct b_array_iterator *it)
|
||||
{
|
||||
it->_a = array;
|
||||
it->_a_p = b_object_get_private(array, B_TYPE_ARRAY);
|
||||
it->i = 0;
|
||||
it->_base.it_ops = &it_ops;
|
||||
|
||||
if (array->ar_len > 0) {
|
||||
it->value = array->ar_data[0];
|
||||
if (it->_a_p->ar_len > 0) {
|
||||
it->value = it->_a_p->ar_data[0];
|
||||
} else {
|
||||
it->value = NULL;
|
||||
}
|
||||
@@ -321,7 +404,7 @@ int b_array_iterator_begin(struct b_array *array, struct b_array_iterator *it)
|
||||
|
||||
bool b_array_iterator_next(struct b_array_iterator *it)
|
||||
{
|
||||
struct b_array *array = it->_a;
|
||||
struct b_array_p *array = it->_a_p;
|
||||
|
||||
if (it->value == NULL || it->i >= array->ar_len) {
|
||||
return false;
|
||||
@@ -340,7 +423,7 @@ bool b_array_iterator_next(struct b_array_iterator *it)
|
||||
|
||||
b_status b_array_iterator_erase(struct b_array_iterator *it)
|
||||
{
|
||||
struct b_array *array = it->_a;
|
||||
struct b_array_p *array = it->_a_p;
|
||||
if (it->i >= array->ar_len) {
|
||||
return B_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
@@ -349,7 +432,7 @@ b_status b_array_iterator_erase(struct b_array_iterator *it)
|
||||
return B_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
b_array_remove(array, it->i);
|
||||
array_remove(array, it->i);
|
||||
|
||||
if (it->i < array->ar_len) {
|
||||
it->value = array->ar_data[it->i];
|
||||
@@ -362,7 +445,7 @@ b_status b_array_iterator_erase(struct b_array_iterator *it)
|
||||
|
||||
bool b_array_iterator_is_valid(const struct b_array_iterator *it)
|
||||
{
|
||||
struct b_array *array = it->_a;
|
||||
struct b_array_p *array = it->_a_p;
|
||||
if (it->i >= array->ar_len) {
|
||||
return false;
|
||||
}
|
||||
@@ -373,8 +456,3 @@ bool b_array_iterator_is_valid(const struct b_array_iterator *it)
|
||||
|
||||
return it->value != NULL;
|
||||
}
|
||||
|
||||
b_dsref_type_id b_array_type_id(void)
|
||||
{
|
||||
return (b_dsref_type_id)&array_type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user