diff options
| author | Tyge Løvset <[email protected]> | 2021-05-23 14:05:40 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-23 14:05:40 +0200 |
| commit | 771a8dff31b0c0a905c57ec6f590cd03975f8817 (patch) | |
| tree | ffc6d482d84cdc8ac5b7b8722f192ce023cc49c2 | |
| parent | 7472580ee0159d7164bf2a219c292cd060b904a2 (diff) | |
| download | STC-modified-771a8dff31b0c0a905c57ec6f590cd03975f8817.tar.gz STC-modified-771a8dff31b0c0a905c57ec6f590cd03975f8817.zip | |
Added csmap_find.c example.
| -rw-r--r-- | docs/csview_api.md | 6 | ||||
| -rw-r--r-- | examples/csmap_find.c | 65 | ||||
| -rw-r--r-- | include/stc/csview.h | 23 |
3 files changed, 85 insertions, 9 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md index 8fd91ed5..16d59eae 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -93,8 +93,12 @@ 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));` | -## Associative cstr-containers with csview emplace/lookup API +## 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) diff --git a/examples/csmap_find.c b/examples/csmap_find.c new file mode 100644 index 00000000..d32cd1b5 --- /dev/null +++ b/examples/csmap_find.c @@ -0,0 +1,65 @@ +// This implements the c++ std::map::find example at:
+// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
+#include <stc/csmap.h>
+#include <stc/cvec.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+using_csmap_strval(istr, int);
+using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare);
+
+void print_elem(csmap_istr_rawvalue_t p) {
+ printf("(%d, %s) ", p.first, p.second);
+}
+
+#define using_print_collection(CX) \
+ void print_collection_##CX(CX t) { \
+ printf("%zu elements: ", CX##_size(t)); \
+ \
+ c_foreach (p, CX, t) { \
+ print_elem(CX##_value_toraw(p.ref)); \
+ } \
+ puts(""); \
+ }
+
+using_print_collection(csmap_istr);
+using_print_collection(cvec_istr);
+
+
+void findit(csmap_istr c, csmap_istr_key_t val) {
+ printf("Trying find() on value %d\n", val);
+ csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
+ if (result) {
+ printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
+ } else {
+ puts("Element not found.");
+ }
+}
+
+int main()
+{
+ c_withvar (csmap_istr, m1)
+ c_withvar (cvec_istr, v) {
+ c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
+ puts("The starting map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+
+ typedef cvec_istr_value_t pair;
+ cvec_istr_emplace_back(&v, (pair){43, "Tc"});
+ cvec_istr_emplace_back(&v, (pair){41, "Nb"});
+ cvec_istr_emplace_back(&v, (pair){46, "Pd"});
+ cvec_istr_emplace_back(&v, (pair){42, "Mo"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
+
+ puts("Inserting the following vector data into m1:");
+ print_collection_cvec_istr(v);
+ c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
+
+ puts("The modified map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+ puts("");
+ findit(m1, 45);
+ findit(m1, 6);
+ }
+}
\ No newline at end of file diff --git a/include/stc/csview.h b/include/stc/csview.h index 324498e0..95c6be20 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -29,18 +29,17 @@ typedef struct { const char* str; size_t size; } csview; typedef struct { const char *ref; } csview_iter_t;
typedef char csview_value_t;
#define csview_null c_make(csview){"", 0}
-#define csview_ARG(sv) (int)(sv).size, (sv).str
+#define csview_ARG(sv) (int)(sv).size, (sv).str
-#define c_lit(literal) \
- csview_lit(literal)
-STC_INLINE csview c_sv(cstr s)
- { return c_make(csview){s.str, _cstr_rep(&s)->size}; }
+#define c_lit(literal) csview_lit(literal)
+#define c_sv(s) csview_from_s(s)
+
+#define csview_lit(literal) \
+ c_make(csview){literal, sizeof c_make(strlit_t){literal} - 1}
STC_INLINE csview csview_from(const char* str)
{ return c_make(csview){str, strlen(str)}; }
STC_INLINE csview csview_from_n(const char* str, size_t n)
{ return c_make(csview){str, n}; }
-#define csview_lit(literal) \
- c_make(csview){literal, sizeof c_make(strlit_t){literal} - 1}
STC_INLINE csview csview_from_s(cstr s)
{ return c_make(csview){s.str, _cstr_rep(&s)->size}; }
STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n)
@@ -128,7 +127,15 @@ 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)
-/* ---- Associative cstr-containers with csview emplace/lookup API ---- */
+/* ---- 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_svkey(...) c_MACRO_OVERLOAD(using_csmap_svkey, __VA_ARGS__)
|
