From 289df4cb64a365ad0a0e55864efc79aa32c36b94 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 21 Jul 2020 16:17:13 +0200 Subject: Reordered declare_CMap/CSet argument list. Fixed examples. --- examples/README.md | 2 +- examples/advanced.c | 2 +- examples/benchmark.c | 2 +- examples/geek2.c | 12 ++++++------ examples/geek4.c | 16 ++++++++-------- examples/heap.c | 36 ++++++++++++++++++------------------ stc/carray.h | 2 +- stc/cdefs.h | 4 ++-- stc/clist.h | 2 +- stc/cmap.h | 52 ++++++++++++++++++++++++++-------------------------- stc/cvec.h | 10 +++++++++- 11 files changed, 74 insertions(+), 66 deletions(-) diff --git a/examples/README.md b/examples/README.md index e98ae59d..4d43a7d2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -60,7 +60,7 @@ Viking viking_fromVw(VikingVw vw) { ``` With this in place, we use the full declare_CMap() macro to define {Viking -> int} hash map type: ``` -declare_CMap(vk, Viking, int, c_emptyDestroy, vikingvw_hash, vikingvw_equals, +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. diff --git a/examples/advanced.c b/examples/advanced.c index bd06c95d..5d0bd2ed 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -53,7 +53,7 @@ Viking viking_fromVw(VikingVw vw) { // Using the full declare_CMap() macro to define [Viking -> int] hash map type: -declare_CMap(vk, Viking, int, c_emptyDestroy, vikingvw_hash, vikingvw_equals, +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. diff --git a/examples/benchmark.c b/examples/benchmark.c index cadb220c..87242ec8 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -19,7 +19,7 @@ static inline uint32_t fibonacci_hash(const void* data, size_t len) { const uint64_t key = *(const uint64_t *) data; return (uint32_t) (key * 11400714819323198485llu); } -declare_CMap(ii, int64_t, int64_t, c_emptyDestroy, fibonacci_hash); +declare_CMap(ii, int64_t, int64_t, c_defaultDestroy, c_defaultEquals, fibonacci_hash); KHASH_MAP_INIT_INT64(ii, uint64_t) diff --git a/examples/geek2.c b/examples/geek2.c index 38916243..32c0d03b 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -4,18 +4,18 @@ #include declare_CMap_str(ss, CStr, cstr_destroy); -declare_CSet_str(ss); +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_ss set = cset_init; - cset_ss_put(&set, "Hello"); - cset_ss_put(&set, "You"); - cset_ss_put(&set, "Tube"); - c_foreach (i, cset_ss, set) + CSet_str set = cset_init; + cset_str_put(&set, "Hello"); + cset_str_put(&set, "You"); + cset_str_put(&set, "Tube"); + c_foreach (i, cset_str, set) printf("%s ", i.item->key.str); puts(""); // Review some books. diff --git a/examples/geek4.c b/examples/geek4.c index 243301f9..28c77e9f 100644 --- a/examples/geek4.c +++ b/examples/geek4.c @@ -37,13 +37,13 @@ Efficient Approach: For all the words of the first sentence, we can check if it #include #include -declare_CVec_str(s); +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_s S) +int commonWords(CVec_str S) { int m, n, i, j; @@ -136,14 +136,14 @@ int commonWords(CVec_s S) // Driver code int main() { - CVec_s S = cvec_init; - cvec_s_pushBack(&S, cstr_make("there is a cow")); - cvec_s_pushBack(&S, cstr_make("cow is our mother")); - cvec_s_pushBack(&S, cstr_make("cow gives us milk and milk is sweet")); - cvec_s_pushBack(&S, cstr_make("there is a boy who loves cow")); + 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")); + cvec_str_pushBack(&S, cstr_make("there is a boy who loves cow")); printf("%d\n", commonWords(S)); - cvec_s_destroy(&S); + cvec_str_destroy(&S); return 0; } diff --git a/examples/heap.c b/examples/heap.c index f9bc59e3..6fbb346f 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -1,36 +1,36 @@ #include #include -#include "stc/cvecque.h" +#include "stc/cvecpq.h" #include "stc/crandom.h" declare_CVec(f, float); -declare_CVecque(f); +declare_CVec_priorityQ(f, >); int main() { - //float arr[] = {1, 3, 6, 5, 9, 8, -2}; - float arr[] = {9., 3., 5., 10., 2., 4., 16., 7., 12., 8.}; uint32_t seed = time(NULL); pcg32_random_t pcg = pcg32_seed(seed, 0); int N = 30000000, M = 100; CVec_f vec = cvec_init; clock_t start = clock(); - for (int i=0; i is */ -#define declare_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyHashRaw, keyEqualsRaw, \ +/* CHASH full: use 'void' for Value if ctype is cset */ +#define declare_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ typedef struct CType##Entry_##tag { \ Key key; \ @@ -186,7 +186,7 @@ ctype##_##tag##_begin(CType##_##tag* map); \ STC_API ctype##_##tag##_iter_t \ ctype##_##tag##_next(ctype##_##tag##_iter_t it); \ \ -implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyHashRaw, keyEqualsRaw, \ +implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ typedef Key CType##Key_##tag; \ typedef Value CType##Value_##tag @@ -194,7 +194,7 @@ typedef Value CType##Value_##tag /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyHashRaw, keyEqualsRaw, \ +#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ \ STC_API CType##_##tag \ @@ -369,7 +369,7 @@ ctype##_##tag##_next(ctype##_##tag##_iter_t it) { \ } #else -#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyHashRaw, keyEqualsRaw, \ +#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) #endif diff --git a/stc/cvec.h b/stc/cvec.h index 80ed051d..8974486a 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -34,7 +34,7 @@ #define declare_CVec(...) c_MACRO_OVERLOAD(declare_CVec, __VA_ARGS__) #define declare_CVec_2(tag, Value) \ - declare_CVec_3(tag, Value, c_emptyDestroy) + declare_CVec_3(tag, Value, c_defaultDestroy) #define declare_CVec_3(tag, Value, valueDestroy) \ declare_CVec_4(tag, Value, valueDestroy, c_defaultCompare) #define declare_CVec_4(tag, Value, valueDestroy, valueCompare) \ @@ -113,6 +113,14 @@ cvec_##tag##_make(size_t size, Value null) { \ _cvec_size(vec) = size; \ for (size_t i=0; i