From b707e18ae93c60f2b659bad39f1daf54604445f7 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 1 Apr 2022 18:08:58 +0200 Subject: Simplified internal logic on enabling default cloning --- include/stc/carc.h | 3 +-- include/stc/carr2.h | 4 ++-- include/stc/carr3.h | 4 ++-- include/stc/cbox.h | 4 +--- include/stc/cdeq.h | 22 +++++++++------------- include/stc/clist.h | 8 ++++---- include/stc/cmap.h | 14 ++++++-------- include/stc/cpque.h | 6 ++---- include/stc/csmap.h | 14 +++++--------- include/stc/cstack.h | 8 +++----- include/stc/cvec.h | 13 +++++-------- include/stc/template.h | 23 ++++++++++++++--------- 12 files changed, 54 insertions(+), 69 deletions(-) diff --git a/include/stc/carc.h b/include/stc/carc.h index 76535db8..a0c4ba77 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -74,7 +74,6 @@ int main() { #ifndef _i_prefix #define _i_prefix carc_ #endif -#define _i_has_internal_clone #include "template.h" typedef i_valraw _cx_raw; @@ -144,7 +143,7 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { *self = _cx_memb(_from)(val); } -#if !c_option(c_no_clone) && !defined _i_no_raw +#if !defined _i_no_from STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } #endif diff --git a/include/stc/carr2.h b/include/stc/carr2.h index 757df11e..2205356a 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -65,7 +65,7 @@ STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* storage); STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self src); STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); #endif @@ -114,7 +114,7 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { return _arr; } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim); diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 1cb0de19..c6c82019 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -67,7 +67,7 @@ STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage); STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self src); STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); #endif @@ -120,7 +120,7 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i return _arr; } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim, src.zdim); diff --git a/include/stc/cbox.h b/include/stc/cbox.h index fe4bca9d..ee2e0a45 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -117,11 +117,9 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { else self->get = c_new(i_val, val); } -#if !c_option(c_no_clone) -#if !defined _i_no_raw +#if !defined _i_no_from STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } -#endif STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 6b8e7efa..410db96f 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -49,14 +49,12 @@ STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); #ifndef _i_queue -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -#if !defined _i_no_raw STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); -#endif -#endif // !c_no_clone +#endif // _c_no_from #if !c_option(c_no_cmp) STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, i_valraw raw); @@ -68,12 +66,11 @@ STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos const _cx_value* p1, const _cx_value* p2); #endif // !_i_queue -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self cx); -#if !defined _i_no_raw + STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) { return _cx_memb(_push_back)(self, i_valfrom(raw)); } -#endif STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { @@ -143,7 +140,7 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref); } -#if !c_option(c_no_clone) && !defined _i_no_raw +#if !defined _i_no_from STC_INLINE _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) { @@ -166,7 +163,7 @@ STC_INLINE _cx_iter _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) { return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); } -#endif // !c_no_clone +#endif // _c_no_from #if !c_option(c_no_cmp) @@ -374,8 +371,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { return c_make(_cx_iter){p1}; } -#if !c_option(c_no_clone) -#if !defined _i_no_raw +#if !defined _i_no_from STC_DEF _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); @@ -383,7 +379,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, co for (; p1 != p2; ++p1) *pos++ = i_valfrom(*p1); return it; } -#endif + STC_DEF _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { @@ -392,7 +388,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, for (; p1 != p2; ++p1) *pos++ = i_valfrom(i_valto(p1)); return it; } -#endif // !c_option(c_no_clone) +#endif // !_i_no_from #if !c_option(c_no_cmp) diff --git a/include/stc/clist.h b/include/stc/clist.h index a2642ed9..70620534 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -106,7 +106,7 @@ STC_API _cx_iter _cx_memb(_splice)(_cx_self* self, _cx_iter it, _cx_self* STC_API _cx_self _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2); STC_API _cx_node* _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node); -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } @@ -115,7 +115,7 @@ _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->last == other.last) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -#if !defined _i_no_raw + STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) { return _cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw) @@ -123,7 +123,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw) STC_INLINE _cx_iter _cx_memb(_emplace)(_cx_self* self, _cx_iter it, i_valraw raw) { return _cx_memb(_insert)(self, it, i_valfrom(raw)); } #endif -#endif + STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; } STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; } STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.last == NULL; } @@ -185,7 +185,7 @@ _cx_memb(_get_mut)(_cx_self* self, i_valraw val) { // -------------------------- IMPLEMENTATION ------------------------- #if defined(_i_implement) -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_init)(); diff --git a/include/stc/cmap.h b/include/stc/cmap.h index cac11234..bbe021a7 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -87,7 +87,7 @@ typedef _i_SET_ONLY( i_keyraw ) _cx_raw; STC_API _cx_self _cx_memb(_with_capacity)(size_t cap); -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self map); #endif STC_API void _cx_memb(_drop)(_cx_self* self); @@ -110,7 +110,7 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } #ifndef _i_isset - #if !c_option(c_no_clone) && !defined _i_no_raw + #if !defined _i_no_from STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped); #endif STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped); @@ -128,7 +128,7 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) } #endif -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->table == other.table) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); @@ -141,7 +141,6 @@ _cx_memb(_value_clone)(_cx_value _val) { return _val; } -#if !defined _i_no_raw STC_INLINE _cx_result _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) { _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); @@ -151,8 +150,7 @@ _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped) } return _res; } -#endif -#endif // !c_no_clone +#endif // !_i_no_from STC_INLINE _cx_raw _cx_memb(_value_toraw)(_cx_value* val) { @@ -274,7 +272,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { _res.ref->second = _mapped; return _res; } - #if !c_option(c_no_clone) && !defined _i_no_raw + #if !defined _i_no_from STC_DEF _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); @@ -315,7 +313,7 @@ _cx_memb(_insert_entry_)(_cx_self* self, i_keyraw rkey) { return res; } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self m) { _cx_self clone = { diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 159cbf06..d8480a17 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -88,7 +88,7 @@ STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) STC_INLINE void _cx_memb(_pop)(_cx_self* self) { _cx_memb(_erase_at)(self, 0); } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self q); STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { @@ -98,11 +98,9 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) { return i_valfrom(i_valto(&val)); } -#if !defined _i_no_raw STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { _cx_memb(_push)(self, i_valfrom(raw)); } #endif -#endif /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(_i_implement) @@ -124,7 +122,7 @@ _cx_memb(_make_heap)(_cx_self* self) { _cx_memb(_sift_down_)(arr, k, n); } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { _cx_self out = _cx_memb(_with_capacity)(q.size); for (; out.size < out.capacity; ++out.size, ++q.data) diff --git a/include/stc/csmap.h b/include/stc/csmap.h index c360df08..49110221 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -93,12 +93,10 @@ typedef _i_SET_ONLY( i_keyraw ) _i_MAP_ONLY( struct { i_keyraw first; i_valraw second; } ) _cx_raw; -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self tree); STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); STC_API _cx_value _cx_memb(_value_clone)(_cx_value _val); -#endif -#if !defined _i_no_raw STC_API _cx_result _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)); #endif STC_API _cx_self _cx_memb(_init)(void); @@ -156,7 +154,7 @@ _cx_memb(_value_drop)(_cx_value* val) { } #ifndef _i_isset - #if !c_option(c_no_clone) && !defined _i_no_raw + #if !defined _i_no_from STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped); #endif STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped); @@ -277,7 +275,7 @@ _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped)) { res.ref->second = mapped; return res; } - #if !c_option(c_no_clone) && !defined _i_no_raw + #if !defined _i_no_from STC_DEF _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { _cx_result res = _cx_memb(_insert_entry_)(self, rkey); @@ -460,7 +458,7 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { } } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->nodes == other.nodes) return; @@ -493,7 +491,6 @@ _cx_memb(_clone)(_cx_self tree) { return clone; } -#if !defined _i_no_raw STC_DEF _cx_result _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) { _cx_result res = _cx_memb(_insert_entry_)(self, rkey); @@ -503,8 +500,7 @@ _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped) } return res; } -#endif -#endif // !c_no_clone +#endif // !_i_no_from static void _cx_memb(_drop_r_)(_cx_node* d, _cx_size tn) { diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 2a6f0d4a..29b13c90 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -98,13 +98,12 @@ STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) { assert(idx < self->size); return self->data + idx; } -#if !c_option(c_no_clone) -#if !defined _i_no_raw +#if !defined _i_no_from STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_valfrom(raw)); } STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_valfrom(raw)); } -#endif + STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { _cx_self out = {(_cx_value *) c_malloc(v.size*sizeof(_cx_value)), v.size, v.size}; for (size_t i = 0; i < v.size; ++i, ++v.data) out.data[i] = i_valfrom(i_valto(v.data)); @@ -120,8 +119,7 @@ STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) { return i_valfrom(i_valto(&val)); } STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* val) { return i_valto(val); } - -#endif // !c_no_clone +#endif // !_i_no_from STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { return c_make(_cx_iter){self->data}; } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index f3ae9d47..7ba9fb85 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -92,7 +92,7 @@ STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_valraw raw); #endif -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); @@ -104,7 +104,6 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -#if !defined _i_no_raw STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) @@ -123,8 +122,7 @@ STC_INLINE _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) { return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref); } -#endif -#endif +#endif // !_c_no_from STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cvec_rep_(&cx)->size; } STC_INLINE size_t _cx_memb(_capacity)(_cx_self cx) { return cvec_rep_(&cx)->cap; } @@ -332,7 +330,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { return c_make(_cx_iter){.ref = p1}; } -#if !c_option(c_no_clone) +#if !defined _i_no_from STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { @@ -350,7 +348,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, for (; p1 != p2; ++p1) *pos++ = i_valfrom(i_valto(p1)); return it; } -#if !defined _i_no_raw + STC_DEF _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { @@ -359,8 +357,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, for (; p1 != p2; ++p1) *pos++ = i_valfrom(*p1); return it; } -#endif // !_i_no_raw -#endif // !c_no_clone +#endif // !_i_no_from #if !c_option(c_no_cmp) diff --git a/include/stc/template.h b/include/stc/template.h index 6e058f19..da6f1d9c 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -157,8 +157,12 @@ #ifndef i_tag #define i_tag i_key #endif - #if !defined i_keyfrom - #define _i_no_keyraw + #if !defined i_keyfrom && defined i_keydrop + #define _i_no_from + #elif defined i_keyfrom && !defined i_keydrop + #define _i_no_drop + #endif + #ifndef i_keyfrom #define i_keyfrom c_default_from #endif #ifndef i_keyraw @@ -177,10 +181,12 @@ #ifndef i_tag #define i_tag i_val #endif -#if !defined i_valfrom - #if !defined i_key || defined _i_no_keyraw - #define _i_no_raw - #endif +#if (!defined i_valfrom && defined i_valdrop) || c_option(c_no_clone) + #define _i_no_from +#elif defined i_valfrom && !defined i_valdrop + #define _i_no_drop +#endif +#ifndef i_valfrom #define i_valfrom c_default_from #endif #ifndef i_valraw @@ -236,8 +242,7 @@ #undef i_keydrop #undef _i_prefix -#undef _i_no_raw -#undef _i_no_keyraw -#undef _i_has_internal_clone +#undef _i_no_from +#undef _i_no_drop #undef _i_template #endif -- cgit v1.2.3