mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 11:31:37 +08:00
heap修改接口函数
This commit is contained in:
parent
a3c60c7d38
commit
e49add3f16
14
README.md
14
README.md
@ -16,17 +16,17 @@
|
||||
| stack_new | 链表 | |
|
||||
| stack_new2 | 动态数组 | |
|
||||
| **list** | | **列表**
|
||||
| list_init2 | 动态数组 | |
|
||||
| list_new2 | 动态数组 | |
|
||||
| **queue** | | **队列**
|
||||
| queue_init | 单向链表 | |
|
||||
| queue_init2 | 数组 | FIFO/空/满 |
|
||||
| queue_new | 单向链表 | |
|
||||
| queue_new2 | 数组 | FIFO/空/满 |
|
||||
| **deque** | |**双端队列** |
|
||||
| deque_init | 双向循环链表 | |
|
||||
| deque_new | 双向循环链表 | |
|
||||
| **tree** | |**树** |
|
||||
| tree_avl_init | 二叉搜索树 | AVL树 |
|
||||
| tree_rb_init | 二叉搜索树 | 红黑树 |
|
||||
| tree_avl_new | 二叉搜索树 | AVL树 |
|
||||
| tree_rb_new | 二叉搜索树 | 红黑树 |
|
||||
| **heap** | |**堆** |
|
||||
| heap_init2 | 数组 | 最大堆/最小堆 |
|
||||
| heap_new2 | 数组 | 最大堆/最小堆 |
|
||||
|
||||
## 特点
|
||||
| 原理 | 说明 |
|
||||
|
@ -20,8 +20,7 @@ void demo_heap_num(void)
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new();
|
||||
heap_init2(heap, sizeof(int), 64);
|
||||
heap_t heap = heap_new2(sizeof(int), 64);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
@ -91,8 +90,7 @@ static void demo_heap_struct(void)
|
||||
struct _student temp = {0};
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new();
|
||||
heap_init2(heap, sizeof(struct _student), 64);
|
||||
heap_t heap = heap_new2(sizeof(struct _student), 64);
|
||||
heap->print_obj = print_struct;
|
||||
heap->compare = compare_struct;
|
||||
|
||||
|
@ -56,9 +56,9 @@ struct _heap
|
||||
};
|
||||
typedef struct _heap* heap_t;
|
||||
|
||||
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity);
|
||||
// create and free heap
|
||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity);
|
||||
|
||||
heap_t heap_new(void);
|
||||
void heap_free(heap_t* heap);
|
||||
|
||||
#endif // _HEAP_H_
|
||||
|
34
src/heap.c
34
src/heap.c
@ -27,7 +27,7 @@ static int parent(int i)
|
||||
return (i-1) >> 1;
|
||||
}
|
||||
|
||||
bool heap_peek(struct _heap* self, void* obj)
|
||||
static bool heap_peek(struct _heap* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(obj != NULL);
|
||||
@ -101,7 +101,7 @@ static void heap_fixed_up(struct _heap* self, int i)
|
||||
}
|
||||
}
|
||||
|
||||
bool heap_push(struct _heap* self, void* obj)
|
||||
static bool heap_push(struct _heap* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
if(self->size(self) > self->_capacity)
|
||||
@ -174,7 +174,7 @@ static void heap_fixed_down(struct _heap* self, int i)
|
||||
}
|
||||
}
|
||||
|
||||
bool heap_pop(struct _heap* self, void* obj)
|
||||
static bool heap_pop(struct _heap* self, void* obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
if(self->empty(self))
|
||||
@ -192,32 +192,32 @@ bool heap_pop(struct _heap* self, void* obj)
|
||||
return true;
|
||||
}
|
||||
|
||||
void heap_setmin(struct _heap* self, bool min_flag)
|
||||
static void heap_setmin(struct _heap* self, bool min_flag)
|
||||
{
|
||||
assert(self != NULL);
|
||||
self->_min_flag = min_flag;
|
||||
}
|
||||
|
||||
uint32_t heap_size(struct _heap* self)
|
||||
static uint32_t heap_size(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return self->_size;
|
||||
}
|
||||
|
||||
bool heap_empty(struct _heap* self)
|
||||
static bool heap_empty(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
return self->size(self) == 0;
|
||||
}
|
||||
|
||||
bool heap_clear(struct _heap* self)
|
||||
static bool heap_clear(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
self->_size = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void heap_destory(struct _heap* self)
|
||||
static void heap_destory(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
self->clear(self);
|
||||
@ -227,7 +227,7 @@ void heap_destory(struct _heap* self)
|
||||
}
|
||||
}
|
||||
|
||||
void heap_print(struct _heap* self)
|
||||
static void heap_print(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->obj != NULL);
|
||||
@ -243,7 +243,7 @@ void heap_print(struct _heap* self)
|
||||
}
|
||||
}
|
||||
|
||||
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
assert(self != NULL);
|
||||
|
||||
@ -271,9 +271,19 @@ bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
return true;
|
||||
}
|
||||
|
||||
heap_t heap_new(void)
|
||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
return (struct _heap*)malloc(sizeof(struct _heap));
|
||||
heap_t heap = NULL;
|
||||
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
||||
if(heap != NULL)
|
||||
{
|
||||
if(heap_init2(heap, obj_size, capacity) != true)
|
||||
{
|
||||
free(heap);
|
||||
heap = NULL;
|
||||
}
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
|
||||
void heap_free(heap_t* heap)
|
||||
|
@ -46,10 +46,8 @@ static void test_heap_num(void)
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new();
|
||||
heap_t heap = heap_new2(sizeof(int), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
|
||||
heap_init2(heap, sizeof(int), 64);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
@ -95,10 +93,8 @@ static void test_heap_struct(void)
|
||||
struct _student temp = {0};
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new();
|
||||
heap_t heap = heap_new2(sizeof(struct _student), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
|
||||
heap_init2(heap, sizeof(struct _student), 64);
|
||||
heap->print_obj = print_struct;
|
||||
heap->compare = compare_struct;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user