From 183a89859ba9914ee0546e4482b40be199e52292 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 16 Dec 2021 07:32:16 +0100 Subject: Changed default comparison of csbox and csptr objects to comparing addresses to containing objects. If i_cmp is defined, it is used instead. --- examples/box2.c | 2 -- examples/sptr_erase.c | 8 ++++---- examples/sptr_music.c | 1 - examples/sptr_pthread.c | 1 - examples/sptr_to_maps.c | 3 +-- examples/words.c | 7 +++---- include/stc/cbox.h | 6 +++--- include/stc/csptr.h | 6 +++--- include/stc/template.h | 37 ++++++++++++++++++++----------------- 9 files changed, 34 insertions(+), 37 deletions(-) diff --git a/examples/box2.c b/examples/box2.c index 86374a96..48266963 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -18,11 +18,9 @@ struct { } typedef Rectangle; #define i_val Point -#define i_opt c_no_compare #include // cbox_Point #define i_val Rectangle -#define i_opt c_no_compare #include // cbox_Rectangle // Box in box: diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c index eada6fd6..4707abb1 100644 --- a/examples/sptr_erase.c +++ b/examples/sptr_erase.c @@ -4,10 +4,10 @@ void show_del(int* x) { printf("del: %d\n", *x); } #define i_val int #define i_del show_del // this "destroy" func shows which elements are destroyed -// csptr/cbox will try to use default comparison of i_val (int) if no cmp func is specified, -// unless 'i_opt c_no_compare' is defined, in which case pointer addresses are compared. -// See the different results by commenting in the next line. -//#define i_opt c_no_compare +// csptr/cbox will use pointer address comparison of i_val if no i_cmp func is specified, +// or 'i_opt c_no_compare' is defined, otherwise i_cmp is used to compare object values. +// See the different results by commenting out the next line. +#define i_cmp(x, y) (*(x) - *(y)) #include // csptr_int: shared pointer to int #define i_val_ref csptr_int diff --git a/examples/sptr_music.c b/examples/sptr_music.c index e8e08b5f..fcb7a43f 100644 --- a/examples/sptr_music.c +++ b/examples/sptr_music.c @@ -20,7 +20,6 @@ void Song_del(Song* s) { #define i_val Song #define i_del Song_del #define i_tag song -#define i_opt c_no_compare #include // define csptr_song #define i_val_ref csptr_song diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index d82ea938..cfb845b6 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -15,7 +15,6 @@ void Base_del(Base* b) { printf("Base::~Base()\n"); } #define i_val Base #define i_del Base_del -#define i_opt c_no_compare #define i_tag base #include diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c index 77871304..a66e5b31 100644 --- a/examples/sptr_to_maps.c +++ b/examples/sptr_to_maps.c @@ -9,9 +9,8 @@ #define i_type Arc // (atomic) ref. counted type #define i_val Map #define i_del(p) (printf("del Arc:\n"), Map_del(p)) -// no comparison of Maps needed (or available), and // no need for atomic ref. count in single thread: -#define i_opt c_no_compare|c_no_atomic +#define i_opt c_no_atomic #include #define i_type Stack diff --git a/examples/words.c b/examples/words.c index 951c3ded..6c0636dc 100644 --- a/examples/words.c +++ b/examples/words.c @@ -4,7 +4,6 @@ #define i_val_str #include -#define i_tag strn #define i_key_str #define i_val int #include @@ -12,7 +11,7 @@ int main1() { c_auto (cvec_str, words) - c_auto (cmap_strn, word_map) + c_auto (cmap_str, word_map) { c_apply(cvec_str, emplace_back, &words, { "this", "sentence", "is", "not", "a", "sentence", @@ -20,10 +19,10 @@ int main1() }); c_foreach (w, cvec_str, words) { - cmap_strn_emplace(&word_map, w.ref->str, 0).ref->second += 1; + cmap_str_emplace(&word_map, w.ref->str, 0).ref->second += 1; } - c_foreach (i, cmap_strn, word_map) { + c_foreach (i, cmap_str, word_map) { printf("%d occurrences of word '%s'\n", i.ref->second, i.ref->first.str); } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 445057dd..89bd272e 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -151,9 +151,9 @@ _cx_memb(_take)(_cx_self* self, _cx_self other) { STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self, size_t n) { - #if c_option(c_no_compare) && SIZE_MAX >> 32 + #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32 return c_hash64(&self->get, 8); - #elif c_option(c_no_compare) + #elif (c_option(c_no_compare) || defined _i_default_compare) return c_hash32(&self->get, 4); #else i_valraw raw = i_valto(self->get); @@ -163,7 +163,7 @@ _cx_memb(_hash)(const _cx_self* self, size_t n) { STC_INLINE int _cx_memb(_compare)(const _cx_self* x, const _cx_self* y) { - #if c_option(c_no_compare) + #if (c_option(c_no_compare) || defined _i_default_compare) return (int)(x->get - y->get); #else i_valraw rx = i_valto(x->get); diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 06790e01..4753ef12 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -185,9 +185,9 @@ _cx_memb(_take)(_cx_self* self, _cx_self ptr) { STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self, size_t n) { - #if c_option(c_no_compare) && SIZE_MAX >> 32 + #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32 return c_hash64(&self->get, 8); - #elif c_option(c_no_compare) + #elif (c_option(c_no_compare) || defined _i_default_compare) return c_hash32(&self->get, 4); #else i_valraw raw = i_valto(self->get); @@ -197,7 +197,7 @@ _cx_memb(_hash)(const _cx_self* self, size_t n) { STC_INLINE int _cx_memb(_compare)(const _cx_self* x, const _cx_self* y) { - #if c_option(c_no_compare) + #if (c_option(c_no_compare) || defined _i_default_compare) return (int)(x->get - y->get); #else i_valraw rx = i_valto(x->get); diff --git a/include/stc/template.h b/include/stc/template.h index e788d4eb..85c4a940 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -102,13 +102,29 @@ #endif #endif +/* Resolve i_del and i_from here */ +#if defined i_del && defined i_isset + #define i_keydel i_del +#elif defined i_del && !defined i_key + #define i_valdel i_del +#elif defined i_del + #error "i_del not supported for maps, define i_keydel / i_valdel instead." +#endif +#if defined i_from && defined i_isset + #define i_keyfrom i_from +#elif defined i_from && !defined i_key + #define i_valfrom i_from +#elif defined i_from + #error "i_from not supported for maps, define i_keyfrom / i_valfrom instead." +#endif + #if defined i_val_ref #define i_val i_val_ref #define i_valfrom c_PASTE(i_val, _clone) #if !defined i_cmp && !defined i_key #define i_cmp c_PASTE(i_val, _compare) #endif - #if !defined i_valdel && !defined i_del + #if !defined i_valdel #define i_valdel c_PASTE(i_val, _del) #endif @@ -124,26 +140,11 @@ #if !defined i_cmp && !defined i_key #define i_cmp c_rawstr_compare #endif - #if !defined i_valdel && !defined i_del + #if !defined i_valdel #define i_valdel cstr_del #endif #endif -#if defined i_del && defined i_isset - #define i_keydel i_del -#elif defined i_del && !defined i_key - #define i_valdel i_del -#elif defined i_del - #error "i_del not supported for maps, define i_keydel / i_valdel instead." -#endif -#if defined i_from && defined i_isset - #define i_keyfrom i_from -#elif defined i_from && !defined i_key - #define i_valfrom i_from -#elif defined i_from - #error "i_from not supported for maps, define i_keyfrom / i_valfrom instead." -#endif - #ifdef i_key #ifdef _i_isset #define i_val i_key @@ -190,6 +191,7 @@ #define i_valdel c_default_del #endif #ifndef i_cmp + #define _i_default_compare #define i_cmp c_default_compare #endif #ifndef i_hash @@ -225,6 +227,7 @@ #undef i_keyto #undef _i_prefix +#undef _i_default_compare #undef _i_has_internal_clone #undef _i_template #endif -- cgit v1.2.3