mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
Compare commits
17 Commits
0d6ba14451
...
0369d58147
Author | SHA1 | Date | |
---|---|---|---|
0369d58147 | |||
02b09e729d | |||
bd9f07ae65 | |||
5d4a461543 | |||
b3d7c96484 | |||
aac984ee08 | |||
07bb44a4d8 | |||
a249cae244 | |||
8f5540743f | |||
4fa56e1ed8 | |||
21485efb51 | |||
cc357caf6a | |||
86855420e2 | |||
b396d80672 | |||
07b2c142b8 | |||
d805d56fc9 | |||
0768e026dc |
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -25,6 +25,7 @@
|
|||||||
"stdarg.h": "c",
|
"stdarg.h": "c",
|
||||||
"graph.h": "c",
|
"graph.h": "c",
|
||||||
"unicstl_config.h": "c",
|
"unicstl_config.h": "c",
|
||||||
"iter.h": "c"
|
"iter.h": "c",
|
||||||
|
"iterator.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
55
README.md
55
README.md
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
> 标准:--std=c99
|
> 标准:--std=c99
|
||||||
|
|
||||||
[数据结构详细说明](http://wenjianfeng.top)
|
[数据结构详细说明](https://blog.wenjianfeng.top)
|
||||||
|
|
||||||
## 数据结构
|
## 数据结构
|
||||||
|数据结构 | 原理 |说明 |
|
|数据结构 | 原理 |说明 |
|
||||||
@ -45,7 +45,52 @@
|
|||||||
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
||||||
|
|
||||||
|
|
||||||
## 版本
|
## 规范
|
||||||
| 版本 | 说明 |
|
### 版本说明
|
||||||
|:----:|:----:|
|
|
||||||
| 0.xx.xx | 测试版本 |
|
| 版本 | 范围 | 更新说明 |
|
||||||
|
|:----:|:----:|:----:|
|
||||||
|
| 主版本号 | 0 ~ 99 | 代码框架大改,完全不兼容旧版 |
|
||||||
|
| 次版本号 | 0 ~ 99 | 代码框架尽量兼容旧版,增信大功能、修复重大bug等 |
|
||||||
|
| 小版本号 | 0 ~ 99 | 代码框架兼容旧版,新增小功能、修复bug等 |
|
||||||
|
|
||||||
|
举例说明:
|
||||||
|
```c
|
||||||
|
// 若 major > 0 ,则代表正式发布版本
|
||||||
|
#define VER_MAJOR 1
|
||||||
|
#define VER_MINOR 2
|
||||||
|
#define VER_MICRO 5
|
||||||
|
```
|
||||||
|
|
||||||
|
### 工程命名
|
||||||
|
`工程名(_功能)_v版本号_日期(_时间)(-其他信息)`
|
||||||
|
> 括号内表示可选项
|
||||||
|
|
||||||
|
| 其他信息后缀 | 说明 | 详细
|
||||||
|
|:----:|:----:|----
|
||||||
|
| aX | alpha内测版 | 可能仅测试了新增功能,但没有测试新增功能对其他模块的影响
|
||||||
|
| bX | beta公测版 | 不仅测试了新增功能,也测试了其他模块,尽量保证新增的功能不影响原来的旧功能。
|
||||||
|
| ... | | 或者添加其他有有效信息
|
||||||
|
|
||||||
|
举例说明:
|
||||||
|
```shell
|
||||||
|
unicstl_stack_v1.2.5_20240717-a0.zip
|
||||||
|
|
||||||
|
# 带a或者b后缀,表示当前版本发布前的测试版。如果发布后,则直接更新版本号了
|
||||||
|
```
|
||||||
|
|
||||||
|
## 修改日志
|
||||||
|
|
||||||
|
### Unicstl 0.0.01 (2025-04-24)
|
||||||
|
new features:
|
||||||
|
- add stack
|
||||||
|
- add queue
|
||||||
|
- add deque
|
||||||
|
- add list
|
||||||
|
- add heap
|
||||||
|
- add tree
|
||||||
|
- add graph
|
||||||
|
bugfixed:
|
||||||
|
- none
|
||||||
|
others:
|
||||||
|
- none
|
||||||
|
@ -20,14 +20,10 @@ 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_new2(sizeof(int), 64);
|
heap_t heap = heap_min_new2(sizeof(int), 64);
|
||||||
heap->print_obj = print_num;
|
heap->print_obj = print_num;
|
||||||
heap->compare = compare_num;
|
heap->compare = compare_num;
|
||||||
|
|
||||||
// default: maxheap
|
|
||||||
// maxheap or minheap
|
|
||||||
heap->setmin(heap, true);
|
|
||||||
|
|
||||||
printf("\n\n----- demo_heap_num -----\n");
|
printf("\n\n----- demo_heap_num -----\n");
|
||||||
|
|
||||||
printf("----- push -----\n");
|
printf("----- push -----\n");
|
||||||
@ -90,7 +86,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_new2(sizeof(struct _student), 64);
|
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
|
||||||
heap->print_obj = print_struct;
|
heap->print_obj = print_struct;
|
||||||
heap->compare = compare_struct;
|
heap->compare = compare_struct;
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ static void demo_stack_struct(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stack->destory(stack);
|
stack->_destory(stack);
|
||||||
stack_free(&stack);
|
stack_free(&stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,9 +11,11 @@
|
|||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
// #ifdef UNICSTL_CONFIG
|
#define UNICSTL_CONFIG
|
||||||
|
|
||||||
|
#ifdef UNICSTL_CONFIG
|
||||||
#include "unicstl_config.h"
|
#include "unicstl_config.h"
|
||||||
// #endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -23,4 +25,18 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef UNICSTL_ITERATOR
|
||||||
|
#include "iterator.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief obj compare with obj2
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* obj < obj2 return -1
|
||||||
|
* obj == obj2 return 0
|
||||||
|
* obj > obj2 return 1
|
||||||
|
*/
|
||||||
|
typedef int (*cmp_fun_t)(void* obj, void* obj2);
|
||||||
|
|
||||||
#endif // _COMMON_H_
|
#endif // _COMMON_H_
|
||||||
|
@ -22,6 +22,7 @@ struct _deque_node
|
|||||||
|
|
||||||
struct _deque
|
struct _deque
|
||||||
{
|
{
|
||||||
|
// -------------------- private --------------------
|
||||||
struct _deque_node* _head;
|
struct _deque_node* _head;
|
||||||
struct _deque_node* _tail;
|
struct _deque_node* _tail;
|
||||||
|
|
||||||
@ -30,6 +31,11 @@ struct _deque
|
|||||||
// uint32_t _capacity;
|
// uint32_t _capacity;
|
||||||
// uint32_t _ratio;
|
// uint32_t _ratio;
|
||||||
|
|
||||||
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*_destory)(struct _deque* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*push_back)(struct _deque* self, void* obj);
|
bool (*push_back)(struct _deque* self, void* obj);
|
||||||
bool (*push_front)(struct _deque* self, void* obj);
|
bool (*push_front)(struct _deque* self, void* obj);
|
||||||
@ -37,30 +43,30 @@ struct _deque
|
|||||||
bool (*pop_front)(struct _deque* self, void* obj);
|
bool (*pop_front)(struct _deque* self, void* obj);
|
||||||
bool (*back)(struct _deque* self, void* obj);
|
bool (*back)(struct _deque* self, void* obj);
|
||||||
bool (*front)(struct _deque* self, void* obj);
|
bool (*front)(struct _deque* self, void* obj);
|
||||||
|
bool (*empty)(struct _deque* self);
|
||||||
|
|
||||||
|
// base
|
||||||
|
uint32_t(*size)(struct _deque* self);
|
||||||
|
bool (*clear)(struct _deque* self);
|
||||||
|
|
||||||
|
// iter
|
||||||
|
iterator_t (*iter)(struct _deque* self);
|
||||||
|
|
||||||
|
// ohters
|
||||||
bool (*insert)(struct _deque* self, int index, void* obj);
|
bool (*insert)(struct _deque* self, int index, void* obj);
|
||||||
bool (*erase)(struct _deque* self, int index, void* obj);
|
bool (*erase)(struct _deque* self, int index, void* obj);
|
||||||
|
|
||||||
int (*index)(struct _deque* self, void* obj);
|
int (*index)(struct _deque* self, void* obj);
|
||||||
bool (*remove)(struct _deque* self, void* obj);
|
bool (*remove)(struct _deque* self, void* obj);
|
||||||
|
|
||||||
bool (*clear)(struct _deque* self);
|
|
||||||
|
|
||||||
bool (*get)(struct _deque* self, int index, void* obj);
|
bool (*get)(struct _deque* self, int index, void* obj);
|
||||||
bool (*set)(struct _deque* self, int index, void* obj);
|
bool (*set)(struct _deque* self, int index, void* obj);
|
||||||
|
|
||||||
// size
|
|
||||||
uint32_t(*size)(struct _deque* self);
|
|
||||||
bool (*empty)(struct _deque* self);
|
|
||||||
|
|
||||||
// free
|
|
||||||
void (*destory)(struct _deque* self);
|
|
||||||
|
|
||||||
// compare
|
// compare
|
||||||
int (*cmp)(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);
|
||||||
|
|
||||||
// print
|
// -------------------- debug --------------------
|
||||||
void (*print)(struct _deque* self);
|
void (*print)(struct _deque* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
@ -13,28 +13,36 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
HEAP_MIN = 0,
|
||||||
|
HEAP_MAX = 1,
|
||||||
|
}heap_type;
|
||||||
|
|
||||||
struct _heap
|
struct _heap
|
||||||
{
|
{
|
||||||
void * obj;
|
// -------------------- private --------------------
|
||||||
|
void* obj;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
uint32_t _obj_size;
|
uint32_t _obj_size;
|
||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
bool _min_flag;
|
heap_type _type;
|
||||||
|
|
||||||
|
void (*_destory)(struct _heap* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*peek)(struct _heap* self, void* obj);
|
bool (*peek)(struct _heap* self, void* obj);
|
||||||
bool (*push)(struct _heap* self, void* obj);
|
bool (*push)(struct _heap* self, void* obj);
|
||||||
bool (*pop)(struct _heap* self, void* obj);
|
bool (*pop)(struct _heap* self, void* obj);
|
||||||
|
bool (*empty)(struct _heap* self);
|
||||||
// default: max heap
|
|
||||||
void (*setmin)(struct _heap* self, bool min_flag);
|
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t(*size)(struct _heap* self);
|
uint32_t(*size)(struct _heap* self);
|
||||||
bool (*empty)(struct _heap* self);
|
bool (*clear)(struct _heap* self);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief obj compare with obj2
|
* @brief obj compare with obj2
|
||||||
@ -46,18 +54,15 @@ struct _heap
|
|||||||
*/
|
*/
|
||||||
int (*compare)(void* obj, void* obj2);
|
int (*compare)(void* obj, void* obj2);
|
||||||
|
|
||||||
// others
|
// -------------------- debug --------------------
|
||||||
bool (*clear)(struct _heap* self);
|
|
||||||
void (*destory)(struct _heap* self);
|
|
||||||
|
|
||||||
// print
|
|
||||||
void (*print)(struct _heap* self);
|
void (*print)(struct _heap* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
typedef struct _heap* heap_t;
|
typedef struct _heap* heap_t;
|
||||||
|
|
||||||
// create and free heap
|
// create and free heap
|
||||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity);
|
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity);
|
||||||
|
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity);
|
||||||
|
|
||||||
void heap_free(heap_t* heap);
|
void heap_free(heap_t* heap);
|
||||||
|
|
||||||
|
@ -15,17 +15,14 @@
|
|||||||
|
|
||||||
struct _iterator
|
struct _iterator
|
||||||
{
|
{
|
||||||
void* obj;
|
// ---------- private ----------
|
||||||
void* self;
|
void* _parent;
|
||||||
void* parent;
|
void* _cur_node;
|
||||||
|
uint32_t _cur;
|
||||||
void* (*begin)(struct _iterator* self);
|
|
||||||
void* (*next)(struct _iterator* self);
|
|
||||||
void* (*end)(struct _iterator* self);
|
|
||||||
|
|
||||||
|
// ---------- public ----------
|
||||||
bool (*hasnext)(struct _iterator* self);
|
bool (*hasnext)(struct _iterator* self);
|
||||||
|
const void* (*next)(struct _iterator* self);
|
||||||
void* (*data)(struct _iterator* self);
|
|
||||||
};
|
};
|
||||||
typedef struct _iterator* iterator_t;
|
typedef struct _iterator* iterator_t;
|
||||||
|
|
@ -12,7 +12,7 @@
|
|||||||
#define _LIST_H_
|
#define _LIST_H_
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "iter.h"
|
#include "iterator.h"
|
||||||
|
|
||||||
struct _list
|
struct _list
|
||||||
{
|
{
|
||||||
@ -25,7 +25,6 @@ struct _list
|
|||||||
uint32_t _cur;
|
uint32_t _cur;
|
||||||
|
|
||||||
struct _iterator _iter;
|
struct _iterator _iter;
|
||||||
iterator_t (*iter)(struct _list* self);
|
|
||||||
|
|
||||||
// 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.
|
||||||
@ -39,9 +38,7 @@ struct _list
|
|||||||
bool (*set)(struct _list* self, int index, void* obj);
|
bool (*set)(struct _list* self, int index, void* obj);
|
||||||
|
|
||||||
// iter
|
// iter
|
||||||
void* (*begin)(struct _list* self);
|
iterator_t (*iter)(struct _list* self);
|
||||||
void* (*next)(struct _list* self);
|
|
||||||
void* (*end)(struct _list* self);
|
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t(*size)(struct _list* self);
|
uint32_t(*size)(struct _list* self);
|
||||||
|
@ -21,6 +21,7 @@ struct _queue_node
|
|||||||
|
|
||||||
struct _queue
|
struct _queue
|
||||||
{
|
{
|
||||||
|
// -------------------- private --------------------
|
||||||
struct _queue_node * _front;
|
struct _queue_node * _front;
|
||||||
struct _queue_node * _back;
|
struct _queue_node * _back;
|
||||||
|
|
||||||
@ -32,24 +33,28 @@ struct _queue
|
|||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*_destory)(struct _queue* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*push)(struct _queue* self, void* obj);
|
bool (*push)(struct _queue* self, void* obj);
|
||||||
bool (*pop)(struct _queue* self, void* obj);
|
bool (*pop)(struct _queue* self, void* obj);
|
||||||
|
|
||||||
bool (*back)(struct _queue* self, void* obj);
|
bool (*back)(struct _queue* self, void* obj);
|
||||||
bool (*front)(struct _queue* self, void* obj);
|
bool (*front)(struct _queue* self, void* obj);
|
||||||
|
|
||||||
// base
|
|
||||||
bool (*empty)(struct _queue* self);
|
bool (*empty)(struct _queue* self);
|
||||||
bool (*full)(struct _queue* self);
|
bool (*full)(struct _queue* self);
|
||||||
|
|
||||||
|
// base
|
||||||
uint32_t (*size)(struct _queue* self);
|
uint32_t (*size)(struct _queue* self);
|
||||||
uint32_t (*capacity)(struct _queue* self);
|
uint32_t (*capacity)(struct _queue* self);
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
bool (*clear)(struct _queue* self);
|
bool (*clear)(struct _queue* self);
|
||||||
void (*destory)(struct _queue* self);
|
|
||||||
|
|
||||||
// print
|
// iter
|
||||||
|
iterator_t (*iter)(struct _queue* self);
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
void (*print)(struct _queue* self);
|
void (*print)(struct _queue* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
@ -21,6 +21,12 @@ struct _stack_node
|
|||||||
|
|
||||||
struct _stack
|
struct _stack
|
||||||
{
|
{
|
||||||
|
// -------------------- private --------------------
|
||||||
|
/**
|
||||||
|
* @brief head pointer of stack
|
||||||
|
* 1. linklist: head->next is valid, head->obj is NULL
|
||||||
|
* 2. array: head->obj is valid, head->next is NULL,
|
||||||
|
*/
|
||||||
struct _stack_node * _head;
|
struct _stack_node * _head;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
@ -28,21 +34,26 @@ struct _stack
|
|||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
|
struct _iterator _iter;
|
||||||
|
|
||||||
|
void (*_destory)(struct _stack* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
// kernel
|
// kernel
|
||||||
bool (*push)(struct _stack* self, void* obj);
|
bool (*push)(struct _stack* self, void* obj);
|
||||||
bool (*pop)(struct _stack* self, void* obj);
|
bool (*pop)(struct _stack* self, void* obj);
|
||||||
bool (*peek)(struct _stack* self, void* obj);
|
bool (*peek)(struct _stack* self, void* obj);
|
||||||
|
bool (*empty)(struct _stack* self);
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t (*size)(struct _stack* self);
|
uint32_t (*size)(struct _stack* self);
|
||||||
bool (*empty)(struct _stack* self);
|
|
||||||
uint32_t (*capacity)(struct _stack* self);
|
uint32_t (*capacity)(struct _stack* self);
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
bool (*clear)(struct _stack* self);
|
bool (*clear)(struct _stack* self);
|
||||||
void (*destory)(struct _stack* self);
|
|
||||||
|
|
||||||
// print
|
// iter
|
||||||
|
iterator_t (*iter)(struct _stack* self);
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
void (*print)(struct _stack* self);
|
void (*print)(struct _stack* self);
|
||||||
void (*print_obj)(void* obj);
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
|
@ -21,13 +21,24 @@
|
|||||||
#define UNICSTL_TREE
|
#define UNICSTL_TREE
|
||||||
#define UNICSTL_HEAP
|
#define UNICSTL_HEAP
|
||||||
#define UNICSTL_GRAPH
|
#define UNICSTL_GRAPH
|
||||||
|
#define UNICSTL_ITERATOR
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief debug
|
* @brief debug
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#define NDEBUG
|
#define NDEBUG // assert disable
|
||||||
|
|
||||||
#define UNICSTL_DEBUG
|
#define UNICSTL_DEBUG
|
||||||
|
|
||||||
|
#ifdef UNICSTL_DEBUG
|
||||||
|
#define UNICSTL_DEBUG_STACK
|
||||||
|
#define UNICSTL_DEBUG_QUEUE
|
||||||
|
#define UNICSTL_DEBUG_DEQUE
|
||||||
|
#define UNICSTL_DEBUG_TREE
|
||||||
|
#define UNICSTL_DEBUG_HEAP
|
||||||
|
#define UNICSTL_DEBUG_GRAPH
|
||||||
|
// #define UNICSTL_DEBUG_ITERATOR
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
120
src/deque.c
120
src/deque.c
@ -295,36 +295,100 @@ static void deque_print(struct _deque* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iterator_t deque_iter(struct _deque* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
iterator_t iter = &self->_iter;
|
||||||
|
|
||||||
|
iter->_parent = self;
|
||||||
|
iter->_cur = 0;
|
||||||
|
iter->_cur_node = self->_head;
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deque_iter_hasnext(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
deque_t self = (deque_t)iter->_parent;
|
||||||
|
if(iter->_cur < self->size(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const void* deque_iter_next(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
deque_t self = (deque_t)iter->_parent;
|
||||||
|
void *obj = NULL;
|
||||||
|
|
||||||
|
// base on linklist
|
||||||
|
struct _deque_node * node = (struct _deque_node *)iter->_cur_node;
|
||||||
|
if(node != NULL)
|
||||||
|
{
|
||||||
|
obj = node->obj;
|
||||||
|
iter->_cur_node = node->next;
|
||||||
|
}
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
static bool deque_init(struct _deque* self, uint32_t obj_size)
|
static bool deque_init(struct _deque* self, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
// attribute
|
assert(self != NULL);
|
||||||
|
if(obj_size == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// -------------------- private --------------------
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
// self->_capacity = 64;
|
// self->_capacity = 64;
|
||||||
// self->_ratio = 2;
|
// self->_ratio = 2;
|
||||||
|
|
||||||
// function
|
|
||||||
self->back = deque_back;
|
|
||||||
self->clear = deque_clear;
|
|
||||||
self->destory = deque_destory;
|
|
||||||
self->empty = deque_empty;
|
|
||||||
self->erase = deque_erase;
|
|
||||||
self->front = deque_front;
|
|
||||||
self->get = deque_get;
|
|
||||||
self->index = deque_index;
|
|
||||||
self->insert = deque_insert;
|
|
||||||
self->pop_back = deque_pop_back;
|
|
||||||
self->pop_front = deque_pop_front;
|
|
||||||
self->push_back = deque_push_back;
|
|
||||||
self->push_front = deque_push_front;
|
|
||||||
self->print = deque_print;
|
|
||||||
self->remove = deque_remove;
|
|
||||||
self->set = deque_set;
|
|
||||||
self->size = deque_size;
|
|
||||||
|
|
||||||
self->_head = NULL;
|
self->_head = NULL;
|
||||||
self->_tail = NULL;
|
self->_tail = NULL;
|
||||||
|
|
||||||
|
self->_iter.hasnext = deque_iter_hasnext;
|
||||||
|
self->_iter.next = deque_iter_next;
|
||||||
|
|
||||||
|
self->_destory = deque_destory;
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
self->push_back = deque_push_back;
|
||||||
|
self->push_front = deque_push_front;
|
||||||
|
self->pop_back = deque_pop_back;
|
||||||
|
self->pop_front = deque_pop_front;
|
||||||
|
self->back = deque_back;
|
||||||
|
self->front = deque_front;
|
||||||
|
self->empty = deque_empty;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->clear = deque_clear;
|
||||||
|
self->size = deque_size;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = deque_iter;
|
||||||
|
|
||||||
|
// others
|
||||||
|
self->insert = deque_insert;
|
||||||
|
self->erase = deque_erase;
|
||||||
|
|
||||||
|
self->index = deque_index;
|
||||||
|
self->remove = deque_remove;
|
||||||
|
|
||||||
|
self->set = deque_set;
|
||||||
|
self->get = deque_get;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print = deque_print;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,13 +396,15 @@ deque_t deque_new(uint32_t obj_size)
|
|||||||
{
|
{
|
||||||
struct _deque* deque = NULL;
|
struct _deque* deque = NULL;
|
||||||
deque = (struct _deque*)malloc(sizeof(struct _deque));
|
deque = (struct _deque*)malloc(sizeof(struct _deque));
|
||||||
if(deque != NULL)
|
if(deque == NULL)
|
||||||
{
|
{
|
||||||
if(deque_init(deque, obj_size) != true)
|
return NULL;
|
||||||
{
|
}
|
||||||
free(deque);
|
|
||||||
deque = NULL;
|
if(deque_init(deque, obj_size) != true)
|
||||||
}
|
{
|
||||||
|
free(deque);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return deque;
|
return deque;
|
||||||
}
|
}
|
||||||
@ -347,7 +413,7 @@ void deque_free(deque_t *deque)
|
|||||||
{
|
{
|
||||||
if(*deque != NULL)
|
if(*deque != NULL)
|
||||||
{
|
{
|
||||||
(*deque)->destory(*deque);
|
(*deque)->_destory(*deque);
|
||||||
free(*deque);
|
free(*deque);
|
||||||
}
|
}
|
||||||
*deque = NULL;
|
*deque = NULL;
|
||||||
|
81
src/heap.c
81
src/heap.c
@ -71,7 +71,7 @@ static void heap_fixed_up(struct _heap* self, int i)
|
|||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
int p = 0;
|
int p = 0;
|
||||||
if(self->_min_flag != true)
|
if(self->_type == HEAP_MAX)
|
||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
@ -85,7 +85,7 @@ static void heap_fixed_up(struct _heap* self, int i)
|
|||||||
i = p;
|
i = p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else /* if(self->_type == HEAP_MIN) */
|
||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
@ -122,7 +122,7 @@ 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->_min_flag != true)
|
if(self->_type == HEAP_MAX)
|
||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
@ -147,7 +147,7 @@ static void heap_fixed_down(struct _heap* self, int i)
|
|||||||
i = max;
|
i = max;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else /* if(self->_type == HEAP_MIN) */
|
||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
@ -192,12 +192,6 @@ static bool heap_pop(struct _heap* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heap_setmin(struct _heap* self, bool min_flag)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
self->_min_flag = min_flag;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t heap_size(struct _heap* self)
|
static uint32_t heap_size(struct _heap* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -247,42 +241,71 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
|
|||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
|
||||||
// 1. set attr
|
// -------------------- private --------------------
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_capacity = capacity;
|
self->_capacity = capacity;
|
||||||
self->_ratio = 2;
|
self->_ratio = 2;
|
||||||
|
|
||||||
self->peek = heap_peek;
|
|
||||||
self->push = heap_push;
|
|
||||||
self->pop = heap_pop;
|
|
||||||
self->size = heap_size;
|
|
||||||
self->empty = heap_empty;
|
|
||||||
self->clear = heap_clear;
|
|
||||||
self->destory = heap_destory;
|
|
||||||
self->setmin = heap_setmin;
|
|
||||||
self->print = heap_print;
|
|
||||||
|
|
||||||
self->obj = (void*)malloc(self->_capacity * self->_obj_size);
|
self->obj = (void*)malloc(self->_capacity * self->_obj_size);
|
||||||
if(self->obj == NULL)
|
if(self->obj == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self->_destory = heap_destory;
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
self->peek = heap_peek;
|
||||||
|
self->push = heap_push;
|
||||||
|
self->pop = heap_pop;
|
||||||
|
self->empty = heap_empty;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = heap_size;
|
||||||
|
self->clear = heap_clear;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print = heap_print;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_t heap_new2(uint32_t obj_size, uint32_t capacity)
|
heap_t heap_max_new2(uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
heap_t heap = NULL;
|
heap_t heap = NULL;
|
||||||
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
||||||
if(heap != NULL)
|
if(heap == NULL)
|
||||||
{
|
{
|
||||||
if(heap_init2(heap, obj_size, capacity) != true)
|
return NULL;
|
||||||
{
|
|
||||||
free(heap);
|
|
||||||
heap = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(heap_init2(heap, obj_size, capacity) != true)
|
||||||
|
{
|
||||||
|
free(heap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->_type = HEAP_MAX;
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap_t heap_min_new2(uint32_t obj_size, uint32_t capacity)
|
||||||
|
{
|
||||||
|
heap_t heap = NULL;
|
||||||
|
heap = (struct _heap*)malloc(sizeof(struct _heap));
|
||||||
|
if(heap == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(heap_init2(heap, obj_size, capacity) != true)
|
||||||
|
{
|
||||||
|
free(heap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->_type = HEAP_MIN;
|
||||||
return heap;
|
return heap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,7 +313,7 @@ void heap_free(heap_t* heap)
|
|||||||
{
|
{
|
||||||
if(*heap != NULL)
|
if(*heap != NULL)
|
||||||
{
|
{
|
||||||
(*heap)->destory(*heap);
|
(*heap)->_destory(*heap);
|
||||||
free(*heap);
|
free(*heap);
|
||||||
}
|
}
|
||||||
*heap = NULL;
|
*heap = NULL;
|
||||||
|
123
src/list.c
123
src/list.c
@ -179,29 +179,32 @@ static void list_print(struct _list* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* list_begin(struct _list* self)
|
static const void* list_iter_next(struct _iterator* iter)
|
||||||
{
|
{
|
||||||
self->_cur = 0;
|
list_t self = (list_t)iter->_parent;
|
||||||
return self->obj;
|
void *obj = self->obj + self->_iter._cur * self->_obj_size;
|
||||||
}
|
self->_iter._cur += 1;
|
||||||
|
|
||||||
static void* list_end(struct _list* self)
|
|
||||||
{
|
|
||||||
return (char*)self->obj + self->_size * self->_obj_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void* list_next(struct _list* self)
|
|
||||||
{
|
|
||||||
void *obj = NULL;
|
|
||||||
// if add this, can't go to end
|
|
||||||
// if(self->_cur < self->_size - 1)
|
|
||||||
{
|
|
||||||
self->_cur += 1;
|
|
||||||
}
|
|
||||||
obj = (char*)self->obj + self->_cur * self->_obj_size;
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool list_iter_hasnext(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
list_t self = (list_t)iter->_parent;
|
||||||
|
|
||||||
|
if(self->_iter._cur < self->size(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t list_iter(struct _list* self)
|
||||||
|
{
|
||||||
|
self->_iter._parent = self;
|
||||||
|
self->_iter._cur = 0;
|
||||||
|
return &self->_iter;
|
||||||
|
}
|
||||||
|
|
||||||
static 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);
|
||||||
@ -236,9 +239,10 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
|||||||
list->size = list_size;
|
list->size = list_size;
|
||||||
list->sort = list_sort;
|
list->sort = list_sort;
|
||||||
|
|
||||||
list->begin = list_begin;
|
// iterator
|
||||||
list->next = list_next;
|
list->iter = list_iter;
|
||||||
list->end = list_end;
|
list->_iter.next = list_iter_next;
|
||||||
|
list->_iter.hasnext = list_iter_hasnext;
|
||||||
|
|
||||||
// 3. set array
|
// 3. set array
|
||||||
// list->obj = (void*)calloc(list->_capacity, list->_obj_size);
|
// list->obj = (void*)calloc(list->_capacity, list->_obj_size);
|
||||||
@ -250,81 +254,20 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator_t list_iter(struct _list* self)
|
|
||||||
{
|
|
||||||
self->_cur = 0;
|
|
||||||
self->_iter.obj = self->obj;
|
|
||||||
return &self->_iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void* list_iter_begin(struct _iterator* iter)
|
|
||||||
{
|
|
||||||
list_t self = (list_t)iter->parent;
|
|
||||||
self->_cur = 0;
|
|
||||||
iter->obj = self->obj;
|
|
||||||
return iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void* list_iter_end(struct _iterator* iter)
|
|
||||||
{
|
|
||||||
list_t self = (list_t)iter->parent;
|
|
||||||
iter->obj = (char*)self->obj + self->_size * self->_obj_size;
|
|
||||||
return iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void* list_iter_next(struct _iterator* iter)
|
|
||||||
{
|
|
||||||
list_t self = (list_t)iter->parent;
|
|
||||||
|
|
||||||
// if add this, can't go to end
|
|
||||||
// if(self->_cur < self->_size - 1)
|
|
||||||
{
|
|
||||||
self->_cur += 1;
|
|
||||||
}
|
|
||||||
iter->obj = (char*)self->obj + self->_cur * self->_obj_size;
|
|
||||||
return iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool list_iter_hasnext(struct _iterator* iter)
|
|
||||||
{
|
|
||||||
list_t self = (list_t)iter->parent;
|
|
||||||
|
|
||||||
if(self->_cur < self->_size)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void* list_iter_data(struct _iterator* iter)
|
|
||||||
{
|
|
||||||
return iter->obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
list_t list_new2(uint32_t obj_size, uint32_t capacity)
|
list_t list_new2(uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
struct _list* list = NULL;
|
struct _list* list = NULL;
|
||||||
list = (struct _list*)calloc(1, sizeof(struct _list));
|
list = (struct _list*)calloc(1, sizeof(struct _list));
|
||||||
if(list != NULL)
|
if(list == NULL)
|
||||||
{
|
{
|
||||||
if(list_init2(list, obj_size, capacity) != true)
|
return NULL;
|
||||||
{
|
|
||||||
free(list);
|
|
||||||
// list = NULL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list->_iter.self = &list->iter;
|
if(list_init2(list, obj_size, capacity) != true)
|
||||||
list->_iter.parent = list;
|
{
|
||||||
|
free(list);
|
||||||
list->_iter.begin = list_iter_begin;
|
return NULL;
|
||||||
list->_iter.next = list_iter_next;
|
}
|
||||||
list->_iter.end = list_iter_end;
|
|
||||||
list->_iter.data = list_iter_data;
|
|
||||||
list->_iter.hasnext = list_iter_hasnext;
|
|
||||||
|
|
||||||
list->iter = list_iter;
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
194
src/queue.c
194
src/queue.c
@ -306,6 +306,65 @@ static void queue2_print(struct _queue* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static iterator_t queue_iter(struct _queue* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
iterator_t iter = &self->_iter;
|
||||||
|
|
||||||
|
iter->_parent = self;
|
||||||
|
iter->_cur = 0;
|
||||||
|
iter->_cur_node = self->_front;
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool queue_iter_hasnext(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
queue_t self = (queue_t)iter->_parent;
|
||||||
|
if(iter->_cur < self->size(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const void* queue_iter_next(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
queue_t self = (queue_t)iter->_parent;
|
||||||
|
void *obj = NULL;
|
||||||
|
|
||||||
|
// base on linklist
|
||||||
|
struct _queue_node * node = (struct _queue_node *)iter->_cur_node;
|
||||||
|
if(node != NULL)
|
||||||
|
{
|
||||||
|
obj = node->obj;
|
||||||
|
iter->_cur_node = node->next;
|
||||||
|
}
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const void* queue2_iter_next(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
queue_t self = (queue_t)iter->_parent;
|
||||||
|
void *obj = NULL;
|
||||||
|
|
||||||
|
// base on array
|
||||||
|
uint32_t index = self->_iter._cur;
|
||||||
|
obj = self->_front->obj + self->_obj_size * index;
|
||||||
|
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
static bool queue_init(struct _queue * self, uint32_t obj_size)
|
static bool queue_init(struct _queue * self, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -315,32 +374,43 @@ static bool queue_init(struct _queue * self, uint32_t obj_size)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attribute init
|
// -------------------- private --------------------
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_capacity = UINT32_MAX;
|
self->_capacity = UINT32_MAX;
|
||||||
self->_ratio = 1;
|
self->_ratio = 1;
|
||||||
|
|
||||||
// function init
|
// front & back pointer init
|
||||||
self->push = queue_push;
|
|
||||||
self->pop = queue_pop;
|
|
||||||
|
|
||||||
self->back = queue_back;
|
|
||||||
self->front = queue_front;
|
|
||||||
|
|
||||||
self->clear = queue_clear;
|
|
||||||
self->empty = queue_empty;
|
|
||||||
self->full = queue_full;
|
|
||||||
self->size = queue_size;
|
|
||||||
self->capacity = queue_capacity;
|
|
||||||
|
|
||||||
self->destory = queue_destory;
|
|
||||||
self->print = queue_print;
|
|
||||||
|
|
||||||
// init front & back
|
|
||||||
self->_front = NULL;
|
self->_front = NULL;
|
||||||
self->_back = NULL;
|
self->_back = NULL;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->_destory = queue_destory;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->_iter.hasnext = queue_iter_hasnext;
|
||||||
|
self->_iter.next = queue_iter_next;
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
self->push = queue_push;
|
||||||
|
self->pop = queue_pop;
|
||||||
|
self->back = queue_back;
|
||||||
|
self->front = queue_front;
|
||||||
|
self->empty = queue_empty;
|
||||||
|
self->full = queue_full;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = queue_size;
|
||||||
|
self->capacity = queue_capacity;
|
||||||
|
self->clear = queue_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = queue_iter;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print = queue_print;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -354,51 +424,59 @@ static bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capaci
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attribute init
|
// -------------------- private --------------------
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_capacity = capacity;
|
self->_capacity = capacity;
|
||||||
self->_ratio = 2;
|
self->_ratio = 2;
|
||||||
|
|
||||||
// function init
|
|
||||||
self->push = queue2_push;
|
|
||||||
self->pop = queue2_pop;
|
|
||||||
|
|
||||||
self->back = queue2_back;
|
|
||||||
self->front = queue2_front;
|
|
||||||
|
|
||||||
self->clear = queue2_clear;
|
|
||||||
self->empty = queue_empty;
|
|
||||||
self->full = queue_full;
|
|
||||||
self->size = queue_size;
|
|
||||||
self->capacity = queue_capacity;
|
|
||||||
|
|
||||||
self->destory = queue2_destory;
|
|
||||||
self->print = queue2_print;
|
|
||||||
|
|
||||||
// init front & back
|
|
||||||
self->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
|
self->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
|
||||||
if(self->_front == NULL)
|
if(self->_front == NULL)
|
||||||
{
|
{
|
||||||
goto done;
|
return false;
|
||||||
}
|
}
|
||||||
self->_back = self->_front;
|
self->_back = self->_front;
|
||||||
|
|
||||||
// use self->_front->obj as obj_array
|
// use self->_front->obj as obj_array
|
||||||
|
//
|
||||||
// self->_front->obj = calloc(self->_capacity, self->_obj_size);
|
// self->_front->obj = calloc(self->_capacity, self->_obj_size);
|
||||||
self->_front->obj = malloc(self->_capacity * self->_obj_size);
|
self->_front->obj = malloc(self->_capacity * self->_obj_size);
|
||||||
if(self->_front->obj == NULL)
|
if(self->_front->obj == NULL)
|
||||||
{
|
{
|
||||||
goto done1;
|
free(self->_front);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
self->_index_front = 0;
|
self->_index_front = 0;
|
||||||
self->_index_back = 0;
|
self->_index_back = 0;
|
||||||
|
|
||||||
|
//
|
||||||
|
self->_destory = queue2_destory;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->_iter.hasnext = queue_iter_hasnext;
|
||||||
|
self->_iter.next = queue2_iter_next;
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
self->push = queue2_push;
|
||||||
|
self->pop = queue2_pop;
|
||||||
|
self->back = queue2_back;
|
||||||
|
self->front = queue2_front;
|
||||||
|
self->empty = queue_empty;
|
||||||
|
self->full = queue_full;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = queue_size;
|
||||||
|
self->capacity = queue_capacity;
|
||||||
|
self->clear = queue2_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = queue_iter;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print = queue2_print;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
done1:
|
|
||||||
free(self->_front);
|
|
||||||
done:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -413,13 +491,15 @@ queue_t queue_new(uint32_t obj_size)
|
|||||||
{
|
{
|
||||||
struct _queue * queue = NULL;
|
struct _queue * queue = NULL;
|
||||||
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
|
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
|
||||||
if(queue != NULL)
|
if(queue == NULL)
|
||||||
{
|
{
|
||||||
if(queue_init(queue, obj_size) == false)
|
return NULL;
|
||||||
{
|
}
|
||||||
free(queue);
|
|
||||||
queue = NULL;
|
if(queue_init(queue, obj_size) != true)
|
||||||
}
|
{
|
||||||
|
free(queue);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
@ -437,13 +517,15 @@ queue_t queue_new2(uint32_t obj_size, uint32_t capacity)
|
|||||||
{
|
{
|
||||||
struct _queue * queue = NULL;
|
struct _queue * queue = NULL;
|
||||||
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
|
queue = (struct _queue *)calloc(1, sizeof(struct _queue));
|
||||||
if(queue != NULL)
|
if(queue == NULL)
|
||||||
{
|
{
|
||||||
if(queue_init2(queue, obj_size, capacity) == false)
|
return NULL;
|
||||||
{
|
}
|
||||||
free(queue);
|
|
||||||
queue = NULL;
|
if(queue_init2(queue, obj_size, capacity) != true)
|
||||||
}
|
{
|
||||||
|
free(queue);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
@ -459,9 +541,9 @@ void queue_free(queue_t* queue)
|
|||||||
assert(queue != NULL);
|
assert(queue != NULL);
|
||||||
if(queue != NULL && *queue != NULL)
|
if(queue != NULL && *queue != NULL)
|
||||||
{
|
{
|
||||||
if((*queue)->destory != NULL)
|
if((*queue)->_destory != NULL)
|
||||||
{
|
{
|
||||||
(*queue)->destory(*queue);
|
(*queue)->_destory(*queue);
|
||||||
}
|
}
|
||||||
free(*queue);
|
free(*queue);
|
||||||
*queue = NULL;
|
*queue = NULL;
|
||||||
|
190
src/stack.c
190
src/stack.c
@ -124,6 +124,7 @@ static uint32_t stack_capacity(struct _stack* self)
|
|||||||
static bool stack_clear(struct _stack* self)
|
static bool stack_clear(struct _stack* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
assert(self->_head != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@ -253,40 +254,80 @@ static void stack2_print(struct _stack* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief iterator next
|
||||||
|
* from top to bottom
|
||||||
|
*
|
||||||
|
* @param iter
|
||||||
|
* @return const void*
|
||||||
|
* the value of return is const, so you can't modify it.
|
||||||
|
*/
|
||||||
|
const void* stack_iter_next(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
stack_t self = (stack_t)iter->_parent;
|
||||||
|
void *obj = NULL;
|
||||||
|
|
||||||
|
if(self->_head->obj == NULL)
|
||||||
|
{
|
||||||
|
// base on linklist
|
||||||
|
struct _stack_node* node = (struct _stack_node *)self->_iter._cur_node;
|
||||||
|
if(node != NULL)
|
||||||
|
{
|
||||||
|
obj = node->obj;
|
||||||
|
self->_iter._cur_node = node->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// base on array
|
||||||
|
uint32_t index = self->size(self) - 1 - self->_iter._cur;
|
||||||
|
obj = self->_head->obj + self->_obj_size * index;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->_iter._cur += 1;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool stack_iter_hasnext(struct _iterator* iter)
|
||||||
|
{
|
||||||
|
assert(iter != NULL);
|
||||||
|
assert(iter->parent != NULL);
|
||||||
|
|
||||||
|
stack_t self = (stack_t)iter->_parent;
|
||||||
|
if(self->_iter._cur < self->size(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t stack_iter(struct _stack* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
self->_iter._parent = self;
|
||||||
|
self->_iter._cur = 0;
|
||||||
|
self->_iter._cur_node = self->_head->next;
|
||||||
|
return &self->_iter;
|
||||||
|
}
|
||||||
|
|
||||||
static bool stack_init(struct _stack* self, uint32_t obj_size)
|
static bool stack_init(struct _stack* self, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
// assert(self != NULL);
|
||||||
assert(obj_size != 0);
|
// assert(obj_size != 0);
|
||||||
if(self == NULL || obj_size == 0)
|
if(self == NULL || obj_size == 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. set attr
|
// ---------- private ----------
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_capacity = UINT32_MAX;
|
self->_capacity = UINT32_MAX;
|
||||||
self->_ratio = 1;
|
self->_ratio = 1;
|
||||||
|
|
||||||
// 2. set function
|
|
||||||
// kernel
|
|
||||||
self->push = stack_push;
|
|
||||||
self->pop = stack_pop;
|
|
||||||
self->peek = stack_peek;
|
|
||||||
|
|
||||||
// base
|
|
||||||
self->size = stack_size;
|
|
||||||
self->empty = stack_empty;
|
|
||||||
self->capacity = stack_capacity;
|
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
self->clear = stack_clear;
|
|
||||||
self->destory = stack_destory;
|
|
||||||
// print
|
|
||||||
self->print = stack_print;
|
|
||||||
|
|
||||||
// 3. set node
|
|
||||||
self->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
|
self->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
|
||||||
if (self->_head == NULL)
|
if (self->_head == NULL)
|
||||||
{
|
{
|
||||||
@ -295,51 +336,82 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
|
|||||||
self->_head->obj = NULL;
|
self->_head->obj = NULL;
|
||||||
self->_head->next = NULL;
|
self->_head->next = NULL;
|
||||||
|
|
||||||
|
self->_iter.next = stack_iter_next;
|
||||||
|
self->_iter.hasnext = stack_iter_hasnext;
|
||||||
|
|
||||||
|
self->_destory = stack_destory;
|
||||||
|
|
||||||
|
// ---------- public ----------
|
||||||
|
// kernel
|
||||||
|
self->push = stack_push;
|
||||||
|
self->pop = stack_pop;
|
||||||
|
self->peek = stack_peek;
|
||||||
|
self->empty = stack_empty;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = stack_size;
|
||||||
|
self->capacity = stack_capacity;
|
||||||
|
self->clear = stack_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = stack_iter;
|
||||||
|
|
||||||
|
// ---------- debug ----------
|
||||||
|
self->print = stack_print;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
// assert(self != NULL);
|
||||||
|
if(self == NULL || obj_size == 0 || capacity == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// 1. set attr
|
// ---------- private ----------
|
||||||
self->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
self->_size = 0;
|
self->_size = 0;
|
||||||
self->_capacity = capacity;
|
self->_capacity = capacity;
|
||||||
self->_ratio = 2;
|
self->_ratio = 2;
|
||||||
|
|
||||||
// 2. set function
|
|
||||||
// kernel
|
|
||||||
self->push = stack2_push;
|
|
||||||
self->pop = stack2_pop;
|
|
||||||
self->peek = stack2_peek;
|
|
||||||
|
|
||||||
// others
|
|
||||||
self->empty = stack_empty;
|
|
||||||
self->size = stack_size;
|
|
||||||
self->capacity = stack_capacity;
|
|
||||||
|
|
||||||
// clear and free node
|
|
||||||
self->clear = stack_clear;
|
|
||||||
self->destory = stack2_destory;
|
|
||||||
// print
|
|
||||||
self->print = stack2_print;
|
|
||||||
|
|
||||||
// 3. set node
|
|
||||||
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
|
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
|
||||||
if (self->_head == NULL)
|
if (self->_head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// self->_head->obj = NULL;
|
|
||||||
self->_head->next = NULL;
|
self->_head->next = NULL;
|
||||||
|
|
||||||
// 4. set array
|
|
||||||
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
|
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
|
||||||
if (self->_head->obj == NULL)
|
if (self->_head->obj == NULL)
|
||||||
{
|
{
|
||||||
|
free(self->_head);
|
||||||
|
self->_head = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self->_iter.next = stack_iter_next;
|
||||||
|
self->_iter.hasnext = stack_iter_hasnext;
|
||||||
|
|
||||||
|
self->_destory = stack2_destory;
|
||||||
|
|
||||||
|
// ---------- public ----------
|
||||||
|
// kernel
|
||||||
|
self->push = stack2_push;
|
||||||
|
self->pop = stack2_pop;
|
||||||
|
self->peek = stack2_peek;
|
||||||
|
self->empty = stack_empty;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = stack_size;
|
||||||
|
self->capacity = stack_capacity;
|
||||||
|
self->clear = stack_clear;
|
||||||
|
|
||||||
|
// iter
|
||||||
|
self->iter = stack_iter;
|
||||||
|
|
||||||
|
// ---------- debug ----------
|
||||||
|
self->print = stack2_print;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,13 +419,15 @@ stack_t stack_new(uint32_t obj_size)
|
|||||||
{
|
{
|
||||||
stack_t stack = NULL;
|
stack_t stack = NULL;
|
||||||
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
|
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
|
||||||
if (stack != NULL)
|
if(stack == NULL)
|
||||||
{
|
{
|
||||||
if(stack_init(stack, obj_size) != true)
|
return NULL;
|
||||||
{
|
}
|
||||||
free(stack);
|
|
||||||
stack = NULL;
|
if(stack_init(stack, obj_size) != true)
|
||||||
}
|
{
|
||||||
|
free(stack);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
@ -362,13 +436,15 @@ stack_t stack_new2(uint32_t obj_size, uint32_t capacity)
|
|||||||
{
|
{
|
||||||
stack_t stack = NULL;
|
stack_t stack = NULL;
|
||||||
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
|
stack = (struct _stack*)calloc(1, sizeof(struct _stack));
|
||||||
if (stack != NULL)
|
if (stack == NULL)
|
||||||
{
|
{
|
||||||
if(stack_init2(stack, obj_size, capacity) != true)
|
return NULL;
|
||||||
{
|
}
|
||||||
free(stack);
|
|
||||||
stack = NULL;
|
if(stack_init2(stack, obj_size, capacity) != true)
|
||||||
}
|
{
|
||||||
|
free(stack);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
@ -378,9 +454,9 @@ void stack_free(stack_t *stack)
|
|||||||
assert(stack != NULL);
|
assert(stack != NULL);
|
||||||
if(stack != NULL && *stack != NULL)
|
if(stack != NULL && *stack != NULL)
|
||||||
{
|
{
|
||||||
if((*stack)->destory != NULL)
|
if((*stack)->_destory != NULL)
|
||||||
{
|
{
|
||||||
(*stack)->destory(*stack);
|
(*stack)->_destory(*stack);
|
||||||
}
|
}
|
||||||
free(*stack);
|
free(*stack);
|
||||||
*stack = NULL;
|
*stack = NULL;
|
||||||
|
@ -263,10 +263,61 @@ static void test_deque_struct(void)
|
|||||||
TEST_ASSERT_NULL(deque);
|
TEST_ASSERT_NULL(deque);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_deque_iter(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
|
deque_t deque = deque_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(deque);
|
||||||
|
deque->print_obj = print_num;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(deque->push_back(deque, &data[i]));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(deque->front(deque, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(deque->back(deque, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, deque->size(deque));
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = deque->iter(deque);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter = deque->iter(deque);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
deque_free(&deque);
|
||||||
|
TEST_ASSERT_NULL(deque);
|
||||||
|
}
|
||||||
|
|
||||||
void test_deque(void)
|
void test_deque(void)
|
||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
|
|
||||||
RUN_TEST(test_deque_num);
|
RUN_TEST(test_deque_num);
|
||||||
RUN_TEST(test_deque_struct);
|
RUN_TEST(test_deque_struct);
|
||||||
|
|
||||||
|
RUN_TEST(test_deque_iter);
|
||||||
}
|
}
|
||||||
|
@ -46,15 +46,11 @@ 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_new2(sizeof(int), 64);
|
heap_t heap = heap_min_new2(sizeof(int), 64);
|
||||||
TEST_ASSERT_NOT_NULL(heap);
|
TEST_ASSERT_NOT_NULL(heap);
|
||||||
heap->print_obj = print_num;
|
heap->print_obj = print_num;
|
||||||
heap->compare = compare_num;
|
heap->compare = compare_num;
|
||||||
|
|
||||||
// default: maxheap
|
|
||||||
// maxheap or minheap
|
|
||||||
heap->setmin(heap, true);
|
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
temp = data[i];
|
temp = data[i];
|
||||||
@ -93,7 +89,7 @@ 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_new2(sizeof(struct _student), 64);
|
heap_t heap = heap_min_new2(sizeof(struct _student), 64);
|
||||||
TEST_ASSERT_NOT_NULL(heap);
|
TEST_ASSERT_NOT_NULL(heap);
|
||||||
heap->print_obj = print_struct;
|
heap->print_obj = print_struct;
|
||||||
heap->compare = compare_struct;
|
heap->compare = compare_struct;
|
||||||
@ -107,6 +103,7 @@ static void test_heap_struct(void)
|
|||||||
|
|
||||||
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
|
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);
|
||||||
|
|
||||||
|
@ -284,6 +284,7 @@ static void test_list_struct(void)
|
|||||||
TEST_ASSERT_NULL(list);
|
TEST_ASSERT_NULL(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
static void test_list_iter(void)
|
static void test_list_iter(void)
|
||||||
{
|
{
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
@ -327,8 +328,8 @@ static void test_list_iter(void)
|
|||||||
TEST_ASSERT_TRUE(list->clear(list));
|
TEST_ASSERT_TRUE(list->clear(list));
|
||||||
list_free(&list);
|
list_free(&list);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
void list_iter_test(void)
|
void test_list_iter(void)
|
||||||
{
|
{
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
@ -353,21 +354,19 @@ void list_iter_test(void)
|
|||||||
TEST_ASSERT_FALSE(list->empty(list));
|
TEST_ASSERT_FALSE(list->empty(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator_t it = list->iter(list);
|
iterator_t iter = list->iter(list);
|
||||||
printf("iter start\n");
|
int iter_data = 0;
|
||||||
list->print(list);
|
int idx = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
printf("\n");
|
|
||||||
while(it->hasnext(it))
|
|
||||||
{
|
{
|
||||||
int dd = *(int*)it->data(it);
|
iter_data = *(int*)iter->next(iter);
|
||||||
printf("%d ", dd);
|
// printf("%d ", iter_data);
|
||||||
it->next(it);
|
TEST_ASSERT_EQUAL_INT(data[idx], iter_data);
|
||||||
|
idx++;
|
||||||
}
|
}
|
||||||
printf("\niter end\n");
|
|
||||||
|
|
||||||
list_free(&list);
|
list_free(&list);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void test_list(void)
|
void test_list(void)
|
||||||
{
|
{
|
||||||
@ -382,6 +381,4 @@ void test_list(void)
|
|||||||
RUN_TEST(test_list_struct);
|
RUN_TEST(test_list_struct);
|
||||||
|
|
||||||
RUN_TEST(test_list_iter);
|
RUN_TEST(test_list_iter);
|
||||||
|
|
||||||
RUN_TEST(list_iter_test);
|
|
||||||
}
|
}
|
||||||
|
@ -586,6 +586,84 @@ static void test_queue2_struct(void)
|
|||||||
TEST_ASSERT_NULL(queue);
|
TEST_ASSERT_NULL(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_queue_iter(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
queue = queue_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
queue->print_obj = print_num;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = queue->iter(queue);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_free(&queue);
|
||||||
|
TEST_ASSERT_NULL(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_queue2_iter(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t capacity = len;
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
queue = queue_new2(sizeof(int), capacity);
|
||||||
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
queue->print_obj = print_num;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = queue->iter(queue);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_free(&queue);
|
||||||
|
TEST_ASSERT_NULL(queue);
|
||||||
|
}
|
||||||
|
|
||||||
void test_queue(void)
|
void test_queue(void)
|
||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
@ -600,4 +678,7 @@ void test_queue(void)
|
|||||||
|
|
||||||
RUN_TEST(test_queue2_num);
|
RUN_TEST(test_queue2_num);
|
||||||
RUN_TEST(test_queue2_struct);
|
RUN_TEST(test_queue2_struct);
|
||||||
|
|
||||||
|
RUN_TEST(test_queue_iter);
|
||||||
|
RUN_TEST(test_queue2_iter);
|
||||||
}
|
}
|
||||||
|
@ -440,6 +440,82 @@ static void test_stack2_struct(void)
|
|||||||
TEST_ASSERT_NULL(stack);
|
TEST_ASSERT_NULL(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_stack_iter(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
stack = stack_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
stack->print_obj = print_num;
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = stack->iter(stack);
|
||||||
|
TEST_ASSERT_NOT_NULL(iter);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1 - i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
stack_free(&stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack2_iter(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t capacity = len;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
stack = stack_new2(sizeof(int), capacity);
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
stack->print_obj = print_num;
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator_t iter = stack->iter(stack);
|
||||||
|
TEST_ASSERT_NOT_NULL(iter);
|
||||||
|
i = 0;
|
||||||
|
while(iter->hasnext(iter))
|
||||||
|
{
|
||||||
|
temp = *(int *)iter->next(iter);
|
||||||
|
// printf("%d ", temp);
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1 - i], temp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
void test_stack(void)
|
void test_stack(void)
|
||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
@ -454,4 +530,7 @@ void test_stack(void)
|
|||||||
|
|
||||||
RUN_TEST(test_stack2_num);
|
RUN_TEST(test_stack2_num);
|
||||||
RUN_TEST(test_stack2_struct);
|
RUN_TEST(test_stack2_struct);
|
||||||
|
|
||||||
|
RUN_TEST(test_stack_iter);
|
||||||
|
RUN_TEST(test_stack2_iter);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user