add comments for sorted_array.h
parent
8717f85608
commit
8491c5d155
|
|
@ -17,16 +17,16 @@
|
||||||
#include "sorted_array.h"
|
#include "sorted_array.h"
|
||||||
|
|
||||||
void sorted_array_init(SortedArrayContext *ctx,
|
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 *))
|
int (*compare_func)(const void *, const void *))
|
||||||
{
|
{
|
||||||
ctx->element_size = element_size;
|
ctx->element_size = element_size;
|
||||||
ctx->allow_duplicate = allow_duplicate;
|
ctx->allow_duplication = allow_duplication;
|
||||||
ctx->compare_func = compare_func;
|
ctx->compare_func = compare_func;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base,
|
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 low;
|
||||||
int high;
|
int high;
|
||||||
|
|
@ -40,7 +40,7 @@ static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base,
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
mid = (low + high) / 2;
|
mid = (low + high) / 2;
|
||||||
current = base + ctx->element_size * mid;
|
current = base + ctx->element_size * mid;
|
||||||
compr = ctx->compare_func(current, element);
|
compr = ctx->compare_func(current, elt);
|
||||||
if (compr < 0) {
|
if (compr < 0) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
*insert_pos = low;
|
*insert_pos = low;
|
||||||
|
|
@ -57,7 +57,7 @@ static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base,
|
||||||
}
|
}
|
||||||
|
|
||||||
int sorted_array_insert(SortedArrayContext *ctx,
|
int sorted_array_insert(SortedArrayContext *ctx,
|
||||||
void *base, int *count, const void *element)
|
void *base, int *count, const void *elt)
|
||||||
{
|
{
|
||||||
int insert_pos;
|
int insert_pos;
|
||||||
int move_count;
|
int move_count;
|
||||||
|
|
@ -65,15 +65,15 @@ int sorted_array_insert(SortedArrayContext *ctx,
|
||||||
char *found;
|
char *found;
|
||||||
char *end;
|
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 (found != NULL) {
|
||||||
if (!ctx->allow_duplicate) {
|
if (!ctx->allow_duplication) {
|
||||||
return EEXIST;
|
return EEXIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
found += ctx->element_size;
|
found += ctx->element_size;
|
||||||
end = (char *)base + ctx->element_size * (*count);
|
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++;
|
insert_pos++;
|
||||||
found += ctx->element_size;
|
found += ctx->element_size;
|
||||||
}
|
}
|
||||||
|
|
@ -88,19 +88,19 @@ int sorted_array_insert(SortedArrayContext *ctx,
|
||||||
|
|
||||||
switch (ctx->element_size) {
|
switch (ctx->element_size) {
|
||||||
case 1:
|
case 1:
|
||||||
*current = *((char *)element);
|
*current = *((char *)elt);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
*((short *)current) = *((short *)element);
|
*((short *)current) = *((short *)elt);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
*((int32_t *)current) = *((int32_t *)element);
|
*((int32_t *)current) = *((int32_t *)elt);
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
*((int64_t *)current) = *((int64_t *)element);
|
*((int64_t *)current) = *((int64_t *)elt);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
memcpy(current, element, ctx->element_size);
|
memcpy(current, elt, ctx->element_size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,7 +109,7 @@ int sorted_array_insert(SortedArrayContext *ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
int sorted_array_delete(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;
|
int move_count;
|
||||||
struct {
|
struct {
|
||||||
|
|
@ -119,24 +119,24 @@ int sorted_array_delete(SortedArrayContext *ctx,
|
||||||
} found;
|
} found;
|
||||||
char *array_end;
|
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)
|
ctx->compare_func)) == NULL)
|
||||||
{
|
{
|
||||||
return ENOENT;
|
return ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_end = (char *)base + ctx->element_size * (*count);
|
array_end = (char *)base + ctx->element_size * (*count);
|
||||||
if (ctx->allow_duplicate) {
|
if (ctx->allow_duplication) {
|
||||||
found.start = found.current;
|
found.start = found.current;
|
||||||
while (found.start > (char *)base && ctx->compare_func(
|
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.start -= ctx->element_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
found.end = found.current + ctx->element_size;
|
found.end = found.current + ctx->element_size;
|
||||||
while (found.end < array_end && ctx->compare_func(
|
while (found.end < array_end && ctx->compare_func(
|
||||||
found.end, element) == 0)
|
found.end, elt) == 0)
|
||||||
{
|
{
|
||||||
found.end += ctx->element_size;
|
found.end += ctx->element_size;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
typedef struct sorted_array_context
|
typedef struct sorted_array_context
|
||||||
{
|
{
|
||||||
int element_size;
|
int element_size;
|
||||||
bool allow_duplicate;
|
bool allow_duplication;
|
||||||
int (*compare_func)(const void *, const void *);
|
int (*compare_func)(const void *, const void *);
|
||||||
} SortedArrayContext;
|
} SortedArrayContext;
|
||||||
|
|
||||||
|
|
@ -29,27 +29,56 @@ typedef struct sorted_array_context
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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,
|
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 *));
|
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,
|
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,
|
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);
|
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);
|
int sorted_array_compare_int32(const int32_t *n1, const int32_t *n2);
|
||||||
|
|
||||||
|
|
||||||
#define sorted_i64_array_init(ctx, allow_duplicate) \
|
#define sorted_i64_array_init(ctx, allow_duplication) \
|
||||||
sorted_array_init(ctx, sizeof(int64_t), allow_duplicate, \
|
sorted_array_init(ctx, sizeof(int64_t), allow_duplication, \
|
||||||
(int (*)(const void *, const void *))sorted_array_compare_int64)
|
(int (*)(const void *, const void *))sorted_array_compare_int64)
|
||||||
|
|
||||||
#define sorted_i32_array_init(ctx, allow_duplicate) \
|
#define sorted_i32_array_init(ctx, allow_duplication) \
|
||||||
sorted_array_init(ctx, sizeof(int32_t), allow_duplicate, \
|
sorted_array_init(ctx, sizeof(int32_t), allow_duplication, \
|
||||||
(int (*)(const void *, const void *))sorted_array_compare_int32)
|
(int (*)(const void *, const void *))sorted_array_compare_int32)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ static int test_i64()
|
||||||
{
|
{
|
||||||
const int min_bits = 2;
|
const int min_bits = 2;
|
||||||
const int max_bits = 16;
|
const int max_bits = 16;
|
||||||
const bool allow_duplicate = false;
|
const bool allow_duplication = false;
|
||||||
int result;
|
int result;
|
||||||
int i;
|
int i;
|
||||||
int index;
|
int index;
|
||||||
|
|
@ -48,7 +48,7 @@ static int test_i64()
|
||||||
|
|
||||||
start_time = get_current_time_us();
|
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,
|
if ((result=i64_array_allocator_init(&allocator_ctx,
|
||||||
min_bits, max_bits)) != 0)
|
min_bits, max_bits)) != 0)
|
||||||
{
|
{
|
||||||
|
|
@ -109,7 +109,7 @@ static int test_i32()
|
||||||
{
|
{
|
||||||
const int min_bits = 2;
|
const int min_bits = 2;
|
||||||
const int max_bits = 16;
|
const int max_bits = 16;
|
||||||
const bool allow_duplicate = false;
|
const bool allow_duplication = false;
|
||||||
int result;
|
int result;
|
||||||
int i;
|
int i;
|
||||||
int index;
|
int index;
|
||||||
|
|
@ -123,7 +123,7 @@ static int test_i32()
|
||||||
|
|
||||||
start_time = get_current_time_us();
|
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,
|
if ((result=i32_array_allocator_init(&allocator_ctx,
|
||||||
min_bits, max_bits)) != 0)
|
min_bits, max_bits)) != 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue