diff options
| author | tylo <[email protected]> | 2021-08-25 12:04:46 +0200 |
|---|---|---|
| committer | tylo <[email protected]> | 2021-08-25 12:04:46 +0200 |
| commit | a4bacdaf0feae20d417e53d8467ff332ac29413e (patch) | |
| tree | 38dd5edd8d9d256056b0213fa487a11354fd9d45 /examples | |
| parent | d80bf9bb4ef1b5a5a6d4edb550b93b93a75972f4 (diff) | |
| download | STC-modified-a4bacdaf0feae20d417e53d8467ff332ac29413e.tar.gz STC-modified-a4bacdaf0feae20d417e53d8467ff332ac29413e.zip | |
BREAKING CHANGE: c_forvar_initdel() macro renamed to c_forauto().
Updated doc and improved csmap_erase.c example.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/csmap_erase.c | 154 | ||||
| -rw-r--r-- | examples/priority.c | 2 |
2 files changed, 78 insertions, 78 deletions
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 35afbe3a..6cebffb7 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -1,77 +1,77 @@ -// map_erase.c
-// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
-#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_forvar (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_erase_at(&m1, csmap_my_fwd(csmap_my_begin(&m1), 1));
- puts("After the 2nd element is deleted, the map m1 is:");
- printmap(m1);
- }
-
- c_forvar (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_forvar (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 +// map_erase.c +// From C++ example: https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16 +#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_forauto (csmap_my, m1) + { + // Fill in some data to test with, one at a time + csmap_my_insert(&m1, 1, cstr_lit("A")); + csmap_my_insert(&m1, 2, cstr_lit("B")); + csmap_my_insert(&m1, 3, cstr_lit("C")); + csmap_my_insert(&m1, 4, cstr_lit("D")); + csmap_my_insert(&m1, 5, cstr_lit("E")); + + puts("Starting data of map m1 is:"); + printmap(m1); + // The 1st member function removes an element at a given position + csmap_my_erase_at(&m1, csmap_my_fwd(csmap_my_begin(&m1), 1)); + puts("After the 2nd element is deleted, the map m1 is:"); + printmap(m1); + } + + c_forauto (csmap_my, m2) + { + // Fill in some data to test with, one at a time, using c_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_forauto (csmap_my, 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, 6, "purple"); + csmap_my_emplace(&m3, 7, "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); + } +} diff --git a/examples/priority.c b/examples/priority.c index ee64bf42..4824a294 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -13,7 +13,7 @@ int main() { size_t N = 10000000;
stc64_t rng = stc64_init(time(NULL));
stc64_uniform_t dist = stc64_uniform_init(0, N * 10);
- c_forvar (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
+ c_forauto (cpque_i, heap)
{
// Push ten million random numbers to priority queue
c_forrange (N)
|
