First update on iterators - LIST
This commit is contained in:
1 parent
8843a8fb7a
commit
444f597a98
6 files changed
+361
-76
No files matched your search
+122
-46
@@ -37,76 +37,112 @@
|
||||
#define LIST_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
struct SNAME##_s \
|
||||
{ \
|
||||
V *buffer; \
|
||||
size_t capacity; \
|
||||
size_t count; \
|
||||
}; \
|
||||
\
|
||||
struct SNAME##_iter_s \
|
||||
{ \
|
||||
struct SNAME##_s *target; \
|
||||
size_t cursor; \
|
||||
bool start; \
|
||||
bool end; \
|
||||
}; \
|
||||
\
|
||||
#define LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define LIST_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
typedef struct SNAME##_s SNAME; \
|
||||
typedef struct SNAME##_iter_s SNAME##_iter; \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements*/ \
|
||||
V *buffer; \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size); \
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the list */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the list */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target list */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t capacity); \
|
||||
FMOD SNAME *PFX##_new_from(V *elements, size_t size); \
|
||||
FMOD void PFX##_clear(SNAME *_list_); \
|
||||
FMOD void PFX##_free(SNAME *_list_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_push_front(SNAME *_list_, V element); \
|
||||
FMOD bool PFX##_push(SNAME *_list_, V element, size_t index); \
|
||||
FMOD bool PFX##_push_back(SNAME *_list_, V element); \
|
||||
FMOD bool PFX##_pop_front(SNAME *_list_); \
|
||||
FMOD bool PFX##_pop(SNAME *_list_, size_t index); \
|
||||
FMOD bool PFX##_pop_back(SNAME *_list_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_push_if(SNAME *_list_, V element, size_t index, bool condition); \
|
||||
FMOD bool PFX##_pop_if(SNAME *_list_, size_t index, bool condition); \
|
||||
/* Collection Range Input and Output */ \
|
||||
FMOD bool PFX##_prepend(SNAME *_list_, V *elements, size_t size); \
|
||||
FMOD bool PFX##_insert(SNAME *_list_, V *elements, size_t size, size_t index); \
|
||||
FMOD bool PFX##_append(SNAME *_list_, V *elements, size_t size); \
|
||||
FMOD bool PFX##_remove(SNAME *_list_, size_t from, size_t to); \
|
||||
FMOD SNAME *PFX##_extract(SNAME *_list_, size_t from, size_t to); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_front(SNAME *_list_); \
|
||||
FMOD V PFX##_get(SNAME *_list_, size_t index); \
|
||||
FMOD V *PFX##_get_ref(SNAME *_list_, size_t index); \
|
||||
FMOD V PFX##_back(SNAME *_list_); \
|
||||
FMOD size_t PFX##_indexof(SNAME *_list_, V element, int (*comparator)(V, V), bool from_start); \
|
||||
FMOD bool PFX##_contains(SNAME *_list_, V element, int (*comparator)(V, V)); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_empty(SNAME *_list_); \
|
||||
FMOD bool PFX##_full(SNAME *_list_); \
|
||||
FMOD size_t PFX##_count(SNAME *_list_); \
|
||||
FMOD bool PFX##_fits(SNAME *_list_, size_t size); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_list_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_tostart(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_toend(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter, V *result, size_t *index); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter, V *result, size_t *index); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
FMOD bool PFX##_grow(SNAME *_list_, size_t required); \
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_impl_grow(SNAME *_list_, size_t required); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_list_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_list_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size) \
|
||||
FMOD SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (size < 1) \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_list_ = malloc(sizeof(SNAME)); \
|
||||
@@ -114,7 +150,7 @@
|
||||
if (!_list_) \
|
||||
return NULL; \
|
||||
\
|
||||
_list_->buffer = malloc(size * sizeof(V)); \
|
||||
_list_->buffer = malloc(capacity * sizeof(V)); \
|
||||
\
|
||||
if (!_list_->buffer) \
|
||||
{ \
|
||||
@@ -122,11 +158,14 @@
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_list_->buffer, 0, size * sizeof(V)); \
|
||||
memset(_list_->buffer, 0, capacity * sizeof(V)); \
|
||||
\
|
||||
_list_->capacity = size; \
|
||||
_list_->capacity = capacity; \
|
||||
_list_->count = 0; \
|
||||
\
|
||||
_list_->it_start = PFX##_impl_it_start; \
|
||||
_list_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _list_; \
|
||||
} \
|
||||
\
|
||||
@@ -164,7 +203,7 @@
|
||||
{ \
|
||||
if (PFX##_full(_list_)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + 1)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + 1)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -196,7 +235,7 @@
|
||||
\
|
||||
if (PFX##_full(_list_)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + 1)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + 1)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -213,7 +252,7 @@
|
||||
{ \
|
||||
if (PFX##_full(_list_)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + 1)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + 1)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -291,7 +330,7 @@
|
||||
\
|
||||
if (!PFX##_fits(_list_, size)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + size)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + size)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -317,7 +356,7 @@
|
||||
{ \
|
||||
if (!PFX##_fits(_list_, size)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + size)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + size)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -338,7 +377,7 @@
|
||||
\
|
||||
if (!PFX##_fits(_list_, size)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_list_, _list_->count + size)) \
|
||||
if (!PFX##_impl_grow(_list_, _list_->count + size)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
@@ -517,27 +556,25 @@
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_tostart(SNAME##_iter *iter) \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_toend(SNAME##_iter *iter) \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->cursor = PFX##_empty(iter->target) ? 0 : iter->target->count - 1; \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter, V *result, size_t *index) \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
*index = iter->cursor; \
|
||||
*result = iter->target->buffer[iter->cursor]; \
|
||||
iter->start = false; \
|
||||
\
|
||||
if (iter->cursor == iter->target->count - 1) \
|
||||
@@ -548,13 +585,11 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter, V *result, size_t *index) \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
*index = iter->cursor; \
|
||||
*result = iter->target->buffer[iter->cursor]; \
|
||||
iter->end = false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
@@ -565,7 +600,28 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_grow(SNAME *_list_, size_t required) \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_grow(SNAME *_list_, size_t required) \
|
||||
{ \
|
||||
size_t new_capacity = _list_->capacity * 2; \
|
||||
\
|
||||
@@ -581,6 +637,26 @@
|
||||
_list_->capacity = new_capacity; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_list_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _list_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_list_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _list_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_LIST_H */
|
||||
Reference in new issue
Block a user