From 776b1592e3d67f7a1af94180a0b4c3fcf5320f96 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Fri, 19 Jul 2019 15:21:19 +0200 Subject: same container, mixed type required per type allocator template parameters --- include/dwu/operators | 52 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/dwu/operators b/include/dwu/operators index bb52a57..fde9118 100644 --- a/include/dwu/operators +++ b/include/dwu/operators @@ -23,7 +23,31 @@ #ifndef DWU__OPERATORS__ #define DWU__OPERATORS__ 1 +/* === Implementation Notes === + + == On the use of explicitly implemented min instead of std::min == + +Within the operator functions that work on two containers, the output +length is truncated to the shorter of the two containers by an explicitly +written out min-comparison, like this: + + auto const lsz = l.size(); auto const rsz = r.size(); + Cl v( (lsz < rsz) ? lsz : rsz ); + +This is used instead of container for two reasons: + + 1. It avoids inclusion of a header. Although is rather + lightweight, the extra compile time is measureable. + + 2. std::min expects both inputs to be strictly of the same type. This + however makes it impossible to do mixed container operation with + containers that use a different size type, which however is something + we want to support. + +**/ + namespace dwu { + namespace operators { // same container, same type #define DWU_OPERATORS(O) \ @@ -84,9 +108,10 @@ DWU_OPERATORS(|) #define DWU_OPERATORS_XT(O) \ template< \ template class C, \ - typename Tl, typename A, typename Tr > \ - C operator O (C const &l, Tr const &r) { \ - C v(l.size()); \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C operator O (C const &l, Tr const &r) { \ + C v(l.size()); \ auto vi = v.begin(); auto li = l.begin(); \ while( vi != v.end() ){ \ *vi++ = *li++ O r; \ @@ -95,9 +120,10 @@ DWU_OPERATORS(|) } \ template< \ template class C, \ - typename Tl, typename A, typename Tr > \ - C operator O (Tl const &l, C const &r) { \ - C v(r.size()); \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C operator O (Tl const &l, C const &r) { \ + C v(r.size()); \ auto vi = v.begin(); auto ri = r.begin(); \ while( vi != v.end() ){ \ *vi++ = l O *ri++; \ @@ -106,10 +132,11 @@ DWU_OPERATORS(|) } \ template< \ template class C, \ - typename Tl, typename A, typename Tr > \ - C operator O (C const &l, C const &r) { \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C operator O (C const &l, C const &r) { \ auto const lsz = l.size(); auto const rsz = r.size(); \ - C v( (lsz < rsz) ? lsz : rsz ); \ + C v( (lsz < rsz) ? lsz : rsz ); \ auto vi = v.begin(); auto li = l.begin(); auto ri = r.begin(); \ while( vi != v.end() ){ \ *vi++ = *li++ O *ri++; \ @@ -118,8 +145,9 @@ DWU_OPERATORS(|) } \ template< \ template class C, \ - typename Tl, typename A, typename Tr > \ - C& operator O##= (C &l, Tr const &r){ \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C& operator O##= (C &l, Tr const &r){ \ for(auto &x:l){ x O##= r; } \ return l; \ } @@ -211,5 +239,7 @@ DWU_OPERATORS_XCXT(&) DWU_OPERATORS_XCXT(|) #undef DWU_OPERATORS_XCXT } + } + #endif/*DWU__OPERATORS__*/ -- cgit v1.2.3