From c47f6949cbd6d851eddb83cc1b9aa4e2cf3dbe61 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 22 Jul 2020 13:01:47 +0200 Subject: Renamed CArray to CArr. Renamed cmap_xx_get() to cmap_xx_find(). Changed _front() and _back() signatures. --- examples/advanced.c | 12 +++++++++--- examples/benchmark.c | 2 +- examples/bits.c | 2 +- examples/complex.c | 16 ++++++++-------- examples/demos.c | 34 +++++++++++++++++----------------- examples/geek1.c | 2 +- examples/geek2.c | 6 +++--- examples/geek3.c | 4 ++-- examples/geek5.c | 4 ++-- examples/geek6.c | 2 +- examples/geek7.c | 2 +- examples/mapmap.c | 14 ++++++++++---- 12 files changed, 56 insertions(+), 44 deletions(-) (limited to 'examples') diff --git a/examples/advanced.c b/examples/advanced.c index 5d0bd2ed..b27f214d 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -63,13 +63,19 @@ declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, int main() { +/* CMap_vk vikings = cmap_init; - // emplace constructs the keys cmap_vk_put(&vikings, (VikingVw) {"Einar", "Norway"}, 20); cmap_vk_put(&vikings, (VikingVw) {"Olaf", "Denmark"}, 24); cmap_vk_put(&vikings, (VikingVw) {"Harald", "Iceland"}, 12); - - CMapEntry_vk* e = cmap_vk_get(&vikings, (VikingVw) {"Einar", "Norway"}); +*/ + CMap_vk vikings = cmap_vk_from( (CMapInput_vk[]) { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12} + }, 3); + + CMapEntry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"}); e->value += 5; // update c_foreach (k, cmap_vk, vikings) { diff --git a/examples/benchmark.c b/examples/benchmark.c index 87242ec8..62a6cb52 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -35,7 +35,7 @@ sfc64_random_t rng; /* ; cmap_##tag##_setLoadFactors(&map, maxLoadFactor, 0.0)*/ #define CMAP_PUT(tag, key, val) cmap_##tag##_put(&map, key, val)->value #define CMAP_ERASE(tag, key) cmap_##tag##_erase(&map, key) -#define CMAP_FIND(tag, key) (cmap_##tag##_get(map, key) != NULL) +#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != NULL) #define CMAP_SIZE(tag) cmap_size(map) #define CMAP_BUCKETS(tag) cmap_bucketCount(map) #define CMAP_CLEAR(tag) cmap_##tag##_destroy(&map) diff --git a/examples/bits.c b/examples/bits.c index 9767f8a3..3a10e48c 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -1,5 +1,5 @@ #include -#include "cbitvec.h" +#include int main() { CBitVec vec = cbitvec_make(23, true); diff --git a/examples/complex.c b/examples/complex.c index 6cfed90c..d4800260 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -1,12 +1,12 @@ #include #include #include -#include +#include void check_destroy(float* v) {printf("destroy %g\n", *v);} -declare_CArray(f, float, check_destroy); // normally omit the last argument - float type need no destroy. -declare_CList(t2, CArray2_f, carray2_f_destroy, c_noCompare); +declare_CArr(f, float, check_destroy); // normally omit the last argument - float type need no destroy. +declare_CList(t2, CArr2_f, carr2_f_destroy, c_noCompare); declare_CMap(il, int, CList_t2, clist_t2_destroy); declare_CMap_str(sm, CMap_il, cmap_il_destroy); @@ -17,20 +17,20 @@ int main() { CMap_sm theMap = cmap_init; { // Construct. - CArray2_f table = carray2_f_make(ydim, xdim, 0.f); - printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table)); + CArr2_f table = carr2_f_make(ydim, xdim, 0.f); + printf("table: (%zu, %zu)\n", carr2_ydim(table), carr2_xdim(table)); CList_t2 tableList = clist_init; CMap_il listMap = cmap_init; // Put in some data. - carray2_f_data(table, y)[x] = 3.1415927; // table[y][x] + carr2_f_data(table, y)[x] = 3.1415927; // table[y][x] clist_t2_pushBack(&tableList, table); cmap_il_put(&listMap, tableKey, tableList); cmap_sm_put(&theMap, strKey, listMap); } { // Access the data entry - CArray2_f table = clist_back(cmap_il_get(&cmap_sm_get(&theMap, strKey)->value, tableKey)->value); - printf("value (%d, %d) is: %f\n", y, x, carray2_f_value(table, y, x)); + CArr2_f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value); + printf("value (%d, %d) is: %f\n", y, x, carr2_f_value(table, y, x)); } cmap_sm_destroy(&theMap); // free up the whole shebang! diff --git a/examples/demos.c b/examples/demos.c index f09e0fbc..89918d33 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -123,7 +123,7 @@ void mapdemo1() cmap_ii_put(&nums, 8, 64); cmap_ii_put(&nums, 11, 121); - printf("get 8: %d\n", cmap_ii_get(&nums, 8)->value); + printf("get 8: %d\n", cmap_ii_find(&nums, 8)->value); cmap_ii_destroy(&nums); } @@ -159,7 +159,7 @@ void mapdemo3() cmap_ss_put(&table, "Map", cstr_make("test")); cmap_ss_put(&table, "Make", cstr_make("my")); cmap_ss_put(&table, "Sunny", cstr_make("day")); - CMapEntry_ss *e = cmap_ss_get(&table, "Make"); + CMapEntry_ss *e = cmap_ss_find(&table, "Make"); printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str); cmap_ss_erase(&table, "Make"); //cmap_ss_eraseEntry(&table, e); @@ -172,28 +172,28 @@ void mapdemo3() -declare_CArray(f, float); +declare_CArr(f, float); void arraydemo1() { printf("\nARRAYDEMO1\n"); - CArray3_f a3 = carray3_f_make(30, 20, 10, 0.f); - carray3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] - CArray2_f a2 = carray3_f_at(a3, 5); // sub-array reference (no data copy). + CArr3_f a3 = carr3_f_make(30, 20, 10, 0.f); + carr3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] + CArr2_f a2 = carr3_f_at(a3, 5); // sub-array reference (no data copy). - printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carray3_xdim(a3), carray3_ydim(a3), carray3_zdim(a3), carray3_size(a3)); - printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carray2_xdim(a2), carray2_ydim(a2), carray2_size(a2)); + printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carr3_xdim(a3), carr3_ydim(a3), carr3_zdim(a3), carr3_size(a3)); + printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carr2_xdim(a2), carr2_ydim(a2), carr2_size(a2)); - printf("%f\n", carray2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) - printf("%f\n", carray2_f_data(a2, 4)[3]); // same, but this is writable. - printf("%f\n", carray2_f_at(a2, 4).data[3]); // same, via sub-array access. + printf("%f\n", carr2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) + printf("%f\n", carr2_f_data(a2, 4)[3]); // same, but this is writable. + printf("%f\n", carr2_f_at(a2, 4).data[3]); // same, via sub-array access. - printf("%f\n", carray3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. - printf("%f\n", carray3_f_data(a3, 5, 4)[3]); - printf("%f\n", carray3_f_at2(a3, 5, 4).data[3]); + printf("%f\n", carr3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. + printf("%f\n", carr3_f_data(a3, 5, 4)[3]); + printf("%f\n", carr3_f_at2(a3, 5, 4).data[3]); - carray2_f_destroy(&a2); // does nothing, since it is a sub-array. - carray3_f_destroy(&a3); // also invalidates a2. + carr2_f_destroy(&a2); // does nothing, since it is a sub-array. + carr3_f_destroy(&a3); // also invalidates a2. } diff --git a/examples/geek1.c b/examples/geek1.c index a8cf5b7b..bb4678df 100644 --- a/examples/geek1.c +++ b/examples/geek1.c @@ -38,7 +38,7 @@ int findMaximumPairs(int a[], int n, int k) int first = it.item->key; int second = k - it.item->key; - CMapEntry_ii *hf = cmap_ii_get(&hash, first), + CMapEntry_ii *hf = cmap_ii_find(&hash, first), *hs = cmap_ii_at(&hash, second, 0); // Check for minimal occurrence if (hf->value < hs->value) { diff --git a/examples/geek2.c b/examples/geek2.c index 32c0d03b..1c2f0c05 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -39,7 +39,7 @@ int main() // Check for a specific one. // When collections store owned values (String), they can still be // queried using references (&str). - if (! cmap_ss_get(&book_reviews, "Les Misérables")) { + if (! cmap_ss_find(&book_reviews, "Les Misérables")) { printf("We've got %zu reviews, but Les Misérables ain't one.\n", book_reviews.size); } @@ -50,13 +50,13 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland", NULL}; for (const char** book = to_find; *book; ++book) { - CMapEntry_ss* review = cmap_ss_get(&book_reviews, *book); + CMapEntry_ss* review = cmap_ss_find(&book_reviews, *book); if (review) printf("%s: %s\n", *book, review->value.str); else printf("%s is unreviewed.\n", *book); } // Look up the value for a key (will panic if the key is not found). - printf("Review for Jane: %s\n", cmap_ss_get(&book_reviews, "Pride and Prejudice")->value.str); + printf("Review for Jane: %s\n", cmap_ss_find(&book_reviews, "Pride and Prejudice")->value.str); // Iterate over everything. c_foreach (i, cmap_ss, book_reviews) { diff --git a/examples/geek3.c b/examples/geek3.c index 3ea93fe5..42e50a7c 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -43,8 +43,8 @@ int main () cmap_ss_put(&u, "WHITE", cstr_make("#FFFFFF")); // Output values by key - printf("The HEX of color RED is:[%s]\n", cmap_ss_get(&u, "RED")->value.str); - printf("The HEX of color BLACK is:[%s]\n", cmap_ss_get(&u, "BLACK")->value.str); + printf("The HEX of color RED is:[%s]\n", cmap_ss_find(&u, "RED")->value.str); + printf("The HEX of color BLACK is:[%s]\n", cmap_ss_find(&u, "BLACK")->value.str); cmap_ss_destroy(&u); return 0; } \ No newline at end of file diff --git a/examples/geek5.c b/examples/geek5.c index 2dbd0adb..546cb278 100644 --- a/examples/geek5.c +++ b/examples/geek5.c @@ -32,7 +32,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) CMap_sv M = cmap_init; for (int i = 0; i < n; i++) { const char* temp = arr[i]; - CMapEntry_sv* it = cmap_sv_get(&M, temp); + CMapEntry_sv* it = cmap_sv_find(&M, temp); // If current string doesn't // have an entry in the map @@ -47,7 +47,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) } } - CMapEntry_sv* it = cmap_sv_get(&M, str); + CMapEntry_sv* it = cmap_sv_find(&M, str); // If the given string is not // present in the array diff --git a/examples/geek6.c b/examples/geek6.c index ef86c3e8..6b70dd5e 100644 --- a/examples/geek6.c +++ b/examples/geek6.c @@ -52,7 +52,7 @@ int missingNumber(int a[], int n) // Return the first value starting // from 1 which does not exists in map while (1) { - if (cset_i_get(&mp, index) == NULL) { + if (cset_i_find(&mp, index) == NULL) { return index; } diff --git a/examples/geek7.c b/examples/geek7.c index 9ca34991..bac3f21b 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -48,7 +48,7 @@ void findElementsAfterDel(int arr[], int m, int del[], for (int i = 0; i < m; ++i) { // Search if the element is present - CMapEntry_ii *e = cmap_ii_get(&mp, arr[i]); + CMapEntry_ii *e = cmap_ii_find(&mp, arr[i]); if (e != NULL) { // Decrement its frequency diff --git a/examples/mapmap.c b/examples/mapmap.c index 35b5f510..30d7f0fb 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -11,10 +11,16 @@ declare_CMap(im, int, CMap_ii, cmap_ii_destroy); int main(void) { CMap_im m = cmap_init; - CMap_ii x = cmap_init; - cmap_ii_put(&cmap_im_put(&m, 100, x)->value, 200, 300); - cmap_ii_put(&cmap_im_get(&m, 100)->value, 200, 400); // update - cmap_ii_put(&cmap_im_put(&m, 110, x)->value, 200, 500); + CMap_ii ini = cmap_init; + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2000, 200); + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2001, 201); + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2000, 400); // update + cmap_ii_put(&cmap_im_at(&m, 110, ini)->value, 2000, 500); + cmap_ii_put(&cmap_im_at(&m, 120, ini)->value, 3000, 600); + + c_foreach (i, cmap_im, m) + c_foreach (j, cmap_ii, i.item->value) + printf("%d: %d - %d\n", i.item->key, j.item->key, j.item->value); cmap_im_destroy(&m); } \ No newline at end of file -- cgit v1.2.3