From 86928b4b221b16704c57a3482ab4e1e584673a05 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Tue, 16 Aug 2022 21:11:07 +0300 Subject: [PATCH] Add examples --- examples/Makefile | 40 +++++++++++++++++++++++++++++++ examples/array.c | 37 ++++++++++++++++++++++++++++ examples/dict.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ examples/list.c | 51 +++++++++++++++++++++++++++++++++++++++ examples/map.c | 55 ++++++++++++++++++++++++++++++++++++++++++ examples/set.c | 38 +++++++++++++++++++++++++++++ examples/string.c | 42 ++++++++++++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/array.c create mode 100644 examples/dict.c create mode 100644 examples/list.c create mode 100644 examples/map.c create mode 100644 examples/set.c create mode 100644 examples/string.c diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..2d99f6b --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,40 @@ +# This software is licensed by the MIT License, see LICENSE file +# Copyright © 2022 Gregory Lirent + +######################################################################################################################## + +BUILD_PATH ?= ./bin + +######################################################################################################################## + +CC = clang +MKDIR = mkdir -p +RMRF = rm -rf +AR = ar crs +CP = cp + +######################################################################################################################## + +examples: modules +examples: $(addprefix $(BUILD_PATH)/,$(notdir $(basename $(wildcard ./*.c)))) + +$(BUILD_PATH)/%: ./%.c | $(BUILD_PATH)/ + $(CC) $^ ../bin/release/libcdsb.a ../modules/libunic/bin/libunic.a -o $@ $(CFLAGS) -O2 -Wall + + +$(BUILD_PATH)/: + $(MKDIR) $@ +$(BUILD_PATH)/obj/: | $(BUILD_PATH)/ + $(MKDIR) $@ + +clean: + $(RMRF) ./bin/ + cd ../ && $(MAKE) clean + +######################################################################################################################## + +FORCE: +modules: ../bin/release/libcdsb.a + +../bin/release/libcdsb.a: FORCE + cd ../ && $(MAKE) release diff --git a/examples/array.c b/examples/array.c new file mode 100644 index 0000000..29108c4 --- /dev/null +++ b/examples/array.c @@ -0,0 +1,37 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/array.h" + +typedef vtype_array arr_t; + +int print_value(void* value, ssize_t index, vtype type, void* data) { + const char *n = data; + vtype_int32 *v = value; + + assert(type == VTYPE_INT32); + + printf("%s %d (index: %ld)\n", n, *v, index); + + return 0; +} + +int main(int argc, char** argv) { + + arr_t arr; + + array_init(&arr, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + array_push_back(&arr, i); + } + + array_get_by_index(&arr, 13, "Get value:", print_value); + array_pop_by_index(&arr, 18, "Pop value:", print_value); + + array_foreach(&arr, "Foreach loop:", print_value); + + array_free(&arr); +} diff --git a/examples/dict.c b/examples/dict.c new file mode 100644 index 0000000..1e548a4 --- /dev/null +++ b/examples/dict.c @@ -0,0 +1,61 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include +#include "../include/extra/dict.h" + +typedef vtype_dict dict_t; + +int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) { + const char *n = data; + + switch (key_type) { + default: abort(); + + case VTYPE_INT32: + printf("%s %d: ", n, *(vtype_int32*)key); + break; + case VTYPE_FLOAT: + printf("%s %f: ", n, *(vtype_float*)key); + break; + } + + switch (value_type) { + default: abort(); + + case VTYPE_INT32: + printf("%d\n", *(vtype_int32*)value); + break; + case VTYPE_FLOAT: + printf("%f\n", *(vtype_float*)value); + break; + } + + return 0; +} + +int main(int argc, char** argv) { + + dict_t dict; + vtype_float fl = 0.0; + + dict_init(&dict); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + dict_update(&dict, (vtype_float)fl, (vtype_int32)i); + } else { + dict_update(&dict, (vtype_int32)i, (vtype_float)fl); + } + fl += 0.05; + } + + dict_get(&dict, 14, "Get value:", print_value); + dict_pop(&dict, 0.25, "Pop value:", print_value); + + dict_foreach(&dict, "Foreach loop:", print_value); + + dict_free(&dict); +} diff --git a/examples/list.c b/examples/list.c new file mode 100644 index 0000000..449ba2d --- /dev/null +++ b/examples/list.c @@ -0,0 +1,51 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/list.h" + +typedef vtype_list list_t; + + +int print_value(void* value, ssize_t index, vtype type, void* data) { + const char *n = data; + + switch (type) { + default: abort(); + + case VTYPE_INT32: + printf("%s %d (index: %ld)\n", n, *(vtype_int32*)value, index); + break; + case VTYPE_FLOAT: + printf("%s %f (index: %ld)\n", n, *(vtype_float*)value, index); + break; + } + + return 0; +} + + +int main(int argc, char** argv) { + + list_t list; + vtype_float fl = 0.0; + + list_init(&list); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + list_push_back(&list, (vtype_int32)i); + } else { + list_push_back(&list, (vtype_float)fl); + fl += 0.05; + } + } + + list_get_by_index(&list, 13, "Get value:", print_value); + list_pop_by_index(&list, 18, "Pop value:", print_value); + + list_foreach(&list, "Foreach loop:", print_value); + + list_free(&list); +} diff --git a/examples/map.c b/examples/map.c new file mode 100644 index 0000000..115dc2e --- /dev/null +++ b/examples/map.c @@ -0,0 +1,55 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include +#include "../include/extra/map.h" + +typedef vtype_map map_t; + +int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) { + const char *n = data; + vtype_int32 *k = (void*)key; + + assert(key_type == VTYPE_INT32); + + printf("%s %d: ", n, *k); + + switch (value_type) { + default: abort(); + + case VTYPE_INT32: + printf("%d\n", *(vtype_int32*)value); + break; + case VTYPE_FLOAT: + printf("%f\n", *(vtype_float*)value); + break; + } + + return 0; +} + +int main(int argc, char** argv) { + + map_t map; + vtype_float fl = 0.0; + + map_init(&map, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + if (i%2) { + map_update(&map, i, (vtype_int32)i); + } else { + map_update(&map, i, (vtype_float)fl); + fl += 0.05; + } + } + + map_get(&map, 13, "Get value:", print_value); + map_pop(&map, 18, "Pop value:", print_value); + + map_foreach(&map, "Foreach loop:", print_value); + + map_free(&map); +} diff --git a/examples/set.c b/examples/set.c new file mode 100644 index 0000000..9b9b4d1 --- /dev/null +++ b/examples/set.c @@ -0,0 +1,38 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include +#include "../include/extra/set.h" + +typedef vtype_set vset_t; + +int print_value(const void* value, vtype type, void* data) { + const char *n = data; + vtype_int32 *v = (void*)value; + + assert(type == VTYPE_INT32); + + printf("%s %d\n", n, *v); + + return 0; +} + + +int main(int argc, char** argv) { + + vset_t set; + + vset_init(&set, VTYPE_INT32); + + for (size_t i = 0; i < 28; ++i) { + vset_push(&set, i); + } + + vset_get(&set, 13, "Get value:", print_value); + vset_pop(&set, 18, "Pop value:", print_value); + + vset_foreach(&set, "Foreach loop:", print_value); + + vset_free(&set); +} diff --git a/examples/string.c b/examples/string.c new file mode 100644 index 0000000..4072af5 --- /dev/null +++ b/examples/string.c @@ -0,0 +1,42 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../include/extra/string.h" +#include "../include/extra/array.h" + +typedef vtype_string str_t; +typedef vtype_array arr_t; + +int main(int argc, char** argv) { + + str_t str; + + string_init(&str, "sed ut perspiciatis"); + + string_concat(&str, ", Unde omnis iste natus error sit voluptatem accusantium doloremque laudantium"); + string_concat(&str, ", Totam rem aperiam eaque ipsa"); + string_concat(&str, ", Quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt"); + string_concat(&str, ", Explicabo."); + + string_capitalize(&str); + + string_reverse(&str); + + printf("%s\n", str.buffer); + + arr_t parts = string_split(&str, ',', -1); + + printf("%lu\n", array_size(&parts)); + + for (size_t i = 0; i < array_size(&parts); ++i) { + str_t* value = array_at(&parts, i); + + string_trim_spaces(value); + + printf("%s (%lu)\n", value->buffer, string_nmemb(value)); + } + + array_free(&parts); + string_free(&str); +}