mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 16:56:54 +08:00
Compare commits
No commits in common. "9d81e3e62dd72a79f8f54dcdbdeb55532572ff01" and "ffbcc7a0918c0f55c211abad1c3f868221f80ff4" have entirely different histories.
9d81e3e62d
...
ffbcc7a091
37
src/heap.c
37
src/heap.c
@ -46,25 +46,11 @@ bool heap_peek(struct _heap* self, void* obj)
|
|||||||
static void heap_swap(struct _heap* self, int i, int j)
|
static void heap_swap(struct _heap* self, int i, int j)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
assert(self->_obj_size != 0);
|
|
||||||
// #define C99_VLA
|
|
||||||
#ifdef C99_VLA
|
|
||||||
char tmp[self->_obj_size];
|
char tmp[self->_obj_size];
|
||||||
#else
|
|
||||||
char* tmp = malloc(self->_obj_size);
|
|
||||||
if (tmp == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
memmove(tmp, (char *)self->obj + i * self->_obj_size, self->_obj_size);
|
memmove(tmp, self->obj + i * self->_obj_size, self->_obj_size);
|
||||||
memmove((char*)self->obj + i * self->_obj_size, (char*)self->obj + j * self->_obj_size, self->_obj_size);
|
memmove(self->obj + i * self->_obj_size, self->obj + j * self->_obj_size, self->_obj_size);
|
||||||
memmove((char*)self->obj + j * self->_obj_size, tmp, self->_obj_size);
|
memmove(self->obj + j * self->_obj_size, tmp, self->_obj_size);
|
||||||
|
|
||||||
#ifndef C99_VLA
|
|
||||||
free(tmp);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heap_fixed_up(struct _heap* self, int i)
|
static void heap_fixed_up(struct _heap* self, int i)
|
||||||
@ -77,7 +63,7 @@ static void heap_fixed_up(struct _heap* self, int i)
|
|||||||
{
|
{
|
||||||
p = parent(i);
|
p = parent(i);
|
||||||
// 若当前节点大于其父节点,则交换位置,否则退出循环
|
// 若当前节点大于其父节点,则交换位置,否则退出循环
|
||||||
if(p < 0 || self->compare((char *)self->obj + i * self->_obj_size, (char *)self->obj + p * self->_obj_size) <= 0)
|
if(p < 0 || self->compare(self->obj + i * self->_obj_size, self->obj + p * self->_obj_size) <= 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -91,7 +77,7 @@ static void heap_fixed_up(struct _heap* self, int i)
|
|||||||
{
|
{
|
||||||
p = parent(i);
|
p = parent(i);
|
||||||
// 若当前节点大于其父节点,则交换位置,否则退出循环
|
// 若当前节点大于其父节点,则交换位置,否则退出循环
|
||||||
if(p < 0 || self->compare((char *)self->obj + i * self->_obj_size, (char *)self->obj + p * self->_obj_size) >= 0)
|
if(p < 0 || self->compare(self->obj + i * self->_obj_size, self->obj + p * self->_obj_size) >= 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -109,11 +95,10 @@ bool heap_push(struct _heap* self, void* obj)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint32_t index = self->size(self);
|
uint32_t index = self->size(self);
|
||||||
memmove((char *)self->obj + index * self->_obj_size, obj, self->_obj_size);
|
memmove(self->obj + index * self->_obj_size, obj, self->_obj_size);
|
||||||
self->_size++;
|
self->_size++;
|
||||||
|
|
||||||
heap_fixed_up(self, index);
|
heap_fixed_up(self, index);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heap_fixed_down(struct _heap* self, int i)
|
static void heap_fixed_down(struct _heap* self, int i)
|
||||||
@ -130,12 +115,12 @@ static void heap_fixed_down(struct _heap* self, int i)
|
|||||||
r = right(i);
|
r = right(i);
|
||||||
max = i;
|
max = i;
|
||||||
|
|
||||||
if(l < self->size(self) && self->compare((char *)self->obj + l * self->_obj_size, (char *)self->obj + max * self->_obj_size) > 0)
|
if(l < self->size(self) && self->compare(self->obj + l * self->_obj_size, self->obj + max * self->_obj_size) > 0)
|
||||||
{
|
{
|
||||||
max = l;
|
max = l;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(r < self->size(self) && self->compare((char *)self->obj + r * self->_obj_size, (char *)self->obj + max * self->_obj_size) > 0)
|
if(r < self->size(self) && self->compare(self->obj + r * self->_obj_size, self->obj + max * self->_obj_size) > 0)
|
||||||
{
|
{
|
||||||
max = r;
|
max = r;
|
||||||
}
|
}
|
||||||
@ -155,12 +140,12 @@ static void heap_fixed_down(struct _heap* self, int i)
|
|||||||
r = right(i);
|
r = right(i);
|
||||||
min = i;
|
min = i;
|
||||||
|
|
||||||
if(l < self->size(self) && self->compare((char *)self->obj + l * self->_obj_size, (char *)self->obj + min * self->_obj_size) < 0)
|
if(l < self->size(self) && self->compare(self->obj + l * self->_obj_size, self->obj + min * self->_obj_size) < 0)
|
||||||
{
|
{
|
||||||
min = l;
|
min = l;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(r < self->size(self) && self->compare((char *)self->obj + r * self->_obj_size, (char *)self->obj + min * self->_obj_size) < 0)
|
if(r < self->size(self) && self->compare(self->obj + r * self->_obj_size, self->obj + min * self->_obj_size) < 0)
|
||||||
{
|
{
|
||||||
min = r;
|
min = r;
|
||||||
}
|
}
|
||||||
@ -185,7 +170,7 @@ bool heap_pop(struct _heap* self, void* obj)
|
|||||||
heap_swap(self, 0, index);
|
heap_swap(self, 0, index);
|
||||||
if(obj != NULL)
|
if(obj != NULL)
|
||||||
{
|
{
|
||||||
memmove(obj, (char *)self->obj + index * self->_obj_size, self->_obj_size);
|
memmove(obj, self->obj + index * self->_obj_size, self->_obj_size);
|
||||||
}
|
}
|
||||||
self->_size--;
|
self->_size--;
|
||||||
heap_fixed_down(self, 0);
|
heap_fixed_down(self, 0);
|
||||||
|
@ -35,7 +35,6 @@ void test_stack(void);
|
|||||||
void test_deque(void);
|
void test_deque(void);
|
||||||
void test_queue(void);
|
void test_queue(void);
|
||||||
void test_tree(void);
|
void test_tree(void);
|
||||||
void test_heap(void);
|
|
||||||
|
|
||||||
#endif // _TEST_H_
|
#endif // _TEST_H_
|
||||||
|
|
||||||
|
@ -1,14 +1,6 @@
|
|||||||
|
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
#ifdef max
|
|
||||||
#undef max
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef min
|
|
||||||
#undef min
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* int data[] = { 5,2,3,1,7,8,6 };
|
* int data[] = { 5,2,3,1,7,8,6 };
|
||||||
|
Loading…
Reference in New Issue
Block a user