Added list SAC and examples

This commit is contained in:
Leonardo Vencovsky committed 2023-02-02 09:57:15 +01:00
1 parent 82ac5a4748
commit b330790bf9
11 files changed
+268 -5

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
/* Shows an example of how to use CMC_DEV */
/**
* CMC_DEV is a macro that enables DEV mode. It comes with tracing of functions
* and other things that are printed in the terminal in order to debug the code.
*
* You can always compile this with -DCMC_DEV, but in this example we explicitly
* define it in the file.
*
* CMC_DEV uses the cmc/utl/log.h utility. Through it, you can customize the log
* levels and when to enable it.
*/
#define CMC_DEV
#define CMC_LOG_COLOR /* Optional */
#define V int
#define SNAME list
#define PFX l
#define CMC_EXT_ITER
#define CMC_EXT_STR
#include "cmc/list.h"
#include "cmc/utl/futils.h"
int main()
{
struct list *list = l_new(32, &(struct list_fval){ .str = cmc_i32_str });
if (!list || l_flag(list) != CMC_FLAG_OK)
{
#ifdef CMC_DEV
cmc_log_error("error: %s", cmc_flags_to_str[l_flag(list)]);
#endif
return 1;
}
#ifdef CMC_DEV
/* We don't want to debug this, so disable it for now */
cmc_log_config.enabled = false;
#endif
for (int i = 0; i < 100; i++)
{
if (!l_push_back(list, i))
{
enum cmc_flags flag = l_flag(list);
fprintf(stderr, "%s : push_back failed\n", cmc_flags_to_str[flag]);
}
}
/* Turn it back on on the operations that we actually care to debug */
#ifdef CMC_DEV
cmc_log_config.enabled = true;
#endif
for (struct list_iter it = l_iter_start(list); !l_iter_at_end(&it); l_iter_next(&it))
{
int *val = l_iter_rvalue(&it);
if (val != NULL)
*val *= 2;
}
if (!l_print(list, stdout, "[", ", ", "]\n"))
return 2;
// Free all of its resources
l_free(list);
}
+57
View File
@@ -0,0 +1,57 @@
/* Shows an example of how to use CMC_SAC */
/**
* CMC_SAC makes a data structure have a fixed size buffer to store data.
*/
#define CMC_SAC
#define V int
#define SNAME list
#define PFX l
#define SIZE 100 /* This sets the buffer size */
#define CMC_EXT_ITER
#define CMC_EXT_STR
#define CMC_EXT_INIT
#define CMC_EXT_SEQ
#include "cmc/list.h"
#include "cmc/utl/futils.h"
int main()
{
struct list list = l_init(&(struct list_fval){ .str = cmc_i32_str });
if (l_flag(&list) != CMC_FLAG_OK)
return 1;
for (int i = 1; i < 1000; i++)
{
if (!l_push_back(&list, i))
{
if (l_flag(&list) == CMC_FLAG_FULL)
{
printf("List full, stopping...\n");
break;
}
else
{
enum cmc_flags flag = l_flag(&list);
fprintf(stderr, "%s : push_back failed\n", cmc_flags_to_str[flag]);
}
}
}
for (struct list_iter it = l_iter_start(&list); !l_iter_at_end(&it); l_iter_next(&it))
{
int *val = l_iter_rvalue(&it);
if (val != NULL)
*val *= 2;
}
if (!l_print(&list, stdout, "[", ", ", "]\n"))
return 2;
// Free all of its resources
l_release(list);
}