Skip to content

Commit

Permalink
#14 Fixed iterators for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Mar 22, 2015
1 parent 4e7eba8 commit acfb9e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cx/include/cx_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ typedef struct cx_ll_s* cx_ll;
#define CX_ITERATOR(__type) typedef struct __type {\
void *current;\
cx_collection type;\
cx_bool (*next)(void* iterator);\
union {\
struct { /* CX_ARRAY and CX_SEQUENCE */\
void *array;\
int32_t elementSize;\
void* max;\
} array;\
struct {\
cx_ll *ll;\
cx_iter iter;\
} ll;\
} is;\
Expand Down
77 changes: 41 additions & 36 deletions cx/src/cx_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@
CX_ITERATOR(iteratorType);
CX_SEQUENCE(seqType, cx_object, );

cx_int16 cx_iterator_set(void* _this, void* collection, cx_collection collectionType) {
iteratorType *iter = _this;
iter->type = collectionType;

switch (collectionType->kind) {
case CX_ARRAY:
iter->is.array.array = collection;
iter->is.array.elementSize = cx_type_sizeof(collectionType->elementType);
iter->is.array.max = CX_OFFSET(iter->is.array.array, collectionType->max * iter->is.array.elementSize);
iter->current = CX_OFFSET(collection, -iter->is.array.elementSize);
break;
case CX_SEQUENCE:
iter->is.array.array = ((seqType*)collection)->buffer;
iter->is.array.elementSize = cx_type_sizeof(collectionType->elementType);
iter->is.array.max = CX_OFFSET(iter->is.array.array, ((seqType*)collection)->length * iter->is.array.elementSize);
iter->current = CX_OFFSET(iter->is.array.array, -iter->is.array.elementSize);
break;
case CX_LIST:
iter->is.ll.ll = collection;
iter->is.ll.iter = cx_llIter(collection);
break;
case CX_MAP:
break;
}
return 0;
}

/* Combined hasNext and next */
static cx_bool cx_iterator_next_array(void* iterator) {
iteratorType *iter = iterator;
Expand All @@ -54,7 +27,17 @@ static cx_bool cx_iterator_next_array(void* iterator) {
return result;
}

static cx_bool cx_iterator_next_ll(void* iterator) {
static cx_bool cx_iterator_next_listPtr(void* iterator) {
iteratorType *iter = iterator;
cx_bool result = FALSE;
if ((result = cx_iterHasNext(&iter->is.ll.iter))) {
iter->current = cx_iterNextPtr(&iter->is.ll.iter);
result = TRUE;
}
return result;
}

static cx_bool cx_iterator_next_list(void* iterator) {
iteratorType *iter = iterator;
cx_bool result = FALSE;
if ((result = cx_iterHasNext(&iter->is.ll.iter))) {
Expand All @@ -75,22 +58,44 @@ cx_bool cx_iterator_next(void* _this) {
iteratorType *iterator = _this;
cx_bool result = FALSE;

switch (iterator->type->kind) {
result = iterator->next(iterator);

return result;
}

cx_int16 cx_iterator_set(void* _this, void* collection, cx_collection collectionType) {
iteratorType *iter = _this;
iter->type = collectionType;

switch (collectionType->kind) {
case CX_ARRAY:
iter->is.array.array = collection;
iter->is.array.elementSize = cx_type_sizeof(collectionType->elementType);
iter->is.array.max = CX_OFFSET(iter->is.array.array, collectionType->max * iter->is.array.elementSize);
iter->current = CX_OFFSET(collection, -iter->is.array.elementSize);
iter->next = cx_iterator_next_array;
break;
case CX_SEQUENCE:
result = cx_iterator_next_array(iterator);
iter->is.array.array = ((seqType*)collection)->buffer;
iter->is.array.elementSize = cx_type_sizeof(collectionType->elementType);
iter->is.array.max = CX_OFFSET(iter->is.array.array, ((seqType*)collection)->length * iter->is.array.elementSize);
iter->current = CX_OFFSET(iter->is.array.array, -iter->is.array.elementSize);
iter->next = cx_iterator_next_array;
break;
case CX_LIST:
result = cx_iterator_next_ll(iterator);
iter->is.ll.iter = cx_llIter(*(cx_ll*)collection);
if (cx_collection_elementRequiresAlloc(collectionType)) {
iter->next = cx_iterator_next_list;
} else {
iter->next = cx_iterator_next_listPtr;
}

break;
case CX_MAP:
result = cx_iterator_next_map(iterator);
break;
default:
iter->next = cx_iterator_next_map;
break;
}

return result;
return 0;
}

/* $end */
Expand Down

0 comments on commit acfb9e6

Please sign in to comment.