2015-04-15 21:38:15 +00:00
|
|
|
/* Utilities for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OCTA_UTILITY_H
|
|
|
|
#define OCTA_UTILITY_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2015-04-25 15:13:21 +00:00
|
|
|
#include "octa/type_traits.h"
|
2015-04-18 15:46:44 +00:00
|
|
|
|
2015-04-23 19:53:05 +00:00
|
|
|
namespace octa {
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
/* move */
|
2015-04-17 23:11:16 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline constexpr RemoveReference<T> &&move(T &&v) {
|
|
|
|
return static_cast<RemoveReference<T> &&>(v);
|
2015-06-03 22:01:23 +00:00
|
|
|
}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
/* forward */
|
2015-04-17 23:11:16 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline constexpr T &&forward(RemoveReference<T> &v) {
|
|
|
|
return static_cast<T &&>(v);
|
2015-06-03 22:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline constexpr T &&forward(RemoveReference<T> &&v) {
|
|
|
|
return static_cast<T &&>(v);
|
2015-06-03 22:01:23 +00:00
|
|
|
}
|
2015-04-18 15:46:44 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
/* exchange */
|
2015-06-02 01:01:32 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T, typename U = T>
|
|
|
|
T exchange(T &v, U &&nv) {
|
|
|
|
T old = move(v);
|
|
|
|
v = forward<U>(nv);
|
2015-06-03 22:01:23 +00:00
|
|
|
return old;
|
|
|
|
}
|
2015-06-02 01:01:32 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
/* declval */
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T> AddRvalueReference<T> declval();
|
2015-04-18 16:19:45 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
/* swap */
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
namespace detail {
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
2015-06-03 22:01:23 +00:00
|
|
|
struct SwapTest {
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename U, void (U::*)(U &)> struct Test {};
|
|
|
|
template<typename U> static char test(Test<U, &U::swap> *);
|
|
|
|
template<typename U> static int test(...);
|
|
|
|
static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));
|
2015-05-26 20:28:12 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T> inline void swap(T &a, T &b, EnableIf<
|
|
|
|
octa::detail::SwapTest<T>::value, bool
|
2015-05-26 20:28:12 +00:00
|
|
|
> = true) {
|
2015-06-03 22:01:23 +00:00
|
|
|
a.swap(b);
|
2015-05-26 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T> inline void swap(T &a, T &b, EnableIf<
|
|
|
|
!octa::detail::SwapTest<T>::value, bool
|
2015-05-26 20:28:12 +00:00
|
|
|
> = true) {
|
2015-06-04 21:57:06 +00:00
|
|
|
T c(octa::move(a));
|
2015-06-03 22:01:23 +00:00
|
|
|
a = octa::move(b);
|
|
|
|
b = octa::move(c);
|
2015-04-18 20:46:31 +00:00
|
|
|
}
|
2015-06-03 22:01:23 +00:00
|
|
|
}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T> void swap(T &a, T &b) {
|
2015-06-03 22:01:23 +00:00
|
|
|
octa::detail::swap(a, b);
|
|
|
|
}
|
|
|
|
|
2015-06-08 20:20:12 +00:00
|
|
|
template<typename T, octa::Size N> void swap(T (&a)[N], T (&b)[N]) {
|
|
|
|
for (octa::Size i = 0; i < N; ++i) {
|
2015-06-03 22:01:23 +00:00
|
|
|
octa::swap(a[i], b[i]);
|
2015-05-26 20:28:12 +00:00
|
|
|
}
|
2015-06-03 22:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pair */
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T, typename U>
|
2015-06-03 22:01:23 +00:00
|
|
|
struct Pair {
|
2015-06-04 21:57:06 +00:00
|
|
|
T first;
|
|
|
|
U second;
|
2015-05-26 20:28:12 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
Pair() = default;
|
|
|
|
~Pair() = default;
|
|
|
|
|
|
|
|
Pair(const Pair &) = default;
|
|
|
|
Pair(Pair &&) = default;
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
Pair(const T &x, const U &y): first(x), second(y) {}
|
2015-06-03 22:01:23 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
Pair(TT &&x, UU &&y):
|
|
|
|
first(octa::forward<TT>(x)), second(octa::forward<UU>(y)) {}
|
2015-06-03 22:01:23 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
Pair(const Pair<TT, UU> &v): first(v.first), second(v.second) {}
|
2015-06-03 22:01:23 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
Pair(Pair<TT, UU> &&v):
|
2015-06-03 22:01:23 +00:00
|
|
|
first(octa::move(v.first)), second(octa::move(v.second)) {}
|
|
|
|
|
|
|
|
Pair &operator=(const Pair &v) {
|
|
|
|
first = v.first;
|
|
|
|
second = v.second;
|
|
|
|
return *this;
|
2015-04-18 20:46:31 +00:00
|
|
|
}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
Pair &operator=(const Pair<TT, UU> &v) {
|
2015-06-03 22:01:23 +00:00
|
|
|
first = v.first;
|
|
|
|
second = v.second;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-02 01:01:32 +00:00
|
|
|
|
2015-06-03 22:01:23 +00:00
|
|
|
Pair &operator=(Pair &&v) {
|
|
|
|
first = octa::move(v.first);
|
|
|
|
second = octa::move(v.second);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-02 01:01:32 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
Pair &operator=(Pair<TT, UU> &&v) {
|
|
|
|
first = octa::forward<TT>(v.first);
|
|
|
|
second = octa::forward<UU>(v.second);
|
2015-06-03 22:01:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(Pair &v) {
|
|
|
|
octa::swap(first, v.first);
|
|
|
|
octa::swap(second, v.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T> struct ReferenceWrapper;
|
2015-06-03 22:01:23 +00:00
|
|
|
|
|
|
|
namespace detail {
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
2015-06-03 22:01:23 +00:00
|
|
|
struct MakePairRetBase {
|
2015-06-07 23:55:08 +00:00
|
|
|
using Type = T;
|
2015-06-02 01:01:32 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
|
|
|
struct MakePairRetBase<ReferenceWrapper<T>> {
|
2015-06-07 23:55:08 +00:00
|
|
|
using Type = T &;
|
2015-06-02 01:01:32 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T>
|
2015-06-03 22:01:23 +00:00
|
|
|
struct MakePairRet {
|
2015-06-07 23:55:08 +00:00
|
|
|
using Type = typename octa::detail::MakePairRetBase<octa::Decay<T>>::Type;
|
2015-06-02 01:01:32 +00:00
|
|
|
};
|
2015-06-03 22:01:23 +00:00
|
|
|
} /* namespace detail */
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
Pair<typename octa::detail::MakePairRet<T>::Type,
|
|
|
|
typename octa::detail::MakePairRet<U>::Type
|
|
|
|
> make_pair(T &&a, U &&b) {
|
|
|
|
return Pair<typename octa::detail::MakePairRet<T>::Type,
|
|
|
|
typename octa::detail::MakePairRet<U>::Type
|
|
|
|
>(forward<T>(a), forward<U>(b));;
|
2015-04-15 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 15:32:03 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename U,
|
|
|
|
bool = octa::IsSame<octa::RemoveCv<T>, octa::RemoveCv<U>>::value,
|
|
|
|
bool = octa::IsEmpty<T>::value,
|
|
|
|
bool = octa::IsEmpty<U>::value
|
|
|
|
> struct CompressedPairSwitch;
|
|
|
|
|
|
|
|
/* neither empty */
|
|
|
|
template<typename T, typename U, bool Same>
|
|
|
|
struct CompressedPairSwitch<T, U, Same, false, false> { enum { value = 0 }; };
|
|
|
|
|
|
|
|
/* first empty */
|
|
|
|
template<typename T, typename U, bool Same>
|
|
|
|
struct CompressedPairSwitch<T, U, Same, true, false> { enum { value = 1 }; };
|
|
|
|
|
|
|
|
/* second empty */
|
|
|
|
template<typename T, typename U, bool Same>
|
|
|
|
struct CompressedPairSwitch<T, U, Same, false, true> { enum { value = 2 }; };
|
|
|
|
|
|
|
|
/* both empty, not the same */
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairSwitch<T, U, false, true, true> { enum { value = 3 }; };
|
|
|
|
|
|
|
|
/* both empty and same */
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairSwitch<T, U, true, true, true> { enum { value = 1 }; };
|
|
|
|
|
|
|
|
template<typename T, typename U, octa::Size = CompressedPairSwitch<T, U>::value>
|
|
|
|
struct CompressedPairBase;
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairBase<T, U, 0> {
|
|
|
|
T p_first;
|
|
|
|
U p_second;
|
|
|
|
|
2015-06-13 15:36:47 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
CompressedPairBase(TT &&a, UU &&b): p_first(octa::forward<TT>(a)),
|
|
|
|
p_second(octa::forward<UU>(b)) {}
|
2015-06-13 15:32:03 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
octa::swap(p_first, v.p_first);
|
|
|
|
octa::swap(p_second, v.p_second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairBase<T, U, 1>: T {
|
|
|
|
U p_second;
|
|
|
|
|
2015-06-13 15:36:47 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
CompressedPairBase(TT &&a, UU &&b): T(octa::forward<TT>(a)),
|
|
|
|
p_second(octa::forward<UU>(b)) {}
|
2015-06-13 15:32:03 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
octa::swap(p_second, v.p_second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairBase<T, U, 2>: U {
|
|
|
|
T p_first;
|
|
|
|
|
2015-06-13 15:36:47 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
CompressedPairBase(TT &&a, UU &&b): U(octa::forward<UU>(b)),
|
|
|
|
p_first(octa::forward<TT>(a)) {}
|
2015-06-13 15:32:03 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
octa::swap(p_first, v.p_first);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct CompressedPairBase<T, U, 3>: T, U {
|
2015-06-13 15:36:47 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
CompressedPairBase(TT &&a, UU &&b): T(octa::forward<TT>(a)),
|
|
|
|
U(octa::forward<UU>(b)) {}
|
2015-06-13 15:32:03 +00:00
|
|
|
|
|
|
|
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 01:36:20 +00:00
|
|
|
using Base = CompressedPairBase<T, U>;
|
2015-06-13 15:32:03 +00:00
|
|
|
|
2015-06-13 15:36:47 +00:00
|
|
|
template<typename TT, typename UU>
|
|
|
|
CompressedPair(TT &&a, UU &&b): Base(octa::forward<TT>(a),
|
|
|
|
octa::forward<UU>(b)) {}
|
2015-06-13 15:32:03 +00:00
|
|
|
|
|
|
|
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-06-03 22:01:23 +00:00
|
|
|
} /* namespace octa */
|
|
|
|
|
2015-04-15 21:38:15 +00:00
|
|
|
#endif
|