From 08ba5296bef914a1556ad69cd319d48ddb4ab22a Mon Sep 17 00:00:00 2001 From: jf-home Date: Fri, 25 Apr 2025 01:30:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E6=97=B6=E7=94=A8=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E5=B0=B1=E5=85=88=E5=B1=8F=E8=94=BD?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakelists.txt | 2 +- include/deque.h | 4 +-- include/heap.h | 7 ++---- include/list.h | 19 +++++++------- include/tree.h | 3 +-- include/unicstl.h | 2 +- src/list.c | 63 +++++++++++++++++++++++++++-------------------- src/tree.c | 6 ++--- 8 files changed, 55 insertions(+), 51 deletions(-) diff --git a/CMakelists.txt b/CMakelists.txt index e2c76fa..9946420 100644 --- a/CMakelists.txt +++ b/CMakelists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.29) # 0. 项目信息 -project(demo VERSION 0.0.01) +project(demo VERSION 0.0.02) # 2. 支持GDB set(CMAKE_BUILD_TYPE "Debug") diff --git a/include/deque.h b/include/deque.h index 9b79dcc..6f173d5 100644 --- a/include/deque.h +++ b/include/deque.h @@ -63,8 +63,8 @@ struct _deque bool (*set)(struct _deque* self, int index, void* obj); // compare - int (*cmp)(void* obj, void* obj2); - bool (*sort)(struct _deque* self, uint8_t reserve); + // int (*compare)(void* obj, void* obj2); + // bool (*sort)(struct _deque* self, uint8_t reserve); // -------------------- debug -------------------- void (*print)(struct _deque* self); diff --git a/include/heap.h b/include/heap.h index d6d0708..25e4b81 100644 --- a/include/heap.h +++ b/include/heap.h @@ -49,11 +49,8 @@ struct _heap iterator_t (*iter)(struct _heap* self); // config - // !!! you have to implement this function - compare_fun_t compare; - // register function - // void (*register_compare)(struct _heap* self, compare_fun_t cmp_fun); - + compare_fun_t compare; // !!! you have to implement this function + // -------------------- debug -------------------- void (*print)(struct _heap* self); void (*print_obj)(void* obj); diff --git a/include/list.h b/include/list.h index 74364d5..f5185eb 100644 --- a/include/list.h +++ b/include/list.h @@ -16,6 +16,7 @@ struct _list { + // -------------------- private -------------------- void * obj; uint32_t _obj_size; @@ -26,6 +27,9 @@ struct _list struct _iterator _iter; + void (*_destory)(struct _list* self); + + // -------------------- 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. @@ -37,30 +41,25 @@ struct _list bool (*get)(struct _list* self, int index, void* obj); bool (*set)(struct _list* self, int index, void* obj); - // iter - iterator_t (*iter)(struct _list* self); - // base uint32_t(*size)(struct _list* self); bool (*empty)(struct _list* self); - - // clear and free node bool (*clear)(struct _list* self); - void (*destory)(struct _list* self); + // iter + iterator_t (*iter)(struct _list* self); // sort - bool (*reverse)(struct _list* self); // Reverse *IN PLACE*. - + // 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)); + // bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)); - // print + // -------------------- debug -------------------- void (*print)(struct _list* self); void (*print_obj)(void* obj); }; diff --git a/include/tree.h b/include/tree.h index 4adfc91..6237402 100644 --- a/include/tree.h +++ b/include/tree.h @@ -71,7 +71,7 @@ struct _tree struct _iterator _iter; - void (*destory)(struct _tree* self); + void (*_destory)(struct _tree* self); // -------------------- public -------------------- // kernel @@ -107,4 +107,3 @@ tree_t tree_rb_new(uint32_t obj_size); void tree_free(tree_t* tree); #endif // _TREE_H_ - diff --git a/include/unicstl.h b/include/unicstl.h index 8d4205f..06119a4 100644 --- a/include/unicstl.h +++ b/include/unicstl.h @@ -13,7 +13,7 @@ #define UNICSTL_VERSION_MAJOR 0 #define UNICSTL_VERSION_MINOR 0 -#define UNICSTL_VERSION_MICRO 1 +#define UNICSTL_VERSION_MICRO 2 #define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO) #include "common.h" diff --git a/src/list.c b/src/list.c index de31dc2..053e208 100644 --- a/src/list.c +++ b/src/list.c @@ -215,42 +215,51 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity) return false; } - // 1. set attr + // -------------------- private -------------------- list->_obj_size = obj_size; list->_size = 0; list->_capacity = capacity; list->_ratio = 2; list->_cur = 0; - // 2. set function - // kernel - list->append = list_append; - list->get = list_get; - list->clear = list_clear; - list->destory = list_destory; - list->empty = list_empty; - list->index = list_index; - list->insert = list_insert; - list->pop = list_pop; - list->print = list_print; - list->remove = list_remove; - list->reverse = list_reverse; - list->set = list_set; - list->size = list_size; - list->sort = list_sort; - - // iterator - list->iter = list_iter; - list->_iter.next = list_iter_next; - list->_iter.hasnext = list_iter_hasnext; - - // 3. set array - // list->obj = (void*)calloc(list->_capacity, list->_obj_size); list->obj = (void*)malloc(list->_capacity * list->_obj_size); if (list->obj == NULL) { return false; } + + list->_iter.next = list_iter_next; + list->_iter.hasnext = list_iter_hasnext; + + list->_destory = list_destory; + + // -------------------- public -------------------- + // kernel + list->append = list_append; + list->insert = list_insert; + list->pop = list_pop; + + list->empty = list_empty; + + // base + list->clear = list_clear; + list->size = list_size; + + // 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; + return true; } @@ -276,9 +285,9 @@ void list_free(list_t* list) assert(list != NULL); if(list != NULL && *list != NULL) { - if((*list)->destory != NULL) + if((*list)->_destory != NULL) { - (*list)->destory(*list); + (*list)->_destory(*list); } free(*list); *list = NULL; diff --git a/src/tree.c b/src/tree.c index df815a5..719310b 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1463,7 +1463,7 @@ static bool tree_avl_init(struct _tree* self, uint32_t obj_size) self->_iter.hasnext = tree_iter_hasnext; self->_iter.next = tree_iter_next; - self->destory = tree_destory; + self->_destory = tree_destory; // -------------------- public -------------------- // kernel @@ -1517,7 +1517,7 @@ static bool tree_rb_init(struct _tree* self, uint32_t obj_size) self->_iter.hasnext = tree_iter_hasnext; self->_iter.next = tree_iter_next; - self->destory = tree_destory; + self->_destory = tree_destory; // -------------------- public -------------------- // kernel @@ -1579,7 +1579,7 @@ void tree_free(tree_t* tree) { if (*tree != NULL) { - (*tree)->destory(*tree); + (*tree)->_destory(*tree); free(*tree); } *tree = NULL;