mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
给list添加迭代器,方便遍历
This commit is contained in:
parent
d6811b824c
commit
349889ea98
23
src/list.c
23
src/list.c
@ -181,21 +181,27 @@ void list_print(struct _list* self)
|
||||
|
||||
void* list_begin(struct _list* self)
|
||||
{
|
||||
self->_cur = 0;
|
||||
return self->obj;
|
||||
}
|
||||
|
||||
void* list_next(struct _list* self)
|
||||
{
|
||||
void *obj = (char*)self->obj + self->_cur * self->_obj_size;
|
||||
self->_cur += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void* list_end(struct _list* self)
|
||||
{
|
||||
return (char*)self->obj + self->_size * self->_obj_size;
|
||||
}
|
||||
|
||||
void* list_next(struct _list* self)
|
||||
{
|
||||
void *obj = NULL;
|
||||
// 加了判断之后,回不到下一个了
|
||||
// if(self->_cur < self->_size - 1)
|
||||
{
|
||||
self->_cur += 1;
|
||||
}
|
||||
obj = (char*)self->obj + self->_cur * self->_obj_size;
|
||||
return obj;
|
||||
}
|
||||
|
||||
bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
{
|
||||
assert(list != NULL);
|
||||
@ -235,7 +241,8 @@ bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
list->end = list_end;
|
||||
|
||||
// 3. set array
|
||||
list->obj = (void*)calloc(list->_capacity, list->_obj_size);
|
||||
// list->obj = (void*)calloc(list->_capacity, list->_obj_size);
|
||||
list->obj = (void*)malloc(list->_capacity * list->_obj_size);
|
||||
if (list->obj == NULL)
|
||||
{
|
||||
return false;
|
||||
|
@ -401,10 +401,12 @@ static void test_list2_struct(void)
|
||||
static void test_list_iter(void)
|
||||
{
|
||||
int temp = 0;
|
||||
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||
int data_temp[32];
|
||||
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||
int data[32] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||
// uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||
uint32_t len = 10;
|
||||
uint32_t i = 0;
|
||||
int buff[32];
|
||||
int count = 0;
|
||||
|
||||
list_t list = NULL;
|
||||
|
||||
@ -418,12 +420,21 @@ static void test_list_iter(void)
|
||||
}
|
||||
|
||||
int * iter = NULL;
|
||||
i = 0;
|
||||
for(iter = list->begin(list); iter != list->end(list); iter = list->next(list))
|
||||
|
||||
iter = list->begin(list);
|
||||
for(count = 0, i = 0; i < len + 12; i++)
|
||||
{
|
||||
data_temp[i++] = *iter;
|
||||
if(i < len)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(data[i % len], *iter);
|
||||
}
|
||||
iter = list->next(list);
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT_ARRAY(data, data_temp, i);
|
||||
for(count=0, iter = list->begin(list); iter != list->end(list); iter = list->next(list))
|
||||
{
|
||||
buff[count++] = *iter;
|
||||
}
|
||||
TEST_ASSERT_EQUAL_INT_ARRAY(data, buff, count);
|
||||
|
||||
TEST_ASSERT_FALSE(list->empty(list));
|
||||
TEST_ASSERT_TRUE(list->clear(list));
|
||||
|
Loading…
Reference in New Issue
Block a user