mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 11:31:37 +08:00
heap删除大小堆的旧标志
This commit is contained in:
parent
02b09e729d
commit
0369d58147
@ -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_
|
||||
|
@ -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);
|
||||
|
25
src/heap.c
25
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;
|
||||
|
Loading…
Reference in New Issue
Block a user