mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 11:31:37 +08:00
暂不是用函数注册的方式,毕竟增加了额外内存成本
This commit is contained in:
parent
0369d58147
commit
29db0dae43
@ -37,6 +37,6 @@
|
||||
* obj == obj2 return 0
|
||||
* obj > obj2 return 1
|
||||
*/
|
||||
typedef int (*cmp_fun_t)(void* obj, void* obj2);
|
||||
typedef int (*compare_fun_t)(void* obj, void* obj2);
|
||||
|
||||
#endif // _COMMON_H_
|
||||
|
@ -44,15 +44,11 @@ struct _heap
|
||||
uint32_t(*size)(struct _heap* self);
|
||||
bool (*clear)(struct _heap* self);
|
||||
|
||||
/**
|
||||
* @brief obj compare with obj2
|
||||
*
|
||||
* @return
|
||||
* obj < obj2 return -1
|
||||
* obj == obj2 return 0
|
||||
* obj > obj2 return 1
|
||||
*/
|
||||
int (*compare)(void* obj, void* obj2);
|
||||
// config
|
||||
// !!! you have to implement this function
|
||||
compare_fun_t compare;
|
||||
// register function
|
||||
// void (*register_compare)(struct _heap* self, compare_fun_t cmp_fun);
|
||||
|
||||
// -------------------- debug --------------------
|
||||
void (*print)(struct _heap* self);
|
||||
|
15
src/heap.c
15
src/heap.c
@ -70,7 +70,14 @@ static void heap_swap(struct _heap* self, int i, int j)
|
||||
static void heap_fixed_up(struct _heap* self, int i)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->compare != NULL);
|
||||
int p = 0;
|
||||
|
||||
if(self->compare == NULL)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
if(self->_type == HEAP_MAX)
|
||||
{
|
||||
while(1)
|
||||
@ -122,6 +129,11 @@ static void heap_fixed_down(struct _heap* self, int i)
|
||||
int l = 0,r = 0;
|
||||
int max = 0, min = 0;
|
||||
|
||||
if(self->compare == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(self->_type == HEAP_MAX)
|
||||
{
|
||||
while(1)
|
||||
@ -265,6 +277,9 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
// base
|
||||
self->size = heap_size;
|
||||
self->clear = heap_clear;
|
||||
|
||||
// config
|
||||
self->compare = NULL;
|
||||
|
||||
// -------------------- debug --------------------
|
||||
self->print = heap_print;
|
||||
|
@ -36,7 +36,7 @@ static void* get_min(struct _heap* heap, void *array, int start, int end)
|
||||
return min;
|
||||
}
|
||||
|
||||
static void test_heap_num(void)
|
||||
static void test_heap_min_num(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
// int data[] = { 2,1,3,4};
|
||||
@ -79,7 +79,7 @@ static void test_heap_num(void)
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
static void test_heap_struct(void)
|
||||
static void test_heap_min_struct(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
struct _student data[] = {
|
||||
@ -126,10 +126,55 @@ static void test_heap_struct(void)
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
static void test_heap_max_num(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
// int data[] = { 2,1,3,4};
|
||||
// int data[] = { 1,2,3,4,5,6};
|
||||
// int data[] = { 5,2,3,1,7,8,6 };
|
||||
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_max_new2(sizeof(int), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
TEST_ASSERT_TRUE(heap->push(heap, &temp));
|
||||
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
|
||||
}
|
||||
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
||||
TEST_ASSERT_EQUAL_INT(*(int *)get_max(heap, data, 0, heap->size(heap)), temp);
|
||||
|
||||
TEST_ASSERT_TRUE(heap->clear(heap));
|
||||
TEST_ASSERT_TRUE(heap->empty(heap));
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
TEST_ASSERT_TRUE(heap->push(heap, &temp));
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
|
||||
}
|
||||
TEST_ASSERT_TRUE(heap->empty(heap));
|
||||
|
||||
heap_free(&heap);
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
void test_heap(void)
|
||||
{
|
||||
UnitySetTestFile(__FILE__);
|
||||
|
||||
RUN_TEST(test_heap_num);
|
||||
RUN_TEST(test_heap_struct);
|
||||
RUN_TEST(test_heap_min_num);
|
||||
RUN_TEST(test_heap_min_struct);
|
||||
|
||||
RUN_TEST(test_heap_max_num);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user