diff --git a/README.md b/README.md index 415894d..d1e6db8 100644 --- a/README.md +++ b/README.md @@ -85,19 +85,18 @@ void* iter_next(); // 迭代器下一个元素 // -------------------- 扩展功能 -------------------- // 元素相关操作 bool append(const void* obj); // 追加元素 一般用于list -bool delete(const void* obj); // 删除元素 -// bool remove(const void *obj); // 删除元素 <同delete> +// bool remove(const void *obj); // 删除元素 <暂不使用该命名> bool find(const void* obj); // 查找元素 <返回值,bool/uint32_t/void*待定?> bool contains(const void* obj); // 判断元素是否存在 <返回bool> -uint32_t count(const void* obj); // 统计元素个数 +uint32_t count(const void* obj); // 统计元素obj的个数 // 索引相关操作 uint32_t index(void *obj); // 获取元素索引 bool insert(uint32_t index, const void* obj); // 插入元素 <非树> -bool erase(uint32_t index); // 删除元素 +bool delete(uint32_t index, void* obj); // 删除元素 +// bool erase(uint32_t index); // 删除元素<暂时不用该命名> bool set(uint32_t index, const void* obj); // 设置元素 bool get(uint32_t index, void* obj); // 获取元素 - ``` ## 特点 diff --git a/demo/demo_deque.c b/demo/demo_deque.c index 64a05b8..9e85bca 100644 --- a/demo/demo_deque.c +++ b/demo/demo_deque.c @@ -140,6 +140,7 @@ static void demo_deque_num(void) } } +#if 0 printf("----- push_front -----\n"); for (i = 0; i < len; i++) { @@ -173,6 +174,7 @@ static void demo_deque_num(void) printf("\n"); } } +#endif deque_free(&deque); } @@ -334,7 +336,6 @@ static void demo_deque_struct(void) printf("----- print -----\n"); deque->print(deque); printf("\n"); -#endif printf("----- get -----\n"); for (i = 0; i < len; i++) @@ -346,6 +347,7 @@ static void demo_deque_struct(void) printf("\n"); } } +#endif deque_free(&deque); } diff --git a/demo/demo_list.c b/demo/demo_list.c index c1f6ea2..379c621 100644 --- a/demo/demo_list.c +++ b/demo/demo_list.c @@ -32,15 +32,15 @@ static void demo_list_num(void) printf("\n"); printf("----- pop -----\n"); - list->pop(list, 9, NULL); + list->delete(list, 9, NULL); list->print(list); printf("\n"); - list->pop(list, 0, NULL); + list->delete(list, 0, NULL); list->print(list); printf("\n"); - list->pop(list, 4, NULL); + list->delete(list, 4, NULL); list->print(list); printf("\n"); @@ -129,7 +129,7 @@ static void demo_list_num(void) printf("----- pop -----\n"); for (i = 0; i < len + 1; i++) { - list->pop(list, 0, &temp); + list->pop(list, &temp); if (list->empty(list)) { @@ -167,15 +167,15 @@ static void demo_list_struct(void) printf("\n"); printf("----- pop -----\n"); - list->pop(list, 9, NULL); + list->delete(list, 9, NULL); list->print(list); printf("\n"); - list->pop(list, 0, NULL); + list->delete(list, 0, NULL); list->print(list); printf("\n"); - list->pop(list, 4, NULL); + list->delete(list, 4, NULL); list->print(list); printf("\n"); @@ -286,7 +286,7 @@ static void demo_list_struct(void) printf("----- pop -----\n"); for (i = 0; i < len + 1; i++) { - list->pop(list, 0, &temp); + list->pop(list, &temp); if (list->empty(list)) { diff --git a/include/list.h b/include/list.h index e1334fa..ce43e83 100644 --- a/include/list.h +++ b/include/list.h @@ -35,16 +35,18 @@ struct _list // -------------------- public -------------------- // kernel - bool (*append)(struct _list *self, void *obj); // Append object to the end of the list. - bool (*insert)(struct _list *self, int index, void *obj); // Insert object before index. - bool (*pop)(struct _list *self, int index, void *obj); // Remove and return item at index. - - int (*index)(struct _list *self, void *obj); // Return first index of obj. Return -1 if the obj is not present. - bool (*remove)(struct _list *self, void *obj); // Remove first occurrence of obj. + bool (*append)(struct _list *self, void *obj); + bool (*pop)(struct _list *self, void *obj); + + bool (*insert)(struct _list *self, int index, void *obj); + bool (*delete)(struct _list *self, int index, void *obj); bool (*get)(struct _list *self, int index, void *obj); bool (*set)(struct _list *self, int index, void *obj); + int (*index)(struct _list *self, void *obj); + // bool (*contains)(struct _list *self, void *obj); + // base uint32_t (*size)(struct _list *self); uint32_t (*capacity)(struct _list *self); @@ -54,16 +56,9 @@ struct _list // iter iterator_t (*iter)(struct _list *self); - // sort - // bool (*reverse)(struct _list* self); // Reverse *IN PLACE*. - /** - Sort the list in ascending order and return false. - The sort is in-place (i.e. the list itself is modified) and stable (i.e. the - order of two equal elements is maintained). - The reverse flag can be set to sort in descending order. - */ - // bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)); - + // config + compare_fun_t compare; // !!! you have to implement this function + // -------------------- debug -------------------- void (*print)(struct _list *self); void (*print_obj)(void *obj); diff --git a/src/list.c b/src/list.c index 5bf6b7c..aac1e9d 100644 --- a/src/list.c +++ b/src/list.c @@ -35,6 +35,27 @@ static bool list_append(struct _list* self, void* obj) return true; } +static bool list_pop(struct _list* self, void* obj) +{ + assert(self != NULL); + + if (self->empty(self)) + { + return false; + } + + uint32_t index = self->size(self) - 1; + uint32_t offset = index * self->_obj_size; + uint32_t offset_next = (index + 1) * self->_obj_size; + if (obj != NULL) + { + memmove(obj, (char*)self->obj + offset, self->_obj_size); + } + memmove((char*)self->obj + offset, (char*)self->obj + offset_next, self->_obj_size); + self->_size -= 1; + return true; +} + static bool list_insert(struct _list* self, int index, void* obj) { assert(index >= 0 && index < (int)self->size(self)); @@ -58,7 +79,7 @@ static bool list_insert(struct _list* self, int index, void* obj) return true; } -static bool list_pop(struct _list* self, int index, void* obj) +static bool list_delete(struct _list* self, int index, void* obj) { assert(self != NULL); assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); @@ -90,12 +111,6 @@ static int list_index(struct _list* self, void* obj) return 0; } -static bool list_remove(struct _list* self, void* obj) -{ - assert(self != NULL); - return true; -} - static bool list_clear(struct _list* self) { assert(self != NULL); @@ -142,16 +157,6 @@ static bool list_empty(struct _list* self) return !self->size(self); } -static bool list_reverse(struct _list* self) -{ - return true; -} - -static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)) -{ - return true; -} - // free static void list_destory(struct _list* self) { @@ -240,27 +245,25 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity) // -------------------- public -------------------- // kernel list->append = list_append; - list->insert = list_insert; list->pop = list_pop; - list->empty = list_empty; + list->insert = list_insert; + list->delete = list_delete; + + list->get = list_get; + list->set = list_set; + + list->index = list_index; + // list->contains = list_contains; // base list->clear = list_clear; list->size = list_size; + list->empty = list_empty; // iter list->iter = list_iter; - // others - list->index = list_index; - list->remove = list_remove; - list->get = list_get; - list->set = list_set; - - // list->reverse = list_reverse; - // list->sort = list_sort; - // -------------------- debug -------------------- list->print = list_print; diff --git a/test/test_deque.c b/test/test_deque.c index 58baff5..d5422a6 100644 --- a/test/test_deque.c +++ b/test/test_deque.c @@ -113,6 +113,7 @@ static void test_deque_num(void) } } +#if 0 for (i = 0; i < len; i++) { TEST_ASSERT_TRUE(deque->push_front(deque, &data[i])); @@ -130,6 +131,7 @@ static void test_deque_num(void) { TEST_ASSERT_TRUE(deque->get(deque, i, &temp)); } +#endif deque_free(&deque); TEST_ASSERT_NULL(deque); @@ -254,10 +256,12 @@ static void test_deque_struct(void) TEST_ASSERT_TRUE(deque->push_front(deque, &data[i])); } +#if 0 for (i = 0; i < len; i++) { TEST_ASSERT_TRUE(deque->get(deque, i, &temp)); } +#endif deque_free(&deque); TEST_ASSERT_NULL(deque); diff --git a/test/test_list.c b/test/test_list.c index dfcf180..56c5081 100644 --- a/test/test_list.c +++ b/test/test_list.c @@ -80,10 +80,10 @@ static void test_list_pop(void) } for (i = 0; i < len; i++) { - TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); + TEST_ASSERT_TRUE(list->pop(list, &temp)); } TEST_ASSERT_TRUE(list->empty(list)); - // TEST_ASSERT_FALSE(list->pop(list, 1, &temp)); + TEST_ASSERT_FALSE(list->pop(list, &temp)); list_free(&list); } @@ -129,9 +129,9 @@ static void test_list_num(void) { TEST_ASSERT_TRUE(list->append(list, &data[i])); } - TEST_ASSERT_TRUE(list->pop(list, 9, NULL)); - TEST_ASSERT_TRUE(list->pop(list, 0, NULL)); - TEST_ASSERT_TRUE(list->pop(list, 4, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 9, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 0, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 4, NULL)); TEST_ASSERT_TRUE(list->clear(list)); for (i = 0; i < len; i++) @@ -182,7 +182,7 @@ static void test_list_num(void) for (i = 0; i < len + 1; i++) { - TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); + TEST_ASSERT_TRUE(list->pop(list, &temp)); if (list->empty(list)) { @@ -213,9 +213,9 @@ static void test_list_struct(void) { TEST_ASSERT_TRUE(list->append(list, &data[i])); } - TEST_ASSERT_TRUE(list->pop(list, 9, NULL)); - TEST_ASSERT_TRUE(list->pop(list, 0, NULL)); - TEST_ASSERT_TRUE(list->pop(list, 4, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 9, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 0, NULL)); + TEST_ASSERT_TRUE(list->delete(list, 4, NULL)); TEST_ASSERT_TRUE(list->clear(list)); for (i = 0; i < len; i++) @@ -272,7 +272,7 @@ static void test_list_struct(void) for (i = 0; i < len + 1; i++) { - TEST_ASSERT_TRUE(list->pop(list, 0, &temp)); + TEST_ASSERT_TRUE(list->pop(list, &temp)); if (list->empty(list)) {