diff --git a/src/array_allocator.c b/src/array_allocator.c index 9af926f..b408ef7 100644 --- a/src/array_allocator.c +++ b/src/array_allocator.c @@ -114,3 +114,9 @@ int array_compare_element_int32(const int32_t *n1, const int32_t *n2) { return *n1 - *n2; } + +int array_compare_element_id_name(const id_name_pair_t *pair1, + const id_name_pair_t *pair2) +{ + return fc_compare_int64(pair1->id, pair2->id); +} diff --git a/src/array_allocator.h b/src/array_allocator.h index cc5a17d..36f8764 100644 --- a/src/array_allocator.h +++ b/src/array_allocator.h @@ -39,6 +39,13 @@ typedef struct int32_t elts[0]; } I32Array; +typedef struct +{ + int alloc; + int count; + id_name_pair_t elts[0]; +} IdNameArray; + typedef struct array_allocator_context { struct fast_allocator_context allocator; @@ -67,10 +74,15 @@ extern "C" { fast_allocator_free(&ctx->allocator, array); } + /* comparator for 64 bits integer */ int array_compare_element_int64(const int64_t *n1, const int64_t *n2); + /* comparator for 32 bits integer */ int array_compare_element_int32(const int32_t *n1, const int32_t *n2); + /* comparator for id name pair (sorted by id) */ + int array_compare_element_id_name(const id_name_pair_t *pair1, + const id_name_pair_t *pair2); #define i64_array_allocator_init(ctx, min_bits, max_bits) \ array_allocator_init(ctx, "i64", sizeof(int64_t), min_bits, max_bits) @@ -79,7 +91,8 @@ extern "C" { (I64Array *)array_allocator_alloc(ctx, target_count) #define i64_array_allocator_realloc(ctx, old_array, target_count) \ - (I64Array *)array_allocator_realloc(ctx, (VoidArray *)old_array, target_count) + (I64Array *)array_allocator_realloc(ctx, \ + (VoidArray *)old_array, target_count) #define i64_array_allocator_free(ctx, array) \ array_allocator_free(ctx, (VoidArray *)array) @@ -92,12 +105,28 @@ extern "C" { (I32Array *)array_allocator_alloc(ctx, target_count) #define i32_array_allocator_realloc(ctx, old_array, target_count) \ - (I32Array *)array_allocator_realloc(ctx, (VoidArray *)old_array, target_count) + (I32Array *)array_allocator_realloc(ctx, \ + (VoidArray *)old_array, target_count) #define i32_array_allocator_free(ctx, array) \ array_allocator_free(ctx, (VoidArray *)array) +#define id_name_array_allocator_init(ctx, min_bits, max_bits) \ + array_allocator_init(ctx, "id_name", sizeof(id_name_pair_t), \ + min_bits, max_bits) + +#define id_name_array_allocator_alloc(ctx, target_count) \ + (IdNameArray *)array_allocator_alloc(ctx, target_count) + +#define id_name_array_allocator_realloc(ctx, old_array, target_count) \ + (IdNameArray *)array_allocator_realloc(ctx, \ + (VoidArray *)old_array, target_count) + +#define id_name_array_allocator_free(ctx, array) \ + array_allocator_free(ctx, (VoidArray *)array) + + #ifdef __cplusplus } #endif diff --git a/src/sorted_array.c b/src/sorted_array.c index a23b36d..7d32d45 100644 --- a/src/sorted_array.c +++ b/src/sorted_array.c @@ -154,27 +154,3 @@ int sorted_array_delete(SortedArrayContext *ctx, } return 0; } - -int sorted_array_compare_int64(const int64_t *n1, const int64_t *n2) -{ - int64_t sub; - sub = *n1 - *n2; - if (sub < 0) { - return -1; - } else if (sub > 0) { - return 1; - } else { - return 0; - } -} - -int sorted_array_compare_int32(const int32_t *n1, const int32_t *n2) -{ - return *n1 - *n2; -} - -int sorted_array_compare_id_name_pair(const id_name_pair_t *pair1, - const id_name_pair_t *pair2) -{ - return fc_compare_int64(pair1->id, pair2->id); -} diff --git a/src/sorted_array.h b/src/sorted_array.h index cad1b42..beb6e4f 100644 --- a/src/sorted_array.h +++ b/src/sorted_array.h @@ -16,7 +16,7 @@ #ifndef SORTED_ARRAY_H #define SORTED_ARRAY_H -#include "common_define.h" +#include "array_allocator.h" typedef struct sorted_array_context { @@ -81,29 +81,18 @@ extern "C" { element_size, ctx->compare_func); } - /* comparator for 64 bits integer */ - int sorted_array_compare_int64(const int64_t *n1, const int64_t *n2); - - /* comparator for 32 bits integer */ - int sorted_array_compare_int32(const int32_t *n1, const int32_t *n2); - - /* comparator for id name pair (sorted by id) */ - int sorted_array_compare_id_name_pair(const id_name_pair_t *pair1, - const id_name_pair_t *pair2); - - #define sorted_i64_array_init(ctx, allow_duplication) \ sorted_array_init(ctx, sizeof(int64_t), allow_duplication, \ - (int (*)(const void *, const void *))sorted_array_compare_int64) + (int (*)(const void *, const void *))array_compare_element_int64) #define sorted_i32_array_init(ctx, allow_duplication) \ sorted_array_init(ctx, sizeof(int32_t), allow_duplication, \ - (int (*)(const void *, const void *))sorted_array_compare_int32) + (int (*)(const void *, const void *))array_compare_element_int32) #define sorted_id_name_array_init(ctx, allow_duplication) \ sorted_array_init(ctx, sizeof(id_name_pair_t), allow_duplication, \ (int (*)(const void *, const void *)) \ - sorted_array_compare_id_name_pair) + array_compare_element_id_name) #ifdef __cplusplus