mirror of
https://gitee.com/apaki/unicstl.git
synced 2026-05-28 22:54:19 +08:00
feat(internal): 新增基础类型的通用比较函数
This commit is contained in:
parent
4396d4d337
commit
917cee5d0b
15
demo/demo.c
15
demo/demo.c
@ -17,13 +17,7 @@ void print_num(const void* obj)
|
||||
|
||||
int compare_num(const void *obj, const void *obj2)
|
||||
{
|
||||
int num1 = *(int*)obj;
|
||||
int num2 = *(int*)obj2;
|
||||
if(num1 == num2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return num1 > num2 ? 1 : -1;
|
||||
return compare_int(obj, obj2);
|
||||
}
|
||||
|
||||
void print_struct(const void* obj)
|
||||
@ -55,6 +49,7 @@ void print_str(const void* obj)
|
||||
|
||||
int main()
|
||||
{
|
||||
log_init();
|
||||
printf("----- unicstl demo -----\n");
|
||||
// while (1)
|
||||
{
|
||||
@ -62,11 +57,13 @@ int main()
|
||||
demo_stack();
|
||||
demo_arraylist();
|
||||
demo_deque();
|
||||
demo_tree();
|
||||
|
||||
demo_heap();
|
||||
demo_graph();
|
||||
// demo_tree();
|
||||
// demo_graph();
|
||||
}
|
||||
|
||||
printf("----- unicstl ok -----\n");
|
||||
log_deinit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -348,8 +348,8 @@ static void demo_deque_struct(void)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
deque_free(&deque);
|
||||
log_info("demo_deque_struct end\n");
|
||||
}
|
||||
|
||||
void demo_deque(void)
|
||||
|
||||
@ -39,6 +39,8 @@ void demo_avltree_num(void)
|
||||
int temp = 0;
|
||||
iterator_t iter = NULL;
|
||||
|
||||
log_info("avltree_num\n");
|
||||
|
||||
tree_t tree = tree_avl_new(sizeof(int));
|
||||
tree->print_obj = print_num;
|
||||
tree->compare = compare_num;
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#include "ringbuf.h"
|
||||
#include "segarray.h"
|
||||
|
||||
// 0.rinfbuf 1.segarray
|
||||
#define DEQUE_DEFAULT_SELECT 1
|
||||
|
||||
#if DEQUE_DEFAULT_SELECT == 1
|
||||
|
||||
@ -214,6 +214,28 @@ typedef int (*compare_fun_t)(const void *obj, const void *obj2);
|
||||
int default_compare(const void *obj1, const void *obj2);
|
||||
void default_print_obj(const void *obj);
|
||||
|
||||
int compare_char(const void *obj1, const void *obj2);
|
||||
int compare_int(const void *obj1, const void *obj2);
|
||||
int compare_long(const void *obj1, const void *obj2);
|
||||
|
||||
int compare_int8_t(const void *obj1, const void *obj2);
|
||||
int compare_int16_t(const void *obj1, const void *obj2);
|
||||
int compare_uint16_t(const void *obj1, const void *obj2);
|
||||
int compare_int32_t(const void *obj1, const void *obj2);
|
||||
|
||||
int compare_uint8_t(const void *obj1, const void *obj2);
|
||||
int compare_uint16_t(const void *obj1, const void *obj2);
|
||||
int compare_uint32_t(const void *obj1, const void *obj2);
|
||||
int compare_uint64_t(const void *obj1, const void *obj2);
|
||||
|
||||
int compare_size_t(const void *obj1, const void *obj2);
|
||||
int compare_ssize_t(const void *obj1, const void *obj2);
|
||||
|
||||
int compare_float(const void *obj1, const void *obj2);
|
||||
int compare_double(const void *obj1, const void *obj2);
|
||||
|
||||
int compare_string(const void *obj1, const void *obj2);
|
||||
|
||||
/**
|
||||
* @brief new capacity
|
||||
*
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
/**
|
||||
* @file unicstl_internal.c
|
||||
* @author wenjf (Orig5826@163.com)
|
||||
* @brief
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2025-04-29
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
#include "unicstl_internal.h"
|
||||
|
||||
const char* unicstl_version(void)
|
||||
const char *unicstl_version(void)
|
||||
{
|
||||
return UNICSTL_VERSION_STRING;
|
||||
}
|
||||
|
||||
int default_compare(const void* obj1, const void* obj2)
|
||||
int default_compare(const void *obj1, const void *obj2)
|
||||
{
|
||||
printf("compare is not implemented!\n");
|
||||
unicstl_assert(0);
|
||||
}
|
||||
|
||||
void default_print_obj(const void* obj)
|
||||
void default_print_obj(const void *obj)
|
||||
{
|
||||
printf("print_obj is not implemented!\n");
|
||||
unicstl_assert(0);
|
||||
@ -30,7 +30,7 @@ void default_print_obj(const void* obj)
|
||||
size_t unicstl_new_capacity(size_t capacity)
|
||||
{
|
||||
size_t new_capacity = 0;
|
||||
if(capacity == 0)
|
||||
if (capacity == 0)
|
||||
{
|
||||
new_capacity = UNICSTL_CAPACITY_INIT;
|
||||
}
|
||||
@ -46,3 +46,55 @@ size_t unicstl_new_capacity(size_t capacity)
|
||||
}
|
||||
return new_capacity;
|
||||
}
|
||||
|
||||
#define UNICSTL_COMPARE_FUNC(type) \
|
||||
int compare_##type(const void *obj1, const void *obj2) \
|
||||
{ \
|
||||
const type num1 = *(const type *)obj1; \
|
||||
const type num2 = *(const type *)obj2; \
|
||||
if (num1 < num2) return -1; \
|
||||
if (num1 > num2) return 1; \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
UNICSTL_COMPARE_FUNC(char)
|
||||
UNICSTL_COMPARE_FUNC(short)
|
||||
UNICSTL_COMPARE_FUNC(int)
|
||||
UNICSTL_COMPARE_FUNC(long)
|
||||
UNICSTL_COMPARE_FUNC(int8_t)
|
||||
UNICSTL_COMPARE_FUNC(int16_t)
|
||||
UNICSTL_COMPARE_FUNC(int32_t)
|
||||
UNICSTL_COMPARE_FUNC(int64_t)
|
||||
UNICSTL_COMPARE_FUNC(uint8_t)
|
||||
UNICSTL_COMPARE_FUNC(uint16_t)
|
||||
UNICSTL_COMPARE_FUNC(uint32_t)
|
||||
UNICSTL_COMPARE_FUNC(uint64_t)
|
||||
UNICSTL_COMPARE_FUNC(size_t)
|
||||
UNICSTL_COMPARE_FUNC(ssize_t)
|
||||
|
||||
int compare_double(const void *obj, const void *obj2)
|
||||
{
|
||||
double num1 = *(double *)obj;
|
||||
double num2 = *(double *)obj2;
|
||||
if (num1 - num2 < 1e-9 || num2 - num1 < 1e-9)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return num1 > num2 ? 1 : -1;
|
||||
}
|
||||
|
||||
int compare_float(const void *obj, const void *obj2)
|
||||
{
|
||||
float num1 = *(float *)obj;
|
||||
float num2 = *(float *)obj2;
|
||||
if (num1 - num2 < 1e-9 || num2 - num1 < 1e-9)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return num1 > num2 ? 1 : -1;
|
||||
}
|
||||
|
||||
int compare_string(const void *obj1, const void *obj2)
|
||||
{
|
||||
return strcmp(*(const char **)obj1, *(const char **)obj2);
|
||||
}
|
||||
|
||||
@ -17,13 +17,7 @@ void print_num(const void* obj)
|
||||
|
||||
int compare_num(const void *obj, const void *obj2)
|
||||
{
|
||||
int num1 = *(int*)obj;
|
||||
int num2 = *(int*)obj2;
|
||||
if(num1 == num2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return num1 > num2 ? 1 : -1;
|
||||
return compare_int(obj, obj2);
|
||||
}
|
||||
|
||||
void print_struct(const void* obj)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user