From 692ab82818e2d65177e06d7717d9184b7bc27ff1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 17 Sep 2020 18:37:34 +0200 Subject: Fixed range methods in cvec, and renamed typename_(..) to using_(..). --- README.md | 40 +++++++++--------- examples/README.md | 4 +- examples/advanced.c | 4 +- examples/benchmark.c | 2 +- examples/birthday.c | 6 +-- examples/complex.c | 8 ++-- examples/demos.c | 16 +++---- examples/ex_gaussian.c | 4 +- examples/heap.c | 4 +- examples/inits.c | 12 +++--- examples/list.c | 2 +- examples/mapmap.c | 4 +- examples/phonebook.c | 2 +- examples/priority.c | 4 +- examples/queue.c | 4 +- examples/stack.c | 8 ++-- examples/words.c | 6 +-- stc/carray.h | 18 ++++---- stc/clist.h | 26 ++++++------ stc/cmap.h | 68 +++++++++++++++--------------- stc/cpqueue.h | 6 +-- stc/cqueue.h | 6 +-- stc/cstack.h | 6 +-- stc/cvec.h | 110 +++++++++++++++++++++++++------------------------ 24 files changed, 186 insertions(+), 184 deletions(-) diff --git a/README.md b/README.md index d347b64b..a4289f21 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The usage of the containers is vert similar to the C++ standard containers, so i All containers mentioned above, except cstr_t and cbitset_t are generic and typesafe (similar to templates in C++). No casting is used. A simple example: ``` #include -typedef_cvec(i, int); +using_cvec(i, int); int main(void) { cvec_i vec = cvec_ini; @@ -55,10 +55,10 @@ Because it is headers only, files can simply be included in your program. The fu #include #include "Vec3.h" -typedef_cmap(ii, int, int); -typedef_cset(ix, int64_t); -typedef_cvec(i, int); -typedef_clist(v3, Vec3); +using_cmap(ii, int, int); +using_cset(ix, int64_t); +using_cvec(i, int); +using_clist(v3, Vec3); ... ``` Performance @@ -118,7 +118,7 @@ cmap discussion You can customize the destroy-, hash- and equals- function. **cmap/cset** also supports a few other arguments in the declare-statement that allows to define a convertion from a raw/literal type to the key-type specified. This is very useful when e.g. having cstr as key, as it enables the usage of string literals as key in *put() and find()* functions, instead of requering a constructed cstr. Without it, the code would become: ``` -typedef_cmap(si, cstr_t, int); // don't do this. +using_cmap(si, cstr_t, int); // don't do this. ... cmap_si_put(&map, cstr("mykey"), 12); ``` @@ -129,12 +129,12 @@ int x = cmap_si_find(&map, lookup)->value; cstr_destroy(&lookup); ``` To avoid this, use -- *typedef_cmap_strkey(tag, valuetype)* -- *typedef_cmap_strval(tag, keytype)* -- *typedef_cmap_str()* // cstr_t -> cstr_t -- *typedef_cset_str()* // cstr_t set +- *using_cmap_strkey(tag, valuetype)* +- *using_cmap_strval(tag, keytype)* +- *using_cmap_str()* // cstr_t -> cstr_t +- *using_cset_str()* // cstr_t set ``` -typedef_cmap_strkey(si, int); +using_cmap_strkey(si, int); ... cmap_si map = cmap_ini; cmap_si_put(&map, "mykey", 12); // constructs a cstr_t key from the const char* internally. @@ -142,9 +142,9 @@ int x = cmap_si_find(&map, "mykey")->value; // no allocation of string key happe cmap_si_destroy(&map); ``` An alternative is to use *char* * as key type, but then you must manage allcoated memory of the hash char* keys yourself. -Note that this predefined customization is also available for **cvec** and **clist**. See *typedef_cvec_str()*, *typedef_clist_str()*. +Note that this predefined customization is also available for **cvec** and **clist**. See *using_cvec_str()*, *using_clist_str()*. -To customize your own cmap type to work like cmap_str, you may want to look at **examples/advanced.c**. It demonstrates how to use a custom struct as a hash map key, using the optional parameters to typedef_cmap(). +To customize your own cmap type to work like cmap_str, you may want to look at **examples/advanced.c**. It demonstrates how to use a custom struct as a hash map key, using the optional parameters to using_cmap(). Example usages -------------- @@ -181,7 +181,7 @@ int main() { **cvec** of *int64_t*. ``` #include -typedef_cvec(ix, int64_t); // ix is just an example type tag name. +using_cvec(ix, int64_t); // ix is just an example type tag name. int main() { cvec_ix bignums = cvec_ini; // use cvec_ix_init() if initializing after declaration. @@ -200,7 +200,7 @@ int main() { ``` #include #include -typedef_cvec_str(); +using_cvec_str(); int main() { cvec_str names = cvec_ini; @@ -220,7 +220,7 @@ int main() { ``` #include #include -typedef_cmap(ii, int, int); +using_cmap(ii, int, int); int main() { cmap_ii nums = cmap_ini; @@ -235,7 +235,7 @@ int main() { ``` #include #include -typedef_cset_str(); // cstr set. See the discussion above. +using_cset_str(); // cstr set. See the discussion above. int main() { cset_str words = cset_ini; @@ -254,7 +254,7 @@ int main() { ``` #include #include -typedef_cmap_str(); +using_cmap_str(); int main() { cmap_str table = cmap_ini; @@ -276,7 +276,7 @@ int main() { #include #include #include -typedef_clist(fx, double); +using_clist(fx, double); int main() { clist_fx list = clist_ini; @@ -313,7 +313,7 @@ int main() { ``` #include #include -typedef_carray(f, float); +using_carray(f, float); int main() { diff --git a/examples/README.md b/examples/README.md index 8820f5cf..95880385 100644 --- a/examples/README.md +++ b/examples/README.md @@ -51,9 +51,9 @@ static inline Viking viking_fromVw(VikingVw vw) { // note: parameter is by value Viking vk = {cstr(vw.name), cstr(vw.country)}; return vk; } ``` -With this in place, we use the full typedef_cmap() macro to define {Viking -> int} hash map type: +With this in place, we use the full using_cmap() macro to define {Viking -> int} hash map type: ``` -typedef_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, +using_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, viking_destroy, VikingVw, viking_toVw, 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 2c736e2d..200ab5a3 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -50,8 +50,8 @@ Viking viking_fromVw(VikingVw vw) { Viking vk = {cstr(vw.name), cstr(vw.country)}; return vk; } -// Using the full typedef_cmap() macro to define [Viking -> int] hash map type: -typedef_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, +// Using the full using_cmap() macro to define [Viking -> int] hash map type: +using_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash, viking_destroy, VikingVw, viking_toVw, 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 7b0e1fa5..a02e8be8 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -22,7 +22,7 @@ static inline uint32_t fibonacci_hash(const void* data, size_t len) { } // cmap and khash template expansion -typedef_cmap(ii, int64_t, int64_t, c_default_destroy, c_default_equals, fibonacci_hash); // c_default_hash16); +using_cmap(ii, int64_t, int64_t, c_default_destroy, c_default_equals, fibonacci_hash); // c_default_hash16); KHASH_MAP_INIT_INT64(ii, int64_t) diff --git a/examples/birthday.c b/examples/birthday.c index e085e25c..7bee3658 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -7,7 +7,7 @@ #include #include -typedef_cmap(ic, uint64_t, uint8_t); +using_cmap(ic, uint64_t, uint8_t); const static uint64_t seed = 1234; const static uint64_t N = 1ull << 27; @@ -29,8 +29,8 @@ void repeats(void) } -typedef_cmap(x, uint32_t, uint64_t); -typedef_cvec(x, uint64_t); +using_cmap(x, uint32_t, uint64_t); +using_cvec(x, uint64_t); void distribution(void) { diff --git a/examples/complex.c b/examples/complex.c index 0c3e5c38..12afc47c 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -5,10 +5,10 @@ void check_destroy(float* v) {printf("destroy %g\n", *v);} -typedef_carray(f, float, check_destroy); // normally omit the last argument - float type need no destroy. -typedef_clist(y, carray2f, carray2f_destroy, c_no_compare); -typedef_cmap(g, int, clist_y, clist_y_destroy); -typedef_cmap_strkey(s, cmap_g, cmap_g_destroy); +using_carray(f, float, check_destroy); // normally omit the last argument - float type need no destroy. +using_clist(y, carray2f, carray2f_destroy, c_no_compare); +using_cmap(g, int, clist_y, clist_y_destroy); +using_cmap_strkey(s, cmap_g, cmap_g_destroy); int main() { int xdim = 4, ydim = 6; diff --git a/examples/demos.c b/examples/demos.c index f39b439f..6f5c0207 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -34,7 +34,7 @@ void stringdemo1() } -typedef_cvec(ix, int64_t); // ix is just an example tag name. +using_cvec(ix, int64_t); // ix is just an example tag name. void vectordemo1() { @@ -55,7 +55,7 @@ void vectordemo1() -typedef_cvec_str(); +using_cvec_str(); void vectordemo2() { @@ -73,7 +73,7 @@ void vectordemo2() cvec_str_destroy(&names); } -typedef_clist(ix, int); +using_clist(ix, int); void listdemo1() { @@ -100,7 +100,7 @@ void listdemo1() clist_ix_destroy(&nums); } -typedef_cset(i, int); +using_cset(i, int); void setdemo1() { @@ -115,7 +115,7 @@ void setdemo1() } -typedef_cmap(ii, int, int); +using_cmap(ii, int, int); void mapdemo1() { @@ -128,7 +128,7 @@ void mapdemo1() } -typedef_cmap_strkey(si, int); // Shorthand macro for the general typedef_cmap expansion. +using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expansion. void mapdemo2() { @@ -150,7 +150,7 @@ void mapdemo2() } -typedef_cmap_str(); +using_cmap_str(); void mapdemo3() { @@ -172,7 +172,7 @@ void mapdemo3() } -typedef_carray(f, float); +using_carray(f, float); void arraydemo1() { diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index 3c3a05bf..033c0ebd 100644 --- a/examples/ex_gaussian.c +++ b/examples/ex_gaussian.c @@ -7,14 +7,14 @@ #include // Declare int -> int hashmap. Uses typetag 'i' for ints. -typedef_cmap(i, int, size_t); +using_cmap(i, int, size_t); // Declare int vector with map entries that can be sorted by map keys. static int compare(cmap_i_entry_t *a, cmap_i_entry_t *b) { return c_default_compare(&a->first, &b->first); } // Vector: typetag 'e' for (map) entry -typedef_cvec(e, cmap_i_entry_t, c_default_destroy, compare); +using_cvec(e, cmap_i_entry_t, c_default_destroy, compare); int main() { diff --git a/examples/heap.c b/examples/heap.c index 18ba247c..89f700d2 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -4,8 +4,8 @@ #include #include -typedef_cvec(f, float); -typedef_cpqueue(f, cvec_f, >); +using_cvec(f, float); +using_cpqueue(f, cvec_f, >); int main() { diff --git a/examples/inits.c b/examples/inits.c index 7cad8f4e..c64ca574 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -5,19 +5,19 @@ #include #include -typedef_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t -typedef_cmap_strkey(cnt, int); +using_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t +using_cmap_strkey(cnt, int); typedef struct {int x, y;} ipair_t; inline static int ipair_compare(const ipair_t* a, const ipair_t* b) { int c = c_default_compare(&a->x, &b->x); return c != 0 ? c : c_default_compare(&a->y, &b->y); } -typedef_cvec(ip, ipair_t, c_default_destroy, ipair_compare); -typedef_clist(ip, ipair_t, c_default_destroy, ipair_compare); +using_cvec(ip, ipair_t, c_default_destroy, ipair_compare); +using_clist(ip, ipair_t, c_default_destroy, ipair_compare); -typedef_cvec(f, float); -typedef_cpqueue(f, cvec_f, >); +using_cvec(f, float); +using_cpqueue(f, cvec_f, >); int main(void) { diff --git a/examples/list.c b/examples/list.c index 9c393bcc..abc4d5b7 100644 --- a/examples/list.c +++ b/examples/list.c @@ -2,7 +2,7 @@ #include #include #include -typedef_clist(fx, double); +using_clist(fx, double); int main() { int k; diff --git a/examples/mapmap.c b/examples/mapmap.c index f40d6be5..57bcf384 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -3,8 +3,8 @@ #include #include -typedef_cmap_str(); -typedef_cmap_strkey(cfg, cmap_str, cmap_str_destroy); +using_cmap_str(); +using_cmap_strkey(cfg, cmap_str, cmap_str_destroy); int main(void) { cmap_cfg config = cmap_ini; diff --git a/examples/phonebook.c b/examples/phonebook.c index 66b54e33..25d6ddb0 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -25,7 +25,7 @@ #include #include -typedef_cmap_str(); +using_cmap_str(); void print_phone_book(cmap_str phone_book) { diff --git a/examples/priority.c b/examples/priority.c index d35ec4c7..12e4be25 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -6,8 +6,8 @@ #include #include -typedef_cvec(i, int64_t); -typedef_cpqueue(i, cvec_i, >); // min-heap (increasing values) +using_cvec(i, int64_t); +using_cpqueue(i, cvec_i, >); // min-heap (increasing values) int main() { size_t N = 10000000; diff --git a/examples/queue.c b/examples/queue.c index 330e79fe..e1eaf5d4 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -2,8 +2,8 @@ #include #include -typedef_clist(i, int); -typedef_cqueue(i, clist_i); // min-heap (increasing values) +using_clist(i, int); +using_cqueue(i, clist_i); // min-heap (increasing values) int main() { int n = 10000000; diff --git a/examples/stack.c b/examples/stack.c index c51c420c..e514808d 100644 --- a/examples/stack.c +++ b/examples/stack.c @@ -3,10 +3,10 @@ #include #include -typedef_cvec(i, int); -typedef_cvec(c, char); -typedef_cstack(i, cvec_i); -typedef_cstack(c, cvec_c); +using_cvec(i, int); +using_cvec(c, char); +using_cstack(i, cvec_i); +using_cstack(c, cvec_c); int main() { cstack_i stack = cstack_i_init(); diff --git a/examples/words.c b/examples/words.c index 0f799ea2..e22a2a9b 100644 --- a/examples/words.c +++ b/examples/words.c @@ -4,9 +4,9 @@ #include #include -typedef_cvec_str(); -typedef_clist_str(); -typedef_cmap_strkey(si, int); +using_cvec_str(); +using_clist_str(); +using_cmap_strkey(si, int); int main1() diff --git a/stc/carray.h b/stc/carray.h index b7557f59..a2f0a6ea 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -30,7 +30,7 @@ Multi-dimensional generic array allocated as one block of heap-memory. // demo: #include -typedef_carray(f, float); +using_carray(f, float); int main() { @@ -68,7 +68,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { } -#define typedef_carray_common(D, X, Value, valueDestroy) \ +#define using_carray_common(D, X, Value, valueDestroy) \ typedef struct { Value *get; } carray##D##X##_iter_t; \ \ STC_INLINE carray##D##X##_iter_t \ @@ -91,12 +91,12 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { } \ } -#define typedef_carray(...) c_MACRO_OVERLOAD(typedef_carray, __VA_ARGS__) -#define typedef_carray_2(X, Value) \ - typedef_carray_3(X, Value, c_default_destroy) +#define using_carray(...) c_MACRO_OVERLOAD(using_carray, __VA_ARGS__) +#define using_carray_2(X, Value) \ + using_carray_3(X, Value, c_default_destroy) -#define typedef_carray_3(X, Value, valueDestroy) \ +#define using_carray_3(X, Value, valueDestroy) \ \ typedef Value carray1##X##_value_t; \ typedef carray1##X##_value_t carray2##X##_value_t, carray3##X##_value_t; \ @@ -116,9 +116,9 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { size_t _xdim, _yxdim, _zdim; \ } carray3##X; \ \ - typedef_carray_common(1, X, Value, valueDestroy) \ - typedef_carray_common(2, X, Value, valueDestroy) \ - typedef_carray_common(3, X, Value, valueDestroy) \ + using_carray_common(1, X, Value, valueDestroy) \ + using_carray_common(2, X, Value, valueDestroy) \ + using_carray_common(3, X, Value, valueDestroy) \ \ STC_INLINE carray1##X \ carray1##X##_make(size_t xdim, Value val) { \ diff --git a/stc/clist.h b/stc/clist.h index 8f7128b0..6a8c13f7 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -35,7 +35,7 @@ #include #include #include - typedef_clist(ix, int64_t); + using_clist(ix, int64_t); int main() { clist_ix list = clist_ini; @@ -55,19 +55,19 @@ clist_ix_destroy(&list); } */ -#define typedef_clist(...) c_MACRO_OVERLOAD(typedef_clist, __VA_ARGS__) +#define using_clist(...) c_MACRO_OVERLOAD(using_clist, __VA_ARGS__) -#define typedef_clist_2(X, Value) \ - typedef_clist_3(X, Value, c_default_destroy) -#define typedef_clist_3(X, Value, valueDestroy) \ - typedef_clist_4(X, Value, valueDestroy, c_default_compare) -#define typedef_clist_4(X, Value, valueDestroy, valueCompare) \ - typedef_clist_7(X, Value, valueDestroy, Value, \ +#define using_clist_2(X, Value) \ + using_clist_3(X, Value, c_default_destroy) +#define using_clist_3(X, Value, valueDestroy) \ + using_clist_4(X, Value, valueDestroy, c_default_compare) +#define using_clist_4(X, Value, valueDestroy, valueCompare) \ + using_clist_7(X, Value, valueDestroy, Value, \ valueCompare, c_default_to_raw, c_default_from_raw) -#define typedef_clist_str() typedef_clist_7(str, cstr_t, cstr_destroy, const char*, \ +#define using_clist_str() using_clist_7(str, cstr_t, cstr_destroy, const char*, \ cstr_compare_raw, cstr_to_raw, cstr) -#define typedef_clist_types(X, Value) \ +#define using_clist_types(X, Value) \ typedef Value clist_##X##_value_t; \ \ typedef struct clist_##X##_node { \ @@ -97,13 +97,13 @@ } while (0) -typedef_clist_types(void, int); +using_clist_types(void, int); STC_API size_t _clist_size(const clist_void* self); #define _clist_node(X, vp) c_container_of(vp, clist_##X##_node_t, value) -#define typedef_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ +#define using_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ \ - typedef_clist_types(X, Value); \ + using_clist_types(X, Value); \ typedef RawValue clist_##X##_rawvalue_t; \ typedef clist_##X##_rawvalue_t clist_##X##_input_t; \ \ diff --git a/stc/cmap.h b/stc/cmap.h index e725d264..1cd2f180 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -23,8 +23,8 @@ /* #include #include -typedef_cset(sx, int); // Set of int -typedef_cmap(mx, int, char); // Map of int -> char +using_cset(sx, int); // Set of int +using_cmap(mx, int, char); // Map of int -> char int main(void) { cset_sx s = cset_ini; @@ -76,76 +76,76 @@ int main(void) { enum {chash_HASH = 0x7f, chash_USED = 0x80}; typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; -#define typedef_cmap(...) \ - c_MACRO_OVERLOAD(typedef_cmap, __VA_ARGS__) +#define using_cmap(...) \ + c_MACRO_OVERLOAD(using_cmap, __VA_ARGS__) -#define typedef_cmap_3(X, Key, Mapped) \ - typedef_cmap_4(X, Key, Mapped, c_default_destroy) +#define using_cmap_3(X, Key, Mapped) \ + using_cmap_4(X, Key, Mapped, c_default_destroy) -#define typedef_cmap_4(X, Key, Mapped, valueDestroy) \ - typedef_cmap_6(X, Key, Mapped, valueDestroy, c_default_equals, c_default_hash16) +#define using_cmap_4(X, Key, Mapped, valueDestroy) \ + using_cmap_6(X, Key, Mapped, valueDestroy, c_default_equals, c_default_hash16) -#define typedef_cmap_6(X, Key, Mapped, valueDestroy, keyEquals, keyHash) \ - typedef_cmap_10(X, Key, Mapped, valueDestroy, keyEquals, keyHash, \ +#define using_cmap_6(X, Key, Mapped, valueDestroy, keyEquals, keyHash) \ + using_cmap_10(X, Key, Mapped, valueDestroy, keyEquals, keyHash, \ c_default_destroy, Key, c_default_to_raw, c_default_from_raw) -#define typedef_cmap_10(X, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ +#define using_cmap_10(X, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw) \ _c_typedef_CHASH(X, cmap, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, Mapped, c_default_from_raw) -#define typedef_cmap_12(X, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ +#define using_cmap_12(X, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, RawVal, valueFromRaw) \ _c_typedef_CHASH(X, cmap, Key, Mapped, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, RawVal, valueFromRaw) /* cset: */ -#define typedef_cset(...) \ - c_MACRO_OVERLOAD(typedef_cset, __VA_ARGS__) +#define using_cset(...) \ + c_MACRO_OVERLOAD(using_cset, __VA_ARGS__) -#define typedef_cset_2(X, Key) \ - typedef_cset_4(X, Key, c_default_equals, c_default_hash16) +#define using_cset_2(X, Key) \ + using_cset_4(X, Key, c_default_equals, c_default_hash16) -#define typedef_cset_4(X, Key, keyEquals, keyHash) \ - typedef_cset_5(X, Key, keyEquals, keyHash, c_default_destroy) +#define using_cset_4(X, Key, keyEquals, keyHash) \ + using_cset_5(X, Key, keyEquals, keyHash, c_default_destroy) -#define typedef_cset_5(X, Key, keyEquals, keyHash, keyDestroy) \ - typedef_cset_8(X, Key, keyEquals, keyHash, keyDestroy, \ +#define using_cset_5(X, Key, keyEquals, keyHash, keyDestroy) \ + using_cset_8(X, Key, keyEquals, keyHash, keyDestroy, \ Key, c_default_to_raw, c_default_from_raw) -#define typedef_cset_8(X, Key, keyEqualsRaw, keyHashRaw, keyDestroy, \ +#define using_cset_8(X, Key, keyEqualsRaw, keyHashRaw, keyDestroy, \ RawKey, keyToRaw, keyFromRaw) \ _c_typedef_CHASH(X, cset, Key, Key, void, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, void, c_default_from_raw) /* cset_str, cmap_str, cmap_strkey, cmap_strval: */ -#define typedef_cset_str() \ +#define using_cset_str() \ _c_declare_CHASH_strkey(str, cset, cstr_t, void) -#define typedef_cmap_str() \ +#define using_cmap_str() \ _c_typedef_CHASH(str, cmap, cstr_t, cstr_t, cstr_destroy, cstr_equals_raw, cstr_hash_raw, \ cstr_destroy, const char*, cstr_to_raw, cstr, const char*, cstr) -#define typedef_cmap_strkey(...) \ - c_MACRO_OVERLOAD(typedef_cmap_strkey, __VA_ARGS__) +#define using_cmap_strkey(...) \ + c_MACRO_OVERLOAD(using_cmap_strkey, __VA_ARGS__) -#define typedef_cmap_strkey_2(X, Mapped) \ +#define using_cmap_strkey_2(X, Mapped) \ _c_declare_CHASH_strkey(X, cmap, Mapped, c_default_destroy) -#define typedef_cmap_strkey_3(X, Mapped, ValueDestroy) \ +#define using_cmap_strkey_3(X, Mapped, ValueDestroy) \ _c_declare_CHASH_strkey(X, cmap, Mapped, ValueDestroy) -#define typedef_cmap_strval(...) \ - c_MACRO_OVERLOAD(typedef_cmap_strval, __VA_ARGS__) +#define using_cmap_strval(...) \ + c_MACRO_OVERLOAD(using_cmap_strval, __VA_ARGS__) -#define typedef_cmap_strval_2(X, Key) \ - typedef_cmap_strval_4(X, Key, c_default_equals, c_default_hash16) +#define using_cmap_strval_2(X, Key) \ + using_cmap_strval_4(X, Key, c_default_equals, c_default_hash16) -#define typedef_cmap_strval_4(X, Key, keyEquals, keyHash) \ - typedef_cmap_strval_8(X, Key, keyEquals, keyHash, \ +#define using_cmap_strval_4(X, Key, keyEquals, keyHash) \ + using_cmap_strval_8(X, Key, keyEquals, keyHash, \ c_default_destroy, Key, c_default_to_raw, c_default_from_raw) -#define typedef_cmap_strval_8(X, Key, keyEquals, keyHash, keyDestroy, RawKey, keyToRaw, keyFromRaw) \ +#define using_cmap_strval_8(X, Key, keyEquals, keyHash, keyDestroy, RawKey, keyToRaw, keyFromRaw) \ _c_typedef_CHASH(X, cmap, Key, cstr_t, cstr_destroy, keyEquals, keyHash, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, const char*, cstr) diff --git a/stc/cpqueue.h b/stc/cpqueue.h index 7b2a02ad..03fe09d2 100644 --- a/stc/cpqueue.h +++ b/stc/cpqueue.h @@ -25,8 +25,8 @@ #include #include - typedef_cvec(f, float); - typedef_cpqueue(f, cvec_f, >); // min-heap (increasing values) + using_cvec(f, float); + using_cpqueue(f, cvec_f, >); // min-heap (increasing values) int main() { crand_rng32_t gen = crand_rng32_init(1234); @@ -50,7 +50,7 @@ #include "cvec.h" -#define typedef_cpqueue(X, ctype, cmpOpr) /* cmpOpr: < or > */ \ +#define using_cpqueue(X, ctype, cmpOpr) /* cmpOpr: < or > */ \ \ typedef struct ctype cpqueue_##X; \ typedef ctype##_value_t cpqueue_##X##_value_t; \ diff --git a/stc/cqueue.h b/stc/cqueue.h index 3962f3d8..4a13c335 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -25,8 +25,8 @@ #include #include - typedef_clist(i, int); - typedef_cqueue(i, clist_i); // min-heap (increasing values) + using_clist(i, int); + using_cqueue(i, clist_i); // min-heap (increasing values) int main() { int n = 10000000; @@ -58,7 +58,7 @@ #include "clist.h" -#define typedef_cqueue(X, ctype) \ +#define using_cqueue(X, ctype) \ \ typedef struct ctype cqueue_##X; \ typedef ctype##_value_t cqueue_##X##_value_t; \ diff --git a/stc/cstack.h b/stc/cstack.h index f6a5777e..0d914445 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -26,8 +26,8 @@ #include #include - typedef_cvec(i, int); - typedef_cstack(i, cvec_i); + using_cvec(i, int); + using_cstack(i, cvec_i); int main() { cstack_i stack = cstack_i_init(); @@ -47,7 +47,7 @@ #include "cvec.h" -#define typedef_cstack(X, ctype) \ +#define using_cstack(X, ctype) \ \ typedef struct ctype cstack_##X; \ typedef ctype##_value_t cstack_##X##_value_t; \ diff --git a/stc/cvec.h b/stc/cvec.h index 0b664c9c..6f292b33 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -32,26 +32,27 @@ #define cvec_capacity(v) _cvec_safe_capacity((v).data) #define cvec_empty(v) (cvec_size(v) == 0) -#define typedef_cvec(...) c_MACRO_OVERLOAD(typedef_cvec, __VA_ARGS__) -#define typedef_cvec_2(X, Value) \ - typedef_cvec_3(X, Value, c_default_destroy) -#define typedef_cvec_3(X, Value, valueDestroy) \ - typedef_cvec_4(X, Value, valueDestroy, c_default_compare) -#define typedef_cvec_4(X, Value, valueDestroy, valueCompare) \ - typedef_cvec_7(X, Value, valueDestroy, valueCompare, Value, c_default_to_raw, c_default_from_raw) -#define typedef_cvec_str() \ - typedef_cvec_7(str, cstr_t, cstr_destroy, cstr_compare_raw, const char*, cstr_to_raw, cstr) +#define using_cvec(...) c_MACRO_OVERLOAD(using_cvec, __VA_ARGS__) +#define using_cvec_2(X, Value) \ + using_cvec_3(X, Value, c_default_destroy) +#define using_cvec_3(X, Value, valueDestroy) \ + using_cvec_4(X, Value, valueDestroy, c_default_compare) +#define using_cvec_4(X, Value, valueDestroy, valueCompare) \ + using_cvec_7(X, Value, valueDestroy, valueCompare, Value, c_default_to_raw, c_default_from_raw) +#define using_cvec_str() \ + using_cvec_7(str, cstr_t, cstr_destroy, cstr_compare_raw, const char*, cstr_to_raw, cstr) -#define typedef_cvec_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ +#define using_cvec_7(X, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) \ \ - typedef struct cvec_##X { \ - Value* data; \ - } cvec_##X; \ typedef Value cvec_##X##_value_t; \ typedef RawValue cvec_##X##_rawvalue_t; \ typedef cvec_##X##_rawvalue_t cvec_##X##_input_t; \ - typedef struct { Value *get; } cvec_##X##_iter_t; \ + typedef struct { cvec_##X##_value_t *get; } cvec_##X##_iter_t; \ +\ + typedef struct cvec_##X { \ + cvec_##X##_value_t* data; \ + } cvec_##X; \ \ STC_INLINE cvec_##X \ cvec_##X##_init(void) {cvec_##X v = cvec_ini; return v;} \ @@ -72,7 +73,7 @@ STC_API void \ cvec_##X##_resize(cvec_##X* self, size_t size, Value fill_val); \ STC_INLINE void \ - cvec_##X##_swap(cvec_##X* a, cvec_##X* b) {c_swap(Value*, a->data, b->data);} \ + cvec_##X##_swap(cvec_##X* a, cvec_##X* b) {c_swap(cvec_##X##_value_t*, a->data, b->data);} \ \ STC_INLINE cvec_##X \ cvec_##X##_with_size(size_t size, Value null_val) { \ @@ -100,23 +101,20 @@ valueDestroy(&self->data[--_cvec_size(self)]); \ } \ \ - STC_API cvec_##X##_iter_t \ - cvec_##X##_insert_range(cvec_##X* self, cvec_##X##_iter_t pos, cvec_##X##_iter_t first, cvec_##X##_iter_t finish); \ - \ STC_INLINE cvec_##X##_iter_t \ - cvec_##X##_insert_range_ptr(cvec_##X* self, size_t idx, Value* pfirst, Value* pfinish) { \ - cvec_##X##_iter_t pos = {self->data + idx}, first = {pfirst}, finish = {pfinish}; \ - return cvec_##X##_insert_range(self, pos, first, finish); \ + cvec_##X##_insert_range_p(cvec_##X* self, cvec_##X##_value_t* pos, cvec_##X##_value_t* pfirst, cvec_##X##_value_t* pfinish); \ +\ + STC_API cvec_##X##_iter_t \ + cvec_##X##_insert_range(cvec_##X* self, cvec_##X##_iter_t pos, cvec_##X##_iter_t first, cvec_##X##_iter_t finish) { \ + return cvec_##X##_insert_range_p(self, pos.get, first.get, finish.get); \ } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_insert_at(cvec_##X* self, cvec_##X##_iter_t pos, Value value) { \ - cvec_##X##_iter_t first = {&value}, finish = {&value + 1}; \ - return cvec_##X##_insert_range(self, pos, first, finish); \ + return cvec_##X##_insert_range_p(self, pos.get, &value, &value + 1); \ } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_insert_at_idx(cvec_##X* self, size_t idx, Value value) { \ - cvec_##X##_iter_t pos = {self->data + idx}, first = {&value}, finish = {&value + 1}; \ - return cvec_##X##_insert_range(self, pos, first, finish); \ + return cvec_##X##_insert_range_p(self, self->data + idx, &value, &value + 1); \ } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_emplace_at(cvec_##X* self, cvec_##X##_iter_t pos, RawValue rawValue) { \ @@ -128,22 +126,23 @@ } \ \ STC_API cvec_##X##_iter_t \ - cvec_##X##_erase_range(cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish); \ + cvec_##X##_erase_range_p(cvec_##X* self, cvec_##X##_value_t* first, cvec_##X##_value_t* finish); \ \ + STC_INLINE cvec_##X##_iter_t \ + cvec_##X##_erase_range(cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish) { \ + return cvec_##X##_erase_range_p(self, first.get, finish.get); \ + } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_erase_at(cvec_##X* self, cvec_##X##_iter_t pos) { \ - cvec_##X##_iter_t next = {pos.get + 1}; \ - return cvec_##X##_erase_range(self, pos, next); \ + return cvec_##X##_erase_range_p(self, pos.get, pos.get + 1); \ } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_erase_at_idx(cvec_##X* self, size_t idx) { \ - cvec_##X##_iter_t first = {self->data + idx}, finish = {first.get + 1}; \ - return cvec_##X##_erase_range(self, first, finish); \ + return cvec_##X##_erase_range_p(self, self->data + idx, self->data + idx + 1); \ } \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_erase_range_idx(cvec_##X* self, size_t ifirst, size_t ifinish) { \ - cvec_##X##_iter_t first = {self->data + ifirst}, finish = {self->data + ifinish}; \ - return cvec_##X##_erase_range(self, first, finish); \ + return cvec_##X##_erase_range_p(self, self->data + ifirst, self->data + ifinish); \ } \ \ STC_API cvec_##X##_iter_t \ @@ -151,20 +150,20 @@ STC_API cvec_##X##_iter_t \ cvec_##X##_find_in_range(const cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish, RawValue rawValue); \ \ - STC_INLINE Value* \ + STC_INLINE cvec_##X##_value_t* \ cvec_##X##_front(cvec_##X* self) {return self->data;} \ - STC_INLINE Value* \ + STC_INLINE cvec_##X##_value_t* \ cvec_##X##_back(cvec_##X* self) {return self->data + _cvec_size(self) - 1;} \ - STC_INLINE Value* \ + STC_INLINE cvec_##X##_value_t* \ cvec_##X##_at(cvec_##X* self, size_t i) { \ assert(i < cvec_size(*self)); \ return self->data + i; \ } \ \ STC_API int \ - cvec_##X##_value_compare(const Value* x, const Value* y); \ + cvec_##X##_value_compare(const cvec_##X##_value_t* x, const cvec_##X##_value_t* y); \ STC_INLINE void \ - cvec_##X##_sort_with(cvec_##X* self, size_t ifirst, size_t ifinish, int(*cmp)(const Value*, const Value*)) { \ + cvec_##X##_sort_with(cvec_##X* self, size_t ifirst, size_t ifinish, int(*cmp)(const cvec_##X##_value_t*, const cvec_##X##_value_t*)) { \ qsort(self->data + ifirst, ifinish - ifirst, sizeof(Value), (_cvec_cmp) cmp); \ } \ STC_INLINE void \ @@ -203,8 +202,8 @@ \ STC_API void \ cvec_##X##_clear(cvec_##X* self) { \ - Value* p = self->data; if (p) { \ - for (Value* q = p + _cvec_size(self); p != q; ++p) valueDestroy(p); \ + cvec_##X##_value_t* p = self->data; if (p) { \ + for (cvec_##X##_value_t* q = p + _cvec_size(self); p != q; ++p) valueDestroy(p); \ _cvec_size(self) = 0; \ } \ } \ @@ -240,30 +239,33 @@ } \ \ STC_API cvec_##X##_iter_t \ - cvec_##X##_insert_range(cvec_##X* self, cvec_##X##_iter_t pos, cvec_##X##_iter_t first, cvec_##X##_iter_t finish) { \ - enum {max_buf = c_max_alloca / sizeof(Value) + 1}; Value buf[max_buf]; \ - size_t len = finish.get - first.get, idx = pos.get - self->data, size = cvec_size(*self); \ - Value* xbuf = (Value *) memcpy(len > max_buf ? c_new_n(Value, len) : buf, first.get, len * sizeof(Value)); \ + cvec_##X##_insert_range_p(cvec_##X* self, cvec_##X##_value_t* pos, cvec_##X##_value_t* first, cvec_##X##_value_t* finish) { \ + enum {max_buf = c_max_alloca / sizeof(Value) + 1}; \ + Value buf[max_buf]; \ + size_t len = finish - first, idx = pos - self->data, size = cvec_size(*self); \ + cvec_##X##_value_t* xbuf = (len > max_buf ? c_new_n(cvec_##X##_value_t, len) : &buf[0]); \ + for (size_t i=0; i cvec_capacity(*self)) \ cvec_##X##_reserve(self, 4 + (size + len) * 3 / 2); \ - pos.get = self->data + idx; \ - memmove(pos.get + len, pos.get, (size - idx) * sizeof(Value)); \ - memcpy(pos.get, xbuf, len * sizeof(Value)); \ + pos = self->data + idx; \ + memmove(pos + len, pos, (size - idx) * sizeof(Value)); \ + memcpy(pos, xbuf, len * sizeof(Value)); \ _cvec_size(self) += len; \ if (len > max_buf) free(xbuf); \ - return pos; \ + cvec_##X##_iter_t it = {pos}; return it; \ } \ \ STC_API cvec_##X##_iter_t \ - cvec_##X##_erase_range(cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish) { \ - intptr_t len = finish.get - first.get; \ + cvec_##X##_erase_range_p(cvec_##X* self, cvec_##X##_value_t* first, cvec_##X##_value_t* finish) { \ + intptr_t len = finish - first; \ if (len > 0) { \ - Value* p = first.get, *end = p + _cvec_size(self); \ - while (p != finish.get) valueDestroy(p++); \ - memmove(first.get, finish.get, (end - finish.get) * sizeof(Value)); \ + cvec_##X##_value_t* p = first, *end = self->data + _cvec_size(self); \ + while (p != finish) valueDestroy(p++); \ + memmove(first, finish, (end - finish) * sizeof(Value)); \ _cvec_size(self) -= len; \ } \ - return first; \ + cvec_##X##_iter_t it = {first}; return it; \ } \ \ STC_API cvec_##X##_iter_t \ @@ -280,7 +282,7 @@ } \ \ STC_API int \ - cvec_##X##_value_compare(const Value* x, const Value* y) { \ + cvec_##X##_value_compare(const cvec_##X##_value_t* x, const cvec_##X##_value_t* y) { \ RawValue rx = valueToRaw(x); \ RawValue ry = valueToRaw(y); \ return valueCompareRaw(&rx, &ry); \ -- cgit v1.2.3