Compare commits

..

No commits in common. "08ba5296bef914a1556ad69cd319d48ddb4ab22a" and "0369d58147fe87985a01340271e1fadd52a2b6ec" have entirely different histories.

14 changed files with 1497 additions and 869 deletions

View File

@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.29) cmake_minimum_required(VERSION 3.29)
# 0. 项目信息 # 0. 项目信息
project(demo VERSION 0.0.02) project(demo VERSION 0.0.01)
# 2. 支持GDB # 2. 支持GDB
set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_BUILD_TYPE "Debug")

View File

@ -48,19 +48,18 @@
## 规范 ## 规范
### 版本说明 ### 版本说明
| 命名 |版本说明 | 范围 | 更新说明 | | 版本 | 范围 | 更新说明 |
|:----: |:----:|:----:|:----:| |:----:|:----:|:----:|
| `VERSION_MAJOR` | 主版本号 | 0 ~ 99 | 代码框架大改,完全不兼容旧版 | | 主版本号 | 0 ~ 99 | 代码框架大改,完全不兼容旧版 |
| `VERSION_MINOR` | 次版本号 | 0 ~ 99 | 代码框架尽量兼容旧版增信大功能、修复重大bug等 | | 次版本号 | 0 ~ 99 | 代码框架尽量兼容旧版增信大功能、修复重大bug等 |
| `VERSION_MICRO` | 小版本号 | 0 ~ 99 | 代码框架兼容旧版新增小功能、修复bug等 | | 小版本号 | 0 ~ 99 | 代码框架兼容旧版新增小功能、修复bug等 |
举例说明: 举例说明:
```c ```c
// 若 major > 0 ,则代表正式发布版本 // 若 major > 0 ,则代表正式发布版本
#define UNICSTL_VERSION_MAJOR 1 #define VER_MAJOR 1
#define UNICSTL_VERSION_MINOR 2 #define VER_MINOR 2
#define UNICSTL_VERSION_MICRO 5 #define VER_MICRO 5
#define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO)
``` ```
### 工程命名 ### 工程命名
@ -83,7 +82,7 @@ unicstl_stack_v1.2.5_20240717-a0.zip
## 修改日志 ## 修改日志
### Unicstl 0.0.01 (2025-04-24) ### Unicstl 0.0.01 (2025-04-24)
- new features new features:
- add stack - add stack
- add queue - add queue
- add deque - add deque
@ -91,8 +90,7 @@ unicstl_stack_v1.2.5_20240717-a0.zip
- add heap - add heap
- add tree - add tree
- add graph - add graph
- add iterator bugfixed:
- bugfixed:
- none - none
- others: others:
- none - none

View File

@ -35,9 +35,8 @@ void demo_avltree_num(void)
// int data[] = { 1,2,3,4,5,6}; // int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 }; // int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
uint32_t len = sizeof(data) / sizeof(data[0]);
int temp = 0; int temp = 0;
iterator_t iter = NULL; uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_avl_new(sizeof(int)); tree_t tree = tree_avl_new(sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
@ -54,13 +53,7 @@ void demo_avltree_num(void)
printf("insert = "); printf("insert = ");
tree->print_obj(&temp); tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
} }
@ -88,85 +81,45 @@ void demo_avltree_num(void)
} }
printf("----- preorder -----\n"); printf("----- preorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder -----\n"); printf("----- inorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder -----\n"); printf("----- postorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth -----\n"); printf("----- breadth -----\n");
iter = tree->iter(tree, ORDER_LEFT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n"); printf("----- preorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder(right) -----\n"); printf("----- inorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder(right) -----\n"); printf("----- postorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth(right) -----\n"); printf("----- breadth(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- preorder(left) -----\n"); printf("----- left priority -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->order(tree, false);
while(iter->hasnext(iter)) printf("----- preorder -----\n");
{ tree->preorder(tree, tree->_root);
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -178,13 +131,7 @@ void demo_avltree_num(void)
printf("delete = "); printf("delete = ");
tree->print_obj(&temp); tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
} }
@ -259,9 +206,8 @@ void demo_rbtree_num(void)
// int data[] = { 1,2,3,4,5,6}; // int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 }; // int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
uint32_t len = sizeof(data) / sizeof(data[0]);
int temp = 0; int temp = 0;
iterator_t iter = NULL; uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(int)); tree_t tree = tree_rb_new(sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
@ -278,13 +224,7 @@ void demo_rbtree_num(void)
printf("insert = "); printf("insert = ");
tree->print_obj(&temp); tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if(true != tree_rb_check(tree))
@ -318,84 +258,45 @@ void demo_rbtree_num(void)
} }
printf("----- preorder -----\n"); printf("----- preorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder -----\n"); printf("----- inorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder -----\n"); printf("----- postorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth -----\n"); printf("----- breadth -----\n");
iter = tree->iter(tree, ORDER_LEFT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n"); printf("----- preorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder(right) -----\n"); printf("----- inorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder(right) -----\n"); printf("----- postorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth(right) -----\n"); printf("----- breadth(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n"); printf("----- preorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -408,13 +309,7 @@ void demo_rbtree_num(void)
tree->delete(tree, &temp); tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if(true != tree_rb_check(tree))
@ -428,6 +323,7 @@ void demo_rbtree_num(void)
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
#endif
tree_free(&tree); tree_free(&tree);
} }
@ -443,7 +339,6 @@ void demo_rbtree_struct(void)
}; };
struct _student temp = {0}; struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
iterator_t iter = NULL;
tree_t tree = tree_rb_new(sizeof(struct _student)); tree_t tree = tree_rb_new(sizeof(struct _student));
tree->print_obj = print_struct; tree->print_obj = print_struct;
@ -460,13 +355,7 @@ void demo_rbtree_struct(void)
printf("insert = "); printf("insert = ");
tree->print_obj(&temp); tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if(true != tree_rb_check(tree))
@ -500,84 +389,45 @@ void demo_rbtree_struct(void)
} }
printf("----- preorder -----\n"); printf("----- preorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder -----\n"); printf("----- inorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder -----\n"); printf("----- postorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth -----\n"); printf("----- breadth -----\n");
iter = tree->iter(tree, ORDER_LEFT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n"); printf("----- preorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- inorder(right) -----\n"); printf("----- inorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_IN); tree->inorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- postorder(right) -----\n"); printf("----- postorder(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_POST); tree->postorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
printf("----- breadth(right) -----\n"); printf("----- breadth(right) -----\n");
iter = tree->iter(tree, ORDER_RIGHT_BREADTH); tree->breadth(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n"); printf("----- preorder -----\n");
iter = tree->iter(tree, ORDER_LEFT_PRE); tree->preorder(tree, tree->_root);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -590,13 +440,7 @@ void demo_rbtree_struct(void)
tree->delete(tree, &temp); tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree)); printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
iter = tree->iter(tree, ORDER_LEFT_PRE);
while(iter->hasnext(iter))
{
temp = *(struct _student *)iter->next(iter);
tree->print_obj(&temp);
}
printf("\n"); printf("\n");
if(true != tree_rb_check(tree)) if(true != tree_rb_check(tree))
@ -610,6 +454,7 @@ void demo_rbtree_struct(void)
{ {
printf("----- empty -----\n"); printf("----- empty -----\n");
} }
#endif
tree_free(&tree); tree_free(&tree);
} }

View File

@ -37,6 +37,6 @@
* obj == obj2 return 0 * obj == obj2 return 0
* obj > obj2 return 1 * obj > obj2 return 1
*/ */
typedef int (*compare_fun_t)(void* obj, void* obj2); typedef int (*cmp_fun_t)(void* obj, void* obj2);
#endif // _COMMON_H_ #endif // _COMMON_H_

View File

@ -63,8 +63,8 @@ struct _deque
bool (*set)(struct _deque* self, int index, void* obj); bool (*set)(struct _deque* self, int index, void* obj);
// compare // compare
// int (*compare)(void* obj, void* obj2); int (*cmp)(void* obj, void* obj2);
// bool (*sort)(struct _deque* self, uint8_t reserve); bool (*sort)(struct _deque* self, uint8_t reserve);
// -------------------- debug -------------------- // -------------------- debug --------------------
void (*print)(struct _deque* self); void (*print)(struct _deque* self);

View File

@ -30,7 +30,6 @@ struct _heap
uint32_t _ratio; uint32_t _ratio;
heap_type _type; heap_type _type;
struct _iterator _iter;
void (*_destory)(struct _heap* self); void (*_destory)(struct _heap* self);
@ -45,11 +44,15 @@ struct _heap
uint32_t(*size)(struct _heap* self); uint32_t(*size)(struct _heap* self);
bool (*clear)(struct _heap* self); bool (*clear)(struct _heap* self);
// iter /**
iterator_t (*iter)(struct _heap* self); * @brief obj compare with obj2
*
// config * @return
compare_fun_t compare; // !!! you have to implement this function * obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
int (*compare)(void* obj, void* obj2);
// -------------------- debug -------------------- // -------------------- debug --------------------
void (*print)(struct _heap* self); void (*print)(struct _heap* self);

View File

@ -16,7 +16,6 @@
struct _list struct _list
{ {
// -------------------- private --------------------
void * obj; void * obj;
uint32_t _obj_size; uint32_t _obj_size;
@ -27,9 +26,6 @@ struct _list
struct _iterator _iter; struct _iterator _iter;
void (*_destory)(struct _list* self);
// -------------------- public --------------------
// kernel // kernel
bool (*append)(struct _list* self, void* obj); // Append object to the end of the list. 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 (*insert)(struct _list* self, int index, void* obj); // Insert object before index.
@ -41,25 +37,30 @@ struct _list
bool (*get)(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); bool (*set)(struct _list* self, int index, void* obj);
// base
uint32_t(*size)(struct _list* self);
bool (*empty)(struct _list* self);
bool (*clear)(struct _list* self);
// iter // iter
iterator_t (*iter)(struct _list* self); 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);
// sort // 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. 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 The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained). order of two equal elements is maintained).
The reverse flag can be set to sort in descending order. 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));
// -------------------- debug -------------------- // print
void (*print)(struct _list* self); void (*print)(struct _list* self);
void (*print_obj)(void* obj); void (*print_obj)(void* obj);
}; };

View File

@ -55,7 +55,6 @@ struct _tree_node
struct _tree struct _tree
{ {
// -------------------- private --------------------
struct _tree_node * _root; struct _tree_node * _root;
uint32_t _size; uint32_t _size;
@ -68,34 +67,60 @@ struct _tree
stack_t stack; stack_t stack;
queue_t queue; queue_t queue;
struct _tree_node * cur_node;
struct _iterator _iter;
void (*_destory)(struct _tree* self);
// -------------------- public --------------------
// kernel // kernel
bool (*insert)(struct _tree* self, void* obj); bool (*insert)(struct _tree* self, void* obj);
bool (*delete)(struct _tree* self, void* obj); bool (*delete)(struct _tree* self, void* obj);
struct _tree_node* (*find)(struct _tree* self, void* obj);
struct _tree_node* (*find_min)(struct _tree* self, struct _tree_node* root);
struct _tree_node* (*find_max)(struct _tree* self, struct _tree_node* root);
bool (*rebalance)(struct _tree* self, struct _tree_node* root); bool (*rebalance)(struct _tree* self, struct _tree_node* root);
int32_t (*height)(struct _tree* self, struct _tree_node* root); int32_t (*height)(struct _tree* self, struct _tree_node* root);
bool (*min)(struct _tree* self, void* obj);
bool (*max)(struct _tree* self, void* obj);
// base // base
bool (*clear)(struct _tree* self); bool (*clear)(struct _tree* self);
bool (*empty)(struct _tree* self); bool (*empty)(struct _tree* self);
uint32_t (*size)(struct _tree* self); uint32_t (*size)(struct _tree* self);
// iter // iter
iterator_t (*iter)(struct _tree* self, enum _order); /**
* @brief
*
*/
void (*set_order)(struct _tree* self, enum _order order);
void* (*begin)(struct _tree* self);
void* (*next)(struct _tree* self);
void* (*end)(struct _tree* self);
// others /**
bool (*min)(struct _tree* self, void* obj); * @brief obj compare with obj2
bool (*max)(struct _tree* self, void* obj); *
* @return
* obj < obj2 return -1
* obj == obj2 return 0
* obj > obj2 return 1
*/
int (*compare)(void* obj, void* obj2);
// config // free
compare_fun_t compare; // !!! you have to implement this function void (*destory)(struct _tree* self);
// ----- print -----
// traversal depth
void (*order)(struct _tree* self, bool right_priority);
void (*preorder)(struct _tree* self, struct _tree_node* root);
void (*inorder)(struct _tree* self, struct _tree_node* root);
void (*postorder)(struct _tree* self, struct _tree_node* root);
// traversal breadth
void (*breadth)(struct _tree* self, struct _tree_node* root);
// -------------------- debug --------------------
void (*print_obj)(void* obj); void (*print_obj)(void* obj);
}; };
typedef struct _tree* tree_t; typedef struct _tree* tree_t;
@ -107,3 +132,4 @@ tree_t tree_rb_new(uint32_t obj_size);
void tree_free(tree_t* tree); void tree_free(tree_t* tree);
#endif // _TREE_H_ #endif // _TREE_H_

View File

@ -11,11 +11,6 @@
#ifndef _UNICSTL_H_ #ifndef _UNICSTL_H_
#define _UNICSTL_H_ #define _UNICSTL_H_
#define UNICSTL_VERSION_MAJOR 0
#define UNICSTL_VERSION_MINOR 0
#define UNICSTL_VERSION_MICRO 2
#define UNICSTL_VERSION ((UNICSTL_VERSION_MAJOR << 16) | (UNICSTL_VERSION_MINOR << 8) | UNICSTL_VERSION_MICRO)
#include "common.h" #include "common.h"
#include "list.h" #include "list.h"

View File

@ -70,14 +70,7 @@ static void heap_swap(struct _heap* self, int i, int j)
static void heap_fixed_up(struct _heap* self, int i) static void heap_fixed_up(struct _heap* self, int i)
{ {
assert(self != NULL); assert(self != NULL);
assert(self->compare != NULL);
int p = 0; int p = 0;
if(self->compare == NULL)
{
return ;
}
if(self->_type == HEAP_MAX) if(self->_type == HEAP_MAX)
{ {
while(1) while(1)
@ -129,11 +122,6 @@ static void heap_fixed_down(struct _heap* self, int i)
int l = 0,r = 0; int l = 0,r = 0;
int max = 0, min = 0; int max = 0, min = 0;
if(self->compare == NULL)
{
return;
}
if(self->_type == HEAP_MAX) if(self->_type == HEAP_MAX)
{ {
while(1) while(1)
@ -249,46 +237,6 @@ 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) static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
{ {
assert(self != NULL); assert(self != NULL);
@ -305,9 +253,6 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
return false; return false;
} }
self->_iter.hasnext = heap_iter_hasnext;
self->_iter.next = heap_iter_next;
self->_destory = heap_destory; self->_destory = heap_destory;
// -------------------- public -------------------- // -------------------- public --------------------
@ -321,12 +266,6 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
self->size = heap_size; self->size = heap_size;
self->clear = heap_clear; self->clear = heap_clear;
// iter
self->iter = heap_iter;
// config
self->compare = NULL;
// -------------------- debug -------------------- // -------------------- debug --------------------
self->print = heap_print; self->print = heap_print;
return true; return true;

View File

@ -215,51 +215,42 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
return false; return false;
} }
// -------------------- private -------------------- // 1. set attr
list->_obj_size = obj_size; list->_obj_size = obj_size;
list->_size = 0; list->_size = 0;
list->_capacity = capacity; list->_capacity = capacity;
list->_ratio = 2; list->_ratio = 2;
list->_cur = 0; 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); list->obj = (void*)malloc(list->_capacity * list->_obj_size);
if (list->obj == NULL) if (list->obj == NULL)
{ {
return false; 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; return true;
} }
@ -285,9 +276,9 @@ void list_free(list_t* list)
assert(list != NULL); assert(list != NULL);
if(list != NULL && *list != NULL) if(list != NULL && *list != NULL)
{ {
if((*list)->_destory != NULL) if((*list)->destory != NULL)
{ {
(*list)->_destory(*list); (*list)->destory(*list);
} }
free(*list); free(*list);
*list = NULL; *list = NULL;

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,7 @@ static void* get_min(struct _heap* heap, void *array, int start, int end)
return min; return min;
} }
static void test_heap_min_num(void) static void test_heap_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4}; // int data[] = { 2,1,3,4};
@ -56,10 +56,9 @@ static void test_heap_min_num(void)
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp)); TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap)); TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp)); TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(*(int *)get_min(heap, data, 0, heap->size(heap)), temp); TEST_ASSERT_EQUAL_INT(*(int *)get_min(heap, data, 0, heap->size(heap)), temp);
}
TEST_ASSERT_TRUE(heap->clear(heap)); TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap)); TEST_ASSERT_TRUE(heap->empty(heap));
@ -80,18 +79,12 @@ static void test_heap_min_num(void)
TEST_ASSERT_NULL(heap); TEST_ASSERT_NULL(heap);
} }
static void test_heap_min_struct(void) static void test_heap_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
{"sun", 1003}, {"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
{"zhou", 1005}, "zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
{"wu", 1006},
{"zhao", 1001},
{"qian", 1002},
{"li", 1004},
{"zheng", 1007},
{"wang", 1008},
}; };
struct _student temp = {0}; struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
@ -106,13 +99,13 @@ static void test_heap_min_struct(void)
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp)); TEST_ASSERT_TRUE(heap->push(heap, &temp));
TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap)); TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap));
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp)); TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id); TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name); TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->clear(heap)); TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap)); TEST_ASSERT_TRUE(heap->empty(heap));
@ -133,173 +126,10 @@ static void test_heap_min_struct(void)
TEST_ASSERT_NULL(heap); TEST_ASSERT_NULL(heap);
} }
static void test_heap_max_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 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]);
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);
}
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
TEST_ASSERT_NULL(heap);
}
static void test_heap_max_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"sun", 1003},
{"zhou", 1005},
{"wu", 1006},
{"zhao", 1001},
{"qian", 1002},
{"li", 1004},
{"zheng", 1007},
{"wang", 1008},
};
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_max_new2(sizeof(struct _student), 64);
TEST_ASSERT_NOT_NULL(heap);
heap->print_obj = print_struct;
heap->compare = compare_struct;
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(((struct _student*)get_max(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_max(heap, data, 0, heap->size(heap)))->name, temp.name);
// heap->print_obj(&temp);
// printf("\n");
}
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->clear(heap));
TEST_ASSERT_TRUE(heap->empty(heap));
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->push(heap, &temp));
}
for (i = 0; i < len; i++)
{
temp = data[i];
TEST_ASSERT_TRUE(heap->pop(heap, &temp));
}
TEST_ASSERT_TRUE(heap->empty(heap));
heap_free(&heap);
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) void test_heap(void)
{ {
UnitySetTestFile(__FILE__); UnitySetTestFile(__FILE__);
RUN_TEST(test_heap_min_num); RUN_TEST(test_heap_num);
RUN_TEST(test_heap_min_struct); RUN_TEST(test_heap_struct);
RUN_TEST(test_heap_max_num);
RUN_TEST(test_heap_max_struct);
RUN_TEST(test_heap_max_iter);
} }

View File

@ -19,6 +19,447 @@
#undef min #undef min
#endif #endif
#if 0
/**
* @brief
* int data[] = { 5,2,3,1,7,8,6 };
* 5
* | |
* 2 7
* | | | |
* 1 3 6 8
*/
void test_avltree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 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]);
tree_t tree = tree_avl_new(sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_avltree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
// delete
tree->delete(tree, &temp);
printf("delete = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
tree_free(&tree);
}
static bool tree_rb_check_color(struct _tree* self, struct _tree_node* root, int black_num, int black_num_expected)
{
if (root == NULL)
{
if (black_num != black_num_expected)
{
printf("black_num != black_num_expected\n");
return false;
}
return true;
}
if (root->color == RBT_BLACK)
{
black_num++;
}
if (root->color == RBT_RED && root->parent && root->parent->color == RBT_RED)
{
printf("The red node is adjacent to the red node\n");
return false;
}
return tree_rb_check_color(self, root->left, black_num, black_num_expected) &&
tree_rb_check_color(self, root->right, black_num, black_num_expected);
}
static bool tree_rb_check(struct _tree* self)
{
assert(self != NULL);
if (self->_root == NULL)
{
return true;
}
if (self->_root->color != RBT_BLACK)
{
printf("self->_root->color != RBT_BLACK\n");
return false;
}
int black_num_expected = 0;
struct _tree_node* root = self->_root;
while (root)
{
if (root->color == RBT_BLACK)
{
black_num_expected++;
}
root = root->left;
}
return tree_rb_check_color(self, self->_root, 0, black_num_expected);
}
/**
* @brief
*/
void test_rbtree_num(void)
{
uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[] = { 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]);
tree_t tree = tree_rb_new(sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
printf("\n\n----- demo_rbtree_num -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
/**
* @brief
*/
void test_rbtree_struct(void)
{
uint32_t i = 0;
struct _student data[] = {
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
};
struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;
printf("\n\n----- demo_rbtree_struct -----\n");
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
printf("insert = ");
tree->print_obj(&temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
printf("----- max -----\n");
tree->max(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- min -----\n");
tree->min(tree, &temp);
tree->print_obj(&temp);
printf("\n");
printf("----- tree -----\n");
tree->clear(tree);
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
printf("----- insert -----\n");
for (i = 0; i < len; i++)
{
temp = data[i];
tree->insert(tree, &temp);
}
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
printf("----- right priority -----\n");
tree->order(tree, true);
printf("----- preorder(right) -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
printf("----- inorder(right) -----\n");
tree->inorder(tree, tree->_root);
printf("\n");
printf("----- postorder(right) -----\n");
tree->postorder(tree, tree->_root);
printf("\n");
printf("----- breadth(right) -----\n");
tree->breadth(tree, tree->_root);
printf("\n");
#if 1
printf("----- left priority -----\n");
tree->order(tree, false);
printf("----- preorder -----\n");
tree->preorder(tree, tree->_root);
printf("\n");
for (i = 0; i < len; i++)
{
temp = data[i];
printf("delete = ");
tree->print_obj(&temp);
// delete
tree->delete(tree, &temp);
printf("size = %2d : ", tree->size(tree));
tree->preorder(tree, tree->_root);
printf("\n");
if (true != tree_rb_check(tree))
{
printf("----- rb_check_error -----\n");
return;
}
}
if (tree->empty(tree))
{
printf("----- empty -----\n");
}
#endif
tree_free(&tree);
}
#endif
static const int expected_int_array[9][15] = { static const int expected_int_array[9][15] = {
{ 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13}, // original data { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13}, // original data
{ 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15}, // order_left_pre { 7, 3, 2, 1, 5, 4, 6, 11, 9, 8, 10, 14, 12, 13, 15}, // order_left_pre
@ -67,54 +508,75 @@ static const int expected_int_array_orderpre_delete[15][15] = {
{ 13, }, { 13, },
}; };
static const enum _order order[8] = {
ORDER_LEFT_PRE, ORDER_LEFT_IN, ORDER_LEFT_POST, ORDER_LEFT_BREADTH,
ORDER_RIGHT_PRE, ORDER_RIGHT_IN, ORDER_RIGHT_POST, ORDER_RIGHT_BREADTH
};
static uint32_t iter2array_num(iterator_t iter, int *data)
{
uint32_t count = 0;
while(iter->hasnext(iter))
{
data[count] = *(int *)iter->next(iter);
count++;
}
return count;
}
static void test_avltree_iter(void) static void test_avltree_iter(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_avl_new(sizeof(int)); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp)); TEST_ASSERT_TRUE(tree->insert(tree, &temp));
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
}
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count);
} }
for(i = 0; i < 8; i++) for(i = 1; i < 9; i++)
{ {
iter = tree->iter(tree, order[i]); tree->set_order(tree, i); //ORDER_LEFT_IN
count = iter2array_num(iter, buff); // printf("\n ----- iter test -----\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i + 1], buff, count); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
} }
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
}
#if 0
tree->order(tree, true);
printf("\n\nactual data = \n");
tree->postorder(tree, tree->_root);
printf("\n");
// set order
// tree->set_order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN);
// tree->set_order(tree, ORDER_LEFT_POST);
// tree->set_order(tree, ORDER_LEFT_BREADTH);
// tree->set_order(tree, ORDER_RIGHT_PRE);
// tree->set_order(tree, ORDER_RIGHT_IN);
tree->set_order(tree, ORDER_RIGHT_POST);
// tree->set_order(tree, ORDER_RIGHT_BREADTH);
printf("\n ----- iter data -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[tree->_order], buff, count);
#endif
TEST_ASSERT_FALSE(tree->empty(tree)); TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree)); TEST_ASSERT_TRUE(tree->clear(tree));
@ -127,34 +589,46 @@ static void test_avltree_iter(void)
static void test_avltree_insert(void) static void test_avltree_insert(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_avl_new(sizeof(int)); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp)); TEST_ASSERT_TRUE(tree->insert(tree, &temp));
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
}
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_insert[i], buff, count);
} }
for(i = 0; i < 8; i++) for(i = 1; i < 9; i++)
{ {
iter = tree->iter(tree, order[i]); tree->set_order(tree, i); //ORDER_LEFT_IN
count = iter2array_num(iter, buff); // printf("\n ----- iter test -----\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i + 1], buff, count); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[i], buff, count);
} }
tree_free(&tree); tree_free(&tree);
@ -165,18 +639,19 @@ static void test_avltree_delete(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_avl_new(sizeof(int)); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
memcpy(data, expected_int_array[0], len);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
temp = data[i]; temp = data[i];
@ -185,13 +660,19 @@ static void test_avltree_delete(void)
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
// printf("(%2d ) ", *iter);
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_delete[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array_orderpre_delete[i], buff, count);
temp = data[i]; temp = data[i];
// delete
TEST_ASSERT_TRUE(tree->delete(tree, &temp)); TEST_ASSERT_TRUE(tree->delete(tree, &temp));
} }
//
TEST_ASSERT_FALSE(tree->delete(tree, &temp)); TEST_ASSERT_FALSE(tree->delete(tree, &temp));
tree_free(&tree); tree_free(&tree);
@ -253,15 +734,21 @@ static const int rbt_expected_int_array_orderpre_delete[15][15] = {
static void test_rbtree_iter(void) static void test_rbtree_iter(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
// int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_rb_new(sizeof(int)); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -270,17 +757,50 @@ static void test_rbtree_iter(void)
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp)); TEST_ASSERT_TRUE(tree->insert(tree, &temp));
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
}
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count);
} }
for(i = 0; i < 8; i++) for(i = 1; i < 9; i++)
{ {
iter = tree->iter(tree, order[i]); tree->set_order(tree, i); //ORDER_LEFT_IN
count = iter2array_num(iter, buff); // printf("\n ----- iter test -----\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i + 1], buff, count); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
} }
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i], buff, count);
}
#if 0
tree->order(tree, true);
printf("\n\nactual data = \n");
tree->postorder(tree, tree->_root);
printf("\n");
// set order
// tree->set_order(tree, ORDER_LEFT_PRE);
// tree->set_order(tree, ORDER_LEFT_IN);
// tree->set_order(tree, ORDER_LEFT_POST);
// tree->set_order(tree, ORDER_LEFT_BREADTH);
// tree->set_order(tree, ORDER_RIGHT_PRE);
// tree->set_order(tree, ORDER_RIGHT_IN);
tree->set_order(tree, ORDER_RIGHT_POST);
// tree->set_order(tree, ORDER_RIGHT_BREADTH);
printf("\n ----- iter data -----\n");
for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(expected_int_array[tree->_order], buff, count);
#endif
TEST_ASSERT_FALSE(tree->empty(tree)); TEST_ASSERT_FALSE(tree->empty(tree));
TEST_ASSERT_TRUE(tree->clear(tree)); TEST_ASSERT_TRUE(tree->clear(tree));
@ -293,15 +813,20 @@ static void test_rbtree_iter(void)
static void test_rbtree_insert(void) static void test_rbtree_insert(void)
{ {
uint32_t i = 0; uint32_t i = 0;
// int data[] = { 2,1,3,4};
// int data[] = { 1,2,3,4,5,6};
// int data[] = { 5,2,3,1,7,8,6 };
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(int);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
tree_t tree = tree_rb_new(sizeof(int)); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -310,16 +835,24 @@ static void test_rbtree_insert(void)
temp = data[i]; temp = data[i];
TEST_ASSERT_TRUE(tree->insert(tree, &temp)); TEST_ASSERT_TRUE(tree->insert(tree, &temp));
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
}
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_insert[i], buff, count);
} }
for(i = 0; i < 8; i++) for(i = 1; i < 9; i++)
{ {
iter = tree->iter(tree, order[i]); tree->set_order(tree, i); //ORDER_LEFT_IN
count = iter2array_num(iter, buff); // printf("\n ----- iter test -----\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i + 1], buff, count); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
{
// printf("(%2d ) ", *iter);
buff[count++] = *iter;
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array[i], buff, count);
} }
tree_free(&tree); tree_free(&tree);
@ -330,18 +863,16 @@ static void test_rbtree_delete(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, }; int data[15] = { 5, 2, 3, 1, 7, 8, 6, 4, 9, 10, 12, 11, 15, 14, 13, };
int buff[15]; int buff[32];
uint32_t len = sizeof(data) / sizeof(data[0]);
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
int * iter = NULL;
int count = 0; int count = 0;
iterator_t iter = NULL;
enum _order order[8] = {
ORDER_LEFT_PRE, ORDER_LEFT_IN, ORDER_LEFT_POST, ORDER_LEFT_BREADTH,
ORDER_RIGHT_PRE, ORDER_RIGHT_IN, ORDER_RIGHT_POST, ORDER_RIGHT_BREADTH
};
tree_t tree = tree_rb_new(sizeof(int)); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -353,19 +884,26 @@ static void test_rbtree_delete(void)
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
iter = tree->iter(tree, ORDER_LEFT_PRE); for (count = 0, iter = tree->begin(tree); iter != tree->end(tree); iter = tree->next(tree))
count = iter2array_num(iter, buff); {
buff[count++] = *iter;
// printf("(%2d ) ", *iter);
}
// printf("\n");
TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_delete[i], buff, count); TEST_ASSERT_EQUAL_INT_ARRAY(rbt_expected_int_array_orderpre_delete[i], buff, count);
temp = data[i]; temp = data[i];
// delete
TEST_ASSERT_TRUE(tree->delete(tree, &temp)); TEST_ASSERT_TRUE(tree->delete(tree, &temp));
} }
//
TEST_ASSERT_FALSE(tree->delete(tree, &temp)); TEST_ASSERT_FALSE(tree->delete(tree, &temp));
tree_free(&tree); tree_free(&tree);
TEST_ASSERT_NULL(tree); TEST_ASSERT_NULL(tree);
} }
void test_tree(void) void test_tree(void)
{ {
UnitySetTestFile(__FILE__); UnitySetTestFile(__FILE__);
@ -377,4 +915,8 @@ void test_tree(void)
RUN_TEST(test_rbtree_iter); RUN_TEST(test_rbtree_iter);
RUN_TEST(test_rbtree_insert); RUN_TEST(test_rbtree_insert);
RUN_TEST(test_rbtree_delete); RUN_TEST(test_rbtree_delete);
// RUN_TEST(test_avltree_num);
// RUN_TEST(test_rbtree_num);
// RUN_TEST(test_rbtree_struct);
} }