mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
heap的迭代器实现
This commit is contained in:
parent
d672db224c
commit
88580acb1e
@ -30,6 +30,7 @@ struct _heap
|
||||
uint32_t _ratio;
|
||||
|
||||
heap_type _type;
|
||||
struct _iterator _iter;
|
||||
|
||||
void (*_destory)(struct _heap* self);
|
||||
|
||||
@ -44,6 +45,9 @@ struct _heap
|
||||
uint32_t(*size)(struct _heap* self);
|
||||
bool (*clear)(struct _heap* self);
|
||||
|
||||
// iter
|
||||
iterator_t (*iter)(struct _heap* self);
|
||||
|
||||
// config
|
||||
// !!! you have to implement this function
|
||||
compare_fun_t compare;
|
||||
|
46
src/heap.c
46
src/heap.c
@ -249,6 +249,46 @@ static void heap_print(struct _heap* self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
iterator_t heap_iter(struct _heap* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_cur_node = self->obj;
|
||||
return iter;
|
||||
}
|
||||
|
||||
bool heap_iter_hasnext(struct _iterator* iter)
|
||||
{
|
||||
assert(iter != NULL);
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
heap_t self = (heap_t)iter->_parent;
|
||||
if(iter->_cur < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const void* heap_iter_next(struct _iterator* iter)
|
||||
{
|
||||
assert(iter != NULL);
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
heap_t self = (heap_t)iter->_parent;
|
||||
void *obj = NULL;
|
||||
|
||||
uint32_t index = self->_iter._cur;
|
||||
obj = self->obj + self->_obj_size * index;
|
||||
|
||||
self->_iter._cur += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
assert(self != NULL);
|
||||
@ -265,6 +305,9 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
return false;
|
||||
}
|
||||
|
||||
self->_iter.hasnext = heap_iter_hasnext;
|
||||
self->_iter.next = heap_iter_next;
|
||||
|
||||
self->_destory = heap_destory;
|
||||
|
||||
// -------------------- public --------------------
|
||||
@ -278,6 +321,9 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
||||
self->size = heap_size;
|
||||
self->clear = heap_clear;
|
||||
|
||||
// iter
|
||||
self->iter = heap_iter;
|
||||
|
||||
// config
|
||||
self->compare = NULL;
|
||||
|
||||
|
@ -177,7 +177,6 @@ static void test_heap_max_num(void)
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
|
||||
static void test_heap_max_struct(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
@ -234,6 +233,63 @@ static void test_heap_max_struct(void)
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
const int heap_max_iter_expect[15][15] = {
|
||||
{5},
|
||||
{5, 2},
|
||||
{5, 2, 3},
|
||||
{5, 2, 3, 1},
|
||||
{7, 5, 3, 1, 2},
|
||||
{8, 5, 7, 1, 2, 3},
|
||||
{8, 5, 7, 1, 2, 3, 6},
|
||||
{8, 5, 7, 4, 2, 3, 6, 1},
|
||||
{9, 8, 7, 5, 2, 3, 6, 1, 4},
|
||||
{10, 9, 7, 5, 8, 3, 6, 1, 4, 2},
|
||||
{12, 10, 7, 5, 9, 3, 6, 1, 4, 2, 8},
|
||||
{12, 10, 11, 5, 9, 7, 6, 1, 4, 2, 8, 3},
|
||||
{15, 10, 12, 5, 9, 11, 6, 1, 4, 2, 8, 3, 7},
|
||||
{15, 10, 14, 5, 9, 11, 12, 1, 4, 2, 8, 3, 7, 6},
|
||||
{15, 10, 14, 5, 9, 11, 13, 1, 4, 2, 8, 3, 7, 6, 12}
|
||||
};
|
||||
|
||||
static void test_heap_max_iter(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
int data[15] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
|
||||
int temp = 0;
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
int out[15] = { 0 };
|
||||
int outlen = 0;
|
||||
|
||||
heap_t heap = heap_max_new2(sizeof(int), 64);
|
||||
TEST_ASSERT_NOT_NULL(heap);
|
||||
heap->print_obj = print_num;
|
||||
heap->compare = compare_num;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
temp = data[i];
|
||||
TEST_ASSERT_TRUE(heap->push(heap, &temp));
|
||||
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
|
||||
|
||||
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
||||
TEST_ASSERT_EQUAL_INT(*(int *)get_max(heap, data, 0, heap->size(heap)), temp);
|
||||
|
||||
iterator_t iter = heap->iter(heap);
|
||||
|
||||
memset(out, 0, sizeof(out));
|
||||
outlen = 0;
|
||||
while(iter->hasnext(iter))
|
||||
{
|
||||
temp = *(int*)iter->next(iter);
|
||||
out[outlen] = temp;
|
||||
outlen++;
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT_ARRAY(&heap_max_iter_expect[i], out, outlen);
|
||||
}
|
||||
|
||||
heap_free(&heap);
|
||||
TEST_ASSERT_NULL(heap);
|
||||
}
|
||||
|
||||
void test_heap(void)
|
||||
{
|
||||
@ -244,4 +300,6 @@ void test_heap(void)
|
||||
|
||||
RUN_TEST(test_heap_max_num);
|
||||
RUN_TEST(test_heap_max_struct);
|
||||
|
||||
RUN_TEST(test_heap_max_iter);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user