From a4e2ee22fd57665d2388d5debc17db896a4a389f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 3 Sep 2020 12:59:41 +0200 Subject: Changed constant _init to _ini to avoid conflict with _init() method. Reverted name cprique back to cpqueue. --- README.md | 18 +++---- examples/README.md | 2 +- examples/advanced.c | 2 +- examples/benchmark.c | 2 +- examples/birthday.c | 2 +- examples/complex.c | 6 +-- examples/demos.c | 14 ++--- examples/ex_gaussian.c | 6 +-- examples/geek1.c | 2 +- examples/geek2.c | 4 +- examples/geek3.c | 4 +- examples/geek4.c | 12 ++--- examples/geek5.c | 4 +- examples/geek6.c | 2 +- examples/geek7.c | 16 +++--- examples/heap.c | 18 +++---- examples/inits.c | 14 ++--- examples/list.c | 2 +- examples/mapmap.c | 4 +- examples/phonebook.c | 2 +- examples/priority.c | 18 +++---- examples/random.c | 2 +- examples/stack.c | 5 +- examples/words.c | 6 +-- stc/cbitset.h | 2 +- stc/clist.h | 6 +-- stc/cmap.h | 12 ++--- stc/coption.h | 6 +-- stc/cpqueue.h | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ stc/cprique.h | 141 ------------------------------------------------- stc/cqueue.h | 4 +- stc/cstack.h | 4 +- stc/cstr.h | 29 +++++----- stc/cvec.h | 8 +-- 34 files changed, 264 insertions(+), 256 deletions(-) create mode 100644 stc/cpqueue.h delete mode 100644 stc/cprique.h diff --git a/README.md b/README.md index 8f3c9f84..b5f76405 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ An elegant, fully typesafe, generic, customizable, user-friendly, consistent, an - **stc/cvec.h** - Dynamic generic **vector** class, works well as a **stack**. - **stc/cstack.h** - **stack** adapter, currently works with *cvec*. - **stc/cqueue.h** - **queue** adapter, currently works with *clist*. -- **stc/cprique.h** - **priority queue** adapter. Works with *cvec*. +- **stc/cpqueue.h** - **priority queue** adapter. Works with *cvec*. - **stc/coption.h** - Implementation of a **getopt_long()**-like function, *coption_get()*, to parse command line arguments. - **stc/crandom.h** - A few very efficent modern random number generators *pcg32* and my own *64-bit PRNG* inspired by *sfc64*. Both uniform and normal distributions. - **stc/cdefs.h** - A common include file with a few general definitions. @@ -27,7 +27,7 @@ All containers mentioned above, except cstr_t and cbitset_t are generic and type declare_cvec(i, int); int main(void) { - cvec_i vec = cvec_init; + cvec_i vec = cvec_ini; cvec_i_push_back(&vec, 42); cvec_i_destroy(&vec); } @@ -136,7 +136,7 @@ To avoid this, use ``` declare_cmap_strkey(si, int); ... -cmap_si map = cmap_init; +cmap_si map = cmap_ini; cmap_si_put(&map, "mykey", 12); // constructs a cstr_t key from the const char* internally. int x = cmap_si_find(&map, "mykey")->value; // no allocation of string key happens here. cmap_si_destroy(&map); @@ -184,7 +184,7 @@ int main() { declare_cvec(ix, int64_t); // ix is just an example type tag name. int main() { - cvec_ix bignums = cvec_init; // use cvec_ix_init() if initializing after declaration. + cvec_ix bignums = cvec_ini; // use cvec_ix_init() if initializing after declaration. cvec_ix_reserve(&bignums, 100); for (size_t i = 0; i<100; ++i) cvec_ix_push_back(&bignums, i * i * i); @@ -203,7 +203,7 @@ int main() { declare_cvec_str(); int main() { - cvec_str names = cvec_init; + cvec_str names = cvec_ini; cvec_str_push_back(&names, "Mary"); cvec_str_push_back(&names, "Joe"); cstr_assign(&names.data[1], "Jake"); // replace "Joe". @@ -223,7 +223,7 @@ int main() { declare_cmap(ii, int, int); int main() { - cmap_ii nums = cmap_init; + cmap_ii nums = cmap_ini; cmap_ii_put(&nums, 8, 64); // put() works as c++ std::unordered_map<>::insert_or_replace() cmap_ii_insert(&nums, 11, 121); // only insert value if key does not exists - like std::unordered_map::insert(). @@ -238,7 +238,7 @@ int main() { declare_cset_str(); // cstr set. See the discussion above. int main() { - cset_str words = cset_init; + cset_str words = cset_ini; cset_str_put(&words, "Hello"); cset_str_put(&words, "Cruel"); cset_str_put(&words, "World"); @@ -257,7 +257,7 @@ int main() { declare_cmap_str(); int main() { - cmap_str table = cmap_init; + cmap_str table = cmap_ini; cmap_str_put(&table, "Make", "my"); cmap_str_put(&table, "Rainy", "day"); cmap_str_put(&table, "Sunny", "afternoon"); @@ -279,7 +279,7 @@ int main() { declare_clist(fx, double); int main() { - clist_fx list = clist_init; + clist_fx list = clist_ini; crand_eng64_t eng = crand_eng64_init(time(NULL)); crand_uniform_f64_t dist = crand_uniform_f64_init(100.0, 1000.0); int k; diff --git a/examples/README.md b/examples/README.md index 661f7555..7947f16c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -60,7 +60,7 @@ cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() Finally, main which also demos the generic c_push() of multiple elements: ``` int main() { - cmap_vk vikings = cmap_init; + cmap_vk vikings = cmap_ini; c_push(&vikings, cmap_vk, c_items( { {"Einar", "Norway"}, 20 }, { {"Olaf", "Denmark"}, 24 }, diff --git a/examples/advanced.c b/examples/advanced.c index 03baf5fc..90f800e0 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -62,7 +62,7 @@ declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, int main() { - cmap_vk vikings = cmap_init; + cmap_vk vikings = cmap_ini; c_push(&vikings, cmap_vk, c_items( {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, diff --git a/examples/benchmark.c b/examples/benchmark.c index d4e1e82c..1c94397d 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -34,7 +34,7 @@ crand_rng64_t rng; #define RAND(N) (crand_i64(&rng) & ((1 << N) - 1)) -#define CMAP_SETUP(tt, Key, Value) cmap_##tt map = cmap_init \ +#define CMAP_SETUP(tt, Key, Value) cmap_##tt map = cmap_ini \ ; cmap_##tt##_set_load_factors(&map, max_load_factor, 0.0) #define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val)->value #define CMAP_INSERT(tt, key, val) cmap_##tt##_insert(&map, key, val) diff --git a/examples/birthday.c b/examples/birthday.c index 4f6e7a8b..be2114da 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -16,7 +16,7 @@ const static uint64_t mask = (1ull << 52) - 1; void repeats(void) { crand_rng64_t rng = crand_rng64_init(seed); - cmap_ic m = cmap_init; + cmap_ic m = cmap_ini; cmap_ic_reserve(&m, N); clock_t now = clock(); for (size_t i = 0; i < N; ++i) { diff --git a/examples/complex.c b/examples/complex.c index 8810bac5..25a59eff 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -14,14 +14,14 @@ int main() { int xdim = 4, ydim = 6; int x = 1, y = 5, tableKey = 42; const char* strKey = "first"; - cmap_s myMap = cmap_init; + cmap_s myMap = cmap_ini; { // Construct. carray2f table = carray2f_make(ydim, xdim, 0.f); printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table)); - clist_y tableList = clist_init; + clist_y tableList = clist_ini; // Put in some data. - cmap_g listMap = cmap_init; + cmap_g listMap = cmap_ini; *carray2f_at(&table, y, x) = 3.1415927; // table[y][x] clist_y_push_back(&tableList, table); diff --git a/examples/demos.c b/examples/demos.c index 731197e3..6b2806fa 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -39,7 +39,7 @@ 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_ini; // = (cvec_ix) cvec_ini; if initializing after declaration. cvec_ix_reserve(&bignums, 100); for (size_t i = 0; i<=100; ++i) cvec_ix_push_back(&bignums, i * i * i); @@ -60,7 +60,7 @@ declare_cvec_str(); void vectordemo2() { printf("\nVECTORDEMO2\n"); - cvec_str names = cvec_init; + cvec_str names = cvec_ini; cvec_str_push_back(&names, "Mary"); cvec_str_push_back(&names, "Joe"); cvec_str_push_back(&names, "Chris"); @@ -78,7 +78,7 @@ declare_clist(ix, int); void listdemo1() { printf("\nLISTDEMO1\n"); - clist_ix nums = clist_init, nums2 = clist_init; + clist_ix nums = clist_ini, nums2 = clist_ini; for (int i = 0; i < 10; ++i) clist_ix_push_back(&nums, i); for (int i = 100; i < 110; ++i) @@ -105,7 +105,7 @@ declare_cset(i, int); void setdemo1() { printf("\nSETDEMO1\n"); - cset_i nums = cset_init; + cset_i nums = cset_ini; cset_i_put(&nums, 8); cset_i_put(&nums, 11); @@ -120,7 +120,7 @@ declare_cmap(ii, int, int); void mapdemo1() { printf("\nMAPDEMO1\n"); - cmap_ii nums = cmap_init; + cmap_ii nums = cmap_ini; cmap_ii_put(&nums, 8, 64); cmap_ii_put(&nums, 11, 121); @@ -134,7 +134,7 @@ declare_cmap_strkey(si, int); // Shorthand macro for the general declare_cmap ex void mapdemo2() { printf("\nMAPDEMO2\n"); - cmap_si nums = cmap_init; + cmap_si nums = cmap_ini; cmap_si_put(&nums, "Hello", 64); cmap_si_put(&nums, "Groovy", 121); cmap_si_put(&nums, "Groovy", 200); // overwrite previous @@ -156,7 +156,7 @@ declare_cmap_strkey(ss, cstr_t, cstr_destroy); void mapdemo3() { printf("\nMAPDEMO3\n"); - cmap_ss table = cmap_init; + cmap_ss table = cmap_ini; cmap_ss_put(&table, "Map", cstr_make("test")); cmap_ss_put(&table, "Make", cstr_make("my")); cmap_ss_put(&table, "Sunny", cstr_make("day")); diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index 75efec51..a4682a2b 100644 --- a/examples/ex_gaussian.c +++ b/examples/ex_gaussian.c @@ -29,20 +29,20 @@ int main() crand_normal_f64_t dist = crand_normal_f64_init(rng, Mean, StdDev); // Create histogram map - cmap_i mhist = cmap_init; + cmap_i mhist = cmap_ini; for (size_t i = 0; i < N; ++i) { int index = round( crand_normal_f64(&dist) ); cmap_i_insert(&mhist, index, 0)->value += 1; } // Transfer map to vec and sort it by map keys. - cvec_e vhist = cvec_init; + cvec_e vhist = cvec_ini; c_foreach (i, cmap_i, mhist) cvec_e_push_back(&vhist, *i.item); cvec_e_sort(&vhist); // Print the gaussian bar chart - cstr_t bar = cstr_init; + cstr_t bar = cstr_ini; c_foreach (i, cvec_e, vhist) { size_t n = (size_t) (i.item->value * Mag / N); if (n > 0) { diff --git a/examples/geek1.c b/examples/geek1.c index 44824978..8759e143 100644 --- a/examples/geek1.c +++ b/examples/geek1.c @@ -19,7 +19,7 @@ int findMaximumPairs(int a[], int n, int k) { // Hash-table - cmap_ii hash = cmap_init; + cmap_ii hash = cmap_ini; for (int i = 0; i < n; i++) { cmap_ii_insert(&hash, a[i] % k, 0)->value++; } diff --git a/examples/geek2.c b/examples/geek2.c index 6141518f..0e8b384a 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -10,8 +10,8 @@ int main() { // Lets use an explicit type signature (which would // be `cmap` in this example). - cmap_str book_reviews = cmap_init; - cset_str set = cset_init; + cmap_str book_reviews = cmap_ini; + cset_str set = cset_ini; cset_str_put(&set, "Hello"); cset_str_put(&set, "You"); cset_str_put(&set, "Tube"); diff --git a/examples/geek3.c b/examples/geek3.c index ad2bdfa1..044d7b16 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -8,7 +8,7 @@ declare_cmap_strkey(ss, cstr_t, cstr_destroy); int main () { - cmap_si mymap = cmap_init; + cmap_si mymap = cmap_ini; 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_ini; 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 a62a6fd1..06c93de4 100644 --- a/examples/geek4.c +++ b/examples/geek4.c @@ -49,7 +49,7 @@ int commonWords(cvec_str S) // To store all the words of first string - cvec_sb ans = cvec_init; + cvec_sb ans = cvec_ini; // 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_t word = cstr_init; - cmap_sb_entry_t tmp = {cstr_init, false}; + cstr_t word = cstr_ini; + cmap_sb_entry_t tmp = {cstr_ini, false}; while (i < cstr_size(S.data[0]) && S.data[0].str[i] != ' ') { cstr_push_back(&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_ini; i = 0; while (i < cstr_size(S.data[j])) { - cstr_t word = cstr_init; + cstr_t word = cstr_ini; while (i < cstr_size(S.data[j]) && S.data[j].str[i] != ' ') { cstr_push_back(&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_ini; cvec_str_push_back(&S, "there is a cow"); cvec_str_push_back(&S, "cow is our mother"); cvec_str_push_back(&S, "cow gives us milk and milk is sweet"); diff --git a/examples/geek5.c b/examples/geek5.c index a0531ad3..614bc387 100644 --- a/examples/geek5.c +++ b/examples/geek5.c @@ -29,7 +29,7 @@ declare_cmap_strkey(sv, cvec_i, cvec_i_destroy); 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_ini; for (int i = 0; i < n; i++) { const char* temp = arr[i]; cmap_sv_entry_t *it = cmap_sv_find(&M, temp); @@ -38,7 +38,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) // have an entry in the map // then create the entry if (it == NULL) { - cvec_i A = cvec_init; + cvec_i A = cvec_ini; cvec_i_push_back(&A, i + 1); cmap_sv_put(&M, temp, A); } diff --git a/examples/geek6.c b/examples/geek6.c index db0b3bcf..e906600f 100644 --- a/examples/geek6.c +++ b/examples/geek6.c @@ -37,7 +37,7 @@ declare_cset(i, int); int missingNumber(int a[], int n) { // Declaring an unordered_map - cset_i mp = cset_init; + cset_i mp = cset_ini; // if array value is positive // store it in map diff --git a/examples/geek7.c b/examples/geek7.c index c686b014..4be984ee 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -25,11 +25,11 @@ After inserting all the elements excluding the ones which are to be deleted, Pop #include #include #include -#include +#include declare_cmap(ii, int, int); declare_cvec(i, int); -declare_cprique(i, cvec_i, >); +declare_cpqueue(i, cvec_i, >); // Find k minimum element from arr[0..m-1] after deleting // elements from del[0..n-1] @@ -37,14 +37,14 @@ 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_ini; for (int i = 0; i < n; ++i) { // Increment the count of del[i] cmap_ii_insert(&mp, del[i], 0)->value++; } - cprique_i heap = cprique_i_init(); + cpqueue_i heap = cpqueue_i_init(); for (int i = 0; i < m; ++i) { @@ -63,17 +63,17 @@ void findElementsAfterDel(int arr[], int m, int del[], // Else push it in the min heap else - cprique_i_push(&heap, arr[i]); + cpqueue_i_push(&heap, arr[i]); } // Print top k elements in the min heap for (int i = 0; i < k; ++i) { - printf("%d ", *cprique_i_top(&heap)); + printf("%d ", *cpqueue_i_top(&heap)); // Pop the top element - cprique_i_pop(&heap); + cpqueue_i_pop(&heap); } - cprique_i_destroy(&heap); + cpqueue_i_destroy(&heap); } int main() diff --git a/examples/heap.c b/examples/heap.c index fbcef0cd..5917dd30 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -2,10 +2,10 @@ #include #include #include -#include +#include declare_cvec(f, float); -declare_cprique(f, cvec_f, >); +declare_cpqueue(f, cvec_f, >); int main() { @@ -13,33 +13,33 @@ int main() crand_rng32_t pcg; int N = 3000000, M = 100; - cprique_f pq = cprique_f_init(); + cpqueue_f pq = cpqueue_f_init(); pcg = crand_rng32_init(seed); clock_t start = clock(); for (int i=0; i #include #include -#include +#include #include declare_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t @@ -17,7 +17,7 @@ declare_cvec(ip, ipair_t, c_default_destroy, ipair_compare); declare_clist(ip, ipair_t, c_default_destroy, ipair_compare); declare_cvec(f, float); -declare_cprique(f, cvec_f, >); +declare_cpqueue(f, cvec_f, >); int main(void) { @@ -31,16 +31,16 @@ int main(void) { // CVEC PRIORITY QUEUE - cprique_f_build(&floats); // reorganise vec - c_push(&floats, cprique_f, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f)); + cpqueue_f_build(&floats); // reorganise vec + c_push(&floats, cpqueue_f, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f)); // sorted: while (cvec_size(floats) > 0) { - printf("%.1f ", *cprique_f_top(&floats)); - cprique_f_pop(&floats); + printf("%.1f ", *cpqueue_f_top(&floats)); + cpqueue_f_pop(&floats); } puts("\n"); - cprique_f_destroy(&floats); + cpqueue_f_destroy(&floats); // CMAP ID diff --git a/examples/list.c b/examples/list.c index 3aff4482..3b46f02b 100644 --- a/examples/list.c +++ b/examples/list.c @@ -6,7 +6,7 @@ declare_clist(fx, double); int main() { int k, n = 100000; - clist_fx list = clist_init; + clist_fx list = clist_ini; crand_rng64_t eng = crand_rng64_init(time(NULL)); crand_uniform_f64_t dist = crand_uniform_f64_init(eng, 0.0f, n); diff --git a/examples/mapmap.c b/examples/mapmap.c index d0d8c8fa..a0c1b173 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -7,8 +7,8 @@ declare_cmap_str(); declare_cmap_strkey(cfg, cmap_str, cmap_str_destroy); int main(void) { - cmap_cfg config = cmap_init; - cmap_str init = cmap_init; + cmap_cfg config = cmap_ini; + cmap_str init = cmap_ini; cmap_str_put(&cmap_cfg_insert(&config, "user", init)->value, "name", "Joe"); cmap_str_put(&cmap_cfg_insert(&config, "user", init)->value, "groups", "proj1,proj3"); cmap_str_put(&cmap_cfg_insert(&config, "group", init)->value, "proj1", "Energy"); diff --git a/examples/phonebook.c b/examples/phonebook.c index 15df04e3..d2ed227f 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -36,7 +36,7 @@ void print_phone_book(cmap_str phone_book) int main(int argc, char **argv) { bool erased; - cmap_str phone_book = cmap_init; + cmap_str phone_book = cmap_ini; c_push(&phone_book, cmap_str, c_items( {"Lilia Friedman", "(892) 670-4739"}, {"Tariq Beltran", "(489) 600-7575"}, diff --git a/examples/priority.c b/examples/priority.c index fc0526bf..82e78621 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -2,34 +2,34 @@ #include #include #include -#include +#include #include #include declare_cvec(i, int64_t); -declare_cprique(i, cvec_i, >); // min-heap (increasing values) +declare_cpqueue(i, cvec_i, >); // min-heap (increasing values) int main() { size_t N = 10000000; crand_rng64_t pcg = crand_rng64_init(time(NULL)); crand_uniform_i64_t dist = crand_uniform_i64_init(pcg, 0, N * 10); - cprique_i heap = cprique_i_init(); + cpqueue_i heap = cpqueue_i_init(); // Push ten million random numbers to priority queue for (int i=0; i= 0 && n < R) ++hist[n]; } - cstr_t bar = cstr_init; + cstr_t bar = cstr_ini; for (int i=0; i < R; ++i) { cstr_take(&bar, cstr_with_size(hist[i] * 25ull * R / N2, '*')); printf("%2d %s\n", i, bar.str); diff --git a/examples/stack.c b/examples/stack.c index 321228a2..de30c5b4 100644 --- a/examples/stack.c +++ b/examples/stack.c @@ -1,12 +1,15 @@ -#include #include +#include +#include declare_cvec(i, int); declare_cstack(i, cvec_i); +declare_cstack(c, cstr); int main() { cstack_i stack = cstack_i_init(); + cstack_c chars = cstack_c_init(); for (int i=0; i<100; ++i) cstack_i_push(&stack, i*i); diff --git a/examples/words.c b/examples/words.c index 98134cf6..275ffee7 100644 --- a/examples/words.c +++ b/examples/words.c @@ -11,7 +11,7 @@ declare_cmap_strkey(si, int); int main1() { - clist_str lwords = clist_init; + clist_str lwords = clist_ini; c_push(&lwords, clist_str, c_items( "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" @@ -21,13 +21,13 @@ int main1() printf("%s\n", w.item->value.str); puts(""); - cvec_str words = cvec_init; + cvec_str words = cvec_ini; c_push(&words, cvec_str, c_items( "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" )); - cmap_si word_map = cmap_init; + cmap_si word_map = cmap_ini; c_foreach (w, cvec_str, words) ++cmap_si_insert(&word_map, w.item->str, 0)->value; diff --git a/stc/cbitset.h b/stc/cbitset.h index 31dd3f18..32659a36 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -48,7 +48,7 @@ int main() { typedef struct cbitset { uint64_t* _arr; size_t size; } cbitset_t; -#define cbitset_init {NULL, 0} +#define cbitset_ini {NULL, 0} STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value); STC_API size_t cbitset_count(cbitset_t set); diff --git a/stc/clist.h b/stc/clist.h index 39479a09..530cd222 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -38,7 +38,7 @@ declare_clist(ix, int64_t); int main() { - clist_ix list = clist_init; + clist_ix list = clist_ini; crand_rng32_t pcg = crand_rng32_init(12345); int n; for (int i=0; i<1000000; ++i) // one million @@ -82,7 +82,7 @@ clist_##tag##_node_t *item, *end, **_last; \ } clist_##tag##_iter_t -#define clist_init {NULL} +#define clist_ini {NULL} #define clist_empty(list) ((list).last == NULL) @@ -97,7 +97,7 @@ STC_API size_t _clist_size(const clist_void* self); typedef clist_##tag##_rawvalue_t clist_##tag##_input_t; \ \ STC_INLINE clist_##tag \ - clist_##tag##_init(void) {clist_##tag x = clist_init; return x;} \ + clist_##tag##_init(void) {clist_##tag x = clist_ini; return x;} \ STC_INLINE bool \ clist_##tag##_empty(clist_##tag ls) {return clist_empty(ls);} \ STC_INLINE size_t \ diff --git a/stc/cmap.h b/stc/cmap.h index 615ab7cb..44f39174 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -28,13 +28,13 @@ declare_cset(sx, int); // Set of int declare_cmap(mx, int, char); // Map of int -> char int main(void) { - cset_sx s = cset_init; + cset_sx s = cset_ini; cset_sx_put(&s, 5); cset_sx_put(&s, 8); c_foreach (i, cset_sx, s) printf("set %d\n", i.item->key); cset_sx_destroy(&s); - cmap_mx m = cmap_init; + cmap_mx m = cmap_ini; cmap_mx_put(&m, 5, 'a'); cmap_mx_put(&m, 8, 'b'); cmap_mx_put(&m, 12, 'c'); @@ -53,11 +53,11 @@ int main(void) { #include #include "cdefs.h" -#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f} +#define cmap_ini {NULL, NULL, 0, 0, 0.85f, 0.15f} #define cmap_empty(m) ((m).size == 0) #define cmap_size(m) ((size_t) (m).size) #define cmap_bucket_count(m) ((size_t) (m).bucket_count) -#define cset_init cmap_init +#define cset_ini cmap_ini #define cset_size(s) cmap_size(s) #define cset_bucket_count(s) cmap_bucket_count(s) #define cmap_try_emplace(tag, self, k, v) do { \ @@ -181,7 +181,7 @@ typedef struct { \ } ctype##_##tag##_iter_t; \ \ STC_INLINE ctype##_##tag \ -ctype##_##tag##_init(void) {ctype##_##tag m = cmap_init; return m;} \ +ctype##_##tag##_init(void) {ctype##_##tag m = cmap_ini; return m;} \ STC_INLINE bool \ ctype##_##tag##_empty(ctype##_##tag m) {return m.size == 0;} \ STC_INLINE size_t \ @@ -265,7 +265,7 @@ implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, keyDestroy, RawKey, keyToRaw, keyFromRaw, RawValue, valueFromRaw) \ STC_API ctype##_##tag \ ctype##_##tag##_with_capacity(size_t cap) { \ - ctype##_##tag h = ctype##_init; \ + ctype##_##tag h = ctype##_ini; \ ctype##_##tag##_reserve(&h, cap); \ return h; \ } \ diff --git a/stc/coption.h b/stc/coption.h index e1c6e7cb..4b0712d6 100644 --- a/stc/coption.h +++ b/stc/coption.h @@ -43,7 +43,7 @@ Example: 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_t opt = coption_init; + coption_t opt = coption_ini; while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) { switch (c) { case '?': printf("error: unknown option: %s\n", opt.faulty); break; @@ -83,7 +83,7 @@ typedef struct { int val; } coption_long_t; -static const coption_t coption_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}}; +static const coption_t coption_ini = {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; @@ -93,7 +93,7 @@ static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over argv[j - k] = p; } -/* @param opt output; must be initialized to coption_init on first call +/* @param opt output; must be initialized to coption_ini 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 diff --git a/stc/cpqueue.h b/stc/cpqueue.h new file mode 100644 index 00000000..8873b0cd --- /dev/null +++ b/stc/cpqueue.h @@ -0,0 +1,141 @@ +/* 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. + */ + +/* Priority Queue using heap, with adapter class (normally cvec). + + #include + #include + declare_cvec(f, float); + declare_cpqueue(f, cvec_f, >); // min-heap (increasing values) + + int main() { + crand_rng32_t gen = crand_rng32_init(1234); + crand_uniform_f32_t dist = crand_uniform_f32_init(10.0f, 100.0f); + + cpqueue_f queue = cpqueue_f_init(); + // Push ten million random numbers onto the queue. + for (int i=0; i<10000000; ++i) + cpqueue_f_push(&queue, crand_uniform_f32(&gen, dist)); + // Extract the 100 smallest. + for (int i=0; i<100; ++i) { + printf("%f ", *cpqueue_f_top(queue)); + cpqueue_f_pop(&queue); + } + cpqueue_f_destroy(&queue); + } +*/ + +#ifndef CPQUEUE__H__ +#define CPQUEUE__H__ + +#include "cvec.h" + +#define declare_cpqueue(tag, ctype, cmpOpr) /* cmpOpr: < or > */ \ + \ +typedef struct ctype cpqueue_##tag; \ +typedef ctype##_value_t cpqueue_##tag##_value_t; \ +typedef ctype##_rawvalue_t cpqueue_##tag##_rawvalue_t; \ +typedef ctype##_input_t cpqueue_##tag##_input_t; \ +STC_INLINE cpqueue_##tag \ +cpqueue_##tag##_init() {return ctype##_init();} \ +STC_INLINE size_t \ +cpqueue_##tag##_size(cpqueue_##tag pq) {return ctype##_size(pq);} \ +STC_INLINE bool \ +cpqueue_##tag##_empty(cpqueue_##tag pq) {return ctype##_empty(pq);} \ +STC_INLINE void \ +cpqueue_##tag##_destroy(cpqueue_##tag* self) {ctype##_destroy(self);} \ +STC_API void \ +cpqueue_##tag##_build(cpqueue_##tag* self); \ +STC_API void \ +cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i); \ +STC_INLINE const cpqueue_##tag##_value_t* \ +cpqueue_##tag##_top(const cpqueue_##tag* self) {return &self->data[0];} \ +STC_INLINE void \ +cpqueue_##tag##_pop(cpqueue_##tag* self) {cpqueue_##tag##_erase(self, 0);} \ +STC_API void \ +cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value); \ +STC_INLINE void \ +cpqueue_##tag##_push(cpqueue_##tag* self, cpqueue_##tag##_rawvalue_t rawValue) { \ + cpqueue_##tag##_push_v(self, ctype##_value_from_raw(rawValue)); \ +} \ +STC_API void \ +cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t size); \ + \ +implement_cpqueue(tag, ctype, cmpOpr) + +/* -------------------------- IMPLEMENTATION ------------------------- */ + +#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) +#define implement_cpqueue(tag, ctype, cmpOpr) \ + \ +STC_INLINE void \ +_cpqueue_##tag##_sift_down(cpqueue_##tag##_value_t* arr, size_t i, size_t n) { \ + size_t r = i, c = i << 1; \ + while (c <= n) { \ + if (c < n && ctype##_value_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \ + ++c; \ + if (ctype##_value_compare(&arr[r], &arr[c]) cmpOpr 0) { \ + cpqueue_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \ + } else \ + return; \ + c <<= 1; \ + } \ +} \ + \ +STC_API void \ +cpqueue_##tag##_build(cpqueue_##tag* self) { \ + size_t n = cpqueue_##tag##_size(*self); \ + cpqueue_##tag##_value_t *arr = self->data - 1; \ + for (size_t k = n >> 1; k != 0; --k) \ + _cpqueue_##tag##_sift_down(arr, k, n); \ +} \ + \ +STC_API void \ +cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i) { \ + size_t n = cpqueue_##tag##_size(*self) - 1; \ + self->data[i] = self->data[n]; \ + ctype##_pop_back(self); \ + _cpqueue_##tag##_sift_down(self->data - 1, i + 1, n); \ +} \ + \ +STC_API void \ +cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value) { \ + ctype##_push_back(self, value); /* sift-up the value */ \ + size_t n = cpqueue_##tag##_size(*self), c = n; \ + cpqueue_##tag##_value_t *arr = self->data - 1; \ + for (; c > 1 && ctype##_value_compare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \ + arr[c] = arr[c >> 1]; \ + if (c != n) arr[c] = value; \ +} \ +STC_API void \ +cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t size) { \ + ctype##_reserve(self, cpqueue_##tag##_size(*self) + size); \ + for (size_t i=0; i - #include - declare_cvec(f, float); - declare_cprique(f, cvec_f, >); // min-heap (increasing values) - - int main() { - crand_rng32_t gen = crand_rng32_init(1234); - crand_uniform_f32_t dist = crand_uniform_f32_init(10.0f, 100.0f); - - cprique_f queue = cprique_f_init(); - // Push ten million random numbers onto the queue. - for (int i=0; i<10000000; ++i) - cprique_f_push(&queue, crand_uniform_f32(&gen, dist)); - // Extract the 100 smallest. - for (int i=0; i<100; ++i) { - printf("%f ", *cprique_f_top(queue)); - cprique_f_pop(&queue); - } - cprique_f_destroy(&queue); - } -*/ - -#ifndef CPRIQUE__H__ -#define CPRIQUE__H__ - -#include "cvec.h" - -#define declare_cprique(tag, ctype, cmpOpr) /* cmpOpr: < or > */ \ - \ -typedef ctype cprique_##tag; \ -typedef ctype##_value_t cprique_##tag##_value_t; \ -typedef ctype##_rawvalue_t cprique_##tag##_rawvalue_t; \ -typedef ctype##_input_t cprique_##tag##_input_t; \ -STC_INLINE cprique_##tag \ -cprique_##tag##_init() {return ctype##_init();} \ -STC_INLINE size_t \ -cprique_##tag##_size(cprique_##tag pq) {return ctype##_size(pq);} \ -STC_INLINE bool \ -cprique_##tag##_empty(cprique_##tag pq) {return ctype##_empty(pq);} \ -STC_INLINE void \ -cprique_##tag##_destroy(cprique_##tag* self) {ctype##_destroy(self);} \ -STC_API void \ -cprique_##tag##_build(cprique_##tag* self); \ -STC_API void \ -cprique_##tag##_erase(cprique_##tag* self, size_t i); \ -STC_INLINE const cprique_##tag##_value_t* \ -cprique_##tag##_top(const cprique_##tag* self) {return &self->data[0];} \ -STC_INLINE void \ -cprique_##tag##_pop(cprique_##tag* self) {cprique_##tag##_erase(self, 0);} \ -STC_API void \ -cprique_##tag##_push_v(cprique_##tag* self, cprique_##tag##_value_t value); \ -STC_INLINE void \ -cprique_##tag##_push(cprique_##tag* self, cprique_##tag##_rawvalue_t rawValue) { \ - cprique_##tag##_push_v(self, ctype##_value_from_raw(rawValue)); \ -} \ -STC_API void \ -cprique_##tag##_push_n(cprique_##tag *self, const cprique_##tag##_input_t in[], size_t size); \ - \ -implement_cprique(tag, ctype, cmpOpr) - -/* -------------------------- IMPLEMENTATION ------------------------- */ - -#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define implement_cprique(tag, ctype, cmpOpr) \ - \ -STC_INLINE void \ -_cprique_##tag##_sift_down(cprique_##tag##_value_t* arr, size_t i, size_t n) { \ - size_t r = i, c = i << 1; \ - while (c <= n) { \ - if (c < n && ctype##_value_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \ - ++c; \ - if (ctype##_value_compare(&arr[r], &arr[c]) cmpOpr 0) { \ - cprique_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \ - } else \ - return; \ - c <<= 1; \ - } \ -} \ - \ -STC_API void \ -cprique_##tag##_build(cprique_##tag* self) { \ - size_t n = cprique_##tag##_size(*self); \ - cprique_##tag##_value_t *arr = self->data - 1; \ - for (size_t k = n >> 1; k != 0; --k) \ - _cprique_##tag##_sift_down(arr, k, n); \ -} \ - \ -STC_API void \ -cprique_##tag##_erase(cprique_##tag* self, size_t i) { \ - size_t n = cprique_##tag##_size(*self) - 1; \ - self->data[i] = self->data[n]; \ - ctype##_pop_back(self); \ - _cprique_##tag##_sift_down(self->data - 1, i + 1, n); \ -} \ - \ -STC_API void \ -cprique_##tag##_push_v(cprique_##tag* self, cprique_##tag##_value_t value) { \ - ctype##_push_back(self, value); /* sift-up the value */ \ - size_t n = cprique_##tag##_size(*self), c = n; \ - cprique_##tag##_value_t *arr = self->data - 1; \ - for (; c > 1 && ctype##_value_compare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \ - arr[c] = arr[c >> 1]; \ - if (c != n) arr[c] = value; \ -} \ -STC_API void \ -cprique_##tag##_push_n(cprique_##tag *self, const cprique_##tag##_input_t in[], size_t size) { \ - ctype##_reserve(self, cprique_##tag##_size(*self) + size); \ - for (size_t i=0; i> 4) * 16 - 9) +STC_INLINE cstr_t +cstr_init() {return cstr_ini;} + STC_INLINE void cstr_destroy(cstr_t* self) { if (cstr_capacity(*self)) @@ -81,13 +84,13 @@ cstr_destroy(cstr_t* self) { STC_INLINE cstr_t cstr_with_capacity(size_t cap) { - cstr_t s = cstr_init; + cstr_t s = cstr_ini; cstr_reserve(&s, cap); return s; } STC_INLINE cstr_t cstr_with_size(size_t len, char fill) { - cstr_t s = cstr_init; + cstr_t s = cstr_ini; cstr_resize(&s, len, fill); return s; } @@ -106,6 +109,11 @@ cstr_clear(cstr_t* self) { self->str[_cstr_size(*self) = 0] = '\0'; } +STC_INLINE char* +cstr_front(cstr_t* self) {return self->str;} +STC_INLINE char* +cstr_back(cstr_t* self) {return self->str + _cstr_size(*self) - 1;} + STC_INLINE cstr_iter_t cstr_begin(cstr_t* self) { cstr_iter_t it = {self->str, self->str + cstr_size(*self)}; return it; @@ -128,7 +136,7 @@ cstr_take(cstr_t* self, cstr_t s) { STC_INLINE cstr_t cstr_move(cstr_t* self) { cstr_t tmp = *self; - *self = cstr_init; + *self = cstr_ini; return tmp; } @@ -136,14 +144,11 @@ STC_INLINE cstr_t* cstr_append(cstr_t* self, const char* str) { return cstr_append_n(self, str, strlen(str)); } -/*STC_INLINE void -cstr_push_n(cstr_t* self, const cstr_input_t[] in, size_t n) { - cstr_append_n(self, in, n); -}*/ STC_INLINE cstr_t* cstr_push_back(cstr_t* self, char value) { return cstr_append_n(self, &value, 1); } + STC_INLINE void cstr_pop_back(cstr_t* self) { self->str[ --_cstr_size(*self) ] = '\0'; @@ -228,7 +233,7 @@ cstr_resize(cstr_t* self, size_t len, char fill) { STC_API cstr_t cstr_make_n(const char* str, size_t len) { - if (len == 0) return cstr_init; + if (len == 0) return cstr_ini; size_t *rep = (size_t *) malloc(_cstr_mem(len)); cstr_t s = {(char *) memcpy(rep + 2, str, len)}; s.str[rep[0] = len] = '\0'; @@ -245,7 +250,7 @@ cstr_from(const char* fmt, ...) { # pragma warning(push) # pragma warning(disable: 4996) #endif - cstr_t tmp = cstr_init; + cstr_t tmp = cstr_ini; va_list args, args2; va_start(args, fmt); va_copy(args2, args); diff --git a/stc/cvec.h b/stc/cvec.h index 7a753b70..0e72b142 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -27,7 +27,7 @@ #include #include "cdefs.h" -#define cvec_init {NULL} +#define cvec_ini {NULL} #define cvec_size(v) _cvec_safe_size((v).data) #define cvec_capacity(v) _cvec_safe_capacity((v).data) #define cvec_empty(v) (_cvec_safe_size((v).data) == 0) @@ -55,7 +55,7 @@ typedef RawValue cvec_##tag##_rawvalue_t; \ typedef cvec_##tag##_rawvalue_t cvec_##tag##_input_t; \ \ STC_INLINE cvec_##tag \ -cvec_##tag##_init(void) {cvec_##tag v = cvec_init; return v;} \ +cvec_##tag##_init(void) {cvec_##tag v = cvec_ini; return v;} \ STC_INLINE bool \ cvec_##tag##_empty(cvec_##tag v) {return cvec_empty(v);} \ STC_INLINE size_t \ @@ -97,13 +97,13 @@ cvec_##tag##_value_compare(const Value* x, const Value* y); \ \ STC_INLINE cvec_##tag \ cvec_##tag##_with_size(size_t size, Value null_val) { \ - cvec_##tag x = cvec_init; \ + cvec_##tag x = cvec_ini; \ cvec_##tag##_resize(&x, size, null_val); \ return x; \ } \ STC_INLINE cvec_##tag \ cvec_##tag##_with_capacity(size_t size) { \ - cvec_##tag x = cvec_init; \ + cvec_##tag x = cvec_ini; \ cvec_##tag##_reserve(&x, size); \ return x; \ } \ -- cgit v1.2.3