From 6d3a2ad8aeb2f4633e1ed707c5bd15bd974a06bf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 5 Jun 2021 12:39:35 +0200 Subject: Renamed predefined using_cmap_sv*() using_csmap_sv*() macros to using_cmap_strv*(), ... --- docs/csview_api.md | 48 ++++++++++++++++++++----------------------- examples/svmap.c | 14 ++++++------- include/stc/csview.h | 58 ++++++++++++++++++++++------------------------------ 3 files changed, 54 insertions(+), 66 deletions(-) diff --git a/docs/csview_api.md b/docs/csview_api.md index fc51702f..59c307b4 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -7,7 +7,7 @@ element of the sequence at position zero. The implementation holds two members: **csview** is an efficient replacent for `const char*`. It never allocates memory, and therefore need not be destructed. Its lifetime is limited by the source string storage. It keeps the length of the string, and does not call *strlen()* when passing it around. It is faster when using`csview` as convertion type (raw) than `const char*` in associative -containers with cstr keys. `using_cmap_svkey()` may perform better than `using_cmap_strkey()`. +containers with cstr keys. `using_cmap_strvkey()` may perform better than `using_cmap_strkey()`. Note: a **csview** may ***not be null-terminated***, and must therefore be printed like: `printf("%.*s", csview_ARG(sv))`. @@ -95,25 +95,21 @@ uint64_t csview_hash_ref(const csview* x, size_t ignored); | `c_lit(literal)` | csview constructor | `sview = c_lit("hello, world");` | | `csview_ARG(sv)` | printf argument | `printf("%.*s", csview_ARG(sv));` | -## cstr-containers with csview emplace/lookup API +## Associative cstr-containers with csview emplace/lookup API ``` -using_cvec_sv() -using_cdeq_sv() -using_clist_sv() - -using_csmap_svkey(X, Mapped) -using_csmap_svkey(X, Mapped, mappedDel) -using_csmap_svkey(X, Mapped, mappedDel, mappedClone) -using_csmap_svkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) -using_csmap_sv() -using_csset_sv() - -using_cmap_svkey(X, Mapped) -using_cmap_svkey(X, Mapped, mappedDel) -using_cmap_svkey(X, Mapped, mappedDel, mappedClone) -using_cmap_svkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) -using_cmap_sv() -using_cset_sv() +using_csmap_strvkey(X, Mapped) +using_csmap_strvkey(X, Mapped, mappedDel) +using_csmap_strvkey(X, Mapped, mappedDel, mappedClone) +using_csmap_strvkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) +using_csmap_strv() +using_csset_strv() + +using_cmap_strvkey(X, Mapped) +using_cmap_strvkey(X, Mapped, mappedDel) +using_cmap_strvkey(X, Mapped, mappedDel, mappedClone) +using_cmap_strvkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) +using_cmap_strv() +using_cset_strv() ``` ## Example @@ -146,8 +142,8 @@ red Apples ``` ### Example 2: csview tokenizer (string split) -Splits strings into tokens. *print_split()* calls make **no** memory allocations, *strlen()* calls, or depends on -null-terminated strings. *string_split()* function returns a vector of cstr. +Splits strings into tokens. *print_split()* makes **no** memory allocations or *strlen()* calls, +and does not depend on null-terminated strings. *string_split()* function returns a vector of cstr. ```c #include #include @@ -207,13 +203,13 @@ Output: "" ``` ### Example 3 +cmap cstr => int with csview as convertion type ```c #include #include #include -// cmap with csview as convertion type -using_cmap_svkey(si, int); +using_cmap_strvkey(si, int); int main() { @@ -221,15 +217,15 @@ int main() csview suffix = csview_substr(text, -12, cstr_npos); // from pos -12 to end printf("%.*s\n", csview_ARG(suffix)); - c_forvar (cmap_si map = cmap_si_init(), cmap_si_del(&map)) + c_forvar_initdel (cmap_si, map) { cmap_si_emplace(&map, c_lit("hello"), 100); cmap_si_emplace(&map, c_lit("world"), 200); cmap_si_emplace(&map, c_lit("hello"), 300); // already in map, ignored // Efficient lookup: no string allocation or strlen() takes place: - cmap_si_value_t* v = cmap_si_get(&map, c_lit("hello")); - printf("%s: %d\n", v->first.str, v->second); + cmap_si_value_t* val = cmap_si_get(&map, c_lit("hello")); + printf("%s: %d\n", val->first.str, val->second); } } ``` diff --git a/examples/svmap.c b/examples/svmap.c index 8cacf95a..769913df 100644 --- a/examples/svmap.c +++ b/examples/svmap.c @@ -3,25 +3,25 @@ #include // cmap with csview as convertion key-type (raw): -using_cmap_svkey(si, int); +using_cmap_strvkey(si, int); int main() { csview fox = c_lit("The quick brown fox jumps over the lazy dog."); printf("\"%s\", length=%zu\n", fox.str, fox.size); - c_forvar (cmap_si frequencies = cmap_si_init(), cmap_si_del(&frequencies)) + c_forvar_initdel (cmap_si, frequencies) { // Non-emplace: cstr element API cmap_si_insert(&frequencies, cstr_lit("thousand"), 1000); - cmap_si_insert_or_assign(&frequencies, cstr_lit("thousand"), 2000); // update; same as put() - cmap_si_insert(&frequencies, cstr_lit("thousand"), 3000); // ignored + cmap_si_insert_or_assign(&frequencies, cstr_lit("thousand"), 2000); + cmap_si_insert(&frequencies, cstr_lit("thousand"), 3000); // ignored // Emplace: csview element API const char* key = "hundred"; - cmap_si_emplace(&frequencies, c_lit("hundred"), 300); // c_lit() shorthand for csview_lit() - cmap_si_emplace(&frequencies, csview_from_n(key, 4), 400); // insert "hund" + cmap_si_emplace(&frequencies, c_lit("hundred"), 300); // c_lit() shorthand for csview_lit() + cmap_si_emplace(&frequencies, csview_from_n(key, 4), 400); // insert "hund" cmap_si_emplace_or_assign(&frequencies, csview_from(key), 500); // update - cmap_si_emplace(&frequencies, c_lit("hundred"), 600); // ignored, already inserted + cmap_si_emplace(&frequencies, c_lit("hundred"), 600); // ignored, already inserted // Lookup always uses "raw" type API, i.e. csview here. printf("at(\"hundred\"): %d\n", *cmap_si_at(&frequencies, c_lit("hundred"))); diff --git a/include/stc/csview.h b/include/stc/csview.h index 4a9d1dff..34f8f15c 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -137,51 +137,43 @@ STC_INLINE bool csview_equals_ref(const csview* a, const csview* b) { return a->size == b->size && !memcmp(a->str, b->str, a->size); } #define csview_hash_ref(xp, none) c_default_hash((xp)->str, (xp)->size) -/* ---- cstr-containers with csview emplace/lookup API ---- */ +/* ---- Associative cstr-containers with csview emplace/lookup API ---- */ -#define using_cvec_sv() \ - using_cvec(sv, cstr, csview_compare_ref, cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_cdeq_sv() \ - using_cdeq(sv, cstr, csview_compare_ref, cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_clist_sv() \ - using_clist(sv, cstr, csview_compare_ref, cstr_del, cstr_from_v, cstr_to_v, csview) +#define using_csmap_strvkey(...) c_MACRO_OVERLOAD(using_csmap_strvkey, __VA_ARGS__) - -#define using_csmap_svkey(...) c_MACRO_OVERLOAD(using_csmap_svkey, __VA_ARGS__) - -#define using_csmap_svkey_2(X, Mapped) \ - using_csmap_svkey_4(X, Mapped, c_default_del, c_default_fromraw) -#define using_csmap_svkey_3(X, Mapped, mappedDel) \ - using_csmap_svkey_4(X, Mapped, mappedDel, c_no_clone) -#define using_csmap_svkey_4(X, Mapped, mappedDel, mappedClone) \ - using_csmap_svkey_6(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped) -#define using_csmap_svkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ +#define using_csmap_strvkey_2(X, Mapped) \ + using_csmap_strvkey_4(X, Mapped, c_default_del, c_default_fromraw) +#define using_csmap_strvkey_3(X, Mapped, mappedDel) \ + using_csmap_strvkey_4(X, Mapped, mappedDel, c_no_clone) +#define using_csmap_strvkey_4(X, Mapped, mappedDel, mappedClone) \ + using_csmap_strvkey_6(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped) +#define using_csmap_strvkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ _c_using_aatree(csmap_##X, csmap_, cstr, Mapped, csview_compare_ref, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_csmap_sv() \ - using_csmap_svkey_6(sv, cstr, cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_csset_sv() \ - _c_using_aatree(csset_sv, csset_, cstr, cstr, csview_compare_ref, \ +#define using_csmap_strv() \ + using_csmap_strvkey_6(strv, cstr, cstr_del, cstr_from_v, cstr_to_v, csview) +#define using_csset_strv() \ + _c_using_aatree(csset_strv, csset_, cstr, cstr, csview_compare_ref, \ @@, @@, @@, void, cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_cmap_svkey(...) c_MACRO_OVERLOAD(using_cmap_svkey, __VA_ARGS__) +#define using_cmap_strvkey(...) c_MACRO_OVERLOAD(using_cmap_strvkey, __VA_ARGS__) -#define using_cmap_svkey_2(X, Mapped) \ - using_cmap_svkey_4(X, Mapped, c_default_del, c_default_fromraw) -#define using_cmap_svkey_3(X, Mapped, mappedDel) \ - using_cmap_svkey_4(X, Mapped, mappedDel, c_no_clone) -#define using_cmap_svkey_4(X, Mapped, mappedDel, mappedClone) \ - using_cmap_svkey_6(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped) -#define using_cmap_svkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ +#define using_cmap_strvkey_2(X, Mapped) \ + using_cmap_strvkey_4(X, Mapped, c_default_del, c_default_fromraw) +#define using_cmap_strvkey_3(X, Mapped, mappedDel) \ + using_cmap_strvkey_4(X, Mapped, mappedDel, c_no_clone) +#define using_cmap_strvkey_4(X, Mapped, mappedDel, mappedClone) \ + using_cmap_strvkey_6(X, Mapped, mappedDel, mappedClone, c_default_toraw, Mapped) +#define using_cmap_strvkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ _c_using_chash(cmap_##X, cmap_, cstr, Mapped, csview_equals_ref, csview_hash_ref, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_cmap_sv() \ - using_cmap_svkey_6(sv, cstr, cstr_del, cstr_from_v, cstr_to_v, csview) -#define using_cset_sv() \ - _c_using_chash(cset_sv, cset_, cstr, cstr, csview_equals_ref, csview_hash_ref, \ +#define using_cmap_strv() \ + using_cmap_strvkey_6(strv, cstr, cstr_del, cstr_from_v, cstr_to_v, csview) +#define using_cset_strv() \ + _c_using_chash(cset_strv, cset_, cstr, cstr, csview_equals_ref, csview_hash_ref, \ @@, @@, @@, void, cstr_del, cstr_from_v, cstr_to_v, csview) #endif \ No newline at end of file -- cgit v1.2.3