libostd/ostd/utility.hh

471 lines
12 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>
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
/* move */
2015-04-18 01:11:16 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
inline constexpr RemoveReference<T> &&move(T &&v) {
2015-06-04 23:57:06 +02:00
return static_cast<RemoveReference<T> &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-04-29 01:01:22 +02:00
2015-06-04 00:01:23 +02:00
/* forward */
2015-04-18 01:11:16 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
inline constexpr T &&forward(RemoveReference<T> &v) {
2015-06-04 23:57:06 +02:00
return static_cast<T &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename T>
inline constexpr T &&forward(RemoveReference<T> &&v) {
2015-06-04 23:57:06 +02:00
return static_cast<T &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-04-18 17:46:44 +02:00
2015-06-04 00:01:23 +02:00
/* exchange */
2015-06-02 03:01:32 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U = T>
inline T exchange(T &v, U &&nv) {
2015-06-04 23:57:06 +02:00
T old = move(v);
v = forward<U>(nv);
2015-06-04 00:01:23 +02:00
return old;
}
2015-06-02 03:01:32 +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
) {
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
) {
T c(move(a));
a = move(b);
b = 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>
inline void swap(T &a, T &b) {
detail::swap_fb(a, b);
2015-06-04 00:01:23 +02:00
}
template<typename T, Size N>
inline void swap(T (&a)[N], T (&b)[N]) {
for (Size i = 0; i < N; ++i) {
swap(a[i], b[i]);
}
}
namespace detail {
template<typename T>
inline void swap_adl(T &a, T &b) {
2015-07-13 21:07:14 +02:00
using ostd::swap;
swap(a, b);
}
2015-06-04 00:01:23 +02:00
}
/* pair */
struct PiecewiseConstruct {};
constexpr PiecewiseConstruct piecewise_construct = {};
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2015-06-04 00:01:23 +02:00
struct Pair {
2015-06-04 23:57:06 +02:00
T first;
U second;
2015-06-04 00:01:23 +02:00
Pair() = default;
~Pair() = default;
2016-06-23 20:18:35 +02:00
Pair(Pair const &) = default;
2015-06-04 00:01:23 +02:00
Pair(Pair &&) = default;
2016-06-23 20:18:35 +02:00
Pair(T const &x, U const &y): first(x), second(y) {}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair(TT &&x, UU &&y):
first(forward<TT>(x)), second(forward<UU>(y))
{}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
2016-06-23 20:18:35 +02:00
Pair(Pair<TT, UU> const &v): first(v.first), second(v.second) {}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair(Pair<TT, UU> &&v):
first(move(v.first)), second(move(v.second))
{}
2015-06-04 00:01:23 +02:00
template<typename ...A1, typename ...A2>
Pair(PiecewiseConstruct pc, Tuple<A1...> fa, Tuple<A2...> sa):
Pair(
pc, fa, sa,
detail::MakeTupleIndices<sizeof...(A1)>(),
detail::MakeTupleIndices<sizeof...(A2)>()
)
{}
2016-06-23 20:18:35 +02:00
Pair &operator=(Pair const &v) {
2015-06-04 00:01:23 +02:00
first = v.first;
second = v.second;
return *this;
2015-04-18 22:46:31 +02:00
}
2015-04-29 01:01:22 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
2016-06-23 20:18:35 +02:00
Pair &operator=(Pair<TT, UU> const &v) {
2015-06-04 00:01:23 +02:00
first = v.first;
second = v.second;
return *this;
}
2015-06-02 03:01:32 +02:00
2015-06-04 00:01:23 +02:00
Pair &operator=(Pair &&v) {
first = move(v.first);
second = move(v.second);
2015-06-04 00:01:23 +02:00
return *this;
}
2015-06-02 03:01:32 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair &operator=(Pair<TT, UU> &&v) {
first = forward<TT>(v.first);
second = forward<UU>(v.second);
2015-06-04 00:01:23 +02:00
return *this;
}
void swap(Pair &v) {
detail::swap_adl(first, v.first);
detail::swap_adl(second, v.second);
2015-06-04 00:01:23 +02:00
}
private:
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
Pair(
PiecewiseConstruct, Tuple<A1...> &fa, Tuple<A2...> &sa,
detail::TupleIndices<I1...>, detail::TupleIndices<I2...>
);
2015-06-04 00:01:23 +02:00
};
template<typename T>
struct ReferenceWrapper;
2015-06-04 00:01:23 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 00:01:23 +02:00
struct MakePairRetBase {
2015-06-08 01:55:08 +02:00
using Type = T;
2015-06-02 03:01:32 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct MakePairRetBase<ReferenceWrapper<T>> {
2015-06-08 01:55:08 +02:00
using Type = T &;
2015-06-02 03:01:32 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 00:01:23 +02:00
struct MakePairRet {
using Type = typename detail::MakePairRetBase<Decay<T>>::Type;
2015-06-02 03:01:32 +02:00
};
2015-06-04 00:01:23 +02:00
} /* namespace detail */
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
inline Pair<
typename detail::MakePairRet<T>::Type, typename detail::MakePairRet<U>::Type
> make_pair(T &&a, U &&b) {
return Pair<
typename detail::MakePairRet<T>::Type,
typename detail::MakePairRet<U>::Type
>(forward<T>(a), forward<U>(b));
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(Pair<T, U> const &x, Pair<T, U> const &y) {
return (x.first == y.first) && (x.second == y.second);
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(Pair<T, U> const &x, Pair<T, U> const &y) {
return (x.first != y.first) || (x.second != y.second);
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(Pair<T, U> const &x, Pair<T, U> const &y) {
return (x.first < y.first)
? true
: ((y.first < x.first)
? false
: ((x.second < y.second)
? true
: false));
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(Pair<T, U> const &x, Pair<T, U> const &y) {
return (y < x);
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(Pair<T, U> const &x, Pair<T, U> const &y) {
return !(y < x);
}
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(Pair<T, U> const &x, Pair<T, U> const &y) {
return !(x < y);
}
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
2016-01-19 20:14:02 +01:00
constexpr Size TupleSize<Pair<T, U>> = 2;
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
struct TupleElementBase<0, Pair<T, U>> {
using Type = T;
};
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
struct TupleElementBase<1, Pair<T, U>> {
using Type = U;
};
2015-07-11 03:38:11 +02:00
namespace detail {
template<Size>
struct GetPair;
2015-07-11 03:38:11 +02:00
template<>
struct GetPair<0> {
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
static T &get(Pair<T, U> &p) { return p.first; }
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
static T const &get(Pair<T, U> const &p) { return p.first; }
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
static T &&get(Pair<T, U> &&p) { return forward<T>(p.first); }
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
static T const &&get(Pair<T, U> const &&p) {
return forward<T const>(p.first);
}
2015-07-11 03:38:11 +02:00
};
template<>
struct GetPair<1> {
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
static U &get(Pair<T, U> &p) { return p.second; }
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
static U const &get(Pair<T, U> const &p) { return p.second; }
2015-07-11 03:38:11 +02:00
template<typename T, typename U>
static U &&get(Pair<T, U> &&p) { return forward<U>(p.second); }
template<typename T, typename U>
2016-06-23 20:18:35 +02:00
static T const &&get(Pair<T, U> const &&p) {
return forward<T const>(p.second);
}
2015-07-11 03:38:11 +02:00
};
}
template<Size I, typename T, typename U>
2016-05-04 01:50:52 +02:00
inline TupleElement<I, Pair<T, U>> &get(Pair<T, U> &p) {
2015-07-11 03:38:11 +02:00
return detail::GetPair<I>::get(p);
}
template<Size I, typename T, typename U>
2016-06-23 20:18:35 +02:00
inline TupleElement<I, Pair<T, U>> const &get(Pair<T, U> const &p) {
2015-07-11 03:38:11 +02:00
return detail::GetPair<I>::get(p);
}
template<Size I, typename T, typename U>
2016-05-04 01:50:52 +02:00
inline TupleElement<I, Pair<T, U>> &&get(Pair<T, U> &&p) {
2015-07-11 03:38:11 +02:00
return detail::GetPair<I>::get(move(p));
}
template<Size I, typename T, typename U>
2016-06-23 20:18:35 +02:00
inline TupleElement<I, Pair<T, U>> const &&get(Pair<T, U> const &&p) {
return detail::GetPair<I>::get(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):
p_first(forward<TT>(a)), p_second(forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
PiecewiseConstruct, 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):
T(forward<TT>(a)), p_second(forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
PiecewiseConstruct, 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):
U(forward<UU>(b)), p_first(forward<TT>(a))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
PiecewiseConstruct, 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):
T(forward<TT>(a)), U(forward<UU>(b))
{}
template<typename ...A1, typename ...A2, Size ...I1, Size ...I2>
CompressedPairBase(
PiecewiseConstruct, 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):
Base(forward<TT>(a), forward<UU>(b))
{}
template<typename ...A1, typename ...A2>
CompressedPair(PiecewiseConstruct 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