From 170c2a542194794ecfca96167be2ce6ec3d9e894 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Fri, 30 Aug 2024 14:18:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E7=A0=81=E7=BB=9F=E4=B8=80=E4=BD=BF?= =?UTF-8?q?=E7=94=A8utf8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 2 +- CMakelists.txt | 10 +++++----- demo/demo_tree.c | 2 +- doc/unicstl.md | 1 - include/deque.h | 12 +++++------ include/heap.h | 10 +++++----- include/list.h | 26 ++++++++++++------------ include/queue.h | 8 ++++---- include/stack.h | 8 ++++---- include/tree.h | 10 +++++----- src/heap.c | 4 ++-- src/list.c | 4 ++-- src/stack.c | 8 +------- src/tree.c | 46 +++++++++++++++++++++---------------------- test/test.c | 2 +- test/test_queue.c | 8 -------- 16 files changed, 73 insertions(+), 88 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 64fdc31..d93da67 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "cmake.configureOnOpen": true, - "files.encoding": "gb18030", + "files.encoding": "utf8", "files.associations": { "common.h": "c", "unicstl.h": "c", diff --git a/CMakelists.txt b/CMakelists.txt index 39bcaf1..e2c76fa 100644 --- a/CMakelists.txt +++ b/CMakelists.txt @@ -1,11 +1,11 @@ -# 0. cmake 最低版本号要求 +# 0. cmake 鏈浣庣増鏈彿瑕佹眰 cmake_minimum_required(VERSION 3.29) -# 0. 项目信息 +# 0. 椤圭洰淇℃伅 project(demo VERSION 0.0.01) -# 2. 支持GDB +# 2. 鏀寔GDB set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g --std=c99") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall --std=c99") @@ -14,11 +14,11 @@ set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall --std=c99") set(CMAKE_INSTALL_PREFIX "release") -# 1. 添加头文件路径 +# 1. 娣诲姞澶存枃浠惰矾寰 include_directories(include) include_directories(3rdparty/unicstl-unity/src) -# 1. 添加子目录 +# 1. 娣诲姞瀛愮洰褰 add_subdirectory(src) add_subdirectory(demo) add_subdirectory(3rdparty) diff --git a/demo/demo_tree.c b/demo/demo_tree.c index 7352716..d2ec8d3 100644 --- a/demo/demo_tree.c +++ b/demo/demo_tree.c @@ -10,7 +10,7 @@ */ #include "demo.h" -// vs2022 编译冲突 +// if vs2022 has error: 'max': macro redefinition #ifdef max #undef max #endif diff --git a/doc/unicstl.md b/doc/unicstl.md index 974a4d0..c3062e7 100644 --- a/doc/unicstl.md +++ b/doc/unicstl.md @@ -1,7 +1,6 @@ # tree -鏃ュ織 ``` ----- unicstl test ----- diff --git a/include/deque.h b/include/deque.h index 1533d4e..0f64a50 100644 --- a/include/deque.h +++ b/include/deque.h @@ -22,13 +22,13 @@ struct _deque_node struct _deque { - struct _deque_node* _head; // 头节点 - struct _deque_node* _tail; // 尾节点 + struct _deque_node* _head; + struct _deque_node* _tail; - uint32_t _obj_size; // 元素大小 - uint32_t _size; // 栈大小 - // uint32_t _capacity; // 总容量 - // uint32_t _ratio; // 扩展比率 + uint32_t _obj_size; + uint32_t _size; + // uint32_t _capacity; + // uint32_t _ratio; // kernel bool (*push_back)(struct _deque* self, void* obj); diff --git a/include/heap.h b/include/heap.h index 1196106..d12705d 100644 --- a/include/heap.h +++ b/include/heap.h @@ -17,12 +17,12 @@ struct _heap { void * obj; - uint32_t _size; // 栈大小 - uint32_t _obj_size; // 元素大小 - uint32_t _capacity; // 总容量 - uint32_t _ratio; // 扩展比率 + uint32_t _size; + uint32_t _obj_size; + uint32_t _capacity; + uint32_t _ratio; - bool _min_flag; // 最大/小堆标志 + bool _min_flag; // kernel bool (*peek)(struct _heap* self, void* obj); diff --git a/include/list.h b/include/list.h index 646e5f5..a9c5360 100644 --- a/include/list.h +++ b/include/list.h @@ -17,22 +17,22 @@ struct _list { void * obj; - uint32_t _obj_size; // 元素大小 - uint32_t _size; // 栈大小 - uint32_t _capacity; // 总容量 - uint32_t _ratio; // 扩展比率 - uint32_t _cur; // 当前索引 + uint32_t _obj_size; + uint32_t _size; + uint32_t _capacity; + uint32_t _ratio; + uint32_t _cur; // 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. + 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. + 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 (*get)(struct _list* self, int index, void* obj); // 根据索引,获取对象 - bool (*set)(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); // iter void* (*begin)(struct _list* self); @@ -49,7 +49,7 @@ struct _list // 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. diff --git a/include/queue.h b/include/queue.h index 418edda..ccd974c 100644 --- a/include/queue.h +++ b/include/queue.h @@ -27,10 +27,10 @@ struct _queue uint32_t _index_front; uint32_t _index_back; - uint32_t _obj_size; // 元素大小 - uint32_t _size; // 栈大小 - uint32_t _capacity; // 总容量 - uint32_t _ratio; // 扩展比率 + uint32_t _obj_size; + uint32_t _size; + uint32_t _capacity; + uint32_t _ratio; // kernel bool (*push)(struct _queue* self, void* obj); diff --git a/include/stack.h b/include/stack.h index bfeaf6f..16eb802 100644 --- a/include/stack.h +++ b/include/stack.h @@ -23,10 +23,10 @@ struct _stack { struct _stack_node * _head; - uint32_t _size; // 栈大小 - uint32_t _obj_size; // 元素大小 - uint32_t _capacity; // 总容量 - uint32_t _ratio; // 扩展比率 + uint32_t _size; + uint32_t _obj_size; + uint32_t _capacity; + uint32_t _ratio; // kernel bool (*peek)(struct _stack* self, void* obj); diff --git a/include/tree.h b/include/tree.h index 92547a5..b7d2c9c 100644 --- a/include/tree.h +++ b/include/tree.h @@ -36,12 +36,12 @@ struct _tree { struct _tree_node * _root; - uint32_t _size; // 栈大小 - uint32_t _obj_size; // 元素大小 - uint32_t _capacity; // 总容量 - uint32_t _ratio; // 扩展比率 + uint32_t _size; + uint32_t _obj_size; + uint32_t _capacity; + uint32_t _ratio; - bool _right_priority; // 右优先 + bool _right_priority; // kernel bool (*insert)(struct _tree* self, void* obj); diff --git a/src/heap.c b/src/heap.c index c108903..7dd7707 100644 --- a/src/heap.c +++ b/src/heap.c @@ -76,7 +76,7 @@ static void heap_fixed_up(struct _heap* self, int i) while(1) { p = parent(i); - // 若当前节点大于其父节点,则交换位置,否则退出循环 + // if current node is greater than its parent, swap the position. otherwise break out of loop if(p < 0 || self->compare((char *)self->obj + i * self->_obj_size, (char *)self->obj + p * self->_obj_size) <= 0) { break; @@ -90,7 +90,7 @@ static void heap_fixed_up(struct _heap* self, int i) while(1) { p = parent(i); - // 若当前节点大于其父节点,则交换位置,否则退出循环 + // if current node is less than its parent, swap the position. otherwise break out of loop if(p < 0 || self->compare((char *)self->obj + i * self->_obj_size, (char *)self->obj + p * self->_obj_size) >= 0) { break; diff --git a/src/list.c b/src/list.c index 984022d..fec4205 100644 --- a/src/list.c +++ b/src/list.c @@ -61,7 +61,7 @@ bool list_insert(struct _list* self, int index, void* obj) bool list_pop(struct _list* self, int index, void* obj) { assert(self != NULL); - assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); // list空的时候也会报错,太严格了有点 + assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); if (self->empty(self)) { @@ -193,7 +193,7 @@ void* list_end(struct _list* self) void* list_next(struct _list* self) { void *obj = NULL; - // 加了判断之后,回不到下一个了 + // if add this, can't go to end // if(self->_cur < self->_size - 1) { self->_cur += 1; diff --git a/src/stack.c b/src/stack.c index 2e04e00..85d33b1 100644 --- a/src/stack.c +++ b/src/stack.c @@ -85,14 +85,10 @@ static bool stack_pop(struct _stack* self, void* obj) if (obj != NULL) { - // 将弹出的数据输出 memmove(obj, node->obj, self->_obj_size); } - - // 更新指针 self->_head->next = node->next; - // 释放数据和节点 free(node->obj); free(node); @@ -111,10 +107,8 @@ static bool stack_clear(struct _stack* self) struct _stack_node* node = self->_head->next; while (node != NULL) { - // 更新指针 self->_head->next = node->next; - // 释放数据和节点 free(node->obj); free(node); @@ -321,7 +315,7 @@ bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity) return false; } // self->_head->obj = NULL; - self->_head->next = NULL; // 无效参数 + self->_head->next = NULL; // 4. set array self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size); diff --git a/src/tree.c b/src/tree.c index d78c9b8..51a2e84 100644 --- a/src/tree.c +++ b/src/tree.c @@ -185,14 +185,14 @@ int32_t tree_height(struct _tree* self, struct _tree_node* root) /** * @brief * - * 以 balance = rigth - left 为标准,调整平衡因子 + * if balance = rigth - left锛宻o * - * | 情况 | root->balance | node->balance | 调整方式 | + * | case | root->balance | node->balance | function | * | ---- | ------------ | -------------- | -------- | - * | 1 | 2 | >= 0 | 左旋 - * | 2 | 2 | < 0 | 先右旋后左旋 - * | 3 | -2 | <= 0 | 右旋 - * | 4 | -2 | > 0 | 先左旋后右旋 + * | 1 | 2 | >= 0 | left rotation + * | 2 | 2 | < 0 | first right rotation, then left rotation + * | 3 | -2 | <= 0 | right rotation + * | 4 | -2 | > 0 | forth left rotation, then right rotation * * @param self * @return true @@ -347,10 +347,10 @@ static bool tree_node_free(struct _tree_node* node) } /** - * @brief 在树中查找插入位置 + * @brief find the position to insert or find object * - * @param self 树的指针 - * @param obj 要查找或插入的对象 + * @param self + * @param obj */ struct _tree_node * tree_find_pos(struct _tree* self, void* obj) { @@ -1185,14 +1185,14 @@ bool tree_rb_insert(struct _tree* self, void* obj) /** * @brief * - * 以 balance = rigth - left 为标准,调整平衡因子 + * 浠 balance = rigth - left 涓烘爣鍑嗭紝璋冩暣骞宠 鍥犲瓙 * - * | 情况 | root->balance | node->balance | 调整方式 | + * | 鎯呭喌 | root->balance | node->balance | 璋冩暣鏂瑰紡 | * | ---- | ------------ | -------------- | -------- | - * | 1 | 2 | >= 0 | 左旋 - * | 2 | 2 | < 0 | 先右旋后左旋 - * | 3 | -2 | <= 0 | 右旋 - * | 4 | -2 | > 0 | 先左旋后右旋 + * | 1 | 2 | >= 0 | 宸︽棆 + * | 2 | 2 | < 0 | 鍏堝彸鏃嬪悗宸︽棆 + * | 3 | -2 | <= 0 | 鍙虫棆 + * | 4 | -2 | > 0 | 鍏堝乏鏃嬪悗鍙虫棆 * * @param self * @return true @@ -1210,18 +1210,18 @@ static bool tree_rb_rebalance(struct _tree* self, struct _tree_node* node) struct _tree_node* uncle = NULL; /** - * @brief 新插入节点为红色,且父节点为红色的情况下,需要调整。 + * @brief 鏂版彃鍏ヨ妭鐐逛负绾㈣壊锛屼笖鐖惰妭鐐逛负绾㈣壊鐨勬儏鍐典笅锛岄渶瑕佽皟鏁淬 * - * 主要考虑前三种情况[1-3],其余三种[4-6]对称操作即可 + * 涓昏鑰冭檻鍓嶄笁绉嶆儏鍐礫1-3]锛屽叾浣欎笁绉峓4-6]瀵圭О鎿嶄綔鍗冲彲 * - * 原则只有一个,那就是想办法维持红黑树性质不变 + * 鍘熷垯鍙湁涓涓紝閭e氨鏄兂鍔炴硶缁存寔绾㈤粦鏍戞ц川涓嶅彉 * - * | 情况 | 说明 | 调整方式 | + * | 鎯呭喌 | 璇存槑 | 璋冩暣鏂瑰紡 | * | ---- | --- | -------- | - * | 0 | 父节点为黑色 | 不用处理 - * | 1 | 父红,爷黑,叔红 | 仅变色即可:父黑,爷红,叔黑 - * | 2 | 父红,爷黑,叔黑 | (若爷孙在同一边)父黑,爷红,爷左/右旋 - * | 3 | 父红,爷黑,叔黑 | (若爷孙在不同边)先父左右旋,将新父变黑,随后爷红,爷左/右旋 + * | 0 | 鐖惰妭鐐逛负榛戣壊 | 涓嶇敤澶勭悊 + * | 1 | 鐖剁孩锛岀埛榛戯紝鍙旂孩 | 浠呭彉鑹插嵆鍙細鐖堕粦锛岀埛绾紝鍙旈粦 + * | 2 | 鐖剁孩锛岀埛榛戯紝鍙旈粦 | 锛堣嫢鐖峰瓩鍦ㄥ悓涓杈癸級鐖堕粦锛岀埛绾紝鐖峰乏/鍙虫棆 + * | 3 | 鐖剁孩锛岀埛榛戯紝鍙旈粦 | 锛堣嫢鐖峰瓩鍦ㄤ笉鍚岃竟锛夊厛鐖跺乏鍙虫棆锛屽皢鏂扮埗鍙橀粦锛岄殢鍚庣埛绾紝鐖峰乏/鍙虫棆 */ while(node->parent != NULL && node->parent->color == RBT_RED) { diff --git a/test/test.c b/test/test.c index 4ebb5f9..4245512 100644 --- a/test/test.c +++ b/test/test.c @@ -56,7 +56,7 @@ void print_str(void* obj) // -------------------------------------------------- -// 测试用例 +// 娴嬭瘯鐢ㄤ緥 // -------------------------------------------------- void setUp(void) { diff --git a/test/test_queue.c b/test/test_queue.c index 3d44f88..45cd370 100644 --- a/test/test_queue.c +++ b/test/test_queue.c @@ -10,10 +10,6 @@ */ #include "test.h" -/** - * @brief - * 每成功init一次,就需要对应的destory一次。否则可能存在内存泄漏 - */ static void test_queue_init(void) { struct _queue queue; @@ -36,10 +32,6 @@ static void test_queue_init(void) queue.destory(&queue); } -/** - * @brief - * 每成功init一次,就需要对应的free一次。否则可能存在内存泄漏 - */ static void test_queue_new(void) { queue_t queue = NULL;