From 0369d58147fe87985a01340271e1fadd52a2b6ec Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 24 Apr 2025 16:58:15 +0800 Subject: [PATCH] =?UTF-8?q?heap=E5=88=A0=E9=99=A4=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=A0=86=E7=9A=84=E6=97=A7=E6=A0=87=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/common.h | 10 ++++++++++ include/heap.h | 12 ++++-------- src/heap.c | 25 +++++++++---------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/include/common.h b/include/common.h index 66a2995..796ff4f 100644 --- a/include/common.h +++ b/include/common.h @@ -29,4 +29,14 @@ #include "iterator.h" #endif +/** + * @brief obj compare with obj2 + * + * @return + * obj < obj2 return -1 + * obj == obj2 return 0 + * obj > obj2 return 1 + */ +typedef int (*cmp_fun_t)(void* obj, void* obj2); + #endif // _COMMON_H_ diff --git a/include/heap.h b/include/heap.h index 87c8e02..0e0a84b 100644 --- a/include/heap.h +++ b/include/heap.h @@ -15,8 +15,8 @@ typedef enum { - MIN_HEAP = 0, - MAX_HEAP = 1, + HEAP_MIN = 0, + HEAP_MAX = 1, }heap_type; struct _heap @@ -30,9 +30,8 @@ struct _heap uint32_t _ratio; heap_type _type; - bool _min_flag; - void (*destory)(struct _heap* self); + void (*_destory)(struct _heap* self); // -------------------- public -------------------- // kernel @@ -41,9 +40,6 @@ struct _heap 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 (*clear)(struct _heap* self); @@ -57,7 +53,7 @@ struct _heap * obj > obj2 return 1 */ int (*compare)(void* obj, void* obj2); - + // -------------------- debug -------------------- void (*print)(struct _heap* self); void (*print_obj)(void* obj); diff --git a/src/heap.c b/src/heap.c index d6c6cf1..1f1ebee 100644 --- a/src/heap.c +++ b/src/heap.c @@ -71,7 +71,7 @@ static void heap_fixed_up(struct _heap* self, int i) { assert(self != NULL); int p = 0; - if(self->_min_flag != true) + if(self->_type == HEAP_MAX) { while(1) { @@ -85,7 +85,7 @@ static void heap_fixed_up(struct _heap* self, int i) i = p; } } - else + else /* if(self->_type == HEAP_MIN) */ { while(1) { @@ -122,7 +122,7 @@ static void heap_fixed_down(struct _heap* self, int i) int l = 0,r = 0; int max = 0, min = 0; - if(self->_min_flag != true) + if(self->_type == HEAP_MAX) { while(1) { @@ -147,7 +147,7 @@ static void heap_fixed_down(struct _heap* self, int i) i = max; } } - else + else /* if(self->_type == HEAP_MIN) */ { while(1) { @@ -192,12 +192,6 @@ static bool heap_pop(struct _heap* self, void* obj) return true; } -static void heap_setmin(struct _heap* self, bool min_flag) -{ - assert(self != NULL); - self->_min_flag = min_flag; -} - static uint32_t heap_size(struct _heap* self) { assert(self != NULL); @@ -259,8 +253,8 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) return false; } - self->destory = heap_destory; - + self->_destory = heap_destory; + // -------------------- public -------------------- // kernel self->peek = heap_peek; @@ -273,7 +267,6 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) self->clear = heap_clear; // -------------------- debug -------------------- - self->setmin = heap_setmin; self->print = heap_print; return true; } @@ -293,7 +286,7 @@ heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity) return NULL; } - heap->_min_flag = false; + heap->_type = HEAP_MAX; return heap; } @@ -312,7 +305,7 @@ heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity) return NULL; } - heap->_min_flag = true; + heap->_type = HEAP_MIN; return heap; } @@ -320,7 +313,7 @@ void heap_free(heap_t* heap) { if(*heap != NULL) { - (*heap)->destory(*heap); + (*heap)->_destory(*heap); free(*heap); } *heap = NULL;