acl/lib_acl/include/stdlib/acl_fifo.h

249 lines
6.5 KiB
C
Raw Normal View History

#ifndef ACL_FIFO_INCLUDE_H
2014-11-19 00:25:21 +08:00
#define ACL_FIFO_INCLUDE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "acl_define.h"
#include "acl_slice.h"
#include "acl_iterator.h"
typedef struct ACL_FIFO_INFO ACL_FIFO_INFO;
typedef struct ACL_FIFO_ITER ACL_FIFO_ITER;
typedef struct ACL_FIFO ACL_FIFO;
struct ACL_FIFO_INFO {
void *data;
ACL_FIFO_INFO *prev;
ACL_FIFO_INFO *next;
};
struct ACL_FIFO_ITER {
ACL_FIFO_INFO *ptr;
};
struct ACL_FIFO {
ACL_FIFO_INFO *head;
ACL_FIFO_INFO *tail;
int cnt;
/* 添加及弹出 */
2014-11-19 00:25:21 +08:00
/* 向队列尾部添加动态对象 */
2014-11-19 00:25:21 +08:00
void (*push_back)(struct ACL_FIFO*, void*);
/* 向队列头部添加动态对象 */
2014-11-19 00:25:21 +08:00
void (*push_front)(struct ACL_FIFO*, void*);
/* 弹出队列尾部动态对象 */
2014-11-19 00:25:21 +08:00
void *(*pop_back)(struct ACL_FIFO*);
/* 弹出队列头部动态对象 */
2014-11-19 00:25:21 +08:00
void *(*pop_front)(struct ACL_FIFO*);
/* for acl_iterator */
/* 取迭代器头函数 */
2014-11-19 00:25:21 +08:00
void *(*iter_head)(ACL_ITER*, struct ACL_FIFO*);
/* 取迭代器下一个函数 */
2014-11-19 00:25:21 +08:00
void *(*iter_next)(ACL_ITER*, struct ACL_FIFO*);
/* 取迭代器尾函数 */
2014-11-19 00:25:21 +08:00
void *(*iter_tail)(ACL_ITER*, struct ACL_FIFO*);
/* 取迭代器上一个函数 */
2014-11-19 00:25:21 +08:00
void *(*iter_prev)(ACL_ITER*, struct ACL_FIFO*);
/* 取迭代器关联的当前容器成员结构对象 */
2014-11-19 00:25:21 +08:00
ACL_FIFO_INFO *(*iter_info)(ACL_ITER*, struct ACL_FIFO*);
/* private */
ACL_SLICE_POOL *slice;
};
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO *}
* @example:
* void test(void) {
ACL_FIFO fifo;
acl_fifo_init(&fifo);
* }
*/
ACL_API void acl_fifo_init(ACL_FIFO *fifo);
/**
*
2014-11-19 00:25:21 +08:00
* @return {ACL_FIFO*}
*/
ACL_API ACL_FIFO *acl_fifo_new(void);
/**
*
2014-11-19 00:25:21 +08:00
* @param slice {ACL_SLICE_POOL*}
* @return {ACL_FIFO*}
*/
ACL_API ACL_FIFO *acl_fifo_new1(ACL_SLICE_POOL *slice);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @param data {const void*}
*/
ACL_API int acl_fifo_delete(ACL_FIFO *fifo, const void *data);
ACL_API void acl_fifo_delete_info(ACL_FIFO *fifo, ACL_FIFO_INFO *info);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @param free_fn {void (*)(void*)},
*
2014-11-19 00:25:21 +08:00
*/
ACL_API void acl_fifo_free(ACL_FIFO *fifo, void (*free_fn)(void *));
ACL_API void acl_fifo_free2(ACL_FIFO *fifo, void (*free_fn)(ACL_FIFO_INFO *));
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @param data {void*}
* @return {ACL_FIFO_INFO*} data , NULL
2014-11-19 00:25:21 +08:00
*/
ACL_API ACL_FIFO_INFO *acl_fifo_push_back(ACL_FIFO *fifo, void *data);
#define acl_fifo_push acl_fifo_push_back
ACL_API void acl_fifo_push_info_back(ACL_FIFO *fifo, ACL_FIFO_INFO *info);
#define acl_fifo_push_info acl_fifo_push_info_back
ACL_API ACL_FIFO_INFO *acl_fifo_push_front(ACL_FIFO *fifo, void *data);
/**
* ,
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @return {void*},
2014-11-19 00:25:21 +08:00
*/
ACL_API void *acl_fifo_pop_front(ACL_FIFO *fifo);
#define acl_fifo_pop acl_fifo_pop_front
ACL_API ACL_FIFO_INFO *acl_fifo_pop_info(ACL_FIFO *fifo);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @return {void*},
2014-11-19 00:25:21 +08:00
*/
ACL_API void *acl_fifo_pop_back(ACL_FIFO *fifo);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @return {void*},
2014-11-19 00:25:21 +08:00
*/
ACL_API void *acl_fifo_head(ACL_FIFO *fifo);
ACL_API ACL_FIFO_INFO *acl_fifo_head_info(ACL_FIFO *fifo);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @return {void*},
2014-11-19 00:25:21 +08:00
*/
ACL_API void *acl_fifo_tail(ACL_FIFO *fifo);
ACL_API ACL_FIFO_INFO *acl_fifo_tail_info(ACL_FIFO *fifo);
/**
*
2014-11-19 00:25:21 +08:00
* @param fifo {ACL_FIFO*}
* @return {int}, >= 0
*/
ACL_API int acl_fifo_size(ACL_FIFO *fifo);
/*-------------------- 一些方便快捷的宏操作 --------------------------------*/
2014-11-19 00:25:21 +08:00
/**
* iter
2014-11-19 00:25:21 +08:00
* @param iter {ACL_FIFO_ITER}
*/
#define ACL_FIFO_ITER_VALUE(iter) ((iter).ptr->data)
#define acl_fifo_iter_value ACL_FIFO_ITER_VALUE
/**
* ACL_FIFO
2014-11-19 00:25:21 +08:00
* @param iter {ACL_FIFO_ITER}
* @param fifo {ACL_FIFO}
* @example:
--
2014-11-19 00:25:21 +08:00
void test()
{
ACL_FIFO *fifo_ptr = acl_fifo_new();
ACL_FIFO_ITER iter;
char *data;
int i;
for (i = 0; i < 10; i++) {
data = acl_mymalloc(32);
snprintf(data, 32, "data: %d", i);
acl_fifo_push(fifo_ptr, data);
}
acl_fifo_foreach(iter, fifo_ptr) {
printf("%s\n", (char*) acl_fifo_iter_value(iter));
}
acl_fifo_free(fifo_ptr, acl_myfree_fn);
}
--
2014-11-19 00:25:21 +08:00
void test2()
{
ACL_FIFO *fifo_ptr = acl_fifo_new();
ACL_ITER iter;
char *data;
int i;
for (i = 0; i < 10; i++) {
data = acl_mymalloc(32);
snprintf(data, 32, "data: %d", i);
acl_fifo_push(fifo_ptr, data);
}
acl_foreach(iter, fifo) {
printf("%s\n", (char*) iter.data);
}
acl_fifo_free(fifo_ptr, acl_myfree_fn);
}
*/
#define ACL_FIFO_FOREACH(iter, fifo_ptr) \
for ((iter).ptr = (fifo_ptr)->head; (iter).ptr; (iter).ptr = (iter).ptr->next)
#define acl_fifo_foreach ACL_FIFO_FOREACH
/**
* ACL_FIFO
2014-11-19 00:25:21 +08:00
* @param iter {ACL_FIFO_ITER}
* @param fifo {ACL_FIFO}
* @example:
void test()
{
ACL_FIFO fifo;
ACL_FIFO_ITER iter;
char *data;
int i;
acl_fifo_init(&fifo);
for (i = 0; i < 10; i++) {
data = acl_mymalloc(32);
snprintf(data, 32, "data: %d", i);
acl_fifo_push(&fifo, data);
}
acl_fifo_foreach_reverse(iter, &fifo) {
printf("%s\n", (char*) iter.ptr->data);
}
while (1) {
data = acl_fifo_pop(&fifo);
if (data == NULL)
break;
}
}
*/
#define ACL_FIFO_FOREACH_REVERSE(iter, fifo_ptr) \
for ((iter).ptr = (fifo_ptr)->tail; (iter).ptr; (iter).ptr = (iter).ptr->prev)
#define acl_fifo_foreach_reverse ACL_FIFO_FOREACH_REVERSE
#ifdef __cplusplus
}
#endif
#endif