libostd/ostd/utility.hh

400 lines
10 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
2015-06-04 23:57:06 +02:00
template<typename T> AddRvalueReference<T> declval();
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<
2015-07-11 20:44:58 +02:00
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<
2015-07-11 20:44:58 +02:00
!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 */
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;
Pair(const Pair &) = default;
Pair(Pair &&) = default;
2015-06-04 23:57:06 +02:00
Pair(const T &x, const U &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>
Pair(const Pair<TT, UU> &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
Pair &operator=(const Pair &v) {
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>
Pair &operator=(const Pair<TT, UU> &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
}
};
2015-06-04 23:57:06 +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
2015-06-04 23:57:06 +02:00
> make_pair(T &&a, U &&b) {
return Pair<typename detail::MakePairRet<T>::Type,
typename detail::MakePairRet<U>::Type
2015-06-04 23:57:06 +02:00
>(forward<T>(a), forward<U>(b));;
}
template<typename T, typename U>
inline constexpr bool operator==(const Pair<T, U> &x, const Pair<T, U> &y) {
return (x.first == y.first) && (x.second == y.second);
}
template<typename T, typename U>
inline constexpr bool operator!=(const Pair<T, U> &x, const Pair<T, U> &y) {
return (x.first != y.first) || (x.second != y.second);
}
template<typename T, typename U>
inline constexpr bool operator<(const Pair<T, U> &x, const Pair<T, U> &y) {
return (x.first < y.first)
? true
: ((y.first < x.first)
? false
: ((x.second < y.second)
? true
: false));
}
template<typename T, typename U>
inline constexpr bool operator>(const Pair<T, U> &x, const Pair<T, U> &y) {
return (y < x);
}
template<typename T, typename U>
inline constexpr bool operator<=(const Pair<T, U> &x, const Pair<T, U> &y) {
return !(y < x);
}
template<typename T, typename U>
inline constexpr bool operator>=(const Pair<T, U> &x, const Pair<T, U> &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 {
2015-07-11 03:38:11 +02:00
template<Size> struct GetPair;
template<> struct GetPair<0> {
template<typename T, typename U>
static T &get(Pair<T, U> &p) { return p.first; }
template<typename T, typename U>
static const T &get(const Pair<T, U> &p) { return p.first; }
template<typename T, typename U>
static T &&get(Pair<T, U> &&p) { return forward<T>(p.first); }
template<typename T, typename U>
static const T &&get(const Pair<T, U> &&p) {
return forward<const T>(p.first);
}
2015-07-11 03:38:11 +02:00
};
template<> struct GetPair<1> {
template<typename T, typename U>
static U &get(Pair<T, U> &p) { return p.second; }
template<typename T, typename U>
static const U &get(const Pair<T, U> &p) { return p.second; }
template<typename T, typename U>
static U &&get(Pair<T, U> &&p) { return forward<U>(p.second); }
template<typename T, typename U>
static const T &&get(const Pair<T, U> &&p) {
return forward<const T>(p.second);
}
2015-07-11 03:38:11 +02:00
};
}
template<Size I, typename T, typename U>
TupleElement<I, Pair<T, U>> &get(Pair<T, U> &p) {
return detail::GetPair<I>::get(p);
}
template<Size I, typename T, typename U>
const TupleElement<I, Pair<T, U>> &get(const Pair<T, U> &p) {
return detail::GetPair<I>::get(p);
}
template<Size I, typename T, typename U>
TupleElement<I, Pair<T, U>> &&get(Pair<T, U> &&p) {
return detail::GetPair<I>::get(move(p));
}
template<Size I, typename T, typename U>
const TupleElement<I, Pair<T, U>> &&get(const Pair<T, U> &&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>
2016-01-21 18:58:51 +01:00
> 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)) {}
T &first() { return p_first; }
const T &first() const { return p_first; }
U &second() { return p_second; }
const U &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)) {}
T &first() { return *this; }
const T &first() const { return *this; }
U &second() { return p_second; }
const U &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)) {}
T &first() { return p_first; }
const T &first() const { return p_first; }
U &second() { return *this; }
const U &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)) {}
T &first() { return *this; }
const T &first() const { return *this; }
U &second() { return *this; }
const U &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)) {}
T &first() { return Base::first(); }
const T &first() const { return Base::first(); }
U &second() { return Base::second(); }
const U &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