summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-18 10:08:35 +0200
committerTyge Løvset <[email protected]>2021-05-18 10:08:35 +0200
commitccc45a1dc249160364aa12b3cfecc9844d6760ff (patch)
tree0519dbbf45cabb8869a11219ba64d9ef034759fa /examples
parent60a127bb9a7180d4d30c2b7186456cc1226e812a (diff)
downloadSTC-modified-ccc45a1dc249160364aa12b3cfecc9844d6760ff.tar.gz
STC-modified-ccc45a1dc249160364aa12b3cfecc9844d6760ff.zip
Added new file: csview: String View
Diffstat (limited to 'examples')
-rw-r--r--examples/svmap.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/svmap.c b/examples/svmap.c
new file mode 100644
index 00000000..f4bcc311
--- /dev/null
+++ b/examples/svmap.c
@@ -0,0 +1,38 @@
+#include <stc/csview.h>
+#include <stc/cmap.h>
+#include <stdio.h>
+
+// cmap<cstr, int> with csview as convertion key-type (raw):
+using_cmap_svkey(si, int);
+
+int main() {
+ csview fox = c_sv("The quick brown fox jumps over the lazy dog.");
+ printf("\"%s\", length=%zu\n", fox.str, fox.size);
+
+ cmap_si frequencies = cmap_si_init();
+
+ // Non-emplace: cstr element API
+ cmap_si_insert(&frequencies, cstr_new("thousand"), 1000);
+ cmap_si_insert_or_assign(&frequencies, cstr_new("thousand"), 2000); // update; same as put()
+ cmap_si_insert(&frequencies, cstr_new("thousand"), 3000); // ignored
+
+ // Emplace: csview element API
+ const char* key = "hundred";
+ cmap_si_emplace(&frequencies, c_sv("hundred"), 300); // c_sv() shorthand for csview_new()
+ 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_sv("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_sv("hundred")));
+
+ c_foreach (i, cmap_si, frequencies)
+ printf("%s: %d\n", i.ref->first.str, i.ref->second);
+
+ /* Output: */
+ // "The quick brown fox jumps over the lazy dog.", length=44
+ // at("hundred"): 500
+ // hund: 400
+ // hundred: 500
+ // thousand: 2000
+} \ No newline at end of file