heap删除大小堆的旧标志

This commit is contained in:
建峰 2025-04-24 16:58:15 +08:00
parent 02b09e729d
commit 0369d58147
3 changed files with 23 additions and 24 deletions

View File

@ -29,4 +29,14 @@
#include "iterator.h" #include "iterator.h"
#endif #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_ #endif // _COMMON_H_

View File

@ -15,8 +15,8 @@
typedef enum typedef enum
{ {
MIN_HEAP = 0, HEAP_MIN = 0,
MAX_HEAP = 1, HEAP_MAX = 1,
}heap_type; }heap_type;
struct _heap struct _heap
@ -30,9 +30,8 @@ struct _heap
uint32_t _ratio; uint32_t _ratio;
heap_type _type; heap_type _type;
bool _min_flag;
void (*destory)(struct _heap* self); void (*_destory)(struct _heap* self);
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
@ -41,9 +40,6 @@ struct _heap
bool (*pop)(struct _heap* self, void* obj); bool (*pop)(struct _heap* self, void* obj);
bool (*empty)(struct _heap* self); bool (*empty)(struct _heap* self);
// default: max heap
void (*setmin)(struct _heap* self, bool min_flag);
// base // base
uint32_t(*size)(struct _heap* self); uint32_t(*size)(struct _heap* self);
bool (*clear)(struct _heap* self); bool (*clear)(struct _heap* self);

View File

@ -71,7 +71,7 @@ static void heap_fixed_up(struct _heap* self, int i)
{ {
assert(self != NULL); assert(self != NULL);
int p = 0; int p = 0;
if(self->_min_flag != true) if(self->_type == HEAP_MAX)
{ {
while(1) while(1)
{ {
@ -85,7 +85,7 @@ static void heap_fixed_up(struct _heap* self, int i)
i = p; i = p;
} }
} }
else else /* if(self->_type == HEAP_MIN) */
{ {
while(1) while(1)
{ {
@ -122,7 +122,7 @@ static void heap_fixed_down(struct _heap* self, int i)
int l = 0,r = 0; int l = 0,r = 0;
int max = 0, min = 0; int max = 0, min = 0;
if(self->_min_flag != true) if(self->_type == HEAP_MAX)
{ {
while(1) while(1)
{ {
@ -147,7 +147,7 @@ static void heap_fixed_down(struct _heap* self, int i)
i = max; i = max;
} }
} }
else else /* if(self->_type == HEAP_MIN) */
{ {
while(1) while(1)
{ {
@ -192,12 +192,6 @@ static bool heap_pop(struct _heap* self, void* obj)
return true; 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) static uint32_t heap_size(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
@ -259,7 +253,7 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
return false; return false;
} }
self->destory = heap_destory; self->_destory = heap_destory;
// -------------------- public -------------------- // -------------------- public --------------------
// kernel // kernel
@ -273,7 +267,6 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
self->clear = heap_clear; self->clear = heap_clear;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->setmin = heap_setmin;
self->print = heap_print; self->print = heap_print;
return true; return true;
} }
@ -293,7 +286,7 @@ heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity)
return NULL; return NULL;
} }
heap->_min_flag = false; heap->_type = HEAP_MAX;
return heap; return heap;
} }
@ -312,7 +305,7 @@ heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity)
return NULL; return NULL;
} }
heap->_min_flag = true; heap->_type = HEAP_MIN;
return heap; return heap;
} }
@ -320,7 +313,7 @@ void heap_free(heap_t* heap)
{ {
if(*heap != NULL) if(*heap != NULL)
{ {
(*heap)->destory(*heap); (*heap)->_destory(*heap);
free(*heap); free(*heap);
} }
*heap = NULL; *heap = NULL;