From 97cfafafa2cae9514e34be31cb31cd65de98c1b0 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Wed, 3 Jul 2024 17:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A0=86=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + include/heap.h | 64 +++++++++++++++++++++++++ src/heap.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++ test/test_heap.c | 21 +++++++++ 4 files changed, 207 insertions(+) create mode 100644 include/heap.h create mode 100644 src/heap.c create mode 100644 test/test_heap.c diff --git a/README.md b/README.md index d1098bc..2f170fb 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,5 @@ | **tree** | |**树** | | tree_avl_init | 二叉搜索树 | AVL树 | | tree_rb_init | 二叉搜索树 | 红黑树 | +| **heap** | |**堆** | +| heap_init2 | 数组 | 最大堆/最小堆 | diff --git a/include/heap.h b/include/heap.h new file mode 100644 index 0000000..4b616e1 --- /dev/null +++ b/include/heap.h @@ -0,0 +1,64 @@ +/** + * @file heap.h + * @author wenjf (Orig5826@163.com) + * @brief + * @version 0.1 + * @date 2024-07-03 + * + * @copyright Copyright (c) 2024 + * + */ +#ifndef _HEAP_H_ +#define _HEAP_H_ + +#include "common.h" + +struct _heap +{ + void * obj; + + uint32_t _size; // 栈大小 + uint32_t _obj_size; // 元素大小 + uint32_t _capacity; // 总容量 + uint32_t _ratio; // 扩展比率 + + bool _min_flag; // 最大/小堆标志 + + // kernel + bool (*peek)(struct _heap* self, void* obj); + bool (*push)(struct _heap* self, void* obj); + bool (*pop)(struct _heap* self, void* obj); + + // default: max heap + void (*setmin)(struct _heap* self, bool min_flag); + + // base + uint32_t(*size)(struct _heap* self); + bool (*empty)(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 + void (*print)(struct _heap* self); + void (*print_obj)(void* obj); +}; +typedef struct _heap* heap_t; + +bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity); + +heap_t heap_new(void); +void heap_free(heap_t heap); + +#endif // _HEAP_H_ diff --git a/src/heap.c b/src/heap.c new file mode 100644 index 0000000..59899c2 --- /dev/null +++ b/src/heap.c @@ -0,0 +1,120 @@ +/** + * @brief + * + * @param self + * @param obj_size + * @return true + * @return false + */ +#include "heap.h" + +bool heap_peek(struct _heap* self, void* obj) +{ + assert(self != NULL); + if(obj == NULL) + { + return false; + } + if(self->empty(self)) + { + return false; + } + memmove(obj, self->obj, self->_obj_size); + return true; +} + +bool heap_push(struct _heap* self, void* obj) +{ + +} + +bool heap_pop(struct _heap* self, void* obj) +{ + +} + +void heap_setmin(struct _heap* self, bool min_flag) +{ + assert(self != NULL); + self->_min_flag = min_flag; +} + +uint32_t heap_size(struct _heap* self) +{ + assert(self != NULL); + return self->_size; +} + +bool heap_empty(struct _heap* self) +{ + assert(self != NULL); + return self->size(self) == 0; +} + +bool heap_clear(struct _heap* self) +{ + assert(self != NULL); +} + +void heap_destory(struct _heap* self) +{ + assert(self != NULL); +} + +void heap_print(struct _heap* self) +{ + assert(self != NULL); + assert(self->obj != NULL); + + void* obj = NULL; + uint32_t offset = 0; + + for (int i = self->size(self) - 1; i >= 0; i--) + { + offset = self->_obj_size * i; + obj = (char*)self->obj + offset; + self->print_obj(obj); + } +} + +bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) +{ + assert(self != NULL); + + // 1. set attr + 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; + } + return true; +} + +heap_t heap_new(void) +{ + return (struct _heap*)malloc(sizeof(struct _heap)); +} + +void heap_free(heap_t heap) +{ + if(heap) + { + heap->destory(heap); + free(heap); + } +} diff --git a/test/test_heap.c b/test/test_heap.c new file mode 100644 index 0000000..7892a19 --- /dev/null +++ b/test/test_heap.c @@ -0,0 +1,21 @@ +/** + * @file test_heap.c + * @author wenjf (Orig5826@163.com) + * @brief + * @version 0.1 + * @date 2024-07-03 + * + * @copyright Copyright (c) 2024 + * + */ +#include "test.h" + +void test_heap_num(void) +{ + +} + +void test_heap(void) +{ + test_heap_num(); +}