堆不是最大堆就是最小堆,直接对外拆分成两套接口

This commit is contained in:
建峰 2025-04-24 16:50:35 +08:00
parent bd9f07ae65
commit 02b09e729d
5 changed files with 90 additions and 50 deletions

View File

@ -79,5 +79,18 @@ unicstl_stack_v1.2.5_20240717-a0.zip
# 带a或者b后缀表示当前版本发布前的测试版。如果发布后则直接更新版本号了 # 带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

View File

@ -20,14 +20,10 @@ void demo_heap_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[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->print_obj = print_num;
heap->compare = compare_num; heap->compare = compare_num;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
printf("\n\n----- demo_heap_num -----\n"); printf("\n\n----- demo_heap_num -----\n");
printf("----- push -----\n"); printf("----- push -----\n");
@ -90,7 +86,7 @@ static void demo_heap_struct(void)
struct _student temp = {0}; struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[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->print_obj = print_struct;
heap->compare = compare_struct; heap->compare = compare_struct;

View File

@ -1,64 +1,73 @@
/** /**
* @file heap.h * @file heap.h
* @author wenjf (Orig5826@163.com) * @author wenjf (Orig5826@163.com)
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2024-07-03 * @date 2024-07-03
* *
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2024
* *
*/ */
#ifndef _HEAP_H_ #ifndef _HEAP_H_
#define _HEAP_H_ #define _HEAP_H_
#include "common.h" #include "common.h"
typedef enum
{
MIN_HEAP = 0,
MAX_HEAP = 1,
}heap_type;
struct _heap struct _heap
{ {
void * obj; // -------------------- private --------------------
void* obj;
uint32_t _size; uint32_t _size;
uint32_t _obj_size; uint32_t _obj_size;
uint32_t _capacity; uint32_t _capacity;
uint32_t _ratio; uint32_t _ratio;
heap_type _type;
bool _min_flag; bool _min_flag;
void (*destory)(struct _heap* self);
// -------------------- public --------------------
// kernel // kernel
bool (*peek)(struct _heap* self, void* obj); bool (*peek)(struct _heap* self, void* obj);
bool (*push)(struct _heap* self, void* obj); bool (*push)(struct _heap* self, void* obj);
bool (*pop)(struct _heap* self, void* obj); bool (*pop)(struct _heap* self, void* obj);
bool (*empty)(struct _heap* self);
// default: max heap // default: max heap
void (*setmin)(struct _heap* self, bool min_flag); void (*setmin)(struct _heap* self, bool min_flag);
// base // base
uint32_t(*size)(struct _heap* self); uint32_t(*size)(struct _heap* self);
bool (*empty)(struct _heap* self); bool (*clear)(struct _heap* self);
/** /**
* @brief obj compare with obj2 * @brief obj compare with obj2
* *
* @return * @return
* obj < obj2 return -1 * obj < obj2 return -1
* obj == obj2 return 0 * obj == obj2 return 0
* obj > obj2 return 1 * obj > obj2 return 1
*/ */
int (*compare)(void* obj, void* obj2); int (*compare)(void* obj, void* obj2);
// others // -------------------- debug --------------------
bool (*clear)(struct _heap* self);
void (*destory)(struct _heap* self);
// print
void (*print)(struct _heap* self); void (*print)(struct _heap* self);
void (*print_obj)(void* obj); void (*print_obj)(void* obj);
}; };
typedef struct _heap* heap_t; typedef struct _heap* heap_t;
// create and free heap // 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); void heap_free(heap_t* heap);
#endif // _HEAP_H_ #endif // _HEAP_H_

View File

@ -247,42 +247,72 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
{ {
assert(self != NULL); assert(self != NULL);
// 1. set attr // -------------------- private --------------------
self->_obj_size = obj_size; self->_obj_size = obj_size;
self->_size = 0; self->_size = 0;
self->_capacity = capacity; self->_capacity = capacity;
self->_ratio = 2; 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); self->obj = (void*)malloc(self->_capacity * self->_obj_size);
if(self->obj == NULL) if(self->obj == NULL)
{ {
return false; 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; 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_t heap = NULL;
heap = (struct _heap*)malloc(sizeof(struct _heap)); heap = (struct _heap*)malloc(sizeof(struct _heap));
if(heap != NULL) if(heap == NULL)
{ {
if(heap_init2(heap, obj_size, capacity) != true) return NULL;
{
free(heap);
heap = 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; return heap;
} }

View File

@ -46,15 +46,11 @@ static void test_heap_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[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); TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_num; heap->print_obj = print_num;
heap->compare = compare_num; heap->compare = compare_num;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];
@ -93,15 +89,11 @@ static void test_heap_struct(void)
struct _student temp = {0}; struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[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); TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_struct; heap->print_obj = print_struct;
heap->compare = compare_struct; heap->compare = compare_struct;
// default: maxheap
// maxheap or minheap
heap->setmin(heap, true);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];