From 778c56781c1ebbcc03738418f1875ad62a4714e5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylov@users.noreply.github.com> Date: Mon, 21 Sep 2020 21:40:49 +0200 Subject: Update README.md --- README.md | 92 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index c48335d2..2479fb6e 100644 --- a/README.md +++ b/README.md @@ -141,50 +141,26 @@ Example usages -------------- The examples folder contains further examples. -**cstr** string example. -```C -#include - -int main() { - cstr_t s1 = cstr("one-nine-three-seven-five"); - printf("%s.\n", s1.str); - - cstr_insert(&s1, 3, "-two"); - printf("%s.\n", s1.str); - - cstr_erase(&s1, 7, 5); // -nine - printf("%s.\n", s1.str); - - cstr_replace(&cs, cstr_find(&cs, "seven"), 5, "four"); - printf("%s.\n", s1.str); - - // reassign: - cstr_assign(&s1, "one two three four five six seven"); - cstr_append(&s1, " eight"); - printf("append: %s\n", s1.str); - - cstr_t full_path = cstr_from("%s/%s.%s", "directory", "filename", "ext"); - printf("%s\n", full_path.str); - - c_del(cstr, &s1, &full_path); -} -``` **cvec** of *int64_t*. ```C #include +#include using_cvec(ix, int64_t); // ix is just an example type tag name. int main() { cvec_ix bignums = cvec_INIT; // use cvec_ix_init() if initializing after declaration. - cvec_ix_reserve(&bignums, 100); - c_forrange (i, 100) + cvec_ix_reserve(&bignums, 50); + + c_forrange (i, 50) cvec_ix_push_back(&bignums, i * i * i); - cvec_ix_pop_back(&bignums); // erase the last - uint64_t value; + cvec_ix_pop_back(&bignums); // erase the last c_forrange (i, cvec_ix_size(bignums)) - value = bignums.data[i]; + bignums.data[i] /= i; // make them smaller + + c_foreach (i, cvec_ix, bignums) + printf(" %d", *i.get); cvec_ix_del(&bignums); } ``` @@ -211,6 +187,34 @@ int main() { cvec_str_del(&names); } ``` +**cstr** string example. +```C +#include + +int main() { + cstr_t s1 = cstr("one-nine-three-seven-five"); + printf("%s.\n", s1.str); + + cstr_insert(&s1, 3, "-two"); + printf("%s.\n", s1.str); + + cstr_erase(&s1, 7, 5); // -nine + printf("%s.\n", s1.str); + + cstr_replace(&cs, cstr_find(&cs, "seven"), 5, "four"); + printf("%s.\n", s1.str); + + // reassign: + cstr_assign(&s1, "one two three four five six seven"); + cstr_append(&s1, " eight"); + printf("append: %s\n", s1.str); + + cstr_t full_path = cstr_from("%s/%s.%s", "directory", "filename", "ext"); + printf("%s\n", full_path.str); + + c_del(cstr, &s1, &full_path); +} +``` **cmap** of *int => int*, and **cmap** of *cstr_t* => *cstr_t* ```C #include @@ -324,6 +328,28 @@ int main() cpqueue_i_del(&heap); } ``` +**cqueue** adapter container. Uses singly linked list as representation. +```C +#include +#include + +using_clist(i, int); +using_cqueue(i, clist_i); + +int main() { + cqueue_i queue = cqueue_i_init(); + + // push_front() / push_back() interleaved. + c_forrange (i, 20) { + if (i & 1) cqueue_i_push_front(&queue, i); + else cqueue_i_push_back(&queue, i); + } + c_foreach (i, cqueue_i, queue) { + printf(" %d\n", *i.get); + + cqueue_i_del(&queue); +} +``` **carray**. 1d, 2d and 3d arrays, allocated from heap in one memory block. *carray3* may have sub-array "views" of *carray2* and *carray1* etc., as shown in the following example: ```C #include -- cgit v1.2.3