mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
将index索引转换独立出一个函数
This commit is contained in:
parent
65a60df00b
commit
ccae6a9d8f
68
src/list.c
68
src/list.c
@ -189,6 +189,30 @@ static void list_print(struct _list* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int list_index_exchange(struct _list* self, int index)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
int size = (int)self->size(self);
|
||||||
|
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
index += size;
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (index > size)
|
||||||
|
{
|
||||||
|
index = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief list slice
|
* @brief list slice
|
||||||
* if index < 0, from the end of list. for example:
|
* if index < 0, from the end of list. for example:
|
||||||
@ -210,6 +234,9 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
bool contains_last_obj = false;
|
bool contains_last_obj = false;
|
||||||
int size = (int)self->size(self);
|
int size = (int)self->size(self);
|
||||||
|
int capicity = 1;
|
||||||
|
list_t list = NULL;
|
||||||
|
|
||||||
if (step == 0)
|
if (step == 0)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -241,43 +268,14 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start < 0)
|
start = list_index_exchange(self, start);
|
||||||
{
|
end = list_index_exchange(self, end);
|
||||||
if (start < -size)
|
|
||||||
{
|
|
||||||
start = -size;
|
|
||||||
}
|
|
||||||
start += size;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (start > size)
|
|
||||||
{
|
|
||||||
start = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end < 0)
|
if(abs(end - start) != 0)
|
||||||
{
|
{
|
||||||
if (end < -size)
|
capicity = abs(end - start);
|
||||||
{
|
|
||||||
end = -size;
|
|
||||||
}
|
|
||||||
end += size;
|
|
||||||
}
|
}
|
||||||
else
|
list = list_new2(self->_obj_size, capicity);
|
||||||
{
|
|
||||||
if (end > size)
|
|
||||||
{
|
|
||||||
end = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// printf("start = %d\n", start);
|
|
||||||
// printf("end = %d\n", end);
|
|
||||||
|
|
||||||
uint32_t capicity = (end - start == 0) ? 1 : abs(end - start);
|
|
||||||
list_t list = list_new2(self->_obj_size, capicity);
|
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -285,7 +283,7 @@ struct _list* list_slice(struct _list* self, int start, int end, int step)
|
|||||||
list->compare = self->compare;
|
list->compare = self->compare;
|
||||||
list->print_obj = self->print_obj;
|
list->print_obj = self->print_obj;
|
||||||
|
|
||||||
if (capicity == 0 || start > size || end > size)
|
if (start > size || end > size)
|
||||||
{
|
{
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user