Compare commits

..

4 Commits

17 changed files with 187 additions and 190 deletions

View File

@ -16,17 +16,17 @@
| stack_new | 链表 | | | stack_new | 链表 | |
| stack_new2 | 动态数组 | | | stack_new2 | 动态数组 | |
| **list** | | **列表** | **list** | | **列表**
| list_init2 | 动态数组 | | | list_new2 | 动态数组 | |
| **queue** | | **队列** | **queue** | | **队列**
| queue_init | 单向链表 | | | queue_new | 单向链表 | |
| queue_init2 | 数组 | FIFO/空/满 | | queue_new2 | 数组 | FIFO/空/满 |
| **deque** | |**双端队列** | | **deque** | |**双端队列** |
| deque_init | 双向循环链表 | | | deque_new | 双向循环链表 | |
| **tree** | |**树** | | **tree** | |**树** |
| tree_avl_init | 二叉搜索树 | AVL树 | | tree_avl_new | 二叉搜索树 | AVL树 |
| tree_rb_init | 二叉搜索树 | 红黑树 | | tree_rb_new | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** | | **heap** | |**堆** |
| heap_init2 | 数组 | 最大堆/最小堆 | | heap_new2 | 数组 | 最大堆/最小堆 |
## 特点 ## 特点
| 原理 | 说明 | | 原理 | 说明 |

View File

@ -17,8 +17,7 @@ static void demo_deque_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(); deque_t deque = deque_new(sizeof(int));
deque_init(deque, sizeof(int));
deque->print_obj = print_num; deque->print_obj = print_num;
printf("\n\n----- demo_deque_num -----\n"); printf("\n\n----- demo_deque_num -----\n");
@ -189,8 +188,7 @@ static void demo_deque_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]);
deque_t deque = deque_new(); deque_t deque = deque_new(sizeof(struct _student));
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct; deque->print_obj = print_struct;
printf("\n\n----- demo_deque_struct -----\n"); printf("\n\n----- demo_deque_struct -----\n");

View File

@ -20,8 +20,7 @@ void demo_heap_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new(); heap_t heap = heap_new2(sizeof(int), 64);
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num; heap->print_obj = print_num;
heap->compare = compare_num; heap->compare = compare_num;
@ -91,8 +90,7 @@ static void demo_heap_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]);
heap_t heap = heap_new(); heap_t heap = heap_new2(sizeof(struct _student), 64);
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct; heap->print_obj = print_struct;
heap->compare = compare_struct; heap->compare = compare_struct;

View File

@ -18,8 +18,7 @@ static void demo_list_num(void)
int index = 0; int index = 0;
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new(); list_t list = list_new2(sizeof(int), 64);
list_init2(list, sizeof(int), 64);
list->print_obj = print_num; list->print_obj = print_num;
printf("\n\n----- list_demo_num -----\n"); printf("\n\n----- list_demo_num -----\n");
@ -154,8 +153,7 @@ static void demo_list_struct(void)
int index = 0; int index = 0;
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new(); list_t list = list_new2(sizeof(struct _student), 64);
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct; list->print_obj = print_struct;
printf("\n\n----- list_demo_num -----\n"); printf("\n\n----- list_demo_num -----\n");

View File

@ -38,8 +38,7 @@ void demo_avltree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new(); tree_t tree = tree_avl_new(sizeof(int));
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -210,8 +209,7 @@ void demo_rbtree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new(); tree_t tree = tree_rb_new(sizeof(int));
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -342,8 +340,7 @@ 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]);
tree_t tree = tree_new(); tree_t tree = tree_rb_new(sizeof(struct _student));
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct; tree->print_obj = print_struct;
tree->compare = compare_struct; tree->compare = compare_struct;

View File

@ -66,9 +66,9 @@ struct _deque
}; };
typedef struct _deque* deque_t; typedef struct _deque* deque_t;
bool deque_init(struct _deque* self, uint32_t obj_size); // create and free deque
deque_t deque_new(uint32_t obj_size);
deque_t deque_new(void);
void deque_free(deque_t* deque); void deque_free(deque_t* deque);
#endif #endif

View File

@ -56,9 +56,9 @@ struct _heap
}; };
typedef struct _heap* heap_t; typedef struct _heap* heap_t;
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity); // create and free heap
heap_t heap_new2(uint32_t obj_size, uint32_t capacity);
heap_t heap_new(void);
void heap_free(heap_t* heap); void heap_free(heap_t* heap);
#endif // _HEAP_H_ #endif // _HEAP_H_

View File

@ -65,9 +65,9 @@ struct _list
}; };
typedef struct _list* list_t; typedef struct _list* list_t;
bool list_init2(struct _list* self, uint32_t obj_size, uint32_t capacity); // create and free list
list_t list_new2(uint32_t obj_size, uint32_t capacity);
list_t list_new(void);
void list_free(list_t* list); void list_free(list_t* list);
#endif // _LIST_H_ #endif // _LIST_H_

View File

@ -125,11 +125,10 @@ struct _tree
}; };
typedef struct _tree* tree_t; typedef struct _tree* tree_t;
// bst_tree // create and free tree
bool tree_avl_init(struct _tree *self, uint32_t obj_size); tree_t tree_avl_new(uint32_t obj_size);
bool tree_rb_init(struct _tree *self, uint32_t obj_size); tree_t tree_rb_new(uint32_t obj_size);
tree_t tree_new(void);
void tree_free(tree_t* tree); void tree_free(tree_t* tree);
#endif // _TREE_H_ #endif // _TREE_H_

View File

@ -10,7 +10,7 @@
*/ */
#include "deque.h" #include "deque.h"
bool deque_push_back(struct _deque* self, void* obj) static bool deque_push_back(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _deque_node* front = NULL; struct _deque_node* front = NULL;
@ -54,7 +54,7 @@ bool deque_push_back(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_push_front(struct _deque* self, void* obj) static bool deque_push_front(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _deque_node* front = NULL; struct _deque_node* front = NULL;
@ -98,7 +98,7 @@ bool deque_push_front(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_pop_back(struct _deque* self, void* obj) static bool deque_pop_back(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _deque_node* node = NULL; struct _deque_node* node = NULL;
@ -135,7 +135,7 @@ bool deque_pop_back(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_pop_front(struct _deque* self, void* obj) static bool deque_pop_front(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _deque_node* node = NULL; struct _deque_node* node = NULL;
@ -172,7 +172,7 @@ bool deque_pop_front(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_back(struct _deque* self, void* obj) static bool deque_back(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -185,7 +185,7 @@ bool deque_back(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_front(struct _deque* self, void* obj) static bool deque_front(struct _deque* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -198,27 +198,27 @@ bool deque_front(struct _deque* self, void* obj)
return true; return true;
} }
bool deque_insert(struct _deque* self, int index, void* obj) static bool deque_insert(struct _deque* self, int index, void* obj)
{ {
return true; return true;
} }
bool deque_erase(struct _deque* self, int index, void* obj) static bool deque_erase(struct _deque* self, int index, void* obj)
{ {
return true; return true;
} }
int deque_index(struct _deque* self, void* obj) static int deque_index(struct _deque* self, void* obj)
{ {
return -1; return -1;
} }
bool deque_remove(struct _deque* self, void* obj) static bool deque_remove(struct _deque* self, void* obj)
{ {
return true; return true;
} }
bool deque_clear(struct _deque* self) static bool deque_clear(struct _deque* self)
{ {
while (!self->empty(self)) while (!self->empty(self))
{ {
@ -227,7 +227,7 @@ bool deque_clear(struct _deque* self)
return true; return true;
} }
bool deque_get(struct _deque* self, int index, void* obj) static bool deque_get(struct _deque* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -243,7 +243,7 @@ bool deque_get(struct _deque* self, int index, void* obj)
return true; return true;
} }
bool deque_set(struct _deque* self, int index, void* obj) static bool deque_set(struct _deque* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -259,19 +259,19 @@ bool deque_set(struct _deque* self, int index, void* obj)
return true; return true;
} }
uint32_t deque_size(struct _deque* self) static uint32_t deque_size(struct _deque* self)
{ {
assert(self != NULL); assert(self != NULL);
return self->_size; return self->_size;
} }
bool deque_empty(struct _deque* self) static bool deque_empty(struct _deque* self)
{ {
assert(self != NULL); assert(self != NULL);
return !self->size(self); return !self->size(self);
} }
void deque_destory(struct _deque* self) static void deque_destory(struct _deque* self)
{ {
assert(self != NULL); assert(self != NULL);
self->clear(self); self->clear(self);
@ -282,7 +282,7 @@ void deque_destory(struct _deque* self)
} }
} }
void deque_print(struct _deque* self) static void deque_print(struct _deque* self)
{ {
assert(self != NULL); assert(self != NULL);
@ -295,7 +295,7 @@ void deque_print(struct _deque* self)
} }
} }
bool deque_init(struct _deque* self, uint32_t obj_size) static bool deque_init(struct _deque* self, uint32_t obj_size)
{ {
// attribute // attribute
self->_obj_size = obj_size; self->_obj_size = obj_size;
@ -328,9 +328,19 @@ bool deque_init(struct _deque* self, uint32_t obj_size)
return true; return true;
} }
deque_t deque_new(void) deque_t deque_new(uint32_t obj_size)
{ {
return (struct _deque*)malloc(sizeof(struct _deque)); struct _deque* deque = NULL;
deque = (struct _deque*)malloc(sizeof(struct _deque));
if(deque != NULL)
{
if(deque_init(deque, obj_size) != true)
{
free(deque);
deque = NULL;
}
}
return deque;
} }
void deque_free(deque_t *deque) void deque_free(deque_t *deque)

View File

@ -27,7 +27,7 @@ static int parent(int i)
return (i-1) >> 1; return (i-1) >> 1;
} }
bool heap_peek(struct _heap* self, void* obj) static bool heap_peek(struct _heap* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -101,7 +101,7 @@ static void heap_fixed_up(struct _heap* self, int i)
} }
} }
bool heap_push(struct _heap* self, void* obj) static bool heap_push(struct _heap* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
if(self->size(self) > self->_capacity) if(self->size(self) > self->_capacity)
@ -174,7 +174,7 @@ static void heap_fixed_down(struct _heap* self, int i)
} }
} }
bool heap_pop(struct _heap* self, void* obj) static bool heap_pop(struct _heap* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
if(self->empty(self)) if(self->empty(self))
@ -192,32 +192,32 @@ bool heap_pop(struct _heap* self, void* obj)
return true; return true;
} }
void heap_setmin(struct _heap* self, bool min_flag) static void heap_setmin(struct _heap* self, bool min_flag)
{ {
assert(self != NULL); assert(self != NULL);
self->_min_flag = min_flag; self->_min_flag = min_flag;
} }
uint32_t heap_size(struct _heap* self) static uint32_t heap_size(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
return self->_size; return self->_size;
} }
bool heap_empty(struct _heap* self) static bool heap_empty(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
return self->size(self) == 0; return self->size(self) == 0;
} }
bool heap_clear(struct _heap* self) static bool heap_clear(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
self->_size = 0; self->_size = 0;
return true; return true;
} }
void heap_destory(struct _heap* self) static void heap_destory(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
self->clear(self); self->clear(self);
@ -227,7 +227,7 @@ void heap_destory(struct _heap* self)
} }
} }
void heap_print(struct _heap* self) static void heap_print(struct _heap* self)
{ {
assert(self != NULL); assert(self != NULL);
assert(self->obj != NULL); assert(self->obj != NULL);
@ -243,7 +243,7 @@ void heap_print(struct _heap* self)
} }
} }
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);
@ -271,9 +271,19 @@ bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
return true; return true;
} }
heap_t heap_new(void) heap_t heap_new2(uint32_t obj_size, uint32_t capacity)
{ {
return (struct _heap*)malloc(sizeof(struct _heap)); heap_t heap = NULL;
heap = (struct _heap*)malloc(sizeof(struct _heap));
if(heap != NULL)
{
if(heap_init2(heap, obj_size, capacity) != true)
{
free(heap);
heap = NULL;
}
}
return heap;
} }
void heap_free(heap_t* heap) void heap_free(heap_t* heap)

View File

@ -10,7 +10,7 @@
*/ */
#include "list.h" #include "list.h"
bool list_append(struct _list* self, void* obj) static bool list_append(struct _list* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(self->obj != NULL); assert(self->obj != NULL);
@ -35,7 +35,7 @@ bool list_append(struct _list* self, void* obj)
return true; return true;
} }
bool list_insert(struct _list* self, int index, void* obj) static bool list_insert(struct _list* self, int index, void* obj)
{ {
assert(index >= 0 && index < (int)self->size(self)); assert(index >= 0 && index < (int)self->size(self));
@ -58,7 +58,7 @@ bool list_insert(struct _list* self, int index, void* obj)
return true; return true;
} }
bool list_pop(struct _list* self, int index, void* obj) static bool list_pop(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -85,25 +85,25 @@ bool list_pop(struct _list* self, int index, void* obj)
return true; return true;
} }
int list_index(struct _list* self, void* obj) static int list_index(struct _list* self, void* obj)
{ {
return 0; return 0;
} }
bool list_remove(struct _list* self, void* obj) static bool list_remove(struct _list* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
return true; return true;
} }
bool list_clear(struct _list* self) static bool list_clear(struct _list* self)
{ {
assert(self != NULL); assert(self != NULL);
self->_size = 0; self->_size = 0;
return true; return true;
} }
bool list_get(struct _list* self, int index, void* obj) static bool list_get(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -118,7 +118,7 @@ bool list_get(struct _list* self, int index, void* obj)
return true; return true;
} }
bool list_set(struct _list* self, int index, void* obj) static bool list_set(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -131,29 +131,29 @@ bool list_set(struct _list* self, int index, void* obj)
return true; return true;
} }
uint32_t list_size(struct _list* self) static uint32_t list_size(struct _list* self)
{ {
return self->_size; return self->_size;
} }
bool list_empty(struct _list* self) static bool list_empty(struct _list* self)
{ {
assert(self != NULL); assert(self != NULL);
return !self->size(self); return !self->size(self);
} }
bool list_reverse(struct _list* self) static bool list_reverse(struct _list* self)
{ {
return true; return true;
} }
bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)) static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
{ {
return true; return true;
} }
// free // free
void list_destory(struct _list* self) static void list_destory(struct _list* self)
{ {
assert(self != NULL); assert(self != NULL);
if (self->obj != NULL) if (self->obj != NULL)
@ -163,7 +163,7 @@ void list_destory(struct _list* self)
} }
// print // print
void list_print(struct _list* self) static void list_print(struct _list* self)
{ {
assert(self != NULL); assert(self != NULL);
@ -179,18 +179,18 @@ void list_print(struct _list* self)
} }
} }
void* list_begin(struct _list* self) static void* list_begin(struct _list* self)
{ {
self->_cur = 0; self->_cur = 0;
return self->obj; return self->obj;
} }
void* list_end(struct _list* self) static void* list_end(struct _list* self)
{ {
return (char*)self->obj + self->_size * self->_obj_size; return (char*)self->obj + self->_size * self->_obj_size;
} }
void* list_next(struct _list* self) static void* list_next(struct _list* self)
{ {
void *obj = NULL; void *obj = NULL;
// if add this, can't go to end // if add this, can't go to end
@ -202,7 +202,7 @@ void* list_next(struct _list* self)
return obj; return obj;
} }
bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity) static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
{ {
assert(list != NULL); assert(list != NULL);
assert(obj_size > 0); assert(obj_size > 0);
@ -250,9 +250,19 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
return true; return true;
} }
list_t list_new(void) list_t list_new2(uint32_t obj_size, uint32_t capacity)
{ {
return (struct _list*)calloc(1, sizeof(struct _list)); struct _list* list = NULL;
list = (struct _list*)calloc(1, sizeof(struct _list));
if(list != NULL)
{
if(list_init2(list, obj_size, capacity) != true)
{
free(list);
list = NULL;
}
}
return list;
} }
void list_free(list_t* list) void list_free(list_t* list)

View File

@ -128,7 +128,7 @@ static struct _tree_node* tree_turn_right_then_left(struct _tree* self, struct _
return node; return node;
} }
int32_t tree_height(struct _tree* self, struct _tree_node* root) static int32_t tree_height(struct _tree* self, struct _tree_node* root)
{ {
#if 0 #if 0
assert(self != NULL); assert(self != NULL);
@ -384,7 +384,7 @@ struct _tree_node* tree_find_pos(struct _tree* self, void* obj)
} }
bool tree_avl_insert(struct _tree* self, void* obj) static bool tree_avl_insert(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -506,7 +506,7 @@ static bool tree_avl_delete_double_child(struct _tree* self, struct _tree_node*
return true; return true;
} }
bool tree_avl_delete(struct _tree* self, void* obj) static bool tree_avl_delete(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -538,7 +538,7 @@ bool tree_avl_delete(struct _tree* self, void* obj)
return true; return true;
} }
struct _tree_node* tree_find(struct _tree* self, void* obj) static struct _tree_node* tree_find(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _tree_node* root = self->_root; struct _tree_node* root = self->_root;
@ -560,7 +560,7 @@ struct _tree_node* tree_find(struct _tree* self, void* obj)
return NULL; return NULL;
} }
bool tree_clear(struct _tree* self) static bool tree_clear(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
if (self->_root == NULL) if (self->_root == NULL)
@ -592,20 +592,20 @@ bool tree_clear(struct _tree* self)
return true; return true;
} }
bool tree_empty(struct _tree* self) static bool tree_empty(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
return !self->size(self); return !self->size(self);
} }
uint32_t tree_size(struct _tree* self) static uint32_t tree_size(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
return self->_size; return self->_size;
} }
// free // free
void tree_destory(struct _tree* self) static void tree_destory(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
self->clear(self); self->clear(self);
@ -621,13 +621,13 @@ void tree_destory(struct _tree* self)
} }
} }
void tree_order(struct _tree* self, bool right_priority) static void tree_order(struct _tree* self, bool right_priority)
{ {
assert(self != NULL); assert(self != NULL);
self->_right_priority = right_priority; self->_right_priority = right_priority;
} }
void tree_preorder(struct _tree* self, struct _tree_node* root) static void tree_preorder(struct _tree* self, struct _tree_node* root)
{ {
#if 0 #if 0
assert(self != NULL); assert(self != NULL);
@ -710,7 +710,7 @@ void tree_preorder(struct _tree* self, struct _tree_node* root)
#endif #endif
} }
void tree_inorder(struct _tree* self, struct _tree_node* root) static void tree_inorder(struct _tree* self, struct _tree_node* root)
{ {
#if 0 #if 0
assert(self != NULL); assert(self != NULL);
@ -794,7 +794,7 @@ void tree_inorder(struct _tree* self, struct _tree_node* root)
#endif #endif
} }
void tree_postorder(struct _tree* self, struct _tree_node* root) static void tree_postorder(struct _tree* self, struct _tree_node* root)
{ {
#if 0 #if 0
assert(self != NULL); assert(self != NULL);
@ -889,7 +889,7 @@ void tree_postorder(struct _tree* self, struct _tree_node* root)
} }
// traversal breadth // traversal breadth
void tree_breadth(struct _tree* self, struct _tree_node* root) static void tree_breadth(struct _tree* self, struct _tree_node* root)
{ {
assert(self != NULL); assert(self != NULL);
if (root == NULL) if (root == NULL)
@ -992,7 +992,7 @@ static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* r
#endif #endif
} }
bool tree_min(struct _tree* self, void* obj) static bool tree_min(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _tree_node* node = tree_find_min(self, self->_root); struct _tree_node* node = tree_find_min(self, self->_root);
@ -1004,7 +1004,7 @@ bool tree_min(struct _tree* self, void* obj)
return true; return true;
} }
bool tree_max(struct _tree* self, void* obj) static bool tree_max(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
struct _tree_node* node = tree_find_max(self, self->_root); struct _tree_node* node = tree_find_max(self, self->_root);
@ -1018,13 +1018,13 @@ bool tree_max(struct _tree* self, void* obj)
rbt_color tree_color(struct _tree_node* node) static rbt_color tree_color(struct _tree_node* node)
{ {
assert(node != NULL); assert(node != NULL);
return node->color; return node->color;
} }
bool tree_set_color(struct _tree_node* node, rbt_color color) static bool tree_set_color(struct _tree_node* node, rbt_color color)
{ {
assert(node != NULL); assert(node != NULL);
node->color = color; node->color = color;
@ -1106,7 +1106,7 @@ static struct _tree_node* tree_rb_turn_right(struct _tree* self, struct _tree_no
return node; return node;
} }
bool tree_rb_insert(struct _tree* self, void* obj) static bool tree_rb_insert(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -1259,7 +1259,7 @@ static bool tree_rb_rebalance(struct _tree* self, struct _tree_node* node)
return true; return true;
} }
bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node) static bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
{ {
assert(self != NULL); assert(self != NULL);
struct _tree_node* father = NULL; struct _tree_node* father = NULL;
@ -1388,7 +1388,7 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
return true; return true;
} }
bool tree_rb_delete(struct _tree* self, void* obj) static bool tree_rb_delete(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
assert(obj != NULL); assert(obj != NULL);
@ -1463,13 +1463,13 @@ bool tree_rb_delete(struct _tree* self, void* obj)
return true; return true;
} }
void tree_set_order(struct _tree* self, enum _order order) static void tree_set_order(struct _tree* self, enum _order order)
{ {
assert(self != NULL); assert(self != NULL);
self->_order = order; self->_order = order;
} }
void* tree_begin(struct _tree* self) static void* tree_begin(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
switch (self->_order) switch (self->_order)
@ -1684,7 +1684,7 @@ void* tree_begin(struct _tree* self)
} }
} }
void* tree_next(struct _tree* self) static void* tree_next(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
switch (self->_order) switch (self->_order)
@ -1845,7 +1845,7 @@ void* tree_next(struct _tree* self)
} }
} }
void* tree_end(struct _tree* self) static void* tree_end(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
switch (self->_order) switch (self->_order)
@ -1888,9 +1888,7 @@ void* tree_end(struct _tree* self)
} }
} }
static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
bool tree_avl_init(struct _tree* self, uint32_t obj_size)
{ {
assert(self != NULL); assert(self != NULL);
@ -1949,7 +1947,7 @@ done:
return true; return true;
} }
bool tree_rb_init(struct _tree* self, uint32_t obj_size) static bool tree_rb_init(struct _tree* self, uint32_t obj_size)
{ {
assert(self != NULL); assert(self != NULL);
self->_obj_size = obj_size; self->_obj_size = obj_size;
@ -2006,9 +2004,33 @@ done:
return false; return false;
} }
tree_t tree_new(void) tree_t tree_avl_new(uint32_t obj_size)
{ {
return (struct _tree*)malloc(sizeof(struct _tree)); tree_t tree = NULL;
tree = (struct _tree*)malloc(sizeof(struct _tree));
if(tree != NULL)
{
if(tree_avl_init(tree, obj_size) != true)
{
free(tree);
tree = NULL;
}
}
return tree;
}
tree_t tree_rb_new(uint32_t obj_size)
{
tree_t tree = NULL;
tree = (struct _tree*)malloc(sizeof(struct _tree));
if(tree != NULL)
{
if(tree_rb_init(tree, obj_size) != true)
{
free(tree);
tree = NULL;
}
}
return tree;
} }
void tree_free(tree_t* tree) void tree_free(tree_t* tree)

View File

@ -17,10 +17,8 @@ static void test_deque_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(); deque_t deque = deque_new(sizeof(int));
TEST_ASSERT_NOT_NULL(deque); TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(int));
deque->print_obj = print_num; deque->print_obj = print_num;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -148,10 +146,8 @@ static void test_deque_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]);
deque_t deque = deque_new(); deque_t deque = deque_new(sizeof(struct _student));
TEST_ASSERT_NOT_NULL(deque); TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct; deque->print_obj = print_struct;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)

View File

@ -46,10 +46,8 @@ static void test_heap_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new(); heap_t heap = heap_new2(sizeof(int), 64);
TEST_ASSERT_NOT_NULL(heap); TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num; heap->print_obj = print_num;
heap->compare = compare_num; heap->compare = compare_num;
@ -95,10 +93,8 @@ static void test_heap_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]);
heap_t heap = heap_new(); heap_t heap = heap_new2(sizeof(struct _student), 64);
TEST_ASSERT_NOT_NULL(heap); TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct; heap->print_obj = print_struct;
heap->compare = compare_struct; heap->compare = compare_struct;

View File

@ -10,39 +10,20 @@
*/ */
#include "test.h" #include "test.h"
static void test_list_init2(void)
{
struct _list list;
// ------------------------------
#ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(&list, 0, 1));
TEST_ASSERT_FALSE(list_init2(&list, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
list.destory(&list);
}
static void test_list_new(void) static void test_list_new(void)
{ {
list_t list = NULL; list_t list = NULL;
list = list_new(); list = list_new2(sizeof(int), 1);
list_free(&list);
// ------------------------------
list = list_new();
TEST_ASSERT_NOT_NULL(list); TEST_ASSERT_NOT_NULL(list);
list_free(&list);
#ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(list, 0, 1));
TEST_ASSERT_FALSE(list_init2(list, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(list_init2(list, sizeof(int), 1));
list_free(&list);
TEST_ASSERT_NULL(list); TEST_ASSERT_NULL(list);
list_free(&list); // list_free(NULL); list_free(&list); // list_free(NULL);
TEST_ASSERT_NULL(list);
//
TEST_ASSERT_NULL(list_new2(0, 1));
TEST_ASSERT_NULL(list_new2(sizeof(int), 0));
} }
@ -56,8 +37,7 @@ static void test_list_append(void)
list_t list = NULL; list_t list = NULL;
// ------------------------------ // ------------------------------
list = list_new(); list = list_new2(sizeof(int), len);
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->empty(list)); TEST_ASSERT_TRUE(list->empty(list));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -73,8 +53,7 @@ static void test_list_append(void)
// ------------------------------ // ------------------------------
// if capacity is less than data len // if capacity is less than data len
list = list_new(); list = list_new2(sizeof(int), len - 2);
list_init2(list, sizeof(int), len - 2);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
TEST_ASSERT_TRUE(list->append(list, &data[i])); TEST_ASSERT_TRUE(list->append(list, &data[i]));
@ -94,8 +73,7 @@ static void test_list_pop(void)
list_t list = NULL; list_t list = NULL;
// ------------------------------ // ------------------------------
list = list_new(); list = list_new2(sizeof(int), len);
list_init2(list, sizeof(int), len);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
list->append(list, &data[i]); list->append(list, &data[i]);
@ -120,8 +98,7 @@ static void test_list_clear(void)
list_t list = NULL; list_t list = NULL;
// ------------------------------ // ------------------------------
list = list_new(); list = list_new2(sizeof(int), len);
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list)); TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -143,10 +120,9 @@ static void test_list_num(void)
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
list_t list = NULL; list_t list = NULL;
list = list_new(); list = list_new2(sizeof(int), 64);
TEST_ASSERT_TRUE(list != NULL); TEST_ASSERT_TRUE(list != NULL);
list_init2(list, sizeof(int), 64);
list->print_obj = print_num; list->print_obj = print_num;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -230,8 +206,7 @@ static void test_list_struct(void)
int index = 0; int index = 0;
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new(); list_t list = list_new2(sizeof(struct _student), 64);
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct; list->print_obj = print_struct;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
@ -322,8 +297,7 @@ static void test_list_iter(void)
list_t list = NULL; list_t list = NULL;
// ------------------------------ // ------------------------------
list = list_new(); list = list_new2(sizeof(int), len);
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list)); TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
{ {
@ -358,7 +332,6 @@ void test_list(void)
{ {
UnitySetTestFile(__FILE__); UnitySetTestFile(__FILE__);
RUN_TEST(test_list_init2);
RUN_TEST(test_list_new); RUN_TEST(test_list_new);
RUN_TEST(test_list_append); RUN_TEST(test_list_append);
RUN_TEST(test_list_pop); RUN_TEST(test_list_pop);

View File

@ -39,8 +39,7 @@ void test_avltree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new, ; tree_t tree = tree_avl_new(sizeof(int));
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -211,8 +210,7 @@ void test_rbtree_num(void)
int temp = 0; int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]); uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_new, ; tree_t tree = tree_rb_new(sizeof(int));
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -343,8 +341,7 @@ void test_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]);
tree_t tree = tree_new, ; tree_t tree = tree_rb_new(sizeof(struct _student));
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct; tree->print_obj = print_struct;
tree->compare = compare_struct; tree->compare = compare_struct;
@ -526,10 +523,8 @@ static void test_avltree_iter(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -605,10 +600,9 @@ static void test_avltree_insert(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -652,10 +646,9 @@ static void test_avltree_delete(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_avl_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -753,10 +746,9 @@ static void test_rbtree_iter(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -832,10 +824,9 @@ static void test_rbtree_insert(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;
@ -879,10 +870,9 @@ static void test_rbtree_delete(void)
int * iter = NULL; int * iter = NULL;
int count = 0; int count = 0;
tree_t tree = tree_new(); tree_t tree = tree_rb_new(sizeof(int));
TEST_ASSERT_NOT_NULL(tree); TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num; tree->print_obj = print_num;
tree->compare = compare_num; tree->compare = compare_num;