mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
Compare commits
4 Commits
802d63641c
...
ce9acda014
Author | SHA1 | Date | |
---|---|---|---|
ce9acda014 | |||
e86657d807 | |||
d2be5d229f | |||
97ae2a54c4 |
194
src/list.c
194
src/list.c
@ -206,6 +206,7 @@ static void list_print(struct _list* self)
|
||||
*/
|
||||
struct _list* list_slice(struct _list* self, int start, int end, int step)
|
||||
{
|
||||
#if 0
|
||||
assert(self != NULL);
|
||||
int i = 0;
|
||||
bool unlimited = false;
|
||||
@ -249,6 +250,13 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
}
|
||||
start += self->size(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (start > self->size(self))
|
||||
{
|
||||
start = self->size(self);
|
||||
}
|
||||
}
|
||||
|
||||
if (end < 0)
|
||||
{
|
||||
@ -258,6 +266,13 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
}
|
||||
end += self->size(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (end > self->size(self))
|
||||
{
|
||||
end = self->size(self);
|
||||
}
|
||||
}
|
||||
|
||||
// printf("start = %d\n", start);
|
||||
// printf("end = %d\n", end);
|
||||
@ -323,6 +338,185 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||
|
||||
done:
|
||||
return list;
|
||||
#else
|
||||
assert(self != NULL);
|
||||
list_t list = NULL;
|
||||
bool empty = false;
|
||||
uint32_t capacity = 1;
|
||||
int i = 0;
|
||||
bool contain_last_obj = false;
|
||||
if (step == 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (step > 0)
|
||||
{
|
||||
// special case
|
||||
if (start == LIST_UNLIMITED)
|
||||
{
|
||||
start = 0;
|
||||
}
|
||||
if (end == LIST_UNLIMITED)
|
||||
{
|
||||
end = self->size(self);
|
||||
contain_last_obj = true;
|
||||
}
|
||||
|
||||
// [start_max, end_min] start < end and limit start_max and end_min
|
||||
if (start >= end || start >= self->size(self) || end < -self->size(self))
|
||||
{
|
||||
empty = true;
|
||||
}
|
||||
|
||||
if (empty != true)
|
||||
{
|
||||
if (start < 0)
|
||||
{
|
||||
if (start < -self->size(self))
|
||||
{
|
||||
start = -self->size(self);
|
||||
}
|
||||
start += self->size(self);
|
||||
}
|
||||
|
||||
if (end < 0)
|
||||
{
|
||||
end += self->size(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (end > self->size(self))
|
||||
{
|
||||
end = self->size(self);
|
||||
contain_last_obj = true;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
list = list_new2(self->_obj_size, capacity);
|
||||
if (list == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
list->compare = self->compare;
|
||||
list->print_obj = self->print_obj;
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool list_iter_hasnext(struct _iterator* iter)
|
||||
|
@ -471,6 +471,17 @@ static void test_list_slice_empty(void)
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
list_free(&list2);
|
||||
|
||||
// -------------------- empty --------------------
|
||||
list2 = list->slice(list, len, len + 10, 1); // if start > start_max
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
list_free(&list2);
|
||||
|
||||
list2 = list->slice(list, -len - 10, -len, 1); // if end < end_min
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
list_free(&list2);
|
||||
|
||||
list_free(&list);
|
||||
}
|
||||
|
||||
@ -507,6 +518,18 @@ static void test_list_slice_positive(void)
|
||||
}
|
||||
list_free(&list2);
|
||||
|
||||
// python: list[0:11] if len(list) == 10
|
||||
list2 = list->slice(list, 0, len + 1, 1);
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
// list2->print(list2); printf("\n");
|
||||
TEST_ASSERT_EQUAL_INT(len, list2->size(list2));
|
||||
for(i = 0; i < list2->size(list2); i++)
|
||||
{
|
||||
TEST_ASSERT_TRUE(list2->get(list2, i, &temp));
|
||||
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||
}
|
||||
list_free(&list2);
|
||||
|
||||
// python: list[-6:8] or list[-6:-2] or list[4:8]
|
||||
list2 = list->slice(list, 4, 8, 1);
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
@ -614,9 +637,9 @@ static void test_list_slice_negative(void)
|
||||
}
|
||||
list_free(&list2);
|
||||
|
||||
// -------------------- step == 2 --------------------
|
||||
// list[-30:-1]
|
||||
list2 = list->slice(list, -30, -1, 1);
|
||||
// -------------------- start_limit --------------------
|
||||
// list[-len-1:-1]
|
||||
list2 = list->slice(list, -len-1, -1, 1);
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
// list2->print(list2); printf("\n");
|
||||
TEST_ASSERT_EQUAL_INT(len - 1, list2->size(list2));
|
||||
@ -627,12 +650,6 @@ static void test_list_slice_negative(void)
|
||||
}
|
||||
list_free(&list2);
|
||||
|
||||
// list[-30:-10]
|
||||
list2 = list->slice(list, -30, -10, 1);
|
||||
TEST_ASSERT_NOT_NULL(list2);
|
||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||
list_free(&list2);
|
||||
|
||||
list_free(&list);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user