Updated list iterator API

This commit is contained in:
Leonardo committed 2020-05-10 21:58:10 -03:00
1 parent 9cd3bf3b31
commit 66c829701d
3 files changed
+516 -111

No files matched your search

+38 -63
View File
@@ -95,12 +95,6 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
/* Value function table */ \
struct SNAME##_fval *f_val; \
\
/* Function that returns an iterator to the start of the list */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the list */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
\
/* Custom allocation functions */ \
struct cmc_alloc_node *alloc; \
\
@@ -194,17 +188,15 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
bool PFX##_print(struct SNAME *_list_, FILE *fptr); \
\
/* Iterator Functions */ \
/* Iterator Allocation and Deallocation */ \
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
void PFX##_iter_free(struct SNAME##_iter *iter); \
/* Iterator Initialization */ \
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
struct SNAME##_iter PFX##_iter_start(struct SNAME *target); \
struct SNAME##_iter PFX##_iter_end(struct SNAME *target); \
/* Iterator State */ \
bool PFX##_iter_start(struct SNAME##_iter *iter); \
bool PFX##_iter_end(struct SNAME##_iter *iter); \
bool PFX##_iter_at_start(struct SNAME##_iter *iter); \
bool PFX##_iter_at_end(struct SNAME##_iter *iter); \
/* Iterator Movement */ \
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_to_start(struct SNAME##_iter *iter); \
bool PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_next(struct SNAME##_iter *iter); \
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
@@ -221,8 +213,7 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
#define CMC_GENERATE_LIST_SOURCE(PFX, SNAME, V) \
\
/* Implementation Detail Functions */ \
static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_list_); \
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_list_); \
/* None */ \
\
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_fval *f_val) \
{ \
@@ -250,8 +241,6 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->f_val = f_val; \
_list_->alloc = alloc; \
_list_->callbacks = NULL; \
_list_->it_start = PFX##_impl_it_start; \
_list_->it_end = PFX##_impl_it_end; \
\
return _list_; \
} \
@@ -288,8 +277,6 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
_list_->f_val = f_val; \
_list_->alloc = alloc; \
_list_->callbacks = callbacks; \
_list_->it_start = PFX##_impl_it_start; \
_list_->it_end = PFX##_impl_it_end; \
\
return _list_; \
} \
@@ -894,61 +881,69 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
return true; \
} \
\
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \
struct SNAME##_iter PFX##_iter_start(struct SNAME *target) \
{ \
struct SNAME##_iter *iter = \
target->alloc->malloc(sizeof(struct SNAME##_iter)); \
struct SNAME##_iter iter; \
\
if (!iter) \
return NULL; \
\
PFX##_iter_init(iter, target); \
iter.target = target; \
iter.cursor = 0; \
iter.start = true; \
iter.end = PFX##_empty(target); \
\
return iter; \
} \
\
void PFX##_iter_free(struct SNAME##_iter *iter) \
struct SNAME##_iter PFX##_iter_end(struct SNAME *target) \
{ \
iter->target->alloc->free(iter); \
struct SNAME##_iter iter; \
\
iter.target = target; \
iter.cursor = 0; \
iter.start = PFX##_empty(target); \
iter.end = true; \
\
if (!PFX##_empty(target)) \
iter.cursor = target->count - 1; \
\
return iter; \
} \
\
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \
{ \
iter->target = target; \
iter->cursor = 0; \
iter->start = true; \
iter->end = PFX##_empty(target); \
} \
\
bool PFX##_iter_start(struct SNAME##_iter *iter) \
bool PFX##_iter_at_start(struct SNAME##_iter *iter) \
{ \
return PFX##_empty(iter->target) || iter->start; \
} \
\
bool PFX##_iter_end(struct SNAME##_iter *iter) \
bool PFX##_iter_at_end(struct SNAME##_iter *iter) \
{ \
return PFX##_empty(iter->target) || iter->end; \
} \
\
void PFX##_iter_to_start(struct SNAME##_iter *iter) \
bool PFX##_iter_to_start(struct SNAME##_iter *iter) \
{ \
if (!PFX##_empty(iter->target)) \
{ \
iter->cursor = 0; \
iter->start = true; \
iter->end = PFX##_empty(iter->target); \
\
return true; \
} \
\
return false; \
} \
\
void PFX##_iter_to_end(struct SNAME##_iter *iter) \
bool PFX##_iter_to_end(struct SNAME##_iter *iter) \
{ \
if (!PFX##_empty(iter->target)) \
{ \
iter->start = PFX##_empty(iter->target); \
iter->cursor = \
PFX##_empty(iter->target) ? 0 : iter->target->count - 1; \
iter->cursor = iter->target->count - 1; \
iter->end = true; \
\
return true; \
} \
\
return false; \
} \
\
bool PFX##_iter_next(struct SNAME##_iter *iter) \
@@ -1065,26 +1060,6 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
size_t PFX##_iter_index(struct SNAME##_iter *iter) \
{ \
return iter->cursor; \
} \
\
static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_list_) \
{ \
struct SNAME##_iter iter; \
\
PFX##_iter_init(&iter, _list_); \
PFX##_iter_to_start(&iter); \
\
return iter; \
} \
\
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_list_) \
{ \
struct SNAME##_iter iter; \
\
PFX##_iter_init(&iter, _list_); \
PFX##_iter_to_end(&iter); \
\
return iter; \
}
#endif /* CMC_LIST_H */