From 211961cc31a3045e503bd6e89f8390471cadc700 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 11 Sep 2016 14:08:56 +0200 Subject: [PATCH] implement piecewise construction for Pair/CompressedPair --- ostd/tuple.hh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ ostd/utility.hh | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/ostd/tuple.hh b/ostd/tuple.hh index 31477ff..e498b5a 100644 --- a/ostd/tuple.hh +++ b/ostd/tuple.hh @@ -634,6 +634,59 @@ inline bool operator>=(Tuple const &x, Tuple const &y) { template constexpr bool UsesAllocator, A> = true; +/* piecewise pair stuff */ + +template +template +Pair::Pair( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices +): + first(forward(get(fa))...), second(forward(get(sa))...) +{} + +namespace detail { + template + template + CompressedPairBase::CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ): + p_first(forward(get(fa))...), + p_second(forward(get(sa))...) + {} + + template + template + CompressedPairBase::CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ): + T(forward(get(fa))...), + p_second(forward(get(sa))...) + {} + + template + template + CompressedPairBase::CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ): + U(forward(get(sa))...), + p_first(forward(get(fa))...) + {} + + template + template + CompressedPairBase::CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ): + T(forward(get(fa))...), + U(forward(get(sa))...) + {} +} /* namespace detail */ + } /* namespace ostd */ #endif diff --git a/ostd/utility.hh b/ostd/utility.hh index be4ba50..b592b46 100644 --- a/ostd/utility.hh +++ b/ostd/utility.hh @@ -94,6 +94,9 @@ namespace detail { /* pair */ +struct PiecewiseConstruct {}; +constexpr PiecewiseConstruct piecewise_construct = {}; + template struct Pair { T first; @@ -120,6 +123,15 @@ struct Pair { first(move(v.first)), second(move(v.second)) {} + template + Pair(PiecewiseConstruct pc, Tuple fa, Tuple sa): + Pair( + pc, fa, sa, + detail::MakeTupleIndices(), + detail::MakeTupleIndices() + ) + {} + Pair &operator=(Pair const &v) { first = v.first; second = v.second; @@ -150,6 +162,13 @@ struct Pair { detail::swap_adl(first, v.first); detail::swap_adl(second, v.second); } + +private: + template + Pair( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ); }; template @@ -324,6 +343,12 @@ namespace detail { p_first(forward(a)), p_second(forward(b)) {} + template + CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ); + T &first() { return p_first; } T const &first() const { return p_first; } @@ -345,6 +370,12 @@ namespace detail { T(forward(a)), p_second(forward(b)) {} + template + CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ); + T &first() { return *this; } T const &first() const { return *this; } @@ -365,6 +396,12 @@ namespace detail { U(forward(b)), p_first(forward(a)) {} + template + CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ); + T &first() { return p_first; } T const &first() const { return p_first; } @@ -383,6 +420,12 @@ namespace detail { T(forward(a)), U(forward(b)) {} + template + CompressedPairBase( + PiecewiseConstruct, Tuple &fa, Tuple &sa, + detail::TupleIndices, detail::TupleIndices + ); + T &first() { return *this; } T const &first() const { return *this; } @@ -401,6 +444,15 @@ namespace detail { Base(forward(a), forward(b)) {} + template + CompressedPair(PiecewiseConstruct pc, Tuple fa, Tuple sa): + Base( + pc, fa, sa, + detail::MakeTupleIndices(), + detail::MakeTupleIndices() + ) + {} + T &first() { return Base::first(); } T const &first() const { return Base::first(); }