diff options
| author | Tyge Løvset <[email protected]> | 2022-09-02 23:45:11 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-09-02 23:45:11 +0200 |
| commit | 491a8cba3ee55a1200fc2de8ef3807dd8436f829 (patch) | |
| tree | f877ef241836ca369561cf62d2db465ef3cbeca0 | |
| parent | a2a18a52df4fc10ad453eba7cdfbe2d02a026f0b (diff) | |
| download | STC-modified-491a8cba3ee55a1200fc2de8ef3807dd8436f829.tar.gz STC-modified-491a8cba3ee55a1200fc2de8ef3807dd8436f829.zip | |
Change: carc and cbox defaults to pointer comparison when none of i_cmp, i_less or i_eq is specified. This removes annoying requirement.
| -rw-r--r-- | docs/carc_api.md | 4 | ||||
| -rw-r--r-- | docs/cbox_api.md | 2 | ||||
| -rw-r--r-- | examples/arc_containers.c | 3 | ||||
| -rw-r--r-- | examples/arcvec_erase.c | 5 | ||||
| -rw-r--r-- | examples/box2.c | 3 | ||||
| -rw-r--r-- | examples/new_sptr.c | 1 | ||||
| -rw-r--r-- | include/stc/carc.h | 14 | ||||
| -rw-r--r-- | include/stc/cbox.h | 10 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 6 | ||||
| -rw-r--r-- | include/stc/clist.h | 6 | ||||
| -rw-r--r-- | include/stc/cvec.h | 6 | ||||
| -rw-r--r-- | include/stc/template.h | 4 |
12 files changed, 33 insertions, 31 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md index d09b1839..56f8ff2e 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -48,7 +48,7 @@ void carc_X_reset(carc_X* self); void carc_X_reset_to(carc_X* self, i_val* p); // assign new carc from ptr. Takes ownership of p. uint64_t carc_X_hash(const carc_X* x); // hash value -int carc_X_cmp(const carc_X* x, const carc_X* y); // compares pointer addresses if 'i_opt c_no_cmp' +int carc_X_cmp(const carc_X* x, const carc_X* y); // compares pointer addresses if no `i_cmp` is specified. // is defined. Otherwise uses 'i_cmp' or default cmp. bool carc_X_eq(const carc_X* x, const carc_X* y); // carc_X_cmp() == 0 @@ -88,7 +88,7 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); #define i_valclone Map_clone // override Map_drop(p): #define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) -#define i_opt c_no_cmp|c_no_atomic // make it non-atomic sharing. +#define i_opt c_no_atomic // make it non-atomic sharing. #include <stc/carc.h> #define i_type Stack diff --git a/docs/cbox_api.md b/docs/cbox_api.md index dc90fa8e..6686dde5 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -45,7 +45,7 @@ void cbox_X_reset(cbox_X* self); void cbox_X_reset_to(cbox_X* self, i_val* p); // assign new cbox from ptr. Takes ownership of p. uint64_t cbox_X_hash(const cbox_X* x); // hash value -int cbox_X_cmp(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_cmp' +int cbox_X_cmp(const cbox_X* x, const cbox_X* y); // compares pointer addresses if no `i_cmp` is specified. // is defined. Otherwise uses 'i_cmp' or default cmp. bool cbox_X_eq(const cbox_X* x, const cbox_X* y); // cbox_X_cmp() == 0 diff --git a/examples/arc_containers.c b/examples/arc_containers.c index debc6617..6aa80dcf 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -12,8 +12,7 @@ #define i_val Map #define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) // no need for atomic ref. count in single thread: -// no compare function available for csmap: -#define i_opt c_no_atomic|c_no_cmp +#define i_opt c_no_atomic #include <stc/carc.h> #define i_type Stack diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index b96f4278..2c9a3daa 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -6,9 +6,8 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #define i_val int #define i_valdrop show_drop // carc/cbox will use pointer address comparison of i_val -// if 'i_opt c_no_cmp' is defined, otherwise i_cmp is used -// to compare object values. See the two differences by -// commenting out the next line. +// if no 'i_cmp' is defined, otherwise 'i_cmp' is used +// to compare object values. #include <stc/carc.h> // Shared pointer to int #define i_type Vec diff --git a/examples/box2.c b/examples/box2.c index 0896388e..75f9970f 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -18,17 +18,14 @@ struct { } typedef Rectangle; #define i_val Point -#define i_opt c_no_cmp #include <stc/cbox.h> // cbox_Point #define i_val Rectangle -#define i_opt c_no_cmp #include <stc/cbox.h> // cbox_Rectangle // Box in box: #define i_val_arcbox cbox_Point // NB: use i_val_arcbox when value is a cbox or carc! // it will auto define i_valdrop, i_valfrom, and i_cmp. -#define i_opt c_no_cmp #define i_tag BoxPoint #include <stc/cbox.h> // cbox_BoxPoint diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 2af7317e..71222799 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -15,7 +15,6 @@ void Person_drop(Person* p) { } #define i_val_bind Person -#define i_opt c_no_cmp // makes cmp and hash not required when using _bind #define i_tag person #include <stc/carc.h> diff --git a/include/stc/carc.h b/include/stc/carc.h index 6e8f7d38..d0adcceb 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -37,7 +37,6 @@ void Person_drop(Person* p) { #define i_type ArcPers #define i_key Person #define i_keydrop Person_drop -#define i_opt c_no_cmp #include <stc/carc.h> int main() { @@ -78,6 +77,9 @@ int main() { #ifndef _i_prefix #define _i_prefix carc_ #endif +#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash) + #define _i_no_cmp +#endif #include "template.h" typedef i_keyraw _cx_raw; @@ -146,10 +148,10 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) { *self = _cx_memb(_from_ptr)(p); } -#if !defined _i_no_clone && !defined _i_no_emplace +#if !defined _i_no_emplace STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); } -#endif // !_i_no_clone +#endif // !_i_no_emplace // does not use i_keyclone, so OK to always define. STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) { @@ -172,7 +174,7 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) { } STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return c_default_hash(&x); #else _cx_raw rx = i_keyto(x); @@ -181,7 +183,7 @@ STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { } STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return c_default_cmp(&x, &y); #else _cx_raw rx = i_keyto(x), ry = i_keyto(y); @@ -190,7 +192,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { } STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return x == y; #else _cx_raw rx = i_keyto(x), ry = i_keyto(y); diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 02a73fdc..fc300de3 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -41,7 +41,6 @@ void Person_drop(Person* p) { } #define i_key_bind Person // bind Person clone+drop fn's -#define i_opt c_no_cmp // compare by .get addresses only #define i_type PBox #include <stc/cbox.h> @@ -71,6 +70,9 @@ int main() { #ifndef _i_prefix #define _i_prefix cbox_ #endif +#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash) + #define _i_no_cmp +#endif #include "template.h" typedef i_keyraw _cx_raw; @@ -147,7 +149,7 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) { } STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return c_default_hash(&x); #else _cx_raw rx = i_keyto(x); @@ -156,7 +158,7 @@ STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) { } STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return c_default_cmp(&x, &y); #else _cx_raw rx = i_keyto(x), ry = i_keyto(y); @@ -165,7 +167,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { } STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { - #if c_option(c_no_cmp) + #if defined _i_no_cmp return x == y; #else _cx_raw rx = i_keyto(x), ry = i_keyto(y); diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index a8c880f2..b4f02081 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -57,7 +57,7 @@ STC_API _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); #endif // _i_no_emplace -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, _cx_raw raw); STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); #endif @@ -177,7 +177,7 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { } #endif // !_i_no_emplace -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_raw raw) { @@ -427,7 +427,7 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, } #endif // !_i_no_emplace -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) { diff --git a/include/stc/clist.h b/include/stc/clist.h index 5b9cf0d3..7e47f7b7 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -101,7 +101,7 @@ STC_API _cx_value* _cx_memb(_push_node_front)(_cx_self* self, _cx_node* nod STC_API _cx_iter _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value); STC_API _cx_iter _cx_memb(_erase_at)(_cx_self* self, _cx_iter it); STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2); -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_API size_t _cx_memb(_remove)(_cx_self* self, _cx_raw val); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val); STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); @@ -181,7 +181,7 @@ _cx_memb(_splice_range)(_cx_self* self, _cx_iter it, return _cx_memb(_splice)(self, it, &tmp); } -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_raw val) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val); @@ -378,7 +378,7 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) { return cx; } -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val) { diff --git a/include/stc/cvec.h b/include/stc/cvec.h index fcf05c10..ff26de1e 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -89,7 +89,7 @@ STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, STC_API _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n); -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw); STC_API _cx_iter _cx_memb(_binary_search_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw, _cx_iter* lower_bound); @@ -213,7 +213,7 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) STC_INLINE size_t _cx_memb(_index)(const _cx_self* cx, _cx_iter it) { return it.ref - cx->data; } -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_raw raw) { @@ -402,7 +402,7 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, } #endif // !_i_no_emplace -#if !c_option(c_no_cmp) +#if !defined _i_no_cmp STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) { const _cx_value* p2 = _it2_ptr(i1, i2); diff --git a/include/stc/template.h b/include/stc/template.h index 4597e02c..41ecf7b0 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -147,6 +147,9 @@ #ifndef i_tag #define i_tag i_key #endif +#if c_option(c_no_cmp) + #define _i_no_cmp +#endif #if c_option(c_no_clone) || (!defined i_keyclone && (defined i_keydrop || defined i_keyraw)) #define _i_no_clone #endif @@ -304,6 +307,7 @@ #undef _i_prefix #undef _i_has_from #undef _i_key_from_val +#undef _i_no_cmp #undef _i_no_clone #undef _i_no_emplace #undef _i_no_hash |
