From 31b2b9877c950b2606b8cff4eab398dd44321937 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 27 Jul 2020 17:59:41 +0200 Subject: API CHANGES: Made types all lower case. E.g.: CMap_ => cmap_, CList_ => clist_. Note: CStr => cstr_t, CBitset => cbitset_t --- examples/README.md | 14 ++-- examples/advanced.c | 16 ++--- examples/benchmark.c | 4 +- examples/bits.c | 4 +- examples/complex.c | 18 ++--- examples/demos.c | 38 +++++------ examples/geek1.c | 6 +- examples/geek2.c | 12 ++-- examples/geek3.c | 8 +-- examples/geek4.c | 20 +++--- examples/geek5.c | 14 ++-- examples/geek6.c | 4 +- examples/geek7.c | 12 ++-- examples/heap.c | 6 +- examples/inits.c | 16 ++--- examples/list.c | 4 +- examples/mapmap.c | 8 +-- examples/prime.c | 2 +- examples/priority.c | 6 +- examples/rngbirthday.c | 10 +-- stc/coptget.h | 173 +++++++++++++++++++++++++++++++++++++++++++++++++ stc/coption.h | 173 ------------------------------------------------- 22 files changed, 284 insertions(+), 284 deletions(-) create mode 100644 stc/coptget.h delete mode 100644 stc/coption.h diff --git a/examples/README.md b/examples/README.md index 4d43a7d2..e222f8bc 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,7 +6,7 @@ Contains various examples and benchmarks. advanced.c Example ------------------ -This demonstrates how to customize hash **CMap** with a user-defined key-type. You need to define two things: +This demonstrates how to customize hash **cmap** with a user-defined key-type. You need to define two things: 1. A hash function; calculates the hash value given an object of the key-type. @@ -40,8 +40,8 @@ int vikingvw_equals(const VikingVw* x, const VikingVw* y) { And the Viking data struct: ``` typedef struct Viking { - CStr name; - CStr country; + cstr_t name; + cstr_t country; } Viking; @@ -58,9 +58,9 @@ Viking viking_fromVw(VikingVw vw) { } ``` -With this in place, we use the full declare_CMap() macro to define {Viking -> int} hash map type: +With this in place, we use the full declare_cmap() macro to define {Viking -> int} hash map type: ``` -declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, +declare_cmap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, viking_destroy, VikingVw, viking_getVw, viking_fromVw); ``` CMap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values. @@ -68,13 +68,13 @@ Finally, the demo: ``` int main() { - CMap_vk vikings = cmap_init; + 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"}); + cmapentry_vk* e = cmap_vk_get(&vikings, (VikingVw) {"Einar", "Norway"}); e->value += 5; // update c_foreach (k, cmap_vk, vikings) { diff --git a/examples/advanced.c b/examples/advanced.c index 68ddc9ef..b5481f94 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -1,5 +1,5 @@ /* - * To be able to use CMap with a user-defined key-type, you need to define two things: + * To be able to use cmap with a user-defined key-type, you need to define two things: * 1. A hash function; this must be a function that calculates the hash value given * an object of the key-type. * 2. A comparison function for equality. @@ -34,8 +34,8 @@ int vikingvw_equals(const VikingVw* x, const VikingVw* y) { // Viking data struct ----------------------- typedef struct Viking { - CStr name; - CStr country; + cstr_t name; + cstr_t country; } Viking; @@ -52,25 +52,25 @@ Viking viking_fromVw(VikingVw vw) { } -// Using the full declare_CMap() macro to define [Viking -> int] hash map type: -declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, +// Using the full declare_cmap() macro to define [Viking -> int] hash map type: +declare_cmap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, viking_destroy, VikingVw, viking_getVw, viking_fromVw); -// CMap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. +// cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. // cmap_vk_destroy() will free all memory allocated for Viking keys and the hash table values. // Main ---------------------------- int main() { - CMap_vk vikings = cmap_init; + cmap_vk vikings = cmap_init; c_push(&vikings, cmap_vk, c_items( {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, {{"Harald", "Iceland"}, 12}, )); - CMapEntry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"}); + 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 c9a82d6d..8b9f33c2 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -14,7 +14,7 @@ // Visual Studio: compile with -TP to force C++: cl -TP -EHsc -O2 benchmark.c -declare_CMap(ii, int64_t, int64_t, c_defaultDestroy, c_defaultEquals, c_fibonacciHash64); +declare_cmap(ii, int64_t, int64_t, c_defaultDestroy, c_defaultEquals, c_fibonacciHash64); KHASH_MAP_INIT_INT64(ii, uint64_t) @@ -26,7 +26,7 @@ crandom64_t rng; #define RAND(N) (crandom64(&rng) & ((1 << N) - 1)) -#define CMAP_SETUP(tag, Key, Value) CMap_##tag map = cmap_init \ +#define CMAP_SETUP(tag, Key, Value) cmap_##tag map = cmap_init \ ; 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) diff --git a/examples/bits.c b/examples/bits.c index 16adaf96..7c048c80 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -2,7 +2,7 @@ #include int main() { - CBitset set = cbitset_make(23, true); + cbitset_t set = cbitset_make(23, true); printf("count %zu, %zu\n", cbitset_count(set), set.size); cbitset_reset(&set, 9); cbitset_resize(&set, 43, false); @@ -21,7 +21,7 @@ int main() { printf("%d", cbitset_test(set, i)); puts(""); - CBitset s2 = cbitset_from(set); + cbitset_t s2 = cbitset_from(set); cbitset_flipAll(&s2); cbitset_set(&s2, 16); cbitset_set(&s2, 17); diff --git a/examples/complex.c b/examples/complex.c index 40dc9722..87caaa4e 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -5,22 +5,22 @@ 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, CArray2f, carray2f_destroy, c_noCompare); -declare_CMap(il, int, CList_t2, clist_t2_destroy); -declare_CMap_str(sm, CMap_il, cmap_il_destroy); +declare_carray(f, float, check_destroy); // normally omit the last argument - float type need no destroy. +declare_clist(t2, carray2f, carray2f_destroy, c_noCompare); +declare_cmap(il, int, clist_t2, clist_t2_destroy); +declare_cmap_str(sm, cmap_il, cmap_il_destroy); int main() { int xdim = 4, ydim = 6; int x = 1, y = 5, tableKey = 42; const char* strKey = "first"; - CMap_sm theMap = cmap_init; + cmap_sm theMap = cmap_init; { // Construct. - CArray2f table = carray2f_make(ydim, xdim, 0.f); + carray2f table = carray2f_make(ydim, xdim, 0.f); printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table)); - CList_t2 tableList = clist_init; - CMap_il listMap = cmap_init; + clist_t2 tableList = clist_init; + cmap_il listMap = cmap_init; // Put in some data. carray2f_data(table, y)[x] = 3.1415927; // table[y][x] @@ -29,7 +29,7 @@ int main() { cmap_sm_put(&theMap, strKey, listMap); } { // Access the data entry - CArray2f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value); + carray2f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value); printf("value (%d, %d) is: %f\n", y, x, carray2f_value(table, y, x)); } diff --git a/examples/demos.c b/examples/demos.c index 03d8a21d..3a29f7d6 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -8,7 +8,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - CStr cs = cstr_make("one-nine-three-seven-five"); + cstr_t cs = cstr_make("one-nine-three-seven-five"); printf("%s.\n", cs.str); cstr_insert(&cs, 3, "-two"); @@ -33,12 +33,12 @@ void stringdemo1() } -declare_CVec(ix, int64_t); // ix is just an example tag name. +declare_cvec(ix, int64_t); // ix is just an example tag name. void vectordemo1() { printf("\nVECTORDEMO1\n"); - CVec_ix bignums = cvec_init; // = (CVec_ix) cvec_init; if initializing after declaration. + cvec_ix bignums = cvec_init; // = (cvec_ix) cvec_init; if initializing after declaration. cvec_ix_reserve(&bignums, 100); for (size_t i = 0; i<=100; ++i) cvec_ix_pushBack(&bignums, i * i * i); @@ -54,12 +54,12 @@ void vectordemo1() -declare_CVec(cs, CStr, cstr_destroy, cstr_compare); // supply inline destructor of values +declare_cvec(cs, cstr_t, cstr_destroy, cstr_compare); // supply inline destructor of values void vectordemo2() { printf("\nVECTORDEMO2\n"); - CVec_cs names = cvec_init; + cvec_cs names = cvec_init; cvec_cs_pushBack(&names, cstr_make("Mary")); cvec_cs_pushBack(&names, cstr_make("Joe")); cvec_cs_pushBack(&names, cstr_make("Chris")); @@ -72,12 +72,12 @@ void vectordemo2() cvec_cs_destroy(&names); } -declare_CList(ix, int); +declare_clist(ix, int); void listdemo1() { printf("\nLISTDEMO1\n"); - CList_ix nums = clist_init, nums2 = clist_init; + clist_ix nums = clist_init, nums2 = clist_init; for (int i = 0; i < 10; ++i) clist_ix_pushBack(&nums, i); for (int i = 100; i < 110; ++i) @@ -99,12 +99,12 @@ void listdemo1() clist_ix_destroy(&nums); } -declare_CSet(i, int); +declare_cset(i, int); void setdemo1() { printf("\nSETDEMO1\n"); - CSet_i nums = cset_init; + cset_i nums = cset_init; cset_i_put(&nums, 8); cset_i_put(&nums, 11); @@ -114,12 +114,12 @@ void setdemo1() } -declare_CMap(ii, int, int); +declare_cmap(ii, int, int); void mapdemo1() { printf("\nMAPDEMO1\n"); - CMap_ii nums = cmap_init; + cmap_ii nums = cmap_init; cmap_ii_put(&nums, 8, 64); cmap_ii_put(&nums, 11, 121); @@ -128,12 +128,12 @@ void mapdemo1() } -declare_CMap_str(si, int); // Shorthand macro for the general declare_CMap expansion. +declare_cmap_str(si, int); // Shorthand macro for the general declare_cmap expansion. void mapdemo2() { printf("\nMAPDEMO2\n"); - CMap_si nums = cmap_init; + cmap_si nums = cmap_init; cmap_si_put(&nums, "Hello", 64); cmap_si_put(&nums, "Groovy", 121); cmap_si_put(&nums, "Groovy", 200); // overwrite previous @@ -150,16 +150,16 @@ void mapdemo2() } -declare_CMap_str(ss, CStr, cstr_destroy); +declare_cmap_str(ss, cstr_t, cstr_destroy); void mapdemo3() { printf("\nMAPDEMO3\n"); - CMap_ss table = cmap_init; + cmap_ss table = cmap_init; 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_find(&table, "Make"); + cmapentry_ss *e = cmap_ss_find(&table, "Make"); c_foreach (i, cmap_ss, table) printf("entry: %s: %s\n", i.item->key.str, i.item->value.str); printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str); @@ -173,14 +173,14 @@ void mapdemo3() } -declare_CArray(f, float); +declare_carray(f, float); void arraydemo1() { printf("\nARRAYDEMO1\n"); - CArray3f a3 = carray3f_make(30, 20, 10, 0.f); + carray3f a3 = carray3f_make(30, 20, 10, 0.f); carray3f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] - CArray2f a2 = carray3f_at(a3, 5); // sub-array reference (no data copy). + carray2f a2 = carray3f_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)); diff --git a/examples/geek1.c b/examples/geek1.c index f8471122..1df74024 100644 --- a/examples/geek1.c +++ b/examples/geek1.c @@ -12,14 +12,14 @@ int a[] = { 1, 2, 2, 3, 2, 4, 10 }; #include #include -declare_CMap(ii, int, int); +declare_cmap(ii, int, int); // Function to maximize the number of pairs int findMaximumPairs(int a[], int n, int k) { // Hash-table - CMap_ii hash = cmap_init; + cmap_ii hash = cmap_init; for (int i = 0; i < n; i++) { cmap_ii_insert(&hash, a[i] % k, 0)->value++; } @@ -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_find(&hash, first), + cmapentry_ii *hf = cmap_ii_find(&hash, first), *hs = cmap_ii_insert(&hash, second, 0); // Check for minimal occurrence if (hf->value < hs->value) { diff --git a/examples/geek2.c b/examples/geek2.c index 1c2f0c05..988f1f98 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -3,15 +3,15 @@ #include #include -declare_CMap_str(ss, CStr, cstr_destroy); -declare_CSet_str(); +declare_cmap_str(ss, cstr_t, cstr_destroy); +declare_cset_str(); int main() { // Lets use an explicit type signature (which would - // be `CMap` in this example). - CMap_ss book_reviews = cmap_init; - CSet_str set = cset_init; + // be `cmap` in this example). + cmap_ss book_reviews = cmap_init; + cset_str set = cset_init; cset_str_put(&set, "Hello"); cset_str_put(&set, "You"); cset_str_put(&set, "Tube"); @@ -50,7 +50,7 @@ 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_find(&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); } diff --git a/examples/geek3.c b/examples/geek3.c index f6f8942a..b8de97c8 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -3,12 +3,12 @@ #include #include -declare_CMap_str(si, int); -declare_CMap_str(ss, CStr, cstr_destroy); +declare_cmap_str(si, int); +declare_cmap_str(ss, cstr_t, cstr_destroy); int main () { - CMap_si mymap = cmap_init; + cmap_si mymap = cmap_init; cmap_si_put(&mymap, "Mars", 3000); cmap_si_put(&mymap, "Saturn", 60000); cmap_si_put(&mymap, "Jupiter", 70000); @@ -28,7 +28,7 @@ int main () puts("------------------------"); // Create an unordered_map of three strings (that map to strings) - CMap_ss u = cmap_init; + cmap_ss u = cmap_init; cmap_ss_put(&u, "RED", cstr_make("#FF0000")); cmap_ss_put(&u, "GREEN", cstr_make("#00FF00")); cmap_ss_put(&u, "BLUE", cstr_make("#0000FF")); diff --git a/examples/geek4.c b/examples/geek4.c index e7522bb3..87f9b919 100644 --- a/examples/geek4.c +++ b/examples/geek4.c @@ -37,19 +37,19 @@ Efficient Approach: For all the words of the first sentence, we can check if it #include #include -declare_CVec_str(); -declare_CMap_str(sb, bool); -declare_CVec(sb, CMapEntry_sb, cmapentry_sb_destroy, c_noCompare); +declare_cvec_str(); +declare_cmap_str(sb, bool); +declare_cvec(sb, cmapentry_sb, cmapentry_sb_destroy, c_noCompare); // Function to return the count of common words // in all the sentences -int commonWords(CVec_str S) +int commonWords(cvec_str S) { int m, n, i, j; // To store all the words of first string - CVec_sb ans = cvec_init; + cvec_sb ans = cvec_init; // m will store number of strings in given vector m = cvec_size(S); @@ -59,8 +59,8 @@ int commonWords(CVec_str S) // Extract all words of first string and store it in ans while (i < cstr_size(S.data[0])) { // To store separate words - CStr word = cstr_init; - CMapEntry_sb tmp = {cstr_init, false}; + cstr_t word = cstr_init; + cmapentry_sb tmp = {cstr_init, false}; while (i < cstr_size(S.data[0]) && S.data[0].str[i] != ' ') { cstr_pushBack(&word, S.data[0].str[i]); @@ -85,11 +85,11 @@ int commonWords(CVec_str S) for (j = 1; j < m; j++) { // It will be used to check if a word is present // in a particuler string - CMap_sb has = cmap_init; + cmap_sb has = cmap_init; i = 0; while (i < cstr_size(S.data[j])) { - CStr word = cstr_init; + cstr_t word = cstr_init; while (i < cstr_size(S.data[j]) && S.data[j].str[i] != ' ') { cstr_pushBack(&word, S.data[j].str[i]); i++; @@ -136,7 +136,7 @@ int commonWords(CVec_str S) // Driver code int main() { - CVec_str S = cvec_init; + cvec_str S = cvec_init; cvec_str_pushBack(&S, cstr_make("there is a cow")); cvec_str_pushBack(&S, cstr_make("cow is our mother")); cvec_str_pushBack(&S, cstr_make("cow gives us milk and milk is sweet")); diff --git a/examples/geek5.c b/examples/geek5.c index 546cb278..b1e2f2b9 100644 --- a/examples/geek5.c +++ b/examples/geek5.c @@ -21,24 +21,24 @@ Output: 0 #include #include -declare_CVec(i, int); -declare_CMap_str(sv, CVec_i, cvec_i_destroy); +declare_cvec(i, int); +declare_cmap_str(sv, cvec_i, cvec_i_destroy); // Function to return the number of occurrences of int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) { // To store the indices of strings in the array - CMap_sv M = cmap_init; + cmap_sv M = cmap_init; for (int i = 0; i < n; i++) { const char* temp = arr[i]; - CMapEntry_sv* it = cmap_sv_find(&M, temp); + cmapentry_sv* it = cmap_sv_find(&M, temp); // If current string doesn't // have an entry in the map // then create the entry if (it == NULL) { - CVec_i A = cvec_init; + cvec_i A = cvec_init; cvec_i_pushBack(&A, i + 1); cmap_sv_put(&M, temp, A); } @@ -47,7 +47,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) } } - CMapEntry_sv* it = cmap_sv_find(&M, str); + cmapentry_sv* it = cmap_sv_find(&M, str); // If the given string is not // present in the array @@ -56,7 +56,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) // If the given string is present // in the array - CVec_i A = it->value; + cvec_i A = it->value; int y = 0, x = 0; for (; y < cvec_size(A); ++y) if (A.data[y] > R) break; for (; x < cvec_size(A); ++x) if (A.data[x] > L - 1) break; diff --git a/examples/geek6.c b/examples/geek6.c index 6b70dd5e..4689d934 100644 --- a/examples/geek6.c +++ b/examples/geek6.c @@ -30,14 +30,14 @@ operation in almost O(1) time complexity. #include #include -declare_CSet(i, int); +declare_cset(i, int); // Function to find the smallest positive // missing number int missingNumber(int a[], int n) { // Declaring an unordered_map - CSet_i mp = cset_init; + cset_i mp = cset_init; // if array value is positive // store it in map diff --git a/examples/geek7.c b/examples/geek7.c index e304718d..88cfa33c 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -26,9 +26,9 @@ After inserting all the elements excluding the ones which are to be deleted, Pop #include #include -declare_CMap(ii, int, int); -declare_CVec(i, int); -declare_CVec_priority_queue(i, >); +declare_cmap(ii, int, int); +declare_cvec(i, int); +declare_cvec_priority_queue(i, >); // Find k minimum element from arr[0..m-1] after deleting // elements from del[0..n-1] @@ -36,19 +36,19 @@ void findElementsAfterDel(int arr[], int m, int del[], int n, int k) { // Hash Map of the numbers to be deleted - CMap_ii mp = cmap_init; + cmap_ii mp = cmap_init; for (int i = 0; i < n; ++i) { // Increment the count of del[i] cmap_ii_insert(&mp, del[i], 0)->value++; } - CVec_i heap = cvec_init; + cvec_i heap = cvec_init; for (int i = 0; i < m; ++i) { // Search if the element is present - CMapEntry_ii *e = cmap_ii_find(&mp, arr[i]); + cmapentry_ii *e = cmap_ii_find(&mp, arr[i]); if (e != NULL) { // Decrement its frequency diff --git a/examples/heap.c b/examples/heap.c index e4e91544..94409371 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -3,15 +3,15 @@ #include "stc/cvecpq.h" #include "stc/crandom.h" -declare_CVec(f, float); -declare_CVec_priority_queue(f, >); +declare_cvec(f, float); +declare_cvec_priority_queue(f, >); int main() { uint32_t seed = time(NULL); crandom32_t pcg = crandom32_init(seed); int N = 30000000, M = 100; - CVec_f vec = cvec_init; + cvec_f vec = cvec_init; clock_t start = clock(); for (int i=0; i #include -declare_CMap(id, int, CStr, cstr_destroy); // Map of int -> CStr -declare_CMap_str(cnt, int); +declare_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t +declare_cmap_str(cnt, int); typedef struct {int x, y;} ipair_t; -declare_CVec(ip, ipair_t, c_defaultDestroy, c_memCompare); -declare_CList(ip, ipair_t, c_defaultDestroy, c_memCompare); +declare_cvec(ip, ipair_t, c_defaultDestroy, c_memCompare); +declare_clist(ip, ipair_t, c_defaultDestroy, c_memCompare); int main(void) { int year = 2020; - CMap_id idnames = cmap_init; + cmap_id idnames = cmap_init; c_push(&idnames, cmap_id, c_items( {100, cstr_make("Hello")}, {110, cstr_make("World")}, @@ -27,7 +27,7 @@ int main(void) { // ------------------ - CMap_cnt countries = cmap_init; + cmap_cnt countries = cmap_init; cmap_cnt_insert(&countries, "Greenland", 0)->value += 20; c_push(&countries, cmap_cnt, c_items( @@ -46,7 +46,7 @@ int main(void) { // ------------------ - CVec_ip pairs1 = cvec_init; + cvec_ip pairs1 = cvec_init; c_push(&pairs1, cvec_ip, c_items( {1, 2}, {3, 4}, @@ -61,7 +61,7 @@ int main(void) { // ------------------ - CList_ip pairs2 = clist_init; + clist_ip pairs2 = clist_init; c_push(&pairs2, clist_ip, c_items( {1, 2}, {3, 4}, diff --git a/examples/list.c b/examples/list.c index 53b8711f..2eb1f4c0 100644 --- a/examples/list.c +++ b/examples/list.c @@ -2,10 +2,10 @@ #include #include #include -declare_CList(ix, uint64_t); +declare_clist(ix, uint64_t); int main() { - CList_ix list = clist_init; + clist_ix list = clist_init; crandom32_t pcg = crandom32_init(time(NULL)); int n; for (int i=0; i<10000000; ++i) // ten million diff --git a/examples/mapmap.c b/examples/mapmap.c index 67672dee..b349e8d3 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -6,12 +6,12 @@ static void test_destr(int* x) { printf("destroy int: %d\n", *x); } -declare_CMap(ii, int, int, test_destr); -declare_CMap(im, int, CMap_ii, cmap_ii_destroy); +declare_cmap(ii, int, int, test_destr); +declare_cmap(im, int, cmap_ii, cmap_ii_destroy); int main(void) { - CMap_im m = cmap_init; - CMap_ii ini = cmap_init; + cmap_im m = cmap_init; + cmap_ii ini = cmap_init; cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2000, 200); cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2001, 201); cmap_ii_put(&cmap_im_insert(&m, 100, ini)->value, 2000, 400); // update diff --git a/examples/prime.c b/examples/prime.c index 05ef4d57..611690ac 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -10,7 +10,7 @@ static inline void sieveOfEratosthenes(size_t n) { - CBitset prime = cbitset_make(n + 1, true); + cbitset_t prime = cbitset_make(n + 1, true); printf("computing prime numbers up to %zu\n", n); cbitset_reset(&prime, 0); cbitset_reset(&prime, 1); diff --git a/examples/priority.c b/examples/priority.c index c1890f8c..37e7be60 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -5,12 +5,12 @@ #include #include -declare_CVec(i, uint32_t); -declare_CVec_priority_queue(i, >); // min-heap (increasing values) +declare_cvec(i, uint32_t); +declare_cvec_priority_queue(i, >); // min-heap (increasing values) int main() { crandom32_t pcg = crandom32_init(time(NULL)); - CVec_i heap = cvec_init; + cvec_i heap = cvec_init; // Push ten million random numbers to queue for (int i=0; i<10000000; ++i) diff --git a/examples/rngbirthday.c b/examples/rngbirthday.c index 1e6178aa..5e77fe43 100644 --- a/examples/rngbirthday.c +++ b/examples/rngbirthday.c @@ -7,7 +7,7 @@ #include #include -declare_CMap(ic, uint64_t, uint8_t); +declare_cmap(ic, uint64_t, uint8_t); const static uint64_t seed = 1234; const static uint64_t N = 1ull << 27; @@ -16,7 +16,7 @@ const static uint64_t mask = (1ull << 52) - 1; void repeats(void) { crandom64_t rng = crandom64_init(seed); - CMap_ic m = cmap_init; + cmap_ic m = cmap_init; cmap_ic_reserve(&m, N); clock_t now = clock(); for (size_t i = 0; i < N; ++i) { @@ -29,14 +29,14 @@ void repeats(void) } -declare_CMap(x, uint32_t, uint64_t); -declare_CVec(x, uint64_t); +declare_cmap(x, uint32_t, uint64_t); +declare_cvec(x, uint64_t); void distribution(void) { crandom32_t rng = crandom32_init(seed); // time(NULL), time(NULL)); const size_t N = 1ull << 28, M = 1ull << 9; // 1ull << 10; - CMap_x map = cmap_x_make(M); + cmap_x map = cmap_x_make(M); clock_t now = clock(); for (size_t i = 0; i < N; ++i) { ++cmap_x_insert(&map, crandom32b(&rng, M), 0)->value; diff --git a/stc/coptget.h b/stc/coptget.h new file mode 100644 index 00000000..49c895e0 --- /dev/null +++ b/stc/coptget.h @@ -0,0 +1,173 @@ +/* MIT License + * + * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef COPT__H__ +#define COPT__H__ + +/* Inspired by https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c + Fixed major bugs with option arguments (both long and short). + Added arg->faulty output field, and has a more consistent API. + + copt_get() is similar to GNU's getopt_long(). Each call parses one option and + returns the option name. opt->arg points to the option argument if present. + The function returns -1 when all command-line arguments are parsed. In this case, + opt->ind is the index of the first non-option argument. +Example: + int main(int argc, char *argv[]) + { + copt_long_t longopts[] = { + {"foo", copt_no_argument, 'f'}, + {"bar", copt_required_argument, 'b'}, + {"opt", copt_optional_argument, 'o'}, + {NULL} + }; + const char* optstr = "xy:z::123"; + printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n"); + int c; + copt_t opt = copt_init; + while ((c = copt_get(&opt, argc, argv, optstr, longopts)) != -1) { + switch (c) { + case '?': printf("error: unknown option: %s\n", opt.faulty); break; + case ':': printf("error: missing argument for %s\n", opt.faulty); break; + default: printf("option: %c [%s]\n", c, opt.arg ? opt.arg : ""); break; + } + } + printf("\nNon-option arguments:"); + for (int i = opt.ind; i < argc; ++i) + printf(" %s", argv[i]); + putchar('\n'); + return 0; + } +*/ + +#include +#include + +enum { + copt_no_argument = 0, + copt_required_argument = 1, + copt_optional_argument = 2 +}; +typedef struct { + int ind; /* equivalent to optind */ + int opt; /* equivalent to optopt */ + char *arg; /* equivalent to optarg */ + char *faulty; /* points to the faulty option */ + int longindex; /* idx of long option; or -1 if short */ + int _i, _pos, _nargs; + char _faulty[4]; +} copt_t; + +typedef struct { + char *name; + int has_arg; + int val; +} copt_long_t; + +static const copt_t copt_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}}; + +static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */ + int k; + char *p = argv[j]; + for (k = 0; k < n; ++k) + argv[j - k] = argv[j - k - 1]; + argv[j - k] = p; +} + +/* @param opt output; must be initialized to copt_init on first call + * @return ASCII val for a short option; longopt.val for a long option; + * -1 if argv[] is fully processed; '?' for an unknown option or + * an ambiguous long option; ':' if an option argument is missing + */ +static int copt_get(copt_t *opt, int argc, char *argv[], + const char *shortopts, const copt_long_t *longopts) { + int optc = -1, i0, j, posixly_correct = (shortopts[0] == '+'); + if (!posixly_correct) { + while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0')) + ++opt->_i, ++opt->_nargs; + } + opt->arg = 0, opt->longindex = -1, i0 = opt->_i; + if (opt->_i >= argc || argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0') { + opt->ind = opt->_i - opt->_nargs; + return -1; + } + if (argv[opt->_i][0] == '-' && argv[opt->_i][1] == '-') { /* "--" or a long option */ + if (argv[opt->_i][2] == '\0') { /* a bare "--" */ + _copt_permute(argv, opt->_i, opt->_nargs); + ++opt->_i, opt->ind = opt->_i - opt->_nargs; + return -1; + } + opt->opt = 0, optc = '?', opt->_pos = -1; + if (longopts) { /* parse long options */ + int k, n_exact = 0, n_partial = 0; + const copt_long_t *o = 0, *o_exact = 0, *o_partial = 0; + for (j = 2; argv[opt->_i][j] != '\0' && argv[opt->_i][j] != '='; ++j) {} /* find the end of the option name */ + for (k = 0; longopts[k].name != 0; ++k) + if (strncmp(&argv[opt->_i][2], longopts[k].name, j - 2) == 0) { + if (longopts[k].name[j - 2] == 0) ++n_exact, o_exact = &longopts[k]; + else ++n_partial, o_partial = &longopts[k]; + } + opt->faulty = argv[opt->_i]; + if (n_exact > 1 || (n_exact == 0 && n_partial > 1)) return '?'; + o = n_exact == 1? o_exact : n_partial == 1? o_partial : 0; + if (o) { + opt->opt = optc = o->val, opt->longindex = o - longopts; + if (o->has_arg != copt_no_argument) { + if (argv[opt->_i][j] == '=') + opt->arg = &argv[opt->_i][j + 1]; + else if (argv[opt->_i][j] == '\0' && opt->_i < argc - 1 && (o->has_arg == copt_required_argument || + argv[opt->_i + 1][0] != '-')) + opt->arg = argv[++opt->_i]; + else if (o->has_arg == copt_required_argument) + optc = ':'; /* missing option argument */ + } + } + } + } else { /* a short option */ + const char *p; + if (opt->_pos == 0) opt->_pos = 1; + optc = opt->opt = argv[opt->_i][opt->_pos++]; + opt->_faulty[1] = optc, opt->faulty = opt->_faulty; + p = strchr((char *) shortopts, optc); + if (p == 0) { + optc = '?'; /* unknown option */ + } else if (p[1] == ':') { + if (argv[opt->_i][opt->_pos] != '\0') + opt->arg = &argv[opt->_i][opt->_pos]; + else if (opt->_i < argc - 1 && (p[2] != ':' || argv[opt->_i + 1][0] != '-')) + opt->arg = argv[++opt->_i]; + else if (p[2] != ':') + optc = ':'; + opt->_pos = -1; + } + } + if (opt->_pos < 0 || argv[opt->_i][opt->_pos] == 0) { + ++opt->_i, opt->_pos = 0; + if (opt->_nargs > 0) /* permute */ + for (j = i0; j < opt->_i; ++j) + _copt_permute(argv, j, opt->_nargs); + } + opt->ind = opt->_i - opt->_nargs; + return optc; +} + +#endif diff --git a/stc/coption.h b/stc/coption.h deleted file mode 100644 index 78500210..00000000 --- a/stc/coption.h +++ /dev/null @@ -1,173 +0,0 @@ -/* MIT License - * - * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef COPTION__H__ -#define COPTION__H__ - -/* Inspired by https://attractivechaos.wordpress.com/2018/08/31/a-survey-of-argument-parsing-libraries-in-c-c - Fixed major bugs with option arguments (both long and short). - Added arg->faulty output field, and has a more consistent API. - - coption_get() has a very similar interface to GNU's getopt_long(). Each call - parses one option and returns the option name. opt->arg points to the option - argument if present. The function returns -1 when all command-line arguments - are parsed. In this case, opt->ind is the index of the first non-option argument. -Example: - int main(int argc, char *argv[]) - { - COptLong longopts[] = { - {"foo", copt_no_argument, 'f'}, - {"bar", copt_required_argument, 'b'}, - {"opt", copt_optional_argument, 'o'}, - {NULL} - }; - const char* optstr = "xy:z::123"; - printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n"); - int c; - COption opt = coption_init; - while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) { - switch (c) { - case '?': printf("error: unknown option: %s\n", opt.faulty); break; - case ':': printf("error: missing argument for %s\n", opt.faulty); break; - default: printf("option: %c [%s]\n", c, opt.arg ? opt.arg : ""); break; - } - } - printf("\nNon-option arguments:"); - for (int i = opt.ind; i < argc; ++i) - printf(" %s", argv[i]); - putchar('\n'); - return 0; - } -*/ - -#include -#include - -enum { - copt_no_argument = 0, - copt_required_argument = 1, - copt_optional_argument = 2 -}; -typedef struct { - int ind; /* equivalent to optind */ - int opt; /* equivalent to optopt */ - char *arg; /* equivalent to optarg */ - char *faulty; /* points to the faulty option */ - int longindex; /* idx of long option; or -1 if short */ - int _i, _pos, _nargs; - char _faulty[4]; -} COption; - -typedef struct { - char *name; - int has_arg; - int val; -} COptLong; - -static const COption coption_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}}; - -static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */ - int k; - char *p = argv[j]; - for (k = 0; k < n; ++k) - argv[j - k] = argv[j - k - 1]; - argv[j - k] = p; -} - -/* @param opt output; must be initialized to coption_init on first call - * @return ASCII val for a short option; longopt.val for a long option; - * -1 if argv[] is fully processed; '?' for an unknown option or - * an ambiguous long option; ':' if an option argument is missing - */ -static int coption_get(COption *opt, int argc, char *argv[], - const char *shortopts, const COptLong *longopts) { - int optc = -1, i0, j, posixly_correct = (shortopts[0] == '+'); - if (!posixly_correct) { - while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0')) - ++opt->_i, ++opt->_nargs; - } - opt->arg = 0, opt->longindex = -1, i0 = opt->_i; - if (opt->_i >= argc || argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0') { - opt->ind = opt->_i - opt->_nargs; - return -1; - } - if (argv[opt->_i][0] == '-' && argv[opt->_i][1] == '-') { /* "--" or a long option */ - if (argv[opt->_i][2] == '\0') { /* a bare "--" */ - _coption_permute(argv, opt->_i, opt->_nargs); - ++opt->_i, opt->ind = opt->_i - opt->_nargs; - return -1; - } - opt->opt = 0, optc = '?', opt->_pos = -1; - if (longopts) { /* parse long options */ - int k, n_exact = 0, n_partial = 0; - const COptLong *o = 0, *o_exact = 0, *o_partial = 0; - for (j = 2; argv[opt->_i][j] != '\0' && argv[opt->_i][j] != '='; ++j) {} /* find the end of the option name */ - for (k = 0; longopts[k].name != 0; ++k) - if (strncmp(&argv[opt->_i][2], longopts[k].name, j - 2) == 0) { - if (longopts[k].name[j - 2] == 0) ++n_exact, o_exact = &longopts[k]; - else ++n_partial, o_partial = &longopts[k]; - } - opt->faulty = argv[opt->_i]; - if (n_exact > 1 || (n_exact == 0 && n_partial > 1)) return '?'; - o = n_exact == 1? o_exact : n_partial == 1? o_partial : 0; - if (o) { - opt->opt = optc = o->val, opt->longindex = o - longopts; - if (o->has_arg != copt_no_argument) { - if (argv[opt->_i][j] == '=') - opt->arg = &argv[opt->_i][j + 1]; - else if (argv[opt->_i][j] == '\0' && opt->_i < argc - 1 && (o->has_arg == copt_required_argument || - argv[opt->_i + 1][0] != '-')) - opt->arg = argv[++opt->_i]; - else if (o->has_arg == copt_required_argument) - optc = ':'; /* missing option argument */ - } - } - } - } else { /* a short option */ - const char *p; - if (opt->_pos == 0) opt->_pos = 1; - optc = opt->opt = argv[opt->_i][opt->_pos++]; - opt->_faulty[1] = optc, opt->faulty = opt->_faulty; - p = strchr((char *) shortopts, optc); - if (p == 0) { - optc = '?'; /* unknown option */ - } else if (p[1] == ':') { - if (argv[opt->_i][opt->_pos] != '\0') - opt->arg = &argv[opt->_i][opt->_pos]; - else if (opt->_i < argc - 1 && (p[2] != ':' || argv[opt->_i + 1][0] != '-')) - opt->arg = argv[++opt->_i]; - else if (p[2] != ':') - optc = ':'; - opt->_pos = -1; - } - } - if (opt->_pos < 0 || argv[opt->_i][opt->_pos] == 0) { - ++opt->_i, opt->_pos = 0; - if (opt->_nargs > 0) /* permute */ - for (j = i0; j < opt->_i; ++j) - _coption_permute(argv, j, opt->_nargs); - } - opt->ind = opt->_i - opt->_nargs; - return optc; -} - -#endif -- cgit v1.2.3