diff --git a/src/cmc/bitset.h b/src/cmc/bitset.h index 25e4915..f4b2a09 100644 --- a/src/cmc/bitset.h +++ b/src/cmc/bitset.h @@ -206,7 +206,7 @@ static const char *cmc_cmc_string_fmt_bitset = "struct %s " struct SNAME CMC_(PFX, _init_custom)(size_t n_bits, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * CMC_UNUSED(callbacks)) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ struct SNAME _bitset_ = { 0 }; \ \ @@ -245,7 +245,7 @@ static const char *cmc_cmc_string_fmt_bitset = "struct %s " void CMC_(PFX, _customize)(struct SNAME * _bitset_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _bitset_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/deque.h b/src/cmc/deque.h index 74c3956..d2783b4 100644 --- a/src/cmc/deque.h +++ b/src/cmc/deque.h @@ -155,7 +155,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -231,7 +231,7 @@ void CMC_(PFX, _customize)(struct SNAME * _deque_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _deque_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/hashbidimap.h b/src/cmc/hashbidimap.h index e5ef175..9a92328 100644 --- a/src/cmc/hashbidimap.h +++ b/src/cmc/hashbidimap.h @@ -262,14 +262,14 @@ struct SNAME *CMC_(PFX, _new)(size_t capacity, double load, struct CMC_DEF_FKEY(SNAME) * f_key, \ struct CMC_DEF_FVAL(SNAME) * f_val) \ { \ - return CMC_(PFX, _new_custom)(capacity, load, f_key, f_val, NULL, NULL);\ + return CMC_(PFX, _new_custom)(capacity, load, f_key, f_val, NULL, NULL); \ } \ \ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DEF_FKEY(SNAME) * f_key, \ struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || load <= 0 || load >= 1) \ return NULL; \ @@ -350,7 +350,7 @@ void CMC_(PFX, _customize)(struct SNAME * _map_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _map_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/hashmap.h b/src/cmc/hashmap.h index 2eb00a9..c1d7529 100644 --- a/src/cmc/hashmap.h +++ b/src/cmc/hashmap.h @@ -194,7 +194,7 @@ struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || load <= 0 || load >= 1) \ return NULL; \ @@ -285,7 +285,7 @@ void CMC_(PFX, _customize)(struct SNAME * _map_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _map_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/hashmultimap.h b/src/cmc/hashmultimap.h index ac79010..b6eb64e 100644 --- a/src/cmc/hashmultimap.h +++ b/src/cmc/hashmultimap.h @@ -223,7 +223,7 @@ struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || load <= 0) \ return NULL; \ @@ -339,7 +339,7 @@ void CMC_(PFX, _customize)(struct SNAME * _map_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _map_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/hashmultiset.h b/src/cmc/hashmultiset.h index 673613b..0ee8fc2 100644 --- a/src/cmc/hashmultiset.h +++ b/src/cmc/hashmultiset.h @@ -177,7 +177,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || load <= 0 || load >= 1) \ return NULL; \ @@ -262,7 +262,7 @@ void CMC_(PFX, _customize)(struct SNAME * _set_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _set_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/hashset.h b/src/cmc/hashset.h index 7acb6be..1cc9b87 100644 --- a/src/cmc/hashset.h +++ b/src/cmc/hashset.h @@ -156,7 +156,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || load <= 0 || load >= 1) \ return NULL; \ @@ -240,7 +240,7 @@ void CMC_(PFX, _customize)(struct SNAME * _set_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _set_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/heap.h b/src/cmc/heap.h index d0d52d2..6d0d19e 100644 --- a/src/cmc/heap.h +++ b/src/cmc/heap.h @@ -152,7 +152,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, enum cmc_heap_order HO, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -223,7 +223,7 @@ void CMC_(PFX, _customize)(struct SNAME * _heap_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _heap_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/intervalheap.h b/src/cmc/intervalheap.h index ad04db3..ac82682 100644 --- a/src/cmc/intervalheap.h +++ b/src/cmc/intervalheap.h @@ -146,7 +146,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity == 0 || capacity == UINTMAX_MAX) \ return NULL; \ @@ -218,7 +218,7 @@ void CMC_(PFX, _customize)(struct SNAME * _heap_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _heap_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/linkedlist.h b/src/cmc/linkedlist.h index 3e223f6..af1f82c 100644 --- a/src/cmc/linkedlist.h +++ b/src/cmc/linkedlist.h @@ -158,7 +158,7 @@ struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!f_val) \ return NULL; \ @@ -216,7 +216,7 @@ void CMC_(PFX, _customize)(struct SNAME * _list_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _list_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/list.h b/src/cmc/list.h index 0a53453..1e67123 100644 --- a/src/cmc/list.h +++ b/src/cmc/list.h @@ -165,7 +165,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -228,7 +228,7 @@ void CMC_(PFX, _customize)(struct SNAME * _list_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _list_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/queue.h b/src/cmc/queue.h index 652b715..abbc466 100644 --- a/src/cmc/queue.h +++ b/src/cmc/queue.h @@ -181,7 +181,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -256,7 +256,7 @@ void CMC_(PFX, _customize)(struct SNAME * _queue_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _queue_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/sortedlist.h b/src/cmc/sortedlist.h index 847f047..34879ed 100644 --- a/src/cmc/sortedlist.h +++ b/src/cmc/sortedlist.h @@ -27,21 +27,6 @@ * ------------------------------------------------------------------------- */ #include "../cor/core.h" -/* ------------------------------------------------------------------------- - * SortedList specific - * ------------------------------------------------------------------------- */ -/* to_string format */ -static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " - "at %p { " - "buffer:%p, " - "capacity:%" PRIuMAX ", " - "count:%" PRIuMAX ", " - "is_sorted:%s, " - "flag:%d, " - "f_val:%p, " - "alloc:%p, " - "callbacks:%p }"; - /** * Core SortedList implementation */ @@ -109,22 +94,6 @@ static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " /* Priority function */ \ CMC_DEF_FTAB_PRI(V); \ }; \ -\ - /* List Iterator */ \ - struct CMC_DEF_ITER(SNAME) \ - { \ - /* Target list */ \ - struct SNAME *target; \ -\ - /* Cursor's position (index) */ \ - 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; \ - }; \ \ /* Collection Functions */ \ /* Collection Allocation and Deallocation */ \ @@ -155,28 +124,7 @@ static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " bool CMC_(PFX, _resize)(struct SNAME * _list_, size_t capacity); \ void CMC_(PFX, _sort)(struct SNAME * _list_); \ struct SNAME *CMC_(PFX, _copy_of)(struct SNAME * _list_); \ - bool CMC_(PFX, _equals)(struct SNAME * _list1_, struct SNAME * _list2_); \ - struct cmc_string CMC_(PFX, _to_string)(struct SNAME * _list_); \ - bool CMC_(PFX, _print)(struct SNAME * _list_, FILE * fptr); \ -\ - /* Iterator Functions */ \ - /* Iterator Initialization */ \ - struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target); \ - struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target); \ - /* Iterator State */ \ - bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter); \ - bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter); \ - /* Iterator Movement */ \ - bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter); \ - bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter); \ - bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter); \ - bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter); \ - bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps); \ - bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps); \ - bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index); \ - /* Iterator Access */ \ - V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter); \ - size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter); + bool CMC_(PFX, _equals)(struct SNAME * _list1_, struct SNAME * _list2_); /* ------------------------------------------------------------------------- * Source @@ -197,7 +145,7 @@ static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -258,7 +206,7 @@ static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " void CMC_(PFX, _customize)(struct SNAME * _list_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _list_->alloc = &cmc_alloc_node_default; \ @@ -514,208 +462,6 @@ static const char *cmc_cmc_string_fmt_sortedlist = "struct %s<%s> " \ return true; \ } \ -\ - struct cmc_string CMC_(PFX, _to_string)(struct SNAME * _list_) \ - { \ - struct cmc_string str; \ - struct SNAME *l_ = _list_; \ -\ - int n = \ - snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_sortedlist, #SNAME, #V, l_, l_->buffer, l_->capacity, \ - l_->count, l_->is_sorted ? "true" : "false", l_->flag, l_->f_val, l_->alloc, l_->callbacks); \ -\ - return n >= 0 ? str : (struct cmc_string){ 0 }; \ - } \ -\ - bool CMC_(PFX, _print)(struct SNAME * _list_, FILE * fptr) \ - { \ - CMC_(PFX, _sort)(_list_); \ -\ - for (size_t i = 0; i < _list_->count; i++) \ - { \ - if (!_list_->f_val->str(fptr, _list_->buffer[i])) \ - return false; \ - } \ -\ - return true; \ - } \ -\ - struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target) \ - { \ - CMC_(PFX, _sort)(target); \ -\ - struct CMC_DEF_ITER(SNAME) iter; \ -\ - iter.target = target; \ - iter.cursor = 0; \ - iter.start = true; \ - iter.end = CMC_(PFX, _empty)(target); \ -\ - return iter; \ - } \ -\ - struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target) \ - { \ - CMC_(PFX, _sort)(target); \ -\ - struct CMC_DEF_ITER(SNAME) iter; \ -\ - iter.target = target; \ - iter.cursor = 0; \ - iter.start = CMC_(PFX, _empty)(target); \ - iter.end = true; \ -\ - if (!CMC_(PFX, _empty)(target)) \ - iter.cursor = target->count - 1; \ -\ - return iter; \ - } \ -\ - bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - return CMC_(PFX, _empty)(iter->target) || iter->start; \ - } \ -\ - bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - return CMC_(PFX, _empty)(iter->target) || iter->end; \ - } \ -\ - bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - if (!CMC_(PFX, _empty)(iter->target)) \ - { \ - iter->cursor = 0; \ - iter->start = true; \ - iter->end = CMC_(PFX, _empty)(iter->target); \ -\ - return true; \ - } \ -\ - return false; \ - } \ -\ - bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - if (!CMC_(PFX, _empty)(iter->target)) \ - { \ - iter->start = CMC_(PFX, _empty)(iter->target); \ - iter->cursor = iter->target->count - 1; \ - iter->end = true; \ -\ - return true; \ - } \ -\ - return false; \ - } \ -\ - bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - if (iter->end) \ - return false; \ -\ - if (iter->cursor + 1 == iter->target->count) \ - { \ - iter->end = true; \ - return false; \ - } \ -\ - iter->start = CMC_(PFX, _empty)(iter->target); \ -\ - iter->cursor++; \ -\ - return true; \ - } \ -\ - bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - if (iter->start) \ - return false; \ -\ - if (iter->cursor == 0) \ - { \ - iter->start = true; \ - return false; \ - } \ -\ - iter->end = CMC_(PFX, _empty)(iter->target); \ -\ - iter->cursor--; \ -\ - return true; \ - } \ -\ - /* Returns true only if the iterator moved */ \ - bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps) \ - { \ - if (iter->end) \ - return false; \ -\ - if (iter->cursor + 1 == iter->target->count) \ - { \ - iter->end = true; \ - return false; \ - } \ -\ - if (steps == 0 || iter->cursor + steps >= iter->target->count) \ - return false; \ -\ - iter->start = CMC_(PFX, _empty)(iter->target); \ -\ - iter->cursor += steps; \ -\ - return true; \ - } \ -\ - /* Returns true only if the iterator moved */ \ - bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps) \ - { \ - if (iter->start) \ - return false; \ -\ - if (iter->cursor == 0) \ - { \ - iter->start = true; \ - return false; \ - } \ -\ - if (steps == 0 || iter->cursor < steps) \ - return false; \ -\ - iter->end = CMC_(PFX, _empty)(iter->target); \ -\ - iter->cursor -= steps; \ -\ - return true; \ - } \ -\ - /* Returns true only if the iterator was able to be positioned at the */ \ - /* given index */ \ - bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index) \ - { \ - if (index >= iter->target->count) \ - return false; \ -\ - if (iter->cursor > index) \ - return CMC_(PFX, _iter_rewind)(iter, iter->cursor - index); \ - else if (iter->cursor < index) \ - return CMC_(PFX, _iter_advance)(iter, index - iter->cursor); \ -\ - return true; \ - } \ -\ - V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - if (CMC_(PFX, _empty)(iter->target)) \ - return (V){ 0 }; \ -\ - return iter->target->buffer[iter->cursor]; \ - } \ -\ - size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter) \ - { \ - return iter->cursor; \ - } \ \ static size_t CMC_(PFX, _impl_binary_search_first)(struct SNAME * _list_, V value) \ { \ diff --git a/src/cmc/stack.h b/src/cmc/stack.h index 0afa5a3..9a0bf53 100644 --- a/src/cmc/stack.h +++ b/src/cmc/stack.h @@ -193,7 +193,7 @@ static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> " struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (capacity < 1) \ return NULL; \ @@ -255,7 +255,7 @@ static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> " void CMC_(PFX, _customize)(struct SNAME * _stack_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _stack_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/treemap.h b/src/cmc/treemap.h index e653751..e9479b1 100644 --- a/src/cmc/treemap.h +++ b/src/cmc/treemap.h @@ -247,7 +247,7 @@ static const char *cmc_cmc_string_fmt_treemap = "struct %s<%s, %s> " struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FKEY(SNAME) * f_key, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!f_key || !f_val) \ return NULL; \ @@ -347,7 +347,7 @@ static const char *cmc_cmc_string_fmt_treemap = "struct %s<%s, %s> " void CMC_(PFX, _customize)(struct SNAME * _map_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _map_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cmc/treeset.h b/src/cmc/treeset.h index 300a340..e4349d5 100644 --- a/src/cmc/treeset.h +++ b/src/cmc/treeset.h @@ -232,7 +232,7 @@ static const char *cmc_cmc_string_fmt_treeset = "struct %s<%s> " struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!f_val) \ return NULL; \ @@ -327,7 +327,7 @@ static const char *cmc_cmc_string_fmt_treeset = "struct %s<%s> " void CMC_(PFX, _customize)(struct SNAME * _set_, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ if (!alloc) \ _set_->alloc = &cmc_alloc_node_default; \ diff --git a/src/cor/core.h b/src/cor/core.h index 5c13c38..dc0f400 100644 --- a/src/cor/core.h +++ b/src/cor/core.h @@ -102,8 +102,7 @@ * Marks a function parameter used or unused depending on the expanded value of * MACRO. It need to expand to either TRUE or FALSE. */ -#define CMC_UNUSED_PARAM(MACRO, PARAM) \ - CMC_(CMC_UNUSED_PARAM_, MACRO)(PARAM)\ +#define CMC_UNUSED_PARAM(MACRO, PARAM) CMC_(CMC_UNUSED_PARAM_, MACRO)(PARAM) #define CMC_UNUSED_PARAM_FALSE(PARAM) PARAM #define CMC_UNUSED_PARAM_TRUE(PARAM) diff --git a/src/ext/cmc/deque.h b/src/ext/cmc/deque.h index dca4386..fb19ec6 100644 --- a/src/ext/cmc/deque.h +++ b/src/ext/cmc/deque.h @@ -51,7 +51,7 @@ struct SNAME CMC_(PFX, _init_custom)(size_t capacity, struct CMC_DEF_FVAL(SNAME) * f_val, \ struct CMC_ALLOC_NODE_NAME * alloc, struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ struct SNAME _deque_ = { 0 }; \ \ diff --git a/src/ext/cmc/hashmap.h b/src/ext/cmc/hashmap.h index 320c8c6..89a87de 100644 --- a/src/ext/cmc/hashmap.h +++ b/src/ext/cmc/hashmap.h @@ -57,7 +57,7 @@ struct CMC_DEF_FVAL(SNAME) * f_val, struct CMC_ALLOC_NODE_NAME * alloc, \ struct CMC_CALLBACKS_NAME * callbacks) \ { \ - CMC_CALLBACKS_MAYBE_UNUSED(callbacks);\ + CMC_CALLBACKS_MAYBE_UNUSED(callbacks); \ \ struct SNAME _map_ = { 0 }; \ \ diff --git a/src/ext/cmc/list.h b/src/ext/cmc/list.h index 63b2294..ad971c8 100644 --- a/src/ext/cmc/list.h +++ b/src/ext/cmc/list.h @@ -406,7 +406,8 @@ \ size_t length = to - from + 1; \ \ - struct SNAME *result = CMC_(PFX, _new_custom)(length, _list_->f_val, _list_->alloc, CMC_CALLBACKS_GET(_list_)); \ + struct SNAME *result = \ + CMC_(PFX, _new_custom)(length, _list_->f_val, _list_->alloc, CMC_CALLBACKS_GET(_list_)); \ \ if (!result) \ { \ diff --git a/src/ext/cmc/sortedlist.h b/src/ext/cmc/sortedlist.h new file mode 100644 index 0000000..fdfe0e4 --- /dev/null +++ b/src/ext/cmc/sortedlist.h @@ -0,0 +1,310 @@ +/** + * ext/cmc/sortedlist.h + * + * Creation Date: 06/06/2020 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ + +#ifndef CMC_EXT_CMC_SORTEDLIST_H +#define CMC_EXT_CMC_SORTEDLIST_H + +#include "../../cor/core.h" + +/** + * All the EXT parts of CMC SortedList. + */ +#define CMC_EXT_CMC_SORTEDLIST_PARTS ITER, STR + +/** + * ITER + */ +#define CMC_EXT_CMC_SORTEDLIST_ITER(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_ITER_HEADER(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_ITER_SOURCE(PARAMS) + +#define CMC_EXT_CMC_SORTEDLIST_ITER_HEADER(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_ITER_HEADER_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS)) + +#define CMC_EXT_CMC_SORTEDLIST_ITER_SOURCE(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_ITER_SOURCE_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS)) + +#define CMC_EXT_CMC_SORTEDLIST_ITER_HEADER_(PFX, SNAME, V) \ +\ + /* List Iterator */ \ + struct CMC_DEF_ITER(SNAME) \ + { \ + /* Target list */ \ + struct SNAME *target; \ +\ + /* Cursor's position (index) */ \ + 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; \ + }; \ +\ + /* Iterator Initialization */ \ + struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target); \ + struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target); \ + /* Iterator State */ \ + bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter); \ + bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter); \ + /* Iterator Movement */ \ + bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter); \ + bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter); \ + bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter); \ + bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter); \ + bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps); \ + bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps); \ + bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index); \ + /* Iterator Access */ \ + V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter); \ + size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter); + +#define CMC_EXT_CMC_SORTEDLIST_ITER_SOURCE_(PFX, SNAME, V) \ +\ + struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target) \ + { \ + CMC_(PFX, _sort)(target); \ +\ + struct CMC_DEF_ITER(SNAME) iter; \ +\ + iter.target = target; \ + iter.cursor = 0; \ + iter.start = true; \ + iter.end = CMC_(PFX, _empty)(target); \ +\ + return iter; \ + } \ +\ + struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target) \ + { \ + CMC_(PFX, _sort)(target); \ +\ + struct CMC_DEF_ITER(SNAME) iter; \ +\ + iter.target = target; \ + iter.cursor = 0; \ + iter.start = CMC_(PFX, _empty)(target); \ + iter.end = true; \ +\ + if (!CMC_(PFX, _empty)(target)) \ + iter.cursor = target->count - 1; \ +\ + return iter; \ + } \ +\ + bool CMC_(PFX, _iter_at_start)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + return CMC_(PFX, _empty)(iter->target) || iter->start; \ + } \ +\ + bool CMC_(PFX, _iter_at_end)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + return CMC_(PFX, _empty)(iter->target) || iter->end; \ + } \ +\ + bool CMC_(PFX, _iter_to_start)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + if (!CMC_(PFX, _empty)(iter->target)) \ + { \ + iter->cursor = 0; \ + iter->start = true; \ + iter->end = CMC_(PFX, _empty)(iter->target); \ +\ + return true; \ + } \ +\ + return false; \ + } \ +\ + bool CMC_(PFX, _iter_to_end)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + if (!CMC_(PFX, _empty)(iter->target)) \ + { \ + iter->start = CMC_(PFX, _empty)(iter->target); \ + iter->cursor = iter->target->count - 1; \ + iter->end = true; \ +\ + return true; \ + } \ +\ + return false; \ + } \ +\ + bool CMC_(PFX, _iter_next)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + if (iter->end) \ + return false; \ +\ + if (iter->cursor + 1 == iter->target->count) \ + { \ + iter->end = true; \ + return false; \ + } \ +\ + iter->start = CMC_(PFX, _empty)(iter->target); \ +\ + iter->cursor++; \ +\ + return true; \ + } \ +\ + bool CMC_(PFX, _iter_prev)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + if (iter->start) \ + return false; \ +\ + if (iter->cursor == 0) \ + { \ + iter->start = true; \ + return false; \ + } \ +\ + iter->end = CMC_(PFX, _empty)(iter->target); \ +\ + iter->cursor--; \ +\ + return true; \ + } \ +\ + /* Returns true only if the iterator moved */ \ + bool CMC_(PFX, _iter_advance)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps) \ + { \ + if (iter->end) \ + return false; \ +\ + if (iter->cursor + 1 == iter->target->count) \ + { \ + iter->end = true; \ + return false; \ + } \ +\ + if (steps == 0 || iter->cursor + steps >= iter->target->count) \ + return false; \ +\ + iter->start = CMC_(PFX, _empty)(iter->target); \ +\ + iter->cursor += steps; \ +\ + return true; \ + } \ +\ + /* Returns true only if the iterator moved */ \ + bool CMC_(PFX, _iter_rewind)(struct CMC_DEF_ITER(SNAME) * iter, size_t steps) \ + { \ + if (iter->start) \ + return false; \ +\ + if (iter->cursor == 0) \ + { \ + iter->start = true; \ + return false; \ + } \ +\ + if (steps == 0 || iter->cursor < steps) \ + return false; \ +\ + iter->end = CMC_(PFX, _empty)(iter->target); \ +\ + iter->cursor -= steps; \ +\ + return true; \ + } \ +\ + /* Returns true only if the iterator was able to be positioned at the */ \ + /* given index */ \ + bool CMC_(PFX, _iter_go_to)(struct CMC_DEF_ITER(SNAME) * iter, size_t index) \ + { \ + if (index >= iter->target->count) \ + return false; \ +\ + if (iter->cursor > index) \ + return CMC_(PFX, _iter_rewind)(iter, iter->cursor - index); \ + else if (iter->cursor < index) \ + return CMC_(PFX, _iter_advance)(iter, index - iter->cursor); \ +\ + return true; \ + } \ +\ + V CMC_(PFX, _iter_value)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + if (CMC_(PFX, _empty)(iter->target)) \ + return (V){ 0 }; \ +\ + return iter->target->buffer[iter->cursor]; \ + } \ +\ + size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter) \ + { \ + return iter->cursor; \ + } + +/** + * STR + */ +#define CMC_EXT_CMC_SORTEDLIST_STR(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_STR_HEADER(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_STR_SOURCE(PARAMS) + +#define CMC_EXT_CMC_SORTEDLIST_STR_HEADER(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_STR_HEADER_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS)) + +#define CMC_EXT_CMC_SORTEDLIST_STR_SOURCE(PARAMS) \ + CMC_EXT_CMC_SORTEDLIST_STR_SOURCE_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS)) + +#define CMC_EXT_CMC_SORTEDLIST_STR_HEADER_(PFX, SNAME, V) \ +\ + bool CMC_(PFX, _to_string)(struct SNAME * _list_, FILE * fptr); \ + bool CMC_(PFX, _print)(struct SNAME * _list_, FILE * fptr, const char *start, const char *separator, \ + const char *end); + +#define CMC_EXT_CMC_SORTEDLIST_STR_SOURCE_(PFX, SNAME, V) \ +\ + bool CMC_(PFX, _to_string)(struct SNAME * _list_, FILE * fptr) \ + { \ + struct SNAME *l_ = _list_; \ +\ + return 0 <= fprintf(fptr, \ + "struct %s<%s> " \ + "at %p { " \ + "buffer:%p, " \ + "capacity:%" PRIuMAX ", " \ + "count:%" PRIuMAX ", " \ + "is_sorted:%s, " \ + "flag:%d, " \ + "f_val:%p, " \ + "alloc:%p, " \ + "callbacks:%p }", \ + CMC_TO_STRING(SNAME), CMC_TO_STRING(V), l_, l_->buffer, l_->capacity, l_->count, \ + l_->is_sorted ? "true" : "false", l_->flag, l_->f_val, l_->alloc, CMC_CALLBACKS_GET(l_)); \ + } \ +\ + bool CMC_(PFX, _print)(struct SNAME * _list_, FILE * fptr, const char *start, const char *separator, \ + const char *end) \ + { \ + CMC_(PFX, _sort)(_list_); \ +\ + fprintf(fptr, "%s", start); \ +\ + for (size_t i = 0; i < _list_->count; i++) \ + { \ + if (!_list_->f_val->str(fptr, _list_->buffer[i])) \ + return false; \ +\ + if (i + 1 < _list_->count) \ + fprintf(fptr, "%s", separator); \ + } \ +\ + fprintf(fptr, "%s", end); \ +\ + return true; \ + } + +#endif /* CMC_EXT_CMC_SORTEDLIST_H */ diff --git a/src/macro_collections.h b/src/macro_collections.h index 59301d3..fcd7c96 100644 --- a/src/macro_collections.h +++ b/src/macro_collections.h @@ -64,6 +64,7 @@ #include "ext/cmc/linkedlist.h" /* Added in 03/06/2020 */ #include "ext/cmc/list.h" /* Added in 04/06/2020 */ #include "ext/cmc/queue.h" /* Added in 05/06/2020 */ +#include "ext/cmc/sortedlist.h" /* Added in 06/06/2020 */ #include "utl/assert.h" /* Added in 27/06/2019 */ #include "utl/foreach.h" /* Added in 25/02/2019 */ diff --git a/tests/to_string.c b/tests/to_string.c index e05c0f3..a4de801 100644 --- a/tests/to_string.c +++ b/tests/to_string.c @@ -14,6 +14,7 @@ C_MACRO_COLLECTIONS_ALL(CMC, INTERVALHEAP, ( ih, intervalheap, , , int)) C_MACRO_COLLECTIONS_ALL(CMC, LINKEDLIST, ( ll, linkedlist, , , int)) C_MACRO_COLLECTIONS_ALL(CMC, LIST, ( l, list, , , int)) C_MACRO_COLLECTIONS_ALL(CMC, QUEUE, ( q, queue, , , int)) +C_MACRO_COLLECTIONS_ALL(CMC, SORTEDLIST, ( sl, sortedlist, , , int)) struct { @@ -45,6 +46,7 @@ int main(void) struct linkedlist *ll = ll_new((struct linkedlist_fval *)&int_ftab); struct list *l = l_new(100, (struct list_fval *)&int_ftab); struct queue *q = q_new(100, (struct queue_fval *)&int_ftab); + struct sortedlist *sl = sl_new(100, (struct sortedlist_fval *)&int_ftab); d_to_string(d, stdout); d_print(d, stdout, "\n[ ", ", ", " ]\n"); @@ -68,6 +70,8 @@ int main(void) l_print(l, stdout, "\n[ ", ", ", " ]\n"); q_to_string(q, stdout); q_print(q, stdout, "\n[ ", ", ", " ]\n"); + sl_to_string(sl, stdout); + sl_print(sl, stdout, "\n[ ", ", ", " ]\n"); fprintf(stdout, "\n"); @@ -82,6 +86,7 @@ int main(void) ll_push_back(ll, 0); l_push_back(l, 0); q_enqueue(q, 0); + sl_insert(sl, 0); d_to_string(d, stdout); d_print(d, stdout, "\n[ ", ", ", " ]\n"); @@ -105,6 +110,8 @@ int main(void) l_print(l, stdout, "\n[ ", ", ", " ]\n"); q_to_string(q, stdout); q_print(q, stdout, "\n[ ", ", ", " ]\n"); + sl_to_string(sl, stdout); + sl_print(sl, stdout, "\n[ ", ", ", " ]\n"); for (int i = 1; i < 20; i++) { @@ -119,6 +126,7 @@ int main(void) ll_push_back(ll, i); l_push_back(l, i); q_enqueue(q, i); + sl_insert(sl, i); } fprintf(stdout, "\n"); @@ -145,6 +153,8 @@ int main(void) l_print(l, stdout, "\n[ ", ", ", " ]\n"); q_to_string(q, stdout); q_print(q, stdout, "\n[ ", ", ", " ]\n"); + sl_to_string(sl, stdout); + sl_print(sl, stdout, "\n[ ", ", ", " ]\n"); d_free(d); hbm_free(hbm); @@ -157,6 +167,7 @@ int main(void) ll_free(ll); l_free(l); q_free(q); + sl_free(sl); } // clang-format on