mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
添加堆操作
This commit is contained in:
parent
b4cb7f7a3d
commit
97cfafafa2
@ -25,3 +25,5 @@
|
|||||||
| **tree** | |**Ê÷** |
|
| **tree** | |**Ê÷** |
|
||||||
| tree_avl_init | ¶þ²æËÑË÷Ê÷ | AVLÊ÷ |
|
| tree_avl_init | ¶þ²æËÑË÷Ê÷ | AVLÊ÷ |
|
||||||
| tree_rb_init | ¶þ²æËÑË÷Ê÷ | ºìºÚÊ÷ |
|
| tree_rb_init | ¶þ²æËÑË÷Ê÷ | ºìºÚÊ÷ |
|
||||||
|
| **heap** | |**堆** |
|
||||||
|
| heap_init2 | 数组 | 最大堆/最小堆 |
|
||||||
|
64
include/heap.h
Normal file
64
include/heap.h
Normal file
@ -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_
|
120
src/heap.c
Normal file
120
src/heap.c
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
21
test/test_heap.c
Normal file
21
test/test_heap.c
Normal file
@ -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();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user