先正序的情况修改完成

This commit is contained in:
建峰 2025-04-30 11:33:54 +08:00
parent d2be5d229f
commit e86657d807

View File

@ -206,7 +206,7 @@ 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 1 #if 0
assert(self != NULL); assert(self != NULL);
int i = 0; int i = 0;
bool unlimited = false; bool unlimited = false;
@ -344,6 +344,7 @@ done:
bool empty = false; bool empty = false;
uint32_t capacity = 1; uint32_t capacity = 1;
int i = 0; int i = 0;
bool contain_last_obj = false;
if(step == 0) if(step == 0)
{ {
return NULL; return NULL;
@ -359,6 +360,7 @@ done:
if(end == LIST_UNLIMITED) if(end == LIST_UNLIMITED)
{ {
end = self->size(self); end = self->size(self);
contain_last_obj = true;
} }
// [start_max, end_min] start < end and limit start_max and end_min // [start_max, end_min] start < end and limit start_max and end_min
@ -366,7 +368,6 @@ done:
{ {
empty = true; empty = true;
} }
if(start < 0) if(start < 0)
{ {
if(start < -self->size(self)) if(start < -self->size(self))
@ -380,19 +381,21 @@ done:
{ {
end += self->size(self); end += self->size(self);
} }
}
else else
{ {
if(end > self->size(self))
{
end = self->size(self);
contain_last_obj = true;
}
} }
if(empty != true) if(empty != true)
{ {
// calc capacity // calc capacity
capacity = end - start;
} }
list_t list = list_new2(self->_obj_size, capacity); list = list_new2(self->_obj_size, capacity);
if(list == NULL) if(list == NULL)
{ {
return NULL; return NULL;
@ -400,8 +403,26 @@ done:
list->compare = self->compare; list->compare = self->compare;
list->print_obj = self->print_obj; 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
{
}
done:
return list; return list;
#endif #endif
} }