Compare commits

..

No commits in common. "9d81e3e62dd72a79f8f54dcdbdeb55532572ff01" and "ffbcc7a0918c0f55c211abad1c3f868221f80ff4" have entirely different histories.

3 changed files with 11 additions and 35 deletions

View File

@ -46,25 +46,11 @@ bool heap_peek(struct _heap* self, void* obj)
static void heap_swap(struct _heap* self, int i, int j)
{
assert(self != NULL);
assert(self->_obj_size != 0);
// #define C99_VLA
#ifdef C99_VLA
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((char*)self->obj + i * self->_obj_size, (char*)self->obj + j * self->_obj_size, self->_obj_size);
memmove((char*)self->obj + j * self->_obj_size, tmp, self->_obj_size);
#ifndef C99_VLA
free(tmp);
#endif
memmove(tmp, self->obj + i * self->_obj_size, self->_obj_size);
memmove(self->obj + i * self->_obj_size, self->obj + j * self->_obj_size, self->_obj_size);
memmove(self->obj + j * self->_obj_size, tmp, self->_obj_size);
}
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);
// 若当前节点大于其父节点,则交换位置,否则退出循环
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;
}
@ -91,7 +77,7 @@ static void heap_fixed_up(struct _heap* self, int 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;
}
@ -109,11 +95,10 @@ bool heap_push(struct _heap* self, void* obj)
return false;
}
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++;
heap_fixed_up(self, index);
return true;
}
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);
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;
}
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;
}
@ -155,12 +140,12 @@ static void heap_fixed_down(struct _heap* self, int i)
r = right(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;
}
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;
}
@ -185,7 +170,7 @@ bool heap_pop(struct _heap* self, void* obj)
heap_swap(self, 0, index);
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--;
heap_fixed_down(self, 0);

View File

@ -35,7 +35,6 @@ void test_stack(void);
void test_deque(void);
void test_queue(void);
void test_tree(void);
void test_heap(void);
#endif // _TEST_H_

View File

@ -1,14 +1,6 @@
#include "test.h"
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
/**
* @brief
* int data[] = { 5,2,3,1,7,8,6 };