From 02b09e729dfd3536d4f82d8708a10598c840f81c Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 24 Apr 2025 16:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A0=86=E4=B8=8D=E6=98=AF=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=A0=86=E5=B0=B1=E6=98=AF=E6=9C=80=E5=B0=8F=E5=A0=86=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=AF=B9=E5=A4=96=E6=8B=86=E5=88=86=E6=88=90?= =?UTF-8?q?=E4=B8=A4=E5=A5=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++++++- demo/demo_heap.c | 8 ++---- include/heap.h | 39 +++++++++++++++++----------- src/heap.c | 66 +++++++++++++++++++++++++++++++++++------------- test/test_heap.c | 12 ++------- 5 files changed, 90 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 2326fa0..931d1b6 100644 --- a/README.md +++ b/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 diff --git a/demo/demo_heap.c b/demo/demo_heap.c index 47ad134..b909f54 100644 --- a/demo/demo_heap.c +++ b/demo/demo_heap.c @@ -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; diff --git a/include/heap.h b/include/heap.h index c162c1c..87c8e02 100644 --- a/include/heap.h +++ b/include/heap.h @@ -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_ diff --git a/src/heap.c b/src/heap.c index 6e4cfb8..d6c6cf1 100644 --- a/src/heap.c +++ b/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; } diff --git a/test/test_heap.c b/test/test_heap.c index c37f564..ec80a9e 100644 --- a/test/test_heap.c +++ b/test/test_heap.c @@ -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];