From 8491c5d1554fa24c96e40ebb8a8a7268e1d1a39c Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Tue, 14 Sep 2021 10:05:27 +0800 Subject: [PATCH] add comments for sorted_array.h --- src/sorted_array.c | 36 ++++++++++++++-------------- src/sorted_array.h | 45 ++++++++++++++++++++++++++++------- src/tests/test_sorted_array.c | 8 +++---- 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/sorted_array.c b/src/sorted_array.c index b291738..2e6137e 100644 --- a/src/sorted_array.c +++ b/src/sorted_array.c @@ -17,16 +17,16 @@ #include "sorted_array.h" void sorted_array_init(SortedArrayContext *ctx, - const int element_size, const bool allow_duplicate, + const int element_size, const bool allow_duplication, int (*compare_func)(const void *, const void *)) { ctx->element_size = element_size; - ctx->allow_duplicate = allow_duplicate; + ctx->allow_duplication = allow_duplication; ctx->compare_func = compare_func; } static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base, - const int count, const void *element, int *insert_pos) + const int count, const void *elt, int *insert_pos) { int low; int high; @@ -40,7 +40,7 @@ static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base, while (low <= high) { mid = (low + high) / 2; current = base + ctx->element_size * mid; - compr = ctx->compare_func(current, element); + compr = ctx->compare_func(current, elt); if (compr < 0) { low = mid + 1; *insert_pos = low; @@ -57,7 +57,7 @@ static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base, } int sorted_array_insert(SortedArrayContext *ctx, - void *base, int *count, const void *element) + void *base, int *count, const void *elt) { int insert_pos; int move_count; @@ -65,15 +65,15 @@ int sorted_array_insert(SortedArrayContext *ctx, char *found; char *end; - found = sorted_array_bsearch(ctx, base, *count, element, &insert_pos); + found = sorted_array_bsearch(ctx, base, *count, elt, &insert_pos); if (found != NULL) { - if (!ctx->allow_duplicate) { + if (!ctx->allow_duplication) { return EEXIST; } found += ctx->element_size; end = (char *)base + ctx->element_size * (*count); - while (found < end && ctx->compare_func(found, element) == 0) { + while (found < end && ctx->compare_func(found, elt) == 0) { insert_pos++; found += ctx->element_size; } @@ -88,19 +88,19 @@ int sorted_array_insert(SortedArrayContext *ctx, switch (ctx->element_size) { case 1: - *current = *((char *)element); + *current = *((char *)elt); break; case 2: - *((short *)current) = *((short *)element); + *((short *)current) = *((short *)elt); break; case 4: - *((int32_t *)current) = *((int32_t *)element); + *((int32_t *)current) = *((int32_t *)elt); break; case 8: - *((int64_t *)current) = *((int64_t *)element); + *((int64_t *)current) = *((int64_t *)elt); break; default: - memcpy(current, element, ctx->element_size); + memcpy(current, elt, ctx->element_size); break; } @@ -109,7 +109,7 @@ int sorted_array_insert(SortedArrayContext *ctx, } int sorted_array_delete(SortedArrayContext *ctx, - void *base, int *count, const void *element) + void *base, int *count, const void *elt) { int move_count; struct { @@ -119,24 +119,24 @@ int sorted_array_delete(SortedArrayContext *ctx, } found; char *array_end; - if ((found.current=bsearch(element, base, *count, ctx->element_size, + if ((found.current=bsearch(elt, base, *count, ctx->element_size, ctx->compare_func)) == NULL) { return ENOENT; } array_end = (char *)base + ctx->element_size * (*count); - if (ctx->allow_duplicate) { + if (ctx->allow_duplication) { found.start = found.current; while (found.start > (char *)base && ctx->compare_func( - found.start - ctx->element_size, element) == 0) + found.start - ctx->element_size, elt) == 0) { found.start -= ctx->element_size; } found.end = found.current + ctx->element_size; while (found.end < array_end && ctx->compare_func( - found.end, element) == 0) + found.end, elt) == 0) { found.end += ctx->element_size; } diff --git a/src/sorted_array.h b/src/sorted_array.h index a2bb9e7..43f111e 100644 --- a/src/sorted_array.h +++ b/src/sorted_array.h @@ -21,7 +21,7 @@ typedef struct sorted_array_context { int element_size; - bool allow_duplicate; + bool allow_duplication; int (*compare_func)(const void *, const void *); } SortedArrayContext; @@ -29,27 +29,56 @@ typedef struct sorted_array_context extern "C" { #endif + /** init the context for sorted array + * parameters: + * ctx: the context to init + * element_size: the size of one array element + * allow_duplication: if allow duplication + * compare_func: the compare function (comparator) + * return: none + */ void sorted_array_init(SortedArrayContext *ctx, - const int element_size, const bool allow_duplicate, + const int element_size, const bool allow_duplication, int (*compare_func)(const void *, const void *)); + + /** insert an element into the sorted array + * parameters: + * ctx: the context for sorted array + * base: the pointer of the sorted array (the first array element) + * count: the count of the sorted array (for input and output) + * elt: the element to insert + * return: 0 for success, != 0 for error + */ int sorted_array_insert(SortedArrayContext *ctx, - void *base, int *count, const void *element); + void *base, int *count, const void *elt); + + /** delete an element from the sorted array + * parameters: + * ctx: the context for sorted array + * base: the pointer of the sorted array (the first array element) + * count: the count of the sorted array (for input and output) + * elt: the element to delete + * return: 0 for success, != 0 for error + */ int sorted_array_delete(SortedArrayContext *ctx, - void *base, int *count, const void *element); + void *base, int *count, const void *elt); + + /* 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); -#define sorted_i64_array_init(ctx, allow_duplicate) \ - sorted_array_init(ctx, sizeof(int64_t), allow_duplicate, \ +#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) -#define sorted_i32_array_init(ctx, allow_duplicate) \ - sorted_array_init(ctx, sizeof(int32_t), allow_duplicate, \ +#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) diff --git a/src/tests/test_sorted_array.c b/src/tests/test_sorted_array.c index 577de44..386c883 100644 --- a/src/tests/test_sorted_array.c +++ b/src/tests/test_sorted_array.c @@ -34,7 +34,7 @@ static int test_i64() { const int min_bits = 2; const int max_bits = 16; - const bool allow_duplicate = false; + const bool allow_duplication = false; int result; int i; int index; @@ -48,7 +48,7 @@ static int test_i64() start_time = get_current_time_us(); - sorted_i64_array_init(&sarray_ctx, allow_duplicate); + sorted_i64_array_init(&sarray_ctx, allow_duplication); if ((result=i64_array_allocator_init(&allocator_ctx, min_bits, max_bits)) != 0) { @@ -109,7 +109,7 @@ static int test_i32() { const int min_bits = 2; const int max_bits = 16; - const bool allow_duplicate = false; + const bool allow_duplication = false; int result; int i; int index; @@ -123,7 +123,7 @@ static int test_i32() start_time = get_current_time_us(); - sorted_i32_array_init(&sarray_ctx, allow_duplicate); + sorted_i32_array_init(&sarray_ctx, allow_duplication); if ((result=i32_array_allocator_init(&allocator_ctx, min_bits, max_bits)) != 0) {