add function sorted_array_find and marco sorted_id_name_array_init

pull/37/merge
YuQing 2021-11-16 19:47:14 +08:00
parent ce2ee0f482
commit b03963d4f6
3 changed files with 36 additions and 0 deletions

View File

@ -255,6 +255,12 @@ typedef struct
int count;
} int64_array_t;
typedef struct
{
id_name_pair_t *elts;
int count;
} id_name_array_t;
typedef struct
{
void *elts;

View File

@ -172,3 +172,9 @@ 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);
}

View File

@ -66,12 +66,31 @@ extern "C" {
void *base, int *count, const void *elt);
/** find 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
* key: the element to find
* return: 0 for success, != 0 for error
*/
static inline void *sorted_array_find(SortedArrayContext *ctx,
void *base, const int count, const void *key)
{
return bsearch(key, base, count, ctx->
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, \
@ -81,6 +100,11 @@ extern "C" {
sorted_array_init(ctx, sizeof(int32_t), allow_duplication, \
(int (*)(const void *, const void *))sorted_array_compare_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)
#ifdef __cplusplus
}