libostd/ostd/utility.hh

275 lines
7.7 KiB
C++
Raw Normal View History

/* Utilities for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_UTILITY_HH
#define OSTD_UTILITY_HH
#include <stddef.h>
2017-01-25 01:44:22 +01:00
#include <utility>
2015-07-13 21:08:55 +02:00
#include "ostd/type_traits.hh"
#include "ostd/internal/tuple.hh"
2015-04-18 17:46:44 +02:00
2015-07-13 21:07:14 +02:00
namespace ostd {
2015-04-29 01:01:22 +02:00
2015-06-04 00:01:23 +02:00
/* declval */
2015-04-29 01:01:22 +02:00
template<typename T>
2016-09-11 20:46:34 +02:00
AddRvalueReference<T> declval() noexcept;
2015-04-18 18:19:45 +02:00
2015-06-04 00:01:23 +02:00
/* swap */
2015-04-29 01:01:22 +02:00
2015-06-04 00:01:23 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-07-11 20:44:58 +02:00
auto test_swap(int) ->
BoolConstant<IsVoid<decltype(declval<T>().swap(declval<T &>()))>>;
2015-07-11 20:44:58 +02:00
template<typename>
False test_swap(...);
template<typename T>
inline void swap_fb(
T &a, T &b, EnableIf<decltype(test_swap<T>(0))::value, bool> = true
2016-09-11 21:17:49 +02:00
) noexcept(noexcept(a.swap(b))) {
2015-06-04 00:01:23 +02:00
a.swap(b);
}
template<typename T>
inline void swap_fb(
T &a, T &b, EnableIf<!decltype(test_swap<T>(0))::value, bool> = true
2016-09-11 21:17:49 +02:00
) noexcept(IsNothrowMoveConstructible<T> && IsNothrowMoveAssignable<T>) {
2017-01-25 01:44:22 +01:00
T c(std::move(a));
a = std::move(b);
b = std::move(c);
2015-04-18 22:46:31 +02:00
}
2015-06-04 00:01:23 +02:00
}
2015-04-29 01:01:22 +02:00
template<typename T>
2016-09-11 21:17:49 +02:00
inline void swap(T &a, T &b) noexcept(noexcept(detail::swap_fb(a, b))) {
detail::swap_fb(a, b);
2015-06-04 00:01:23 +02:00
}
template<typename T, Size N>
2016-09-11 21:17:49 +02:00
inline void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))) {
for (Size i = 0; i < N; ++i) {
swap(a[i], b[i]);
}
}
namespace detail {
2016-09-11 21:17:49 +02:00
namespace adl_swap {
2015-07-13 21:07:14 +02:00
using ostd::swap;
2016-09-11 21:17:49 +02:00
template<typename T>
inline void swap_adl(T &a, T &b) noexcept(noexcept(swap(a, b))) {
swap(a, b);
}
}
template<typename T>
inline void swap_adl(T &a, T &b) noexcept(noexcept(adl_swap::swap_adl(a, b))) {
adl_swap::swap_adl(a, b);
}
2015-06-04 00:01:23 +02:00
}
/* pair */
template<typename T, typename U>
constexpr Size TupleSize<std::pair<T, U>> = 2;
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
struct TupleElementBase<0, std::pair<T, U>> {
using Type = T;
};
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
struct TupleElementBase<1, std::pair<T, U>> {
using Type = U;
};
2015-07-11 03:38:11 +02:00
template<Size I, typename T, typename U>
inline TupleElement<I, std::pair<T, U>> &get(std::pair<T, U> &p) noexcept {
return std::get<I>(p);
2015-07-11 03:38:11 +02:00
}
template<Size I, typename T, typename U>
inline TupleElement<I, std::pair<T, U>> const &get(std::pair<T, U> const &p) noexcept {
return std::get<I>(p);
2015-07-11 03:38:11 +02:00
}
template<Size I, typename T, typename U>
inline TupleElement<I, std::pair<T, U>> &&get(std::pair<T, U> &&p) noexcept {
return std::get<I>(std::move(p));
2015-07-11 03:38:11 +02:00
}
template<Size I, typename T, typename U>
inline TupleElement<I, std::pair<T, U>> const &&get(std::pair<T, U> const &&p) noexcept {
return std::get<I>(std::move(p));
}
namespace detail {
template<typename T, typename U,
2016-01-12 23:24:40 +01:00
bool = IsSame<RemoveCv<T>, RemoveCv<U>>,
bool = IsEmpty<T>, bool = IsEmpty<U>
>
constexpr Size CompressedPairSwitch = detail::Undefined<T>();
/* neither empty */
template<typename T, typename U, bool Same>
2016-01-21 18:58:51 +01:00
constexpr Size CompressedPairSwitch<T, U, Same, false, false> = 0;
/* first empty */
template<typename T, typename U, bool Same>
2016-01-21 18:58:51 +01:00
constexpr Size CompressedPairSwitch<T, U, Same, true, false> = 1;
/* second empty */
template<typename T, typename U, bool Same>
2016-01-21 18:58:51 +01:00
constexpr Size CompressedPairSwitch<T, U, Same, false, true> = 2;
/* both empty, not the same */
template<typename T, typename U>
2016-01-21 18:58:51 +01:00
constexpr Size CompressedPairSwitch<T, U, false, true, true> = 3;
/* both empty and same */
template<typename T, typename U>
2016-01-21 18:58:51 +01:00
constexpr Size CompressedPairSwitch<T, U, true, true, true> = 1;
2016-01-21 18:58:51 +01:00
template<typename T, typename U, Size = CompressedPairSwitch<T, U>>
struct CompressedPairBase;
template<typename T, typename U>
struct CompressedPairBase<T, U, 0> {
T p_first;
U p_second;
2015-06-13 17:36:47 +02:00
template<typename TT, typename UU>
CompressedPairBase(TT &&a, UU &&b):
2017-01-25 01:44:22 +01:00
p_first(std::forward<TT>(a)), p_second(std::forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
std::piecewise_construct_t, Tuple<A1...> &fa, Tuple<A2...> &sa,
detail::TupleIndices<I1...>, detail::TupleIndices<I2...>
);
T &first() { return p_first; }
2016-06-23 20:18:35 +02:00
T const &first() const { return p_first; }
U &second() { return p_second; }
2016-06-23 20:18:35 +02:00
U const &second() const { return p_second; }
void swap(CompressedPairBase &v) {
swap_adl(p_first, v.p_first);
swap_adl(p_second, v.p_second);
}
};
template<typename T, typename U>
struct CompressedPairBase<T, U, 1>: T {
U p_second;
2015-06-13 17:36:47 +02:00
template<typename TT, typename UU>
CompressedPairBase(TT &&a, UU &&b):
2017-01-25 01:44:22 +01:00
T(std::forward<TT>(a)), p_second(std::forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
std::piecewise_construct_t, Tuple<A1...> &fa, Tuple<A2...> &sa,
detail::TupleIndices<I1...>, detail::TupleIndices<I2...>
);
T &first() { return *this; }
2016-06-23 20:18:35 +02:00
T const &first() const { return *this; }
U &second() { return p_second; }
2016-06-23 20:18:35 +02:00
U const &second() const { return p_second; }
void swap(CompressedPairBase &v) {
swap_adl(p_second, v.p_second);
}
};
template<typename T, typename U>
struct CompressedPairBase<T, U, 2>: U {
T p_first;
2015-06-13 17:36:47 +02:00
template<typename TT, typename UU>
CompressedPairBase(TT &&a, UU &&b):
2017-01-25 01:44:22 +01:00
U(std::forward<UU>(b)), p_first(std::forward<TT>(a))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
std::piecewise_construct_t, Tuple<A1...> &fa, Tuple<A2...> &sa,
detail::TupleIndices<I1...>, detail::TupleIndices<I2...>
);
T &first() { return p_first; }
2016-06-23 20:18:35 +02:00
T const &first() const { return p_first; }
U &second() { return *this; }
2016-06-23 20:18:35 +02:00
U const &second() const { return *this; }
void swap(CompressedPairBase &v) {
swap_adl(p_first, v.p_first);
}
};
template<typename T, typename U>
struct CompressedPairBase<T, U, 3>: T, U {
2015-06-13 17:36:47 +02:00
template<typename TT, typename UU>
CompressedPairBase(TT &&a, UU &&b):
2017-01-25 01:44:22 +01:00
T(std::forward<TT>(a)), U(std::forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
std::piecewise_construct_t, Tuple<A1...> &fa, Tuple<A2...> &sa,
detail::TupleIndices<I1...>, detail::TupleIndices<I2...>
);
T &first() { return *this; }
2016-06-23 20:18:35 +02:00
T const &first() const { return *this; }
U &second() { return *this; }
2016-06-23 20:18:35 +02:00
U const &second() const { return *this; }
void swap(CompressedPairBase &) {}
};
template<typename T, typename U>
struct CompressedPair: CompressedPairBase<T, U> {
2015-06-14 03:36:20 +02:00
using Base = CompressedPairBase<T, U>;
2015-06-13 17:36:47 +02:00
template<typename TT, typename UU>
CompressedPair(TT &&a, UU &&b):
2017-01-25 01:44:22 +01:00
Base(std::forward<TT>(a), std::forward<UU>(b))
{}
template<typename ...A1, typename ...A2>
CompressedPair(std::piecewise_construct_t pc, Tuple<A1...> fa, Tuple<A2...> sa):
Base(
pc, fa, sa,
detail::MakeTupleIndices<sizeof...(A1)>(),
detail::MakeTupleIndices<sizeof...(A2)>()
)
{}
T &first() { return Base::first(); }
2016-06-23 20:18:35 +02:00
T const &first() const { return Base::first(); }
U &second() { return Base::second(); }
2016-06-23 20:18:35 +02:00
U const &second() const { return Base::second(); }
void swap(CompressedPair &v) {
Base::swap(v);
}
};
} /* namespace detail */
2015-07-13 21:07:14 +02:00
} /* namespace ostd */
2015-06-04 00:01:23 +02:00
2016-02-07 22:17:15 +01:00
#endif