diff options
| author | Tyge Løvset <[email protected]> | 2021-05-20 11:19:37 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-20 11:19:37 +0200 |
| commit | 6270e01be0f105e3512cfde04a7f7d6b67aa8a2e (patch) | |
| tree | f2dc00ef74dff377c3c2ed1d85e9a7dcaf42e5bf /examples | |
| parent | 9c5d58627c784bc123e96fb81587f71994ad26cc (diff) | |
| download | STC-modified-6270e01be0f105e3512cfde04a7f7d6b67aa8a2e.tar.gz STC-modified-6270e01be0f105e3512cfde04a7f7d6b67aa8a2e.zip | |
Changed new API: c_sv(literal) => c_sv(cstr), cstr_new(literal) => cstr_lit(literal), csview_new() => csview_lit(). Added c_lit(literal) alias to csview_lit(literal).
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/csmap_erase.c | 79 | ||||
| -rw-r--r-- | examples/mmap.c | 64 | ||||
| -rw-r--r-- | examples/svmap.c | 14 |
3 files changed, 150 insertions, 7 deletions
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c new file mode 100644 index 00000000..eb3700b1 --- /dev/null +++ b/examples/csmap_erase.c @@ -0,0 +1,79 @@ +// map_erase.c
+// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#emplace
+#include <stc/csmap.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+using_csmap_strval(my, int);
+
+void printmap(csmap_my map)
+{
+ c_foreach (e, csmap_my, map)
+ printf(" [%d, %s]", e.ref->first, e.ref->second.str);
+ printf("\nsize() == %zu\n\n", csmap_my_size(map));
+}
+
+int main()
+{
+ c_with (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
+ {
+ // Fill in some data to test with, one at a time
+ csmap_my_emplace(&m1, 1, "A");
+ csmap_my_emplace(&m1, 2, "B");
+ csmap_my_emplace(&m1, 3, "C");
+ csmap_my_emplace(&m1, 4, "D");
+ csmap_my_emplace(&m1, 5, "E");
+
+ puts("Starting data of map m1 is:");
+ printmap(m1);
+ // The 1st member function removes an element at a given position
+ csmap_my_iter_t it = csmap_my_begin(&m1); csmap_my_next(&it);
+ csmap_my_erase_at(&m1, it);
+ puts("After the 2nd element is deleted, the map m1 is:");
+ printmap(m1);
+ }
+
+ c_with (csmap_my m2 = csmap_my_init(), csmap_my_del(&m2))
+ {
+ // Fill in some data to test with, one at a time, using emplace
+ c_emplace(csmap_my, m2, {
+ {10, "Bob"},
+ {11, "Rob"},
+ {12, "Robert"},
+ {13, "Bert"},
+ {14, "Bobby"}
+ });
+
+ puts("Starting data of map m2 is:");
+ printmap(m2);
+ csmap_my_iter_t it1 = csmap_my_fwd(csmap_my_begin(&m2), 1);
+ csmap_my_iter_t it2 = csmap_my_find(&m2, csmap_my_back(&m2)->first);
+ // The 2nd member function removes elements
+ // in the range [First, Last)
+ csmap_my_erase_range(&m2, it1, it2);
+ puts("After the middle elements are deleted, the map m2 is:");
+ printmap(m2);
+ }
+
+ c_with (csmap_my m3 = csmap_my_init(), csmap_my_del(&m3))
+ {
+ // Fill in some data to test with, one at a time, using emplace
+ csmap_my_emplace(&m3, 1, "red");
+ csmap_my_emplace(&m3, 2, "yellow");
+ csmap_my_emplace(&m3, 3, "blue");
+ csmap_my_emplace(&m3, 4, "green");
+ csmap_my_emplace(&m3, 5, "orange");
+ csmap_my_emplace(&m3, 5, "purple");
+ csmap_my_emplace(&m3, 5, "pink");
+
+ puts("Starting data of map m3 is:");
+ printmap(m3);
+ // The 3rd member function removes elements with a given Key
+ size_t count = csmap_my_erase(&m3, 2);
+ // The 3rd member function also returns the number of elements removed
+ printf("The number of elements removed from m3 is: %zu\n", count);
+ puts("After the element with a key of 2 is deleted, the map m3 is:");
+ printmap(m3);
+ }
+
+}
\ No newline at end of file diff --git a/examples/mmap.c b/examples/mmap.c new file mode 100644 index 00000000..c925db39 --- /dev/null +++ b/examples/mmap.c @@ -0,0 +1,64 @@ +#include <stc/csmap.h>
+#include <stc/clist.h>
+#include <stc/cstr.h>
+
+// This implements the multimap c++ example found at:
+// https://en.cppreference.com/w/cpp/container/multimap/insert
+
+// Map of int => clist_str. Note the negation of c_default_compare
+using_clist_str();
+using_csmap(m, int, clist_str, -c_default_compare, clist_str_del);
+
+void print(const csmap_m mmap)
+{
+ c_foreach (e, csmap_m, mmap) {
+ c_foreach (s, clist_str, e.ref->second)
+ printf("{%d,%s} ", e.ref->first, s.ref->str);
+ }
+ puts("");
+}
+
+void insert(csmap_m* mmap, int key, const char* str)
+{
+ clist_str *list = &csmap_m_insert(mmap, key, clist_str_init()).ref->second;
+ clist_str_emplace_back(list, str);
+}
+
+int main()
+{
+ c_with (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
+ {
+ // list-initialize
+ struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
+ c_forrange (i, c_arraylen(vals)) insert(&mmap, vals[i].i, vals[i].s);
+
+ // insert using value_type
+ insert(&mmap, 5, "pqr");
+ print(mmap);
+
+ // insert using make_pair
+ insert(&mmap, 6, "uvw");
+ print(mmap);
+
+ insert(&mmap, 7, "xyz");
+ print(mmap);
+
+ // insert using initialization_list
+ insert(&mmap, 5, "one");
+ insert(&mmap, 5, "two");
+ print(mmap);
+
+ // erase all entries with key 5
+ csmap_m_erase(&mmap, 5);
+ print(mmap);
+
+ // find and erase a specific entry
+ clist_str_iter_t pos;
+ c_foreach (e, csmap_m, mmap)
+ if ((pos = clist_str_find(&e.ref->second, "bar")).ref) {
+ clist_str_erase(&e.ref->second, pos);
+ break;
+ }
+ print(mmap);
+ }
+}
diff --git a/examples/svmap.c b/examples/svmap.c index 9fd89755..c45ff1ae 100644 --- a/examples/svmap.c +++ b/examples/svmap.c @@ -6,25 +6,25 @@ using_cmap_svkey(si, int);
int main() {
- csview fox = c_sv("The quick brown fox jumps over the lazy dog.");
+ csview fox = c_lit("The quick brown fox jumps over the lazy dog.");
printf("\"%s\", length=%zu\n", fox.str, fox.size);
c_withvar (cmap_si, frequencies)
{
// 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
+ 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
// 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, 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_sv("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_sv("hundred")));
+ printf("at(\"hundred\"): %d\n", *cmap_si_at(&frequencies, c_lit("hundred")));
c_foreach (i, cmap_si, frequencies)
printf("%s: %d\n", i.ref->first.str, i.ref->second);
|
