diff options
| -rw-r--r-- | README.md | 98 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 14 | ||||
| -rw-r--r-- | docs/cpque_api.md | 2 | ||||
| -rw-r--r-- | include/stc/algo/crange.h | 4 | ||||
| -rw-r--r-- | include/stc/cspan.h | 8 | ||||
| -rw-r--r-- | src/utf8code.c | 2 |
6 files changed, 55 insertions, 73 deletions
@@ -165,13 +165,13 @@ int main(void) Floats_sort(&nums); - c_foreach (i, Floats, nums) // Alternative way to iterate nums. + c_foreach (i, Floats, nums) // Alternative and recommended way to iterate nums. printf(" %g", *i.ref); // i.ref is a pointer to the current element. Floats_drop(&nums); // cleanup memory } ``` -To switch to a different container type is easy when using `c_foreach`: +Switching to a different container type is easy: ```c #define i_type Floats #define i_val float @@ -185,7 +185,7 @@ int main() Floats_push(&nums, 40.f); // print the sorted numbers - c_foreach (i, Floats, nums) + c_foreach (i, Floats, nums) // c_foreach works with any container printf(" %g", *i.ref); Floats_drop(&nums); @@ -197,21 +197,22 @@ only works for integral types. *Alternatively, `#define i_opt c_no_cmp` to disab Let's make a vector of vectors that can be cloned. All of its element vectors will be destroyed when destroying the Vec2D. ```c +#include <stdio.h> + #define i_type Vec #define i_val float #include <stc/cvec.h> -#include <stdio.h> #define i_type Vec2D -#define i_valclass Vec // Use i_valclass when element type has "member" functions Vec_clone(), Vec_drop() and Vec_cmp(). -#define i_opt c_no_cmp // Disable search/sort for Vec2D because Vec_cmp() is not defined. +#define i_valclass Vec // Use i_valclass when the element type has "member" functions _clone(), _drop() and _cmp(). +#define i_opt c_no_cmp // However, disable search/sort for Vec2D because Vec_cmp() is not defined. #include <stc/cvec.h> int main(void) { Vec* v; Vec2D vec = {0}; // All containers in STC can be initialized with {0}. - v = Vec2D_push(&vec, Vec_init()); // Returns a pointer to the new element in vec. + v = Vec2D_push(&vec, Vec_init()); // push() returns a pointer to the new element in vec. Vec_push(v, 10.f); Vec_push(v, 20.f); @@ -228,84 +229,69 @@ int main(void) c_drop(Vec2D, &vec, &clone); // Cleanup all (6) vectors. } ``` -Here is an example of using six different container types: +Here is an example of using four different container types: ```c #include <stdio.h> -#include <stc/ccommon.h> - -struct Point { float x, y; }; #define i_key int #include <stc/cset.h> // cset_int: unordered set +struct Point { float x, y; }; +// Define cvec_pnt with a less-comparison function for Point. #define i_val struct Point -// Define a i_less template parameter (alternative to i_cmp) for Point. #define i_less(a, b) a->x < b->x || (a->x == b->x && a->y < b->y) #define i_tag pnt #include <stc/cvec.h> // cvec_pnt: vector of struct Point #define i_val int -#include <stc/cdeq.h> // cdeq_int: deque of int - -#define i_val int #include <stc/clist.h> // clist_int: singly linked list -#define i_val int -#include <stc/cstack.h> // cstack_int - #define i_key int #define i_val int #include <stc/csmap.h> // csmap_int: sorted map int => int int main(void) { - // Define six empty containers + // Define four empty containers cset_int set = {0}; cvec_pnt vec = {0}; - cdeq_int deq = {0}; clist_int lst = {0}; - cstack_int stk = {0}; csmap_int map = {0}; - c_defer( // Drop the containers after the scope is executed + c_defer( // Drop the containers at scope exit cset_int_drop(&set), cvec_pnt_drop(&vec), - cdeq_int_drop(&deq), clist_int_drop(&lst), - cstack_int_drop(&stk), csmap_int_drop(&map) ){ - int nums[4] = {10, 20, 30, 40}; - struct Point pts[4] = { {10, 1}, {20, 2}, {30, 3}, {40, 4} }; - int pairs[4][2] = { {20, 2}, {10, 1}, {30, 3}, {40, 4} }; + enum{N = 5}; + int nums[N] = {10, 20, 30, 40, 50}; + struct Point pts[N] = { {10, 1}, {20, 2}, {30, 3}, {40, 4}, {50, 5} }; + int pairs[N][2] = { {20, 2}, {10, 1}, {30, 3}, {40, 4}, {50, 5} }; // Add some elements to each container - for (int i = 0; i < 4; ++i) { + for (int i = 0; i < N; ++i) { cset_int_insert(&set, nums[i]); cvec_pnt_push(&vec, pts[i]); - cdeq_int_push_back(&deq, nums[i]); clist_int_push_back(&lst, nums[i]); - cstack_int_push(&stk, nums[i]); csmap_int_insert(&map, pairs[i][0], pairs[i][1]); } - // Find an element in each container (except cstack) + // Find an element in each container cset_int_iter i1 = cset_int_find(&set, 20); cvec_pnt_iter i2 = cvec_pnt_find(&vec, (struct Point){20, 2}); - cdeq_int_iter i3 = cdeq_int_find(&deq, 20); - clist_int_iter i4 = clist_int_find(&lst, 20); - csmap_int_iter i5 = csmap_int_find(&map, 20); + clist_int_iter i3 = clist_int_find(&lst, 20); + csmap_int_iter i4 = csmap_int_find(&map, 20); - printf("\nFound: %d, (%g, %g), %d, %d, [%d: %d]\n", - *i1.ref, i2.ref->x, i2.ref->y, *i3.ref, - *i4.ref, i5.ref->first, i5.ref->second); + printf("\nFound: %d, (%g, %g), %d, [%d: %d]\n", + *i1.ref, i2.ref->x, i2.ref->y, *i3.ref, + i4.ref->first, i4.ref->second); // Erase all the elements found cset_int_erase_at(&set, i1); cvec_pnt_erase_at(&vec, i2); - cdeq_int_erase_at(&deq, i3); - clist_int_erase_at(&lst, i4); - csmap_int_erase_at(&map, i5); + clist_int_erase_at(&lst, i3); + csmap_int_erase_at(&map, i4); printf("After erasing the elements found:"); printf("\n set:"); @@ -316,18 +302,10 @@ int main(void) c_foreach (i, cvec_pnt, vec) printf(" (%g, %g)", i.ref->x, i.ref->y); - printf("\n deq:"); - c_foreach (i, cdeq_int, deq) - printf(" %d", *i.ref); - printf("\n lst:"); c_foreach (i, clist_int, lst) printf(" %d", *i.ref); - printf("\n stk:"); - c_foreach (i, cstack_int, stk) - printf(" %d", *i.ref); - printf("\n map:"); c_foreach (i, csmap_int, map) printf(" [%d: %d]", i.ref->first, i.ref->second); @@ -337,14 +315,12 @@ int main(void) Output ``` -Found: 20, (20, 2), 20, 20, [20: 2] -After erasing elements found: - set: 10 30 40 - vec: (10, 1) (30, 3) (40, 4) - deq: 5 10 30 - lst: 5 10 30 - stk: 10 20 30 40 - map: [10: 1] [30: 3] [40: 4] +Found: 20, (20, 2), 20, [20: 2] +After erasing the elements found: + set: 40 10 30 50 + vec: (10, 1) (30, 3) (40, 4) (50, 5) + lst: 10 30 40 50 + map: [10: 1] [30: 3] [40: 4] [50: 5] ``` Installation @@ -480,12 +456,12 @@ cvec_str vec = {0}; cstr s = cstr_lit("a string literal"); const char* hello = "Hello"; -cvec_str_push_back(&vec, cstr_from(hello); // construct and add string from const char* -cvec_str_push_back(&vec, cstr_clone(s)); // clone and append a cstr +cvec_str_push(&vec, cstr_from(hello); // make a cstr from const char* and move it onto vec +cvec_str_push(&vec, cstr_clone(s)); // make a cstr clone and move it onto vec -cvec_str_emplace_back(&vec, "Yay, literal"); // internally constructs cstr from const char* -cvec_str_emplace_back(&vec, cstr_clone(s)); // <-- COMPILE ERROR: expects const char* -cvec_str_emplace_back(&vec, cstr_str(&s)); // Ok: const char* input type. +cvec_str_emplace(&vec, "Yay, literal"); // internally make a cstr from const char* +cvec_str_emplace(&vec, cstr_clone(s)); // <-- COMPILE ERROR: expects const char* +cvec_str_emplace(&vec, cstr_str(&s)); // Ok: const char* input type. cstr_drop(&s) cvec_str_drop(&vec); diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 010ea204..53ba096a 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -113,8 +113,10 @@ int main() { isPrime(*i.ref) && c_flt_skip(i, 24) && c_flt_count(i) % 15 == 1 && - c_flt_take(i, 10)) + c_flt_take(i, 10) + ){ printf(" %lld", *i.ref); + } puts(""); } // out: 1171 1283 1409 1493 1607 1721 1847 1973 2081 2203 @@ -126,7 +128,7 @@ Note that `c_flt_take()` and `c_flt_takewhile()`breaks the loop on false. ### crange A number sequence generator type, similar to [boost::irange](https://www.boost.org/doc/libs/release/libs/range/doc/html/range/reference/ranges/irange.html). The **crange_value** type is `long long`. Below *start*, *stop*, and *step* are of type *crange_value*: ```c -crange& crange_obj(...) // create a compound literal crange object +crange& crange_object(...) // create a compound literal crange object crange crange_make(stop); // will generate 0, 1, ..., stop-1 crange crange_make(start, stop); // will generate start, start+1, ... stop-1 crange crange_make(start, stop, step); // will generate start, start+step, ... upto-not-including stop @@ -144,10 +146,12 @@ c_forfilter (i, crange, r1, isPrime(*i.ref)) // 2. The 11 first primes: printf("2"); -c_forfilter (i, crange, crange_obj(3, INT64_MAX, 2), - isPrime(*i.ref) && - c_flt_take(10)) +c_forfilter (i, crange, crange_object(3, INT64_MAX, 2), + isPrime(*i.ref) && + c_flt_take(10) +){ printf(" %lld", *i.ref); +} // 2 3 5 7 11 13 17 19 23 29 31 ``` ## Algorithms diff --git a/docs/cpque_api.md b/docs/cpque_api.md index b3a1a9ec..134a920f 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -84,7 +84,7 @@ int main() // Add some negative ones. int nums[] = {-231, -32, -873, -4, -343}; - c_forrange (i, c_ARRAYLEN(nums)) + c_forrange (i, c_arraylen(nums)) cpque_i_push(&heap, nums[i]); // Extract and display the fifty smallest. diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index ca06c258..91ffdd56 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -34,7 +34,7 @@ int main() // use a temporary crange object. int a = 100, b = INT32_MAX; - c_forfilter (i, crange, crange_obj(a, b, 8), + c_forfilter (i, crange, crange_object(a, b, 8), c_flt_skip(i, 10) && c_flt_take(i, 3)) printf(" %lld", *i.ref); @@ -46,7 +46,7 @@ int main() #include <stc/ccommon.h> -#define crange_obj(...) \ +#define crange_object(...) \ (*(crange[]){crange_make(__VA_ARGS__)}) typedef long long crange_value; diff --git a/include/stc/cspan.h b/include/stc/cspan.h index d5482200..ac3e9206 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -48,10 +48,12 @@ int demo2() { puts(""); c_forfilter (i, Intspan, span, - c_flt_skipwhile(i, *i.ref < 25) && - (*i.ref & 1) == 0 && // even only - c_flt_take(i, 2)) // break after 2 + c_flt_skipwhile(i, *i.ref < 25) && + (*i.ref & 1) == 0 && // even only + c_flt_take(i, 2) // break after 2 + ){ printf(" %d", *i.ref); + } puts(""); } */ diff --git a/src/utf8code.c b/src/utf8code.c index ecfdd24d..496f5eef 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -142,7 +142,7 @@ bool utf8_isalpha(uint32_t c) { static int16_t groups[] = {U8G_Latin, U8G_Nl, U8G_Greek, U8G_Cyrillic, U8G_Han, U8G_Devanagari, U8G_Arabic}; if (c < 128) return isalpha((int)c) != 0; - for (int j=0; j < c_ARRAYLEN(groups); ++j) + for (int j=0; j < c_arraylen(groups); ++j) if (utf8_isgroup(groups[j], c)) return true; return false; |
