mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
堆不是最大堆就是最小堆,直接对外拆分成两套接口
This commit is contained in:
parent
bd9f07ae65
commit
02b09e729d
15
README.md
15
README.md
@ -79,5 +79,18 @@ unicstl_stack_v1.2.5_20240717-a0.zip
|
||||
# 带a或者b后缀,表示当前版本发布前的测试版。如果发布后,则直接更新版本号了
|
||||
```
|
||||
|
||||
## 更新日志
|
||||
## 修改日志
|
||||
|
||||
### Unicstl 0.0.01 (2025-04-24)
|
||||
new features:
|
||||
- add stack
|
||||
- add queue
|
||||
- add deque
|
||||
- add list
|
||||
- add heap
|
||||
- add tree
|
||||
- add graph
|
||||
bugfixed:
|
||||
- none
|
||||
others:
|
||||
- none
|
||||
|
@ -20,14 +20,10 @@ void demo_heap_num(void)
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new2(sizeof(int), 64);
|
||||
heap_t heap = heap_min_new2(sizeof(int), 64);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
// default: maxheap
|
||||
// maxheap or minheap
|
||||
heap->setmin(heap, true);
|
||||
|
||||
printf("\n\n----- demo_heap_num -----\n");
|
||||
|
||||
printf("----- push -----\n");
|
||||
@ -90,7 +86,7 @@ static void demo_heap_struct(void)
|
||||
struct _student temp = {0};
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new2(sizeof(struct _student), 64);
|
||||
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
|
||||
heap->print_obj = print_struct;
|
||||
heap->compare = compare_struct;
|
||||
|
||||
|
@ -1,64 +1,73 @@
|
||||
/**
|
||||
* @file heap.h
|
||||
* @author wenjf (Orig5826@163.com)
|
||||
* @brief
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2024-07-03
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*
|
||||
*/
|
||||
#ifndef _HEAP_H_
|
||||
#define _HEAP_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MIN_HEAP = 0,
|
||||
MAX_HEAP = 1,
|
||||
}heap_type;
|
||||
|
||||
struct _heap
|
||||
{
|
||||
void * obj;
|
||||
// -------------------- private --------------------
|
||||
void* obj;
|
||||
|
||||
uint32_t _size;
|
||||
uint32_t _obj_size;
|
||||
uint32_t _capacity;
|
||||
uint32_t _ratio;
|
||||
|
||||
heap_type _type;
|
||||
bool _min_flag;
|
||||
|
||||
void (*destory)(struct _heap* self);
|
||||
|
||||
// -------------------- public --------------------
|
||||
// kernel
|
||||
bool (*peek)(struct _heap* self, void* obj);
|
||||
bool (*push)(struct _heap* self, void* obj);
|
||||
bool (*pop)(struct _heap* self, void* obj);
|
||||
bool (*empty)(struct _heap* self);
|
||||
|
||||
// default: max heap
|
||||
void (*setmin)(struct _heap* self, bool min_flag);
|
||||
|
||||
// base
|
||||
uint32_t(*size)(struct _heap* self);
|
||||
bool (*empty)(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);
|
||||
|
||||
// others
|
||||
bool (*clear)(struct _heap* self);
|
||||
void (*destory)(struct _heap* self);
|
||||
|
||||
// print
|
||||
|
||||
// -------------------- debug --------------------
|
||||
void (*print)(struct _heap* self);
|
||||
void (*print_obj)(void* obj);
|
||||
};
|
||||
typedef struct _heap* heap_t;
|
||||
|
||||
// create and free heap
|
||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity);
|
||||
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity);
|
||||
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity);
|
||||
|
||||
void heap_free(heap_t* heap);
|
||||
|
||||
|
||||
#endif // _HEAP_H_
|
||||
|
66
src/heap.c
66
src/heap.c
@ -247,42 +247,72 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
assert(self != NULL);
|
||||
|
||||
// 1. set attr
|
||||
// -------------------- private --------------------
|
||||
self->_obj_size = obj_size;
|
||||
self->_size = 0;
|
||||
self->_capacity = capacity;
|
||||
self->_ratio = 2;
|
||||
|
||||
self->peek = heap_peek;
|
||||
self->push = heap_push;
|
||||
self->pop = heap_pop;
|
||||
self->size = heap_size;
|
||||
self->empty = heap_empty;
|
||||
self->clear = heap_clear;
|
||||
self->destory = heap_destory;
|
||||
self->setmin = heap_setmin;
|
||||
self->print = heap_print;
|
||||
|
||||
self->obj = (void*)malloc(self->_capacity * self->_obj_size);
|
||||
if(self->obj == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
self->destory = heap_destory;
|
||||
|
||||
// -------------------- public --------------------
|
||||
// kernel
|
||||
self->peek = heap_peek;
|
||||
self->push = heap_push;
|
||||
self->pop = heap_pop;
|
||||
self->empty = heap_empty;
|
||||
|
||||
// base
|
||||
self->size = heap_size;
|
||||
self->clear = heap_clear;
|
||||
|
||||
// -------------------- debug --------------------
|
||||
self->setmin = heap_setmin;
|
||||
self->print = heap_print;
|
||||
return true;
|
||||
}
|
||||
|
||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity)
|
||||
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
heap_t heap = NULL;
|
||||
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
||||
if(heap != NULL)
|
||||
if(heap == NULL)
|
||||
{
|
||||
if(heap_init2(heap, obj_size, capacity) != true)
|
||||
{
|
||||
free(heap);
|
||||
heap = NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(heap_init2(heap, obj_size, capacity) != true)
|
||||
{
|
||||
free(heap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
heap->_min_flag = false;
|
||||
return heap;
|
||||
}
|
||||
|
||||
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
heap_t heap = NULL;
|
||||
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
||||
if(heap == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(heap_init2(heap, obj_size, capacity) != true)
|
||||
{
|
||||
free(heap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
heap->_min_flag = true;
|
||||
return heap;
|
||||
}
|
||||
|
||||
|
@ -46,15 +46,11 @@ static void test_heap_num(void)
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new2(sizeof(int), 64);
|
||||
heap_t heap = heap_min_new2(sizeof(int), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
// default: maxheap
|
||||
// maxheap or minheap
|
||||
heap->setmin(heap, true);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
@ -93,15 +89,11 @@ static void test_heap_struct(void)
|
||||
struct _student temp = {0};
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
heap_t heap = heap_new2(sizeof(struct _student), 64);
|
||||
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
heap->print_obj = print_struct;
|
||||
heap->compare = compare_struct;
|
||||
|
||||
// default: maxheap
|
||||
// maxheap or minheap
|
||||
heap->setmin(heap, true);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
|
Loading…
Reference in New Issue
Block a user