mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
修改iter的成员属性_cur为_index更加直观
This commit is contained in:
parent
1982a90a3e
commit
4691b848ef
@ -18,7 +18,7 @@ struct _iterator
|
||||
// ---------- private ----------
|
||||
void* _parent;
|
||||
void* _cur_node;
|
||||
uint32_t _cur;
|
||||
uint32_t _index;
|
||||
|
||||
uint32_t _order;
|
||||
|
||||
|
@ -301,7 +301,7 @@ iterator_t deque_iter(struct _deque* self, enum _deque_order order)
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_index = 0;
|
||||
iter->_order = order;
|
||||
if(iter->_order == DEQUE_FORWARD)
|
||||
{
|
||||
@ -320,7 +320,7 @@ bool deque_iter_hasnext(struct _iterator* iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
deque_t self = (deque_t)iter->_parent;
|
||||
if(iter->_cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -351,7 +351,7 @@ const void* deque_iter_next(struct _iterator* iter)
|
||||
iter->_cur_node = cur_node->prev;
|
||||
}
|
||||
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
@ -828,7 +828,7 @@ iterator_t graph_iter(struct _graph *self, enum _graph_search search_type, void
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_index = 0;
|
||||
iter->_cur_node = self->_head->next;
|
||||
|
||||
struct _graph_node *start_node = find_node(self, start);
|
||||
@ -874,7 +874,7 @@ bool graph_iter_hasnext(struct _iterator *iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
graph_t self = (graph_t)iter->_parent;
|
||||
if (iter->_cur < self->size(self))
|
||||
if (iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -885,11 +885,10 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
{
|
||||
assert(iter != NULL);
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
graph_t self = (graph_t)iter->_parent;
|
||||
void *obj = NULL;
|
||||
|
||||
iter->_cur += 1;
|
||||
iter->_index += 1;
|
||||
switch (self->_search)
|
||||
{
|
||||
case GRAPH_BFS:
|
||||
|
@ -256,7 +256,7 @@ iterator_t heap_iter(struct _heap* self)
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_index = 0;
|
||||
iter->_cur_node = self->obj;
|
||||
return iter;
|
||||
}
|
||||
@ -267,7 +267,7 @@ bool heap_iter_hasnext(struct _iterator* iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
heap_t self = (heap_t)iter->_parent;
|
||||
if(iter->_cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -282,10 +282,10 @@ const void* heap_iter_next(struct _iterator* iter)
|
||||
heap_t self = (heap_t)iter->_parent;
|
||||
void *obj = NULL;
|
||||
|
||||
uint32_t index = self->_iter._cur;
|
||||
uint32_t index = iter->_index;
|
||||
obj = self->obj + self->_obj_size * index;
|
||||
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
15
src/list.c
15
src/list.c
@ -182,8 +182,8 @@ static void list_print(struct _list* self)
|
||||
static const void* list_iter_next(struct _iterator* iter)
|
||||
{
|
||||
list_t self = (list_t)iter->_parent;
|
||||
void *obj = self->obj + self->_iter._cur * self->_obj_size;
|
||||
self->_iter._cur += 1;
|
||||
void *obj = self->obj + iter->_index * self->_obj_size;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ static bool list_iter_hasnext(struct _iterator* iter)
|
||||
{
|
||||
list_t self = (list_t)iter->_parent;
|
||||
|
||||
if(self->_iter._cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -200,9 +200,12 @@ static bool list_iter_hasnext(struct _iterator* iter)
|
||||
|
||||
iterator_t list_iter(struct _list* self)
|
||||
{
|
||||
self->_iter._parent = self;
|
||||
self->_iter._cur = 0;
|
||||
return &self->_iter;
|
||||
assert(self != NULL);
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_index = 0;
|
||||
return iter;
|
||||
}
|
||||
|
||||
static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
|
||||
|
10
src/queue.c
10
src/queue.c
@ -312,7 +312,7 @@ static iterator_t queue_iter(struct _queue* self)
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_index = 0;
|
||||
iter->_cur_node = self->_front;
|
||||
return iter;
|
||||
}
|
||||
@ -323,7 +323,7 @@ static bool queue_iter_hasnext(struct _iterator* iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
queue_t self = (queue_t)iter->_parent;
|
||||
if(iter->_cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -345,7 +345,7 @@ static const void* queue_iter_next(struct _iterator* iter)
|
||||
obj = node->obj;
|
||||
iter->_cur_node = node->next;
|
||||
}
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -358,10 +358,10 @@ static const void* queue2_iter_next(struct _iterator* iter)
|
||||
void *obj = NULL;
|
||||
|
||||
// base on array
|
||||
uint32_t index = self->_iter._cur;
|
||||
uint32_t index = iter->_index;
|
||||
obj = self->_front->obj + self->_obj_size * index;
|
||||
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
16
src/stack.c
16
src/stack.c
@ -283,11 +283,11 @@ const void* stack_iter_next(struct _iterator* iter)
|
||||
else
|
||||
{
|
||||
// base on array
|
||||
uint32_t index = self->size(self) - 1 - self->_iter._cur;
|
||||
uint32_t index = self->size(self) - 1 - iter->_index;
|
||||
obj = self->_head->obj + self->_obj_size * index;
|
||||
}
|
||||
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -297,7 +297,7 @@ bool stack_iter_hasnext(struct _iterator* iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
stack_t self = (stack_t)iter->_parent;
|
||||
if(self->_iter._cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -307,10 +307,12 @@ bool stack_iter_hasnext(struct _iterator* iter)
|
||||
iterator_t stack_iter(struct _stack* self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
self->_iter._parent = self;
|
||||
self->_iter._cur = 0;
|
||||
self->_iter._cur_node = self->_head->next;
|
||||
return &self->_iter;
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_index = 0;
|
||||
iter->_cur_node = self->_head->next;
|
||||
return iter;
|
||||
}
|
||||
|
||||
static bool stack_init(struct _stack* self, uint32_t obj_size)
|
||||
|
@ -1157,7 +1157,7 @@ static iterator_t tree_iter(struct _tree* self, enum _tree_order order)
|
||||
iterator_t iter = &self->_iter;
|
||||
|
||||
iter->_parent = self;
|
||||
iter->_cur = 0;
|
||||
iter->_index = 0;
|
||||
iter->_cur_node = self->_root;
|
||||
|
||||
iter->_order = order;
|
||||
@ -1241,7 +1241,7 @@ static bool tree_iter_hasnext(struct _iterator* iter)
|
||||
assert(iter->parent != NULL);
|
||||
|
||||
tree_t self = (tree_t)iter->_parent;
|
||||
if(iter->_cur < self->size(self))
|
||||
if(iter->_index < self->size(self))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -1394,7 +1394,7 @@ static const void* tree_iter_next(struct _iterator* iter)
|
||||
|
||||
self->_iter._cur_node = cur_node;
|
||||
obj = target_node->obj;
|
||||
self->_iter._cur += 1;
|
||||
iter->_index += 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user