mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
逻辑相对清晰一点,还差部分逻辑没调通
This commit is contained in:
parent
ce9acda014
commit
3f8f418d4e
195
src/list.c
195
src/list.c
@ -345,67 +345,98 @@ done:
|
|||||||
uint32_t capacity = 1;
|
uint32_t capacity = 1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bool contain_last_obj = false;
|
bool contain_last_obj = false;
|
||||||
|
int temp = 0;
|
||||||
|
bool negative = false;
|
||||||
if (step == 0)
|
if (step == 0)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (step > 0)
|
if(step < 0)
|
||||||
{
|
{
|
||||||
// special case
|
negative = true;
|
||||||
if (start == LIST_UNLIMITED)
|
step = -step;
|
||||||
|
|
||||||
|
temp = start;
|
||||||
|
if(start >= 0)
|
||||||
|
{
|
||||||
|
start = end + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
start = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(end >= 0)
|
||||||
|
{
|
||||||
|
end = temp + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
end = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(start < 0)
|
||||||
|
{
|
||||||
|
start += (int)self->size(self);
|
||||||
|
if(start < 0)
|
||||||
{
|
{
|
||||||
start = 0;
|
start = 0;
|
||||||
}
|
}
|
||||||
if (end == LIST_UNLIMITED)
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(start == LIST_UNLIMITED)
|
||||||
{
|
{
|
||||||
end = self->size(self);
|
start = 0;
|
||||||
contain_last_obj = true;
|
contain_last_obj = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// [start_max, end_min] start < end and limit start_max and end_min
|
if(start >= (int)self->size(self))
|
||||||
if (start >= end || start >= self->size(self) || end < -self->size(self))
|
|
||||||
{
|
{
|
||||||
empty = true;
|
empty = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (empty != true)
|
if(end < 0)
|
||||||
|
{
|
||||||
|
end += (int)self->size(self);
|
||||||
|
if(end < 0)
|
||||||
{
|
{
|
||||||
if (start < 0)
|
empty = true;
|
||||||
{
|
}
|
||||||
if (start < -self->size(self))
|
}
|
||||||
{
|
else
|
||||||
start = -self->size(self);
|
{
|
||||||
}
|
if (end == LIST_UNLIMITED || end >= (int)self->size(self))
|
||||||
start += self->size(self);
|
{
|
||||||
}
|
end = (int)self->size(self) - 1;
|
||||||
|
contain_last_obj = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (end < 0)
|
if(start >= end)
|
||||||
{
|
{
|
||||||
end += self->size(self);
|
empty = true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (end > self->size(self))
|
|
||||||
{
|
|
||||||
end = self->size(self);
|
|
||||||
contain_last_obj = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty != true)
|
if (empty != true)
|
||||||
{
|
{
|
||||||
// calc capacity
|
capacity = end - start;
|
||||||
capacity = end - start;
|
}
|
||||||
}
|
list = list_new2(self->_obj_size, capacity);
|
||||||
list = list_new2(self->_obj_size, capacity);
|
if (list == NULL)
|
||||||
if (list == NULL)
|
{
|
||||||
{
|
return NULL;
|
||||||
return NULL;
|
}
|
||||||
}
|
list->compare = self->compare;
|
||||||
list->compare = self->compare;
|
list->print_obj = self->print_obj;
|
||||||
list->print_obj = self->print_obj;
|
|
||||||
|
|
||||||
|
if(empty != true)
|
||||||
|
{
|
||||||
|
if(negative != true)
|
||||||
|
{
|
||||||
if (contain_last_obj != true)
|
if (contain_last_obj != true)
|
||||||
{
|
{
|
||||||
for (i = start; i < end; i += step)
|
for (i = start; i < end; i += step)
|
||||||
@ -423,98 +454,24 @@ done:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
list = list_new2(self->_obj_size, capacity);
|
|
||||||
if (list == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
list->compare = self->compare;
|
|
||||||
list->print_obj = self->print_obj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// special case
|
|
||||||
if (start == LIST_UNLIMITED)
|
|
||||||
{
|
|
||||||
start = self->size(self) - 1;
|
|
||||||
}
|
|
||||||
if (end == LIST_UNLIMITED)
|
|
||||||
{
|
|
||||||
end = 0;
|
|
||||||
contain_last_obj = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// [start_min, end_max] start > end and limit start_min and end_max
|
|
||||||
if (start <= end || end >= self->size(self) || start < -self->size(self))
|
|
||||||
{
|
|
||||||
empty = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty != true)
|
|
||||||
{
|
|
||||||
if (start < 0)
|
|
||||||
{
|
|
||||||
if (start < -self->size(self))
|
|
||||||
{
|
|
||||||
start = -self->size(self);
|
|
||||||
}
|
|
||||||
start += self->size(self);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (start > self->size(self))
|
|
||||||
{
|
|
||||||
start = self->size(self) - 1;
|
|
||||||
contain_last_obj = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end < 0)
|
|
||||||
{
|
|
||||||
end += self->size(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty != true)
|
|
||||||
{
|
|
||||||
// calc capacity
|
|
||||||
capacity = end - start;
|
|
||||||
}
|
|
||||||
list = list_new2(self->_obj_size, capacity);
|
|
||||||
if (list == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
list->compare = self->compare;
|
|
||||||
list->print_obj = self->print_obj;
|
|
||||||
|
|
||||||
if (contain_last_obj != true)
|
if (contain_last_obj != true)
|
||||||
{
|
{
|
||||||
for (i = start; i > end; i += step)
|
for (i = start; i < end; i += step)
|
||||||
{
|
{
|
||||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
list->insert(list, 0, (char*)self->obj + i * self->_obj_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (i = start; i >= end; i += step)
|
for (i = start; i <= end; i += step)
|
||||||
{
|
{
|
||||||
list->append(list, (char*)self->obj + i * self->_obj_size);
|
list->insert(list, 0, (char*)self->obj + i * self->_obj_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
list = list_new2(self->_obj_size, capacity);
|
|
||||||
if (list == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
list->compare = self->compare;
|
|
||||||
list->print_obj = self->print_obj;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
return list;
|
return list;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -472,12 +472,12 @@ static void test_list_slice_empty(void)
|
|||||||
list_free(&list2);
|
list_free(&list2);
|
||||||
|
|
||||||
// -------------------- empty --------------------
|
// -------------------- empty --------------------
|
||||||
list2 = list->slice(list, len, len + 10, 1); // if start > start_max
|
list2 = list->slice(list, len, len + 1, 1); // if start > start_max
|
||||||
TEST_ASSERT_NOT_NULL(list2);
|
TEST_ASSERT_NOT_NULL(list2);
|
||||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||||
list_free(&list2);
|
list_free(&list2);
|
||||||
|
|
||||||
list2 = list->slice(list, -len - 10, -len, 1); // if end < end_min
|
list2 = list->slice(list, -len - 1, -len, 1); // if end < end_min
|
||||||
TEST_ASSERT_NOT_NULL(list2);
|
TEST_ASSERT_NOT_NULL(list2);
|
||||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||||
list_free(&list2);
|
list_free(&list2);
|
||||||
@ -545,7 +545,7 @@ static void test_list_slice_positive(void)
|
|||||||
// python: list[4:0:-1]
|
// python: list[4:0:-1]
|
||||||
list2 = list->slice(list, 4, 0, -1);
|
list2 = list->slice(list, 4, 0, -1);
|
||||||
TEST_ASSERT_NOT_NULL(list2);
|
TEST_ASSERT_NOT_NULL(list2);
|
||||||
//list2->print(list2); printf("\n");
|
// list2->print(list2); printf("\n");
|
||||||
TEST_ASSERT_EQUAL_INT(4, list2->size(list2));
|
TEST_ASSERT_EQUAL_INT(4, list2->size(list2));
|
||||||
for(i = 0; i < list2->size(list2); i++)
|
for(i = 0; i < list2->size(list2); i++)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user