More things added to EXT

This commit is contained in:
Leonardo Vencovsky committed 2020-06-07 16:30:23 -03:00
1 parent 649f02665f
commit 1987152b03
4 files changed
+327 -259

No files matched your search

+1 -259
View File
@@ -33,20 +33,6 @@
* ------------------------------------------------------------------------- */
#include "../cor/core.h"
/* -------------------------------------------------------------------------
* Stack specific
* ------------------------------------------------------------------------- */
/* to_string format */
static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> "
"at %p { "
"buffer:%p, "
"capacity:%" PRIuMAX ", "
"count:%" PRIuMAX ", "
"flag:%d, "
"f_val:%p, "
"alloc:%p, "
"callbacks:%p }";
/**
* Core Stack implementation
*/
@@ -111,22 +97,6 @@ static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> "
/* Priority function */ \
CMC_DEF_FTAB_PRI(V); \
}; \
\
/* Stack Iterator */ \
struct CMC_DEF_ITER(SNAME) \
{ \
/* Target stack */ \
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 */ \
@@ -153,29 +123,7 @@ static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> "
/* Collection Utility */ \
bool CMC_(PFX, _resize)(struct SNAME * _stack_, size_t capacity); \
struct SNAME *CMC_(PFX, _copy_of)(struct SNAME * _stack_); \
bool CMC_(PFX, _equals)(struct SNAME * _stack1_, struct SNAME * _stack2_); \
struct cmc_string CMC_(PFX, _to_string)(struct SNAME * _stack_); \
bool CMC_(PFX, _print)(struct SNAME * _stack_, 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); \
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter); \
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter);
bool CMC_(PFX, _equals)(struct SNAME * _stack1_, struct SNAME * _stack2_);
/* -------------------------------------------------------------------------
* Source
@@ -436,212 +384,6 @@ static const char *cmc_cmc_string_fmt_stack = "struct %s<%s> "
} \
\
return true; \
} \
\
struct cmc_string CMC_(PFX, _to_string)(struct SNAME * _stack_) \
{ \
struct cmc_string str; \
struct SNAME *s_ = _stack_; \
\
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_stack, #SNAME, #V, s_, s_->buffer, s_->capacity, \
s_->count, s_->flag, s_->f_val, s_->alloc, s_->callbacks); \
\
return n >= 0 ? str : (struct cmc_string){ 0 }; \
} \
\
bool CMC_(PFX, _print)(struct SNAME * _stack_, FILE * fptr) \
{ \
for (size_t i = 0; i < _stack_->count; i++) \
{ \
if (!_stack_->f_val->str(fptr, _stack_->buffer[i])) \
return false; \
} \
\
return true; \
} \
\
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target) \
{ \
struct CMC_DEF_ITER(SNAME) iter; \
\
iter.target = target; \
iter.cursor = 0; \
iter.start = true; \
iter.end = CMC_(PFX, _empty)(target); \
\
if (!CMC_(PFX, _empty)(target)) \
iter.cursor = target->count - 1; \
\
return iter; \
} \
\
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target) \
{ \
struct CMC_DEF_ITER(SNAME) iter; \
\
iter.target = target; \
iter.cursor = 0; \
iter.start = CMC_(PFX, _empty)(target); \
iter.end = true; \
\
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 = iter->target->count - 1; \
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->cursor = 0; \
iter->start = CMC_(PFX, _empty)(iter->target); \
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 == 0) \
{ \
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 + 1 == iter->target->count) \
{ \
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 == 0) \
{ \
iter->end = true; \
return false; \
} \
\
if (steps == 0 || iter->cursor < steps) \
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 + 1 == iter->target->count) \
{ \
iter->start = true; \
return false; \
} \
\
if (steps == 0 || iter->cursor + steps >= iter->target->count) \
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_advance)(iter, iter->cursor - index); \
else if (iter->cursor < index) \
return CMC_(PFX, _iter_rewind)(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]; \
} \
\
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter) \
{ \
if (CMC_(PFX, _empty)(iter->target)) \
return NULL; \
\
return &(iter->target->buffer[iter->cursor]); \
} \
\
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter) \
{ \
if (CMC_(PFX, _empty)(iter->target)) \
return 0; \
\
return iter->target->count - 1 - iter->cursor; \
}
#endif /* CMC_CMC_STACK_H */
+315
View File
@@ -0,0 +1,315 @@
/**
* ext/cmc/stack.h
*
* Creation Date: 07/06/2020
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#ifndef CMC_EXT_CMC_STACK_H
#define CMC_EXT_CMC_STACK_H
#include "../../cor/core.h"
/**
* All the EXT parts of CMC Stack.
*/
#define CMC_EXT_CMC_STACK_PARTS ITER, STR
/**
* ITER
*/
#define CMC_EXT_CMC_STACK_ITER(PARAMS) \
CMC_EXT_CMC_STACK_ITER_HEADER(PARAMS) \
CMC_EXT_CMC_STACK_ITER_SOURCE(PARAMS)
#define CMC_EXT_CMC_STACK_ITER_HEADER(PARAMS) \
CMC_EXT_CMC_STACK_ITER_HEADER_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS))
#define CMC_EXT_CMC_STACK_ITER_SOURCE(PARAMS) \
CMC_EXT_CMC_STACK_ITER_SOURCE_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS))
#define CMC_EXT_CMC_STACK_ITER_HEADER_(PFX, SNAME, V) \
\
/* Stack Iterator */ \
struct CMC_DEF_ITER(SNAME) \
{ \
/* Target stack */ \
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); \
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter); \
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter);
#define CMC_EXT_CMC_STACK_ITER_SOURCE_(PFX, SNAME, V) \
\
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_start)(struct SNAME * target) \
{ \
struct CMC_DEF_ITER(SNAME) iter; \
\
iter.target = target; \
iter.cursor = 0; \
iter.start = true; \
iter.end = CMC_(PFX, _empty)(target); \
\
if (!CMC_(PFX, _empty)(target)) \
iter.cursor = target->count - 1; \
\
return iter; \
} \
\
struct CMC_DEF_ITER(SNAME) CMC_(PFX, _iter_end)(struct SNAME * target) \
{ \
struct CMC_DEF_ITER(SNAME) iter; \
\
iter.target = target; \
iter.cursor = 0; \
iter.start = CMC_(PFX, _empty)(target); \
iter.end = true; \
\
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 = iter->target->count - 1; \
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->cursor = 0; \
iter->start = CMC_(PFX, _empty)(iter->target); \
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 == 0) \
{ \
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 + 1 == iter->target->count) \
{ \
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 == 0) \
{ \
iter->end = true; \
return false; \
} \
\
if (steps == 0 || iter->cursor < steps) \
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 + 1 == iter->target->count) \
{ \
iter->start = true; \
return false; \
} \
\
if (steps == 0 || iter->cursor + steps >= iter->target->count) \
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_advance)(iter, iter->cursor - index); \
else if (iter->cursor < index) \
return CMC_(PFX, _iter_rewind)(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]; \
} \
\
V *CMC_(PFX, _iter_rvalue)(struct CMC_DEF_ITER(SNAME) * iter) \
{ \
if (CMC_(PFX, _empty)(iter->target)) \
return NULL; \
\
return &(iter->target->buffer[iter->cursor]); \
} \
\
size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter) \
{ \
if (CMC_(PFX, _empty)(iter->target)) \
return 0; \
\
return iter->target->count - 1 - iter->cursor; \
}
/**
* STR
*/
#define CMC_EXT_CMC_STACK_STR(PARAMS) \
CMC_EXT_CMC_STACK_STR_HEADER(PARAMS) \
CMC_EXT_CMC_STACK_STR_SOURCE(PARAMS)
#define CMC_EXT_CMC_STACK_STR_HEADER(PARAMS) \
CMC_EXT_CMC_STACK_STR_HEADER_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS))
#define CMC_EXT_CMC_STACK_STR_SOURCE(PARAMS) \
CMC_EXT_CMC_STACK_STR_SOURCE_(CMC_PARAM_PFX(PARAMS), CMC_PARAM_SNAME(PARAMS), CMC_PARAM_V(PARAMS))
#define CMC_EXT_CMC_STACK_STR_HEADER_(PFX, SNAME, V) \
\
bool CMC_(PFX, _to_string)(struct SNAME * _stack_, FILE * fptr); \
bool CMC_(PFX, _print)(struct SNAME * _stack_, FILE * fptr, const char *start, const char *separator, \
const char *end);
#define CMC_EXT_CMC_STACK_STR_SOURCE_(PFX, SNAME, V) \
\
bool CMC_(PFX, _to_string)(struct SNAME * _stack_, FILE * fptr) \
{ \
struct SNAME *s_ = _stack_; \
\
return 0 <= fprintf(fptr, \
"struct %s<%s> " \
"at %p { " \
"buffer:%p, " \
"capacity:%" PRIuMAX ", " \
"count:%" PRIuMAX ", " \
"flag:%d, " \
"f_val:%p, " \
"alloc:%p, " \
"callbacks:%p }", \
CMC_TO_STRING(SNAME), CMC_TO_STRING(V), s_, s_->buffer, s_->capacity, s_->count, s_->flag, \
s_->f_val, s_->alloc, CMC_CALLBACKS_GET(s_)); \
} \
\
bool CMC_(PFX, _print)(struct SNAME * _stack_, FILE * fptr, const char *start, const char *separator, \
const char *end) \
{ \
fprintf(fptr, "%s", start); \
\
for (size_t i = _stack_->count; i > 0; i--) \
{ \
if (!_stack_->f_val->str(fptr, _stack_->buffer[i - 1])) \
return false; \
\
if (i - 1 != 0) \
fprintf(fptr, "%s", separator); \
} \
\
fprintf(fptr, "%s", end); \
\
return true; \
}
#endif /* CMC_EXT_CMC_STACK_H */
+1
View File
@@ -65,6 +65,7 @@
#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 "ext/cmc/stack.h" /* Added in 07/06/2020 */
#include "utl/assert.h" /* Added in 27/06/2019 */
#include "utl/foreach.h" /* Added in 25/02/2019 */
+10
View File
@@ -15,6 +15,7 @@ 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))
C_MACRO_COLLECTIONS_ALL(CMC, STACK, ( s, stack, , , int))
struct
{
@@ -47,6 +48,7 @@ int main(void)
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);
struct stack *s = s_new(100, (struct stack_fval *)&int_ftab);
d_to_string(d, stdout);
d_print(d, stdout, "\n[ ", ", ", " ]\n");
@@ -72,6 +74,8 @@ int main(void)
q_print(q, stdout, "\n[ ", ", ", " ]\n");
sl_to_string(sl, stdout);
sl_print(sl, stdout, "\n[ ", ", ", " ]\n");
s_to_string(s, stdout);
s_print(s, stdout, "\n[ ", ", ", " ]\n");
fprintf(stdout, "\n");
@@ -87,6 +91,7 @@ int main(void)
l_push_back(l, 0);
q_enqueue(q, 0);
sl_insert(sl, 0);
s_push(s, 0);
d_to_string(d, stdout);
d_print(d, stdout, "\n[ ", ", ", " ]\n");
@@ -112,6 +117,8 @@ int main(void)
q_print(q, stdout, "\n[ ", ", ", " ]\n");
sl_to_string(sl, stdout);
sl_print(sl, stdout, "\n[ ", ", ", " ]\n");
s_to_string(s, stdout);
s_print(s, stdout, "\n[ ", ", ", " ]\n");
for (int i = 1; i < 20; i++)
{
@@ -127,6 +134,7 @@ int main(void)
l_push_back(l, i);
q_enqueue(q, i);
sl_insert(sl, i);
s_push(s, i);
}
fprintf(stdout, "\n");
@@ -155,6 +163,8 @@ int main(void)
q_print(q, stdout, "\n[ ", ", ", " ]\n");
sl_to_string(sl, stdout);
sl_print(sl, stdout, "\n[ ", ", ", " ]\n");
s_to_string(s, stdout);
s_print(s, stdout, "\n[ ", ", ", " ]\n");
d_free(d);
hbm_free(hbm);