mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 11:51:36 +08:00
Compare commits
No commits in common. "ce9acda014bb74118b7b22fab5fab01ea7d9b093" and "802d63641c1126f016543a3bffbae59f48ab1e33" have entirely different histories.
ce9acda014
...
802d63641c
194
src/list.c
194
src/list.c
@ -206,7 +206,6 @@ static void list_print(struct _list* self)
|
|||||||
*/
|
*/
|
||||||
struct _list* list_slice(struct _list *self, int start, int end, int step)
|
struct _list* list_slice(struct _list *self, int start, int end, int step)
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bool unlimited = false;
|
bool unlimited = false;
|
||||||
@ -250,13 +249,6 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
}
|
}
|
||||||
start += self->size(self);
|
start += self->size(self);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (start > self->size(self))
|
|
||||||
{
|
|
||||||
start = self->size(self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(end < 0)
|
if(end < 0)
|
||||||
{
|
{
|
||||||
@ -266,13 +258,6 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
}
|
}
|
||||||
end += self->size(self);
|
end += self->size(self);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (end > self->size(self))
|
|
||||||
{
|
|
||||||
end = self->size(self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// printf("start = %d\n", start);
|
// printf("start = %d\n", start);
|
||||||
// printf("end = %d\n", end);
|
// printf("end = %d\n", end);
|
||||||
@ -338,185 +323,6 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
|
|
||||||
done:
|
done:
|
||||||
return list;
|
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)
|
static bool list_iter_hasnext(struct _iterator* iter)
|
||||||
|
@ -471,17 +471,6 @@ static void test_list_slice_empty(void)
|
|||||||
TEST_ASSERT_TRUE(list2->empty(list2));
|
TEST_ASSERT_TRUE(list2->empty(list2));
|
||||||
list_free(&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);
|
list_free(&list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -518,18 +507,6 @@ static void test_list_slice_positive(void)
|
|||||||
}
|
}
|
||||||
list_free(&list2);
|
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]
|
// python: list[-6:8] or list[-6:-2] or list[4:8]
|
||||||
list2 = list->slice(list, 4, 8, 1);
|
list2 = list->slice(list, 4, 8, 1);
|
||||||
TEST_ASSERT_NOT_NULL(list2);
|
TEST_ASSERT_NOT_NULL(list2);
|
||||||
@ -637,9 +614,9 @@ static void test_list_slice_negative(void)
|
|||||||
}
|
}
|
||||||
list_free(&list2);
|
list_free(&list2);
|
||||||
|
|
||||||
// -------------------- start_limit --------------------
|
// -------------------- step == 2 --------------------
|
||||||
// list[-len-1:-1]
|
// list[-30:-1]
|
||||||
list2 = list->slice(list, -len-1, -1, 1);
|
list2 = list->slice(list, -30, -1, 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(len - 1, list2->size(list2));
|
TEST_ASSERT_EQUAL_INT(len - 1, list2->size(list2));
|
||||||
@ -650,6 +627,12 @@ static void test_list_slice_negative(void)
|
|||||||
}
|
}
|
||||||
list_free(&list2);
|
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);
|
list_free(&list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user