From 88580acb1e8794154c01d93cf595b040162c2a04 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 24 Apr 2025 18:08:16 +0800 Subject: [PATCH] =?UTF-8?q?heap=E7=9A=84=E8=BF=AD=E4=BB=A3=E5=99=A8?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/heap.h | 4 ++++ src/heap.c | 46 +++++++++++++++++++++++++++++++++++++ test/test_heap.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/include/heap.h b/include/heap.h index 457b2f9..d6d0708 100644 --- a/include/heap.h +++ b/include/heap.h @@ -30,6 +30,7 @@ struct _heap uint32_t _ratio; heap_type _type; + struct _iterator _iter; void (*_destory)(struct _heap* self); @@ -43,6 +44,9 @@ struct _heap // base 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 diff --git a/src/heap.c b/src/heap.c index bf26110..59dc5f4 100644 --- a/src/heap.c +++ b/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; diff --git a/test/test_heap.c b/test/test_heap.c index 068fb46..ad7c32b 100644 --- a/test/test_heap.c +++ b/test/test_heap.c @@ -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); }