summaryrefslogtreecommitdiffstats
path: root/zix/sorted_array.c
diff options
context:
space:
mode:
Diffstat (limited to 'zix/sorted_array.c')
-rw-r--r--zix/sorted_array.c168
1 files changed, 83 insertions, 85 deletions
diff --git a/zix/sorted_array.c b/zix/sorted_array.c
index c96ea2b..0f84227 100644
--- a/zix/sorted_array.c
+++ b/zix/sorted_array.c
@@ -26,26 +26,26 @@
// #define ZIX_SORTED_ARRAY_DUMP 1
struct ZixSortedArrayImpl {
- void* array;
- ZixComparator cmp;
- void* cmp_data;
- size_t elem_size;
- size_t num_elems;
- bool allow_duplicates;
+ void* array;
+ ZixComparator cmp;
+ void* cmp_data;
+ size_t elem_size;
+ size_t num_elems;
+ bool allow_duplicates;
};
#ifdef ZIX_SORTED_ARRAY_DUMP
static void
zix_sorted_array_print(ZixSortedArray* a)
{
- for (size_t i = 0; i < a->num_elems; ++i) {
- printf(" %ld", *(intptr_t*)((char*)a->array + (i * a->elem_size)));
- }
- printf("\n");
+ for (size_t i = 0; i < a->num_elems; ++i) {
+ printf(" %ld", *(intptr_t*)((char*)a->array + (i * a->elem_size)));
+ }
+ printf("\n");
}
-# define DUMP(a) zix_sorted_array_print(a)
+# define DUMP(a) zix_sorted_array_print(a)
#else
-# define DUMP(a)
+# define DUMP(a)
#endif
ZixSortedArray*
@@ -54,27 +54,27 @@ zix_sorted_array_new(bool allow_duplicates,
void* cmp_data,
size_t elem_size)
{
- ZixSortedArray* a = (ZixSortedArray*)malloc(sizeof(ZixSortedArray));
- a->array = NULL;
- a->cmp = cmp;
- a->cmp_data = cmp_data;
- a->elem_size = elem_size;
- a->num_elems = 0;
- a->allow_duplicates = allow_duplicates;
- return a;
+ ZixSortedArray* a = (ZixSortedArray*)malloc(sizeof(ZixSortedArray));
+ a->array = NULL;
+ a->cmp = cmp;
+ a->cmp_data = cmp_data;
+ a->elem_size = elem_size;
+ a->num_elems = 0;
+ a->allow_duplicates = allow_duplicates;
+ return a;
}
void
zix_sorted_array_free(ZixSortedArray* a)
{
- free(a->array);
- free(a);
+ free(a->array);
+ free(a);
}
size_t
zix_sorted_array_size(const ZixSortedArray* a)
{
- return a->num_elems;
+ return a->num_elems;
}
ZixStatus
@@ -82,58 +82,56 @@ zix_sorted_array_insert(ZixSortedArray* a,
const void* e,
ZixSortedArrayIter* ai)
{
- if (a->num_elems == 0) {
- assert(!a->array);
- a->array = malloc(a->elem_size);
- memcpy(a->array, e, a->elem_size);
- ++a->num_elems;
- *ai = a->array;
- return ZIX_STATUS_SUCCESS;
- }
-
- zix_sorted_array_find(a, e, ai);
- assert(*ai);
- const size_t i = (size_t)((char*)*ai - (char*)a->array) / a->elem_size;
-
- a->array = realloc(a->array, ++a->num_elems * a->elem_size);
- memmove((char*)a->array + ((i + 1) * a->elem_size),
- (char*)a->array + (i * a->elem_size),
- (a->num_elems - i - 1) * a->elem_size);
- memcpy((char*)a->array + (i * a->elem_size),
- e,
- a->elem_size);
-
- *ai = (char*)a->array + (i * a->elem_size);
- DUMP(t);
- return ZIX_STATUS_SUCCESS;
+ if (a->num_elems == 0) {
+ assert(!a->array);
+ a->array = malloc(a->elem_size);
+ memcpy(a->array, e, a->elem_size);
+ ++a->num_elems;
+ *ai = a->array;
+ return ZIX_STATUS_SUCCESS;
+ }
+
+ zix_sorted_array_find(a, e, ai);
+ assert(*ai);
+ const size_t i = (size_t)((char*)*ai - (char*)a->array) / a->elem_size;
+
+ a->array = realloc(a->array, ++a->num_elems * a->elem_size);
+ memmove((char*)a->array + ((i + 1) * a->elem_size),
+ (char*)a->array + (i * a->elem_size),
+ (a->num_elems - i - 1) * a->elem_size);
+ memcpy((char*)a->array + (i * a->elem_size), e, a->elem_size);
+
+ *ai = (char*)a->array + (i * a->elem_size);
+ DUMP(t);
+ return ZIX_STATUS_SUCCESS;
}
ZixStatus
zix_sorted_array_remove(ZixSortedArray* a, ZixSortedArrayIter ai)
{
- const size_t i = (size_t)((char*)ai - (char*)a->array) / a->elem_size;
- memmove((char*)a->array + (i * a->elem_size),
- (char*)a->array + ((i + 1) * a->elem_size),
- (a->num_elems - i - 1) * a->elem_size);
- --a->num_elems;
- DUMP(a);
- return ZIX_STATUS_SUCCESS;
+ const size_t i = (size_t)((char*)ai - (char*)a->array) / a->elem_size;
+ memmove((char*)a->array + (i * a->elem_size),
+ (char*)a->array + ((i + 1) * a->elem_size),
+ (a->num_elems - i - 1) * a->elem_size);
+ --a->num_elems;
+ DUMP(a);
+ return ZIX_STATUS_SUCCESS;
}
static inline void*
zix_sorted_array_index_unchecked(const ZixSortedArray* a, size_t index)
{
- return (char*)a->array + (a->elem_size * index);
+ return (char*)a->array + (a->elem_size * index);
}
void*
zix_sorted_array_index(const ZixSortedArray* a, size_t index)
{
- if (index >= a->num_elems) {
- return NULL;
- }
+ if (index >= a->num_elems) {
+ return NULL;
+ }
- return zix_sorted_array_index_unchecked(a, index);
+ return zix_sorted_array_index_unchecked(a, index);
}
ZixStatus
@@ -141,55 +139,55 @@ zix_sorted_array_find(const ZixSortedArray* a,
const void* e,
ZixSortedArrayIter* ai)
{
- intptr_t lower = 0;
- intptr_t upper = a->num_elems - 1;
- while (upper >= lower) {
- const intptr_t i = lower + ((upper - lower) / 2);
- void* const elem_i = zix_sorted_array_index_unchecked(a, i);
- const int cmp = a->cmp(elem_i, e, a->cmp_data);
-
- if (cmp == 0) {
- *ai = elem_i;
- return ZIX_STATUS_SUCCESS;
- }
-
- if (cmp > 0) {
- upper = i - 1;
- } else {
- lower = i + 1;
- }
- }
-
- *ai = zix_sorted_array_index_unchecked(a, lower);
- return ZIX_STATUS_NOT_FOUND;
+ intptr_t lower = 0;
+ intptr_t upper = a->num_elems - 1;
+ while (upper >= lower) {
+ const intptr_t i = lower + ((upper - lower) / 2);
+ void* const elem_i = zix_sorted_array_index_unchecked(a, i);
+ const int cmp = a->cmp(elem_i, e, a->cmp_data);
+
+ if (cmp == 0) {
+ *ai = elem_i;
+ return ZIX_STATUS_SUCCESS;
+ }
+
+ if (cmp > 0) {
+ upper = i - 1;
+ } else {
+ lower = i + 1;
+ }
+ }
+
+ *ai = zix_sorted_array_index_unchecked(a, lower);
+ return ZIX_STATUS_NOT_FOUND;
}
void*
zix_sorted_array_get_data(ZixSortedArrayIter ai)
{
- return ai;
+ return ai;
}
ZixSortedArrayIter
zix_sorted_array_begin(ZixSortedArray* a)
{
- return a->array;
+ return a->array;
}
ZixSortedArrayIter
zix_sorted_array_end(ZixSortedArray* a)
{
- return (char*)a->array + (a->elem_size * a->num_elems);
+ return (char*)a->array + (a->elem_size * a->num_elems);
}
bool
zix_sorted_array_iter_is_end(ZixSortedArray* a, ZixSortedArrayIter i)
{
- return i != zix_sorted_array_end(a);
+ return i != zix_sorted_array_end(a);
}
ZixSortedArrayIter
zix_sorted_array_iter_next(ZixSortedArray* a, ZixSortedArrayIter i)
{
- return (char*)i + a->elem_size;
+ return (char*)i + a->elem_size;
}