diff --git a/README.md b/README.md index f98d21b..6e905b7 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ segarray *-- ringbuf : 组合 segarray *-- rawbuf : 组合 arraylist *-- darray : 组合 -string *-- darray : 组合 +string *-- arraylist : 组合 hashtable *-- darray : 组合 @@ -107,22 +107,26 @@ equeue ..> ringbuf : 依赖 ## 数据结构 ### 底层基础容器 -> 原生裸数据结构 |数据结构 |名称 |说明 | |---|---|---| -| darray | 动态数组 | 扩容 -| ringbuf | 环形缓存区 | 扩容/外部缓存 | linklist | 单链表 | | dlinklist | 双向链表 | +| darray | 动态数组 | 扩容 +| ringbuf | 环形缓存区 | 扩容/外部缓存 | rawbuf | 原始缓冲区 | 动态分配固定容量/外部缓存 | segarray | 分段数组 | 扩容 +### 中层容器 +|数据结构 |名称 |说明 | +|---|---|---| +| segarray | 分段数组 | 扩容 +| arraylist | 动态数组 | 扩容(负索引/切片) + **使用建议** - ringbuf 小数据,少库容 - segarray 大数据,优先扩容 - ### 通用标准容器 > 基于底层封装 @@ -131,6 +135,7 @@ equeue ..> ringbuf : 依赖 | deque | 双端队列 | 扩容 | stack | 栈 | 扩容 | queue | 队列 | 扩容 +| ustring | 字符串 | 扩容 ### 嵌入式专用容器 > 适配嵌入式场景、外置内存、无动态堆分配 @@ -192,6 +197,7 @@ size_t index(void *obj); // 获取元素索引, -1为不存在 bool insert(size_t index, const void* obj); // 插入元素 bool remove(size_t index, const void *obj); // 删除元素 < delete !!!废弃:防止项目用于C++,关键字冲突> +bool erase(size_t index, size_t count); // 删除区间元素 // 随机访问 bool set(uint32_t index, const void* obj); // 设置元素 diff --git a/src/darray.c b/src/darray.c index 72f3808..227b829 100644 --- a/src/darray.c +++ b/src/darray.c @@ -238,7 +238,7 @@ bool darray_iter_hasnext(struct _iterator *iter) } else { - if (iter->_index == 0) + if (iter->_index == -1) { return false; } diff --git a/src/ringbuf.c b/src/ringbuf.c index 740d694..d4c979a 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -306,7 +306,7 @@ bool ringbuf_iter_hasnext(struct _iterator *iter) } else { - if (iter->_index == self->_head) + if (iter->_index == ring_index_prev(self->_head, self->_capacity)) { return false; } diff --git a/src/segarray.c b/src/segarray.c index de9aa13..ae020a1 100644 --- a/src/segarray.c +++ b/src/segarray.c @@ -636,7 +636,7 @@ bool segarray_iter_hasnext(struct _iterator *iter) } else { - if (iter->_index == 0) + if (iter->_index == -1) { return false; } diff --git a/src/ustring.c b/src/ustring.c index 0ec43d2..e88fdf4 100644 --- a/src/ustring.c +++ b/src/ustring.c @@ -324,7 +324,7 @@ bool ustring_iter_hasnext(struct _iterator *iter) } else { - if (iter->_index == 0) + if (iter->_index == (size_t)-1) { return false; } @@ -348,7 +348,7 @@ const void *ustring_iter_next(struct _iterator *iter) { iter->_index = iter->_index - 1; } - return self->_alist->at(self->_alist, index); + return self->at(self, index); } iterator_t ustring_iter(struct _ustring *self, linear_order_t order) @@ -367,7 +367,6 @@ iterator_t ustring_iter(struct _ustring *self, linear_order_t order) { iter->_index = self->len(self) - 1; } - iter->hasnext = ustring_iter_hasnext; iter->next = ustring_iter_next; return iter; @@ -394,8 +393,8 @@ static size_t ustring_count(struct _ustring *self, uview_t uvstr) { unicstl_assert(self != NULL); size_t count = 0; - char *p = self->cstr(self); - while(p != '\0') + char *p = (char *)self->cstr(self); + while(*p != '\0') { p = strstr(p, uvstr.str); if(p == NULL) diff --git a/test/test.h b/test/test.h index a7236d2..b251af5 100644 --- a/test/test.h +++ b/test/test.h @@ -20,7 +20,7 @@ #include "unity.h" -#define UNITTEST_ALL 0 +#define UNITTEST_ALL 1 #define UNITTEST_LINKLIST (UNITTEST_ALL || 0) #define UNITTEST_DLINKLIST (UNITTEST_ALL || 0) diff --git a/test/test_arraylist.c b/test/test_arraylist.c index f7d3ac2..4d110c9 100644 --- a/test/test_arraylist.c +++ b/test/test_arraylist.c @@ -870,7 +870,7 @@ static void test_arraylist_iter(void) TEST_ASSERT_EQUAL_INT(data[i], temp); i--; } - TEST_ASSERT_EQUAL_INT(0, i); + TEST_ASSERT_EQUAL_INT(-1, i); arraylist_free(&arraylist); } diff --git a/test/test_darray.c b/test/test_darray.c index 629811f..1ded611 100644 --- a/test/test_darray.c +++ b/test/test_darray.c @@ -662,8 +662,10 @@ static void test_darray_iter(void) temp = *(int *)iter->next(iter); TEST_ASSERT_EQUAL_INT(data[i], temp); i--; + + // darray->print_obj(&temp); } - TEST_ASSERT_EQUAL_INT(0, i); + TEST_ASSERT_EQUAL_INT(-1, i); darray_free(&darray); } diff --git a/test/test_deque.c b/test/test_deque.c index 5c8d18d..29e0630 100644 --- a/test/test_deque.c +++ b/test/test_deque.c @@ -375,7 +375,7 @@ static void test_deque_iter(void) TEST_ASSERT_EQUAL_INT(data[i], temp); i--; } - TEST_ASSERT_EQUAL_INT(0, i); + TEST_ASSERT_EQUAL_INT(-1, i); deque_free(&deque); } diff --git a/test/test_ringbuf.c b/test/test_ringbuf.c index cae7b2d..26c5eb4 100644 --- a/test/test_ringbuf.c +++ b/test/test_ringbuf.c @@ -511,8 +511,10 @@ static void test_ringbuf_iter(void) temp = *(int *)iter->next(iter); TEST_ASSERT_EQUAL_INT(data[i], temp); i--; + + ringbuf->print_obj(&temp); } - TEST_ASSERT_EQUAL_INT(0, i); + TEST_ASSERT_EQUAL_INT(-1, i); ringbuf_free(&ringbuf); } diff --git a/test/test_segarray.c b/test/test_segarray.c index 04a77f8..0aa0162 100644 --- a/test/test_segarray.c +++ b/test/test_segarray.c @@ -489,7 +489,7 @@ static void test_segarray_iter(void) TEST_ASSERT_EQUAL_INT(data[i], temp); i--; } - TEST_ASSERT_EQUAL_INT(0, i); + TEST_ASSERT_EQUAL_INT(-1, i); log_info("iter-reverse cuccess!"); segarray_free(&segarray); diff --git a/test/test_ustring.c b/test/test_ustring.c index 5495997..6cb1274 100644 --- a/test/test_ustring.c +++ b/test/test_ustring.c @@ -430,6 +430,47 @@ void test_ustring_count(void) ustring_free(&str); } +static void test_ustring_iter(void) +{ + const char *data = "hello wolrd!"; + ustring_t ustring = ustring_new(uv(data)); + + size_t i = 0; + char temp; + size_t len = ustring->len(ustring); + + iterator_t iter = ustring->iter(ustring, LINEAR_FORWARD); + i = 0; + TEST_ASSERT_TRUE(iter->hasnext(iter)); + while(iter->hasnext(iter)) + { + temp = *(int *)iter->next(iter); + TEST_ASSERT_EQUAL_INT(data[i], temp); + i++; + + // ustring->print_obj(&temp); + } + TEST_ASSERT_EQUAL_INT(len, i); + + iter = ustring->iter(ustring, LINEAR_REVERSE); + i = 0; + size_t idx = len - 1; + TEST_ASSERT_TRUE(iter->hasnext(iter)); + while(iter->hasnext(iter)) + { + temp = *(int *)iter->next(iter); + + TEST_ASSERT_EQUAL_INT(data[idx], temp); + idx-=1; + i++; + ustring->print_obj(&temp); + } + TEST_ASSERT_EQUAL_INT(len, i); + + ustring_free(&ustring); +} + + void test_ustring(void) { UnitySetTestFile(__FILE__); @@ -480,4 +521,6 @@ void test_ustring(void) RUN_TEST(test_ustring_substr); RUN_TEST(test_ustring_index); RUN_TEST(test_ustring_count); + + RUN_TEST(test_ustring_iter); }