条件判断还存在问题

This commit is contained in:
建峰 2025-04-30 11:45:22 +08:00
parent e86657d807
commit ce9acda014

View File

@ -15,7 +15,7 @@ static bool list_insert(struct _list* self, int index, void* obj)
{ {
// assert(index >= 0 && index < (int)self->size(self)); // assert(index >= 0 && index < (int)self->size(self));
// assert(index >= 0 && index <= (int)self->size(self)); // assert(index >= 0 && index <= (int)self->size(self));
if(index < 0 || index > (int)self->size(self)) if (index < 0 || index >(int)self->size(self))
{ {
return false; return false;
} }
@ -23,7 +23,7 @@ static bool list_insert(struct _list* self, int index, void* obj)
if (self->size(self) == self->_capacity) if (self->size(self) == self->_capacity)
{ {
int capacity = self->_capacity * self->_ratio; int capacity = self->_capacity * self->_ratio;
void* obj_new = (void *)realloc(self->obj, capacity * self->_obj_size); void* obj_new = (void*)realloc(self->obj, capacity * self->_obj_size);
if (obj_new == NULL) if (obj_new == NULL)
{ {
return false; return false;
@ -47,7 +47,7 @@ static bool list_delete(struct _list* self, int index, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
// assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); // assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
if(index < (int)(0 - self->size(self)) || index >= (int)self->size(self)) if (index < (int)(0 - self->size(self)) || index >= (int)self->size(self))
{ {
return false; return false;
} }
@ -92,14 +92,14 @@ static int list_index(struct _list* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
int index = 0; int index = 0;
if(obj == NULL) if (obj == NULL)
{ {
return -1; return -1;
} }
while(index < (int)self->size(self)) while (index < (int)self->size(self))
{ {
if(self->compare(self->obj + index * self->_obj_size, obj) == 0) if (self->compare(self->obj + index * self->_obj_size, obj) == 0)
{ {
return index; return index;
} }
@ -204,47 +204,47 @@ static void list_print(struct _list* self)
* if step > 0, return a forward list. * if step > 0, return a forward list.
* if step == 0, return NULL. * if step == 0, return NULL.
*/ */
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 #if 0
assert(self != NULL); assert(self != NULL);
int i = 0; int i = 0;
bool unlimited = false; bool unlimited = false;
if(step == 0) if (step == 0)
{ {
return NULL; return NULL;
} }
if(step > 0) if (step > 0)
{ {
if(start == LIST_UNLIMITED) if (start == LIST_UNLIMITED)
{ {
start = 0; start = 0;
} }
if(end == LIST_UNLIMITED) if (end == LIST_UNLIMITED)
{ {
end = self->size(self); end = self->size(self);
} }
} }
else else
{ {
if(start == LIST_UNLIMITED) if (start == LIST_UNLIMITED)
{ {
start = self->size(self) - 1; start = self->size(self) - 1;
unlimited = true; unlimited = true;
} }
if(end == LIST_UNLIMITED) if (end == LIST_UNLIMITED)
{ {
end = 0; end = 0;
unlimited = true; unlimited = true;
} }
} }
if(start < 0) if (start < 0)
{ {
if(start < -self->size(self)) if (start < -self->size(self))
{ {
start = -self->size(self); start = -self->size(self);
} }
@ -252,15 +252,15 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
} }
else else
{ {
if(start > self->size(self)) if (start > self->size(self))
{ {
start = self->size(self); start = self->size(self);
} }
} }
if(end < 0) if (end < 0)
{ {
if(end < -self->size(self)) if (end < -self->size(self))
{ {
end = -self->size(self); end = -self->size(self);
} }
@ -268,7 +268,7 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
} }
else else
{ {
if(end > self->size(self)) if (end > self->size(self))
{ {
end = self->size(self); end = self->size(self);
} }
@ -279,35 +279,35 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
uint32_t capicity = (end - start == 0) ? 1 : abs(end - start); uint32_t capicity = (end - start == 0) ? 1 : abs(end - start);
list_t list = list_new2(self->_obj_size, capicity); list_t list = list_new2(self->_obj_size, capicity);
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(capicity == 0 || start > self->size(self) || end > self->size(self)) if (capicity == 0 || start > self->size(self) || end > self->size(self))
{ {
goto done; goto done;
} }
if(step > 0) if (step > 0)
{ {
if(start > end) if (start > end)
{ {
goto done; goto done;
} }
if(unlimited != true) if (unlimited != 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->append(list, (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->append(list, (char*)self->obj + i * self->_obj_size);
} }
@ -315,21 +315,21 @@ struct _list* list_slice(struct _list *self, int start, int end, int step)
} }
else /*if(step < 0)*/ else /*if(step < 0)*/
{ {
if(start < end) if (start < end)
{ {
goto done; goto done;
} }
if(unlimited != true) if (unlimited != 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->append(list, (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->append(list, (char*)self->obj + i * self->_obj_size);
} }
@ -345,74 +345,77 @@ 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;
if(step == 0) if (step == 0)
{ {
return NULL; return NULL;
} }
if(step > 0) if (step > 0)
{ {
// special case // special case
if(start == LIST_UNLIMITED) if (start == LIST_UNLIMITED)
{ {
start = 0; start = 0;
} }
if(end == LIST_UNLIMITED) if (end == LIST_UNLIMITED)
{ {
end = self->size(self); end = self->size(self);
contain_last_obj = true; 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
if(start >= end || start >= self->size(self) || end < -self->size(self)) if (start >= end || start >= self->size(self) || end < -self->size(self))
{ {
empty = true; empty = true;
} }
if(start < 0)
if (empty != true)
{ {
if(start < -self->size(self)) if (start < 0)
{
if (start < -self->size(self))
{ {
start = -self->size(self); start = -self->size(self);
} }
start += self->size(self); start += self->size(self);
} }
if(end < 0) if (end < 0)
{ {
end += self->size(self); end += self->size(self);
} }
else else
{ {
if(end > self->size(self)) if (end > self->size(self))
{ {
end = self->size(self); end = self->size(self);
contain_last_obj = true; contain_last_obj = true;
} }
} }
if(empty != true) if (empty != true)
{ {
// calc capacity // 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(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->append(list, (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->append(list, (char*)self->obj + i * self->_obj_size);
} }
@ -420,7 +423,96 @@ 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)
{
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; return list;
@ -431,7 +523,7 @@ static bool list_iter_hasnext(struct _iterator* iter)
{ {
list_t self = (list_t)iter->_container; list_t self = (list_t)iter->_container;
if(iter->_index < self->size(self)) if (iter->_index < self->size(self))
{ {
return true; return true;
} }
@ -441,7 +533,7 @@ static bool list_iter_hasnext(struct _iterator* iter)
static const void* list_iter_next(struct _iterator* iter) static const void* list_iter_next(struct _iterator* iter)
{ {
list_t self = (list_t)iter->_container; list_t self = (list_t)iter->_container;
void *obj = self->obj + iter->_index * self->_obj_size; void* obj = self->obj + iter->_index * self->_obj_size;
iter->_index += 1; iter->_index += 1;
return obj; return obj;
} }
@ -462,7 +554,7 @@ iterator_t list_iter(struct _list* self)
static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity) static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
{ {
assert(list != NULL); assert(list != NULL);
if(list == NULL || obj_size == 0 || capacity == 0) if (list == NULL || obj_size == 0 || capacity == 0)
{ {
return false; return false;
} }
@ -524,12 +616,12 @@ list_t list_new2(uint32_t obj_size, uint32_t capacity)
{ {
struct _list* list = NULL; struct _list* list = NULL;
list = (struct _list*)calloc(1, sizeof(struct _list)); list = (struct _list*)calloc(1, sizeof(struct _list));
if(list == NULL) if (list == NULL)
{ {
return NULL; return NULL;
} }
if(list_init2(list, obj_size, capacity) != true) if (list_init2(list, obj_size, capacity) != true)
{ {
free(list); free(list);
return NULL; return NULL;
@ -540,9 +632,9 @@ list_t list_new2(uint32_t obj_size, uint32_t capacity)
void list_free(list_t* list) void list_free(list_t* list)
{ {
assert(list != NULL); assert(list != NULL);
if(list != NULL && *list != NULL) if (list != NULL && *list != NULL)
{ {
if((*list)->_destory != NULL) if ((*list)->_destory != NULL)
{ {
(*list)->_destory(*list); (*list)->_destory(*list);
} }