forked from OctaForge/libostd
basic Pair type implementation
parent
1a2d606ae7
commit
ef47ade61f
|
@ -11,12 +11,16 @@
|
||||||
#include "octa/type_traits.h"
|
#include "octa/type_traits.h"
|
||||||
|
|
||||||
namespace octa {
|
namespace octa {
|
||||||
|
/* move */
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static inline constexpr RemoveReference<T> &&
|
static inline constexpr RemoveReference<T> &&
|
||||||
move(T &&v) noexcept {
|
move(T &&v) noexcept {
|
||||||
return static_cast<RemoveReference<T> &&>(v);
|
return static_cast<RemoveReference<T> &&>(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* forward */
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static inline constexpr T &&
|
static inline constexpr T &&
|
||||||
forward(RemoveReference<T> &v) noexcept {
|
forward(RemoveReference<T> &v) noexcept {
|
||||||
|
@ -29,8 +33,12 @@ namespace octa {
|
||||||
return static_cast<T &&>(v);
|
return static_cast<T &&>(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* declval */
|
||||||
|
|
||||||
template<typename T> AddRvalueReference<T> declval();
|
template<typename T> AddRvalueReference<T> declval();
|
||||||
|
|
||||||
|
/* swap */
|
||||||
|
|
||||||
template<typename T> void swap(T &a, T &b)
|
template<typename T> void swap(T &a, T &b)
|
||||||
noexcept(IsNothrowMoveConstructible<T>::value
|
noexcept(IsNothrowMoveConstructible<T>::value
|
||||||
&& IsNothrowMoveAssignable<T>::value) {
|
&& IsNothrowMoveAssignable<T>::value) {
|
||||||
|
@ -38,12 +46,80 @@ namespace octa {
|
||||||
a = move(b);
|
a = move(b);
|
||||||
b = move(c);
|
b = move(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N])
|
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N])
|
||||||
noexcept(noexcept(swap(*a, *b))) {
|
noexcept(noexcept(swap(*a, *b))) {
|
||||||
for (size_t i = 0; i < N; ++i) {
|
for (size_t i = 0; i < N; ++i) {
|
||||||
swap(a[i], b[i]);
|
swap(a[i], b[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* pair */
|
||||||
|
|
||||||
|
template<typename T, typename U>
|
||||||
|
struct Pair {
|
||||||
|
T first;
|
||||||
|
U second;
|
||||||
|
|
||||||
|
Pair() = default;
|
||||||
|
~Pair() = default;
|
||||||
|
|
||||||
|
Pair(const Pair &) = default;
|
||||||
|
Pair(Pair &&) = default;
|
||||||
|
|
||||||
|
Pair(const T &x, const U &y): first(x), second(y) {}
|
||||||
|
|
||||||
|
template<typename TT, typename UU>
|
||||||
|
Pair(TT &&x, UU &&y): first(forward<TT>(x)), second(forward<UU>(y)) {}
|
||||||
|
|
||||||
|
template<typename TT, typename UU>
|
||||||
|
Pair(const Pair<TT, UU> &v): first(v.first), second(v.second) {}
|
||||||
|
|
||||||
|
template<typename TT, typename UU>
|
||||||
|
Pair(Pair<TT, UU> &&v): first(move(v.first)), second(move(v.second)) {}
|
||||||
|
|
||||||
|
Pair &operator=(const Pair &v) {
|
||||||
|
first = v.first;
|
||||||
|
second = v.second;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TT, typename UU>
|
||||||
|
Pair &operator=(const Pair<TT, UU> &v) {
|
||||||
|
first = v.first;
|
||||||
|
second = v.second;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pair &operator=(Pair &&v) noexcept(
|
||||||
|
IsNothrowMoveAssignable<T>::value
|
||||||
|
&& IsNothrowMoveAssignable<U>::value
|
||||||
|
) {
|
||||||
|
first = move(v.first);
|
||||||
|
second = move(v.second);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename TT, typename UU>
|
||||||
|
Pair &operator=(Pair<TT, UU> &&v) {
|
||||||
|
first = forward<TT>(v.first);
|
||||||
|
second = forward<UU>(v.second);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(Pair &v) noexcept(
|
||||||
|
noexcept(octa::swap(first, v.first))
|
||||||
|
&& noexcept(octa::swap(second, v.second))
|
||||||
|
) {
|
||||||
|
octa::swap(first, v.first);
|
||||||
|
octa::swap(second, v.second);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, typename U>
|
||||||
|
void swap(Pair<T, U> &a, Pair<T, U> &b) noexcept(noexcept(a.swap(b))) {
|
||||||
|
a.swap(b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue