diff options
author | Wolfgang Draxinger <dw@optores.de> | 2019-07-19 15:21:19 +0200 |
---|---|---|
committer | Wolfgang Draxinger <dw@optores.de> | 2019-07-19 15:21:19 +0200 |
commit | 776b1592e3d67f7a1af94180a0b4c3fcf5320f96 (patch) | |
tree | b6dafc584413a2d1031e669cefe80869dbbe476b | |
parent | 64bd8f2702abbd1758eeab499139e2eb0ef820ab (diff) | |
download | dwu-776b1592e3d67f7a1af94180a0b4c3fcf5320f96.tar.gz dwu-776b1592e3d67f7a1af94180a0b4c3fcf5320f96.tar.bz2 |
same container, mixed type required per type allocator template parameters
-rw-r--r-- | example/Makefile | 8 | ||||
-rw-r--r-- | example/operators.cc | 33 | ||||
-rw-r--r-- | include/dwu/operators | 52 |
3 files changed, 74 insertions, 19 deletions
diff --git a/example/Makefile b/example/Makefile index c96a553..0e50a44 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,8 +1,14 @@ CXXFLAGS+=-Wall -Werror -I../include -.PHONY: all +.PHONY: all clean all: operators +clean: + -rm operators + -rm operators.o + operators: CC=$(CXX) operators: operators.o + +operators.o: operators.cc ../include/dwu/operators Makefile diff --git a/example/operators.cc b/example/operators.cc index 14fa00b..68033dc 100644 --- a/example/operators.cc +++ b/example/operators.cc @@ -5,22 +5,20 @@ #include <vector> #include <list> - template<class S, class T> static void dump(S const &s, T const &v) { std::cout << s << '{'; - for( auto const &x : v ){ - std::cout << x << ", "; - } + for( auto const &x : v ){ std::cout << x << ", "; } std::cout << '}' << std::endl; } int main(int argc, char *argv[]) { + // syntactic sugar operators for same container, same type using namespace dwu::operators; - std::vector<int> va = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + std::vector<int> va = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; dump("va: ", va); auto vb = va + 10; @@ -37,12 +35,33 @@ int main(int argc, char *argv[]) dump("vd = vb * vc: ", vd); { + // syntactic sugar for mixed container, mixed type using namespace dwu::operators_xcxt; std::list<float> la = {100, 101, 101, 103, 104, 105}; dump("la: ", la); - auto rr = la - vd; - dump("rr = la - vd: ", rr); + auto r_ad = la - vd; + dump("r_ad = la - vd: ", r_ad); + } + + { + // syntactic sugar for mixed container, same type + using namespace dwu::operators_xc; + std::list<int> lb = {200, 201, 201, 203, 204, 205}; + dump("lb: ", lb); + + auto r_bd = lb + vd; + dump("r_bd = lb + vd: ", r_bd); + } + + { + // syntactic sugar for same container, mixed type + using namespace dwu::operators_xt; + std::vector<float> ve = {300, 301, 301, 303, 304, 305}; + dump("ve: ", ve); + + auto r_ed = ve / vd; + dump("r_ed = ve / vd: ", r_ed); } return 0; 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<T,A> v( (lsz < rsz) ? lsz : rsz ); + +This is used instead of <algorithm> container for two reasons: + + 1. It avoids inclusion of a header. Although <algorithm> 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<typename,typename> class C, \ - typename Tl, typename A, typename Tr > \ - C<Tl,A> operator O (C<Tl,A> const &l, Tr const &r) { \ - C<Tl,A> v(l.size()); \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C<Tl,Al> operator O (C<Tl,Al> const &l, Tr const &r) { \ + C<Tl,Al> 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<typename,typename> class C, \ - typename Tl, typename A, typename Tr > \ - C<Tr,A> operator O (Tl const &l, C<Tr,A> const &r) { \ - C<Tr,A> v(r.size()); \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C<Tr,Ar> operator O (Tl const &l, C<Tr,Ar> const &r) { \ + C<Tr,Ar> 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<typename,typename> class C, \ - typename Tl, typename A, typename Tr > \ - C<Tl,A> operator O (C<Tl,A> const &l, C<Tr,A> const &r) { \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C<Tl,Al> operator O (C<Tl,Al> const &l, C<Tr,Ar> const &r) { \ auto const lsz = l.size(); auto const rsz = r.size(); \ - C<Tl,A> v( (lsz < rsz) ? lsz : rsz ); \ + C<Tl,Al> 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<typename,typename> class C, \ - typename Tl, typename A, typename Tr > \ - C<Tl,A>& operator O##= (C<Tl,A> &l, Tr const &r){ \ + typename Tl, typename Al, \ + typename Tr, typename Ar > \ + C<Tl,Al>& operator O##= (C<Tl,Al> &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__*/ |