mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
先调试通过了,后续再考虑优化
This commit is contained in:
parent
0f18ddf567
commit
2dfc0b2b17
64
src/list.c
64
src/list.c
@ -208,11 +208,14 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
{
|
||||
assert(self != NULL);
|
||||
int i = 0;
|
||||
bool unlimited = false;
|
||||
if(step == 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(step > 0)
|
||||
{
|
||||
if(start == LIST_UNLIMITED)
|
||||
{
|
||||
start = 0;
|
||||
@ -222,16 +225,33 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
{
|
||||
end = self->size(self);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(start == LIST_UNLIMITED)
|
||||
{
|
||||
start = self->size(self) - 1;
|
||||
unlimited = true;
|
||||
}
|
||||
|
||||
if(end == LIST_UNLIMITED)
|
||||
{
|
||||
end = 0;
|
||||
unlimited = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(start < 0)
|
||||
{
|
||||
start += self->size(self);
|
||||
}
|
||||
|
||||
if(end < 0)
|
||||
{
|
||||
end += self->size(self);
|
||||
}
|
||||
uint32_t capicity = (end - start == 0) ? 1 : end - start;
|
||||
|
||||
uint32_t capicity = (end - start == 0) ? 1 : abs(end - start);
|
||||
list_t list = list_new2(self->_obj_size, capicity);
|
||||
if(list == NULL)
|
||||
{
|
||||
@ -240,52 +260,58 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
list->compare = self->compare;
|
||||
list->print_obj = self->print_obj;
|
||||
|
||||
if(capicity == 0)
|
||||
if(capicity == 0 || start > self->size(self) || end > self->size(self))
|
||||
{
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(start >= self->size(self) || end > self->size(self))
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
if(step > 0)
|
||||
{
|
||||
if(start >= end)
|
||||
if(start > end)
|
||||
{
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(unlimited != true)
|
||||
{
|
||||
for(i = start; i < end; i += step)
|
||||
{
|
||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
||||
}
|
||||
|
||||
if(end == LIST_UNLIMITED)
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i = start; i <= end; i += step)
|
||||
{
|
||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
else /*if(step < 0)*/
|
||||
{
|
||||
if(end < self->size(self))
|
||||
{
|
||||
if(start <= end)
|
||||
if(start < end)
|
||||
{
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
end = -1;
|
||||
}
|
||||
|
||||
printf("start = %d\n", start);
|
||||
printf("end = %d\n", end);
|
||||
|
||||
if(unlimited != true)
|
||||
{
|
||||
for(i = start; i > end; i += step)
|
||||
{
|
||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i = start; i >= end; i += step)
|
||||
{
|
||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
return list;
|
||||
|
@ -455,6 +455,14 @@ static void test_list_slice(void)
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
|
||||
list2 = list->slice(list, -1, -5, 1); // if start < end && step < 0
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
|
||||
list2 = list->slice(list, -5, -1, -1); // if start > end && step > 0
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
|
||||
// python: list[0:]
|
||||
list2 = list->slice(list, 0, len, 1);
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
|
Loading…
Reference in New Issue
Block a user