use standard swap
parent
a8f7122d45
commit
2c7f98f57e
|
@ -24,7 +24,8 @@ inline R partition(R range, U pred) {
|
|||
R ret = range;
|
||||
for (; !range.empty(); range.pop_front()) {
|
||||
if (pred(range.front())) {
|
||||
detail::swap_adl(range.front(), ret.front());
|
||||
using std::swap;
|
||||
swap(range.front(), ret.front());
|
||||
ret.pop_front();
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +93,8 @@ namespace detail {
|
|||
sw = ch + 1;
|
||||
}
|
||||
if (sw != r) {
|
||||
detail::swap_adl(range[r], range[sw]);
|
||||
using std::swap;
|
||||
swap(range[r], range[sw]);
|
||||
r = sw;
|
||||
} else {
|
||||
return;
|
||||
|
@ -112,7 +114,8 @@ namespace detail {
|
|||
}
|
||||
RangeSize<R> e = len - 1;
|
||||
while (e > 0) {
|
||||
detail::swap_adl(range[e], range[0]);
|
||||
using std::swap;
|
||||
swap(range[e], range[0]);
|
||||
--e;
|
||||
detail::hs_sift_down(range, 0, e, compare);
|
||||
}
|
||||
|
@ -120,6 +123,7 @@ namespace detail {
|
|||
|
||||
template<typename R, typename C>
|
||||
static void introloop(R range, C &compare, RangeSize<R> depth) {
|
||||
using std::swap;
|
||||
if (range.size() <= 10) {
|
||||
detail::insort(range, compare);
|
||||
return;
|
||||
|
@ -128,16 +132,16 @@ namespace detail {
|
|||
detail::heapsort(range, compare);
|
||||
return;
|
||||
}
|
||||
detail::swap_adl(range[range.size() / 2], range.back());
|
||||
swap(range[range.size() / 2], range.back());
|
||||
RangeSize<R> pi = 0;
|
||||
R pr = range;
|
||||
pr.pop_back();
|
||||
for (; !pr.empty(); pr.pop_front()) {
|
||||
if (compare(pr.front(), range.back())) {
|
||||
detail::swap_adl(pr.front(), range[pi++]);
|
||||
swap(pr.front(), range[pi++]);
|
||||
}
|
||||
}
|
||||
detail::swap_adl(range[pi], range.back());
|
||||
swap(range[pi], range.back());
|
||||
detail::introloop(range.slice(0, pi), compare, depth - 1);
|
||||
detail::introloop(
|
||||
range.slice(pi + 1, range.size()), compare, depth - 1
|
||||
|
@ -656,7 +660,8 @@ inline R2 move(R1 irange, R2 orange) {
|
|||
template<typename R>
|
||||
inline void reverse(R range) {
|
||||
while (!range.empty()) {
|
||||
detail::swap_adl(range.front(), range.back());
|
||||
using std::swap;
|
||||
swap(range.front(), range.back());
|
||||
range.pop_front();
|
||||
range.pop_back();
|
||||
}
|
||||
|
@ -687,7 +692,8 @@ inline void generate(R range, F gen) {
|
|||
template<typename R1, typename R2>
|
||||
inline std::pair<R1, R2> swap_ranges(R1 range1, R2 range2) {
|
||||
while (!range1.empty() && !range2.empty()) {
|
||||
detail::swap_adl(range1.front(), range2.front());
|
||||
using std::swap;
|
||||
swap(range1.front(), range2.front());
|
||||
range1.pop_front();
|
||||
range2.pop_front();
|
||||
}
|
||||
|
|
|
@ -114,9 +114,10 @@ namespace detail {
|
|||
}
|
||||
|
||||
void swap(SignalBase &ev) {
|
||||
detail::swap_adl(p_class, ev.p_class);
|
||||
detail::swap_adl(p_funcs, ev.p_funcs);
|
||||
detail::swap_adl(p_nfuncs, ev.p_nfuncs);
|
||||
using std::swap;
|
||||
swap(p_class, ev.p_class);
|
||||
swap(p_funcs, ev.p_funcs);
|
||||
swap(p_nfuncs, ev.p_nfuncs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -204,6 +205,11 @@ public:
|
|||
void swap(Signal &ev) { p_base.swap(ev.p_base); }
|
||||
};
|
||||
|
||||
template<typename C, typename ...A>
|
||||
inline void swap(Signal<C, A...> &a, Signal<C, A...> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
} /* namespace ostd */
|
||||
|
||||
#endif
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "ostd/types.hh"
|
||||
#include "ostd/range.hh"
|
||||
#include "ostd/vector.hh"
|
||||
|
@ -125,13 +127,14 @@ struct FileInfo {
|
|||
time_t ctime() const { return p_ctime; }
|
||||
|
||||
void swap(FileInfo &i) {
|
||||
detail::swap_adl(i.p_slash, p_slash);
|
||||
detail::swap_adl(i.p_dot, p_dot);
|
||||
detail::swap_adl(i.p_type, p_type);
|
||||
detail::swap_adl(i.p_path, p_path);
|
||||
detail::swap_adl(i.p_atime, p_atime);
|
||||
detail::swap_adl(i.p_mtime, p_mtime);
|
||||
detail::swap_adl(i.p_ctime, p_ctime);
|
||||
using std::swap;
|
||||
swap(i.p_slash, p_slash);
|
||||
swap(i.p_dot, p_dot);
|
||||
swap(i.p_type, p_type);
|
||||
swap(i.p_path, p_path);
|
||||
swap(i.p_atime, p_atime);
|
||||
swap(i.p_mtime, p_mtime);
|
||||
swap(i.p_ctime, p_ctime);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -219,6 +222,10 @@ private:
|
|||
time_t p_atime = 0, p_mtime = 0, p_ctime = 0;
|
||||
};
|
||||
|
||||
inline void swap(FileInfo &a, FileInfo &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
struct DirectoryRange;
|
||||
|
||||
#ifndef OSTD_PLATFORM_WIN32
|
||||
|
@ -312,9 +319,10 @@ struct DirectoryStream {
|
|||
}
|
||||
|
||||
void swap(DirectoryStream &s) {
|
||||
detail::swap_adl(p_d, s.p_d);
|
||||
detail::swap_adl(p_de, s.p_de);
|
||||
detail::swap_adl(p_path, s.p_path);
|
||||
using std::swap;
|
||||
swap(p_d, s.p_d);
|
||||
swap(p_de, s.p_de);
|
||||
swap(p_path, s.p_path);
|
||||
}
|
||||
|
||||
DirectoryRange iter();
|
||||
|
@ -482,9 +490,10 @@ struct DirectoryStream {
|
|||
}
|
||||
|
||||
void swap(DirectoryStream &s) {
|
||||
detail::swap_adl(p_handle, s.p_handle);
|
||||
detail::swap_adl(p_data, s.p_data);
|
||||
detail::swap_adl(p_path, s.p_path);
|
||||
using std::swap;
|
||||
swap(p_handle, s.p_handle);
|
||||
swap(p_data, s.p_data);
|
||||
swap(p_path, s.p_path);
|
||||
}
|
||||
|
||||
DirectoryRange iter();
|
||||
|
@ -517,6 +526,10 @@ private:
|
|||
};
|
||||
#endif /* OSTD_PLATFORM_WIN32 */
|
||||
|
||||
inline void swap(DirectoryStream &a, DirectoryStream &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
struct DirectoryRange: InputRange<
|
||||
DirectoryRange, InputRangeTag, FileInfo, FileInfo, Size, long
|
||||
> {
|
||||
|
|
|
@ -841,10 +841,16 @@ void Function<R(Args...)>::swap(Function &f) noexcept {
|
|||
f.p_f = p_f;
|
||||
p_f = as_base(&p_buf);
|
||||
} else {
|
||||
detail::swap_adl(p_f, f.p_f);
|
||||
using std::swap;
|
||||
swap(p_f, f.p_f);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename R, typename ...Args>
|
||||
inline void swap(Function<R(Args...)> &a, Function<R(Args...)> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<typename R, typename ...Args>
|
||||
inline bool operator==(Function<R(Args...)> const &f, Nullptr) noexcept {
|
||||
return !f;
|
||||
|
|
|
@ -477,15 +477,16 @@ protected:
|
|||
}
|
||||
|
||||
Hashtable &operator=(Hashtable &&ht) {
|
||||
using std::swap;
|
||||
clear();
|
||||
swap_adl(p_size, ht.p_size);
|
||||
swap_adl(p_len, ht.p_len);
|
||||
swap_adl(p_chunks, ht.p_chunks);
|
||||
swap_adl(p_unused, ht.p_unused);
|
||||
swap_adl(p_data.first(), ht.p_data.first());
|
||||
swap_adl(p_data.second().second(), ht.p_data.second().second());
|
||||
swap(p_size, ht.p_size);
|
||||
swap(p_len, ht.p_len);
|
||||
swap(p_chunks, ht.p_chunks);
|
||||
swap(p_unused, ht.p_unused);
|
||||
swap(p_data.first(), ht.p_data.first());
|
||||
swap(p_data.second().second(), ht.p_data.second().second());
|
||||
if (AllocatorPropagateOnContainerMoveAssignment<A>) {
|
||||
swap_adl(p_data.second().first(), ht.p_data.second().first());
|
||||
swap(p_data.second().first(), ht.p_data.second().first());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
@ -506,14 +507,15 @@ protected:
|
|||
}
|
||||
|
||||
void swap(Hashtable &ht) {
|
||||
swap_adl(p_size, ht.p_size);
|
||||
swap_adl(p_len, ht.p_len);
|
||||
swap_adl(p_chunks, ht.p_chunks);
|
||||
swap_adl(p_unused, ht.p_unused);
|
||||
swap_adl(p_data.first(), ht.p_data.first());
|
||||
swap_adl(p_data.second().second(), ht.p_data.second().second());
|
||||
using std::swap;
|
||||
swap(p_size, ht.p_size);
|
||||
swap(p_len, ht.p_len);
|
||||
swap(p_chunks, ht.p_chunks);
|
||||
swap(p_unused, ht.p_unused);
|
||||
swap(p_data.first(), ht.p_data.first());
|
||||
swap(p_data.second().second(), ht.p_data.second().second());
|
||||
if (AllocatorPropagateOnContainerSwap<A>) {
|
||||
swap_adl(p_data.second().first(), ht.p_data.second().first());
|
||||
swap(p_data.second().first(), ht.p_data.second().first());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -128,8 +128,9 @@ struct FileStream: Stream {
|
|||
}
|
||||
|
||||
void swap(FileStream &s) {
|
||||
ostd::swap(p_f, s.p_f);
|
||||
ostd::swap(p_owned, s.p_owned);
|
||||
using std::swap;
|
||||
swap(p_f, s.p_f);
|
||||
swap(p_owned, s.p_owned);
|
||||
}
|
||||
|
||||
FILE *get_file() { return p_f; }
|
||||
|
@ -139,6 +140,10 @@ private:
|
|||
bool p_owned;
|
||||
};
|
||||
|
||||
inline void swap(FileStream &a, FileStream &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
static FileStream in(stdin);
|
||||
static FileStream out(stdout);
|
||||
static FileStream err(stderr);
|
||||
|
|
|
@ -35,7 +35,10 @@ namespace detail {
|
|||
}
|
||||
template<typename U>
|
||||
static inline void set_key(T &, U const &, A &) {}
|
||||
static inline void swap_elem(T &a, T &b) { swap_adl(a, b); }
|
||||
static inline void swap_elem(T &a, T &b) {
|
||||
using std::swap;
|
||||
swap(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
template<
|
||||
|
@ -187,6 +190,11 @@ template<
|
|||
>
|
||||
using Keyset = detail::KeysetImpl<T, H, C, A, false>;
|
||||
|
||||
template<typename T, typename H, typename C, typename A>
|
||||
inline void swap(Keyset<T, H, C, A> &a, Keyset<T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<
|
||||
typename T,
|
||||
typename H = ToHash<detail::KeysetKey<T>>,
|
||||
|
@ -195,6 +203,11 @@ template<
|
|||
>
|
||||
using Multikeyset = detail::KeysetImpl<T, H, C, A, true>;
|
||||
|
||||
template<typename T, typename H, typename C, typename A>
|
||||
inline void swap(Multikeyset<T, H, C, A> &a, Multikeyset<T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
} /* namespace ostd */
|
||||
|
||||
#endif
|
||||
|
|
15
ostd/map.hh
15
ostd/map.hh
|
@ -34,8 +34,9 @@ namespace detail {
|
|||
allocator_construct(alloc, &e, std::forward<U>(key), std::move(T()));
|
||||
}
|
||||
static inline void swap_elem(Element &a, Element &b) {
|
||||
swap_adl(const_cast<K &>(a.first), const_cast<K &>(b.first));
|
||||
swap_adl(a.second, b.second);
|
||||
using std::swap;
|
||||
swap(const_cast<K &>(a.first), const_cast<K &>(b.first));
|
||||
swap(a.second, b.second);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -191,6 +192,11 @@ template<
|
|||
>
|
||||
using Map = detail::MapImpl<K, T, H, C, A, false>;
|
||||
|
||||
template<typename K, typename T, typename H, typename C, typename A>
|
||||
inline void swap(Map<K, T, H, C, A> &a, Map<K, T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<
|
||||
typename K, typename T,
|
||||
typename H = ToHash<K>,
|
||||
|
@ -199,6 +205,11 @@ template<
|
|||
>
|
||||
using Multimap = detail::MapImpl<K, T, H, C, A, true>;
|
||||
|
||||
template<typename K, typename T, typename H, typename C, typename A>
|
||||
inline void swap(Multimap<K, T, H, C, A> &a, Multimap<K, T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
} /* namespace ostd */
|
||||
|
||||
#endif
|
||||
|
|
|
@ -285,9 +285,10 @@ public:
|
|||
}
|
||||
|
||||
void swap(Maybe &v) {
|
||||
using std::swap;
|
||||
if (this->p_engaged == v.p_engaged) {
|
||||
if (this->p_engaged) {
|
||||
detail::swap_adl(this->p_value, v.p_value);
|
||||
swap(this->p_value, v.p_value);
|
||||
}
|
||||
} else {
|
||||
if (this->p_engaged) {
|
||||
|
@ -297,7 +298,7 @@ public:
|
|||
::new(address_of(this->p_value)) Value(std::move(v.p_value));
|
||||
v.p_value.~Value();
|
||||
}
|
||||
detail::swap_adl(this->p_engaged, v.p_engaged);
|
||||
swap(this->p_engaged, v.p_engaged);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -306,6 +307,11 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline void swap(Maybe<T> &a, Maybe<T> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
/* maybe vs maybe */
|
||||
|
||||
template<typename T>
|
||||
|
|
|
@ -257,8 +257,9 @@ namespace detail {
|
|||
}
|
||||
bool operator!=(RangeIterator) const { return !get_ref().empty(); }
|
||||
void swap(RangeIterator &v) {
|
||||
detail::swap_adl(get_ref(). v.get_ref());
|
||||
detail::swap_adl(p_init, v.p_init);
|
||||
using std::swap;
|
||||
swap(get_ref(). v.get_ref());
|
||||
swap(p_init, v.p_init);
|
||||
}
|
||||
private:
|
||||
T &get_ref() { return *reinterpret_cast<T *>(&p_range); }
|
||||
|
|
15
ostd/set.hh
15
ostd/set.hh
|
@ -27,7 +27,10 @@ namespace detail {
|
|||
}
|
||||
template<typename U>
|
||||
static inline void set_key(T &, U const &, A &) {}
|
||||
static inline void swap_elem(T &a, T &b) { swap_adl(a, b); }
|
||||
static inline void swap_elem(T &a, T &b) {
|
||||
using std::swap;
|
||||
swap(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename H, typename C, typename A, bool IsMultihash>
|
||||
|
@ -160,6 +163,11 @@ template<
|
|||
>
|
||||
using Set = detail::SetImpl<T, H, C, A, false>;
|
||||
|
||||
template<typename T, typename H, typename C, typename A>
|
||||
inline void swap(Set<T, H, C, A> &a, Set<T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<
|
||||
typename T,
|
||||
typename H = ToHash<T>,
|
||||
|
@ -168,6 +176,11 @@ template<
|
|||
>
|
||||
using Multiset = detail::SetImpl<T, H, C, A, true>;
|
||||
|
||||
template<typename T, typename H, typename C, typename A>
|
||||
inline void swap(Multiset<T, H, C, A> &a, Multiset<T, H, C, A> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
} /* namespace ostd */
|
||||
|
||||
#endif
|
||||
|
|
|
@ -645,11 +645,12 @@ public:
|
|||
}
|
||||
|
||||
void swap(StringBase &v) {
|
||||
detail::swap_adl(p_len, v.p_len);
|
||||
detail::swap_adl(p_cap, v.p_cap);
|
||||
detail::swap_adl(p_buf.first(), v.p_buf.first());
|
||||
using std::swap;
|
||||
swap(p_len, v.p_len);
|
||||
swap(p_cap, v.p_cap);
|
||||
swap(p_buf.first(), v.p_buf.first());
|
||||
if (AllocatorPropagateOnContainerSwap<A>) {
|
||||
detail::swap_adl(p_buf.second(), v.p_buf.second());
|
||||
swap(p_buf.second(), v.p_buf.second());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -662,6 +663,11 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline void swap(StringBase<T> &a, StringBase<T> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
using String = StringBase<char>;
|
||||
|
||||
/* string literals */
|
||||
|
@ -1098,11 +1104,17 @@ public:
|
|||
RemoveCv<RangeValue<R>> const *get() const { return p_buf; }
|
||||
|
||||
void swap(TempCString &s) {
|
||||
detail::swap_adl(p_buf, s.p_buf);
|
||||
detail::swap_adl(p_allocated, s.p_allocated);
|
||||
using std::swap;
|
||||
swap(p_buf, s.p_buf);
|
||||
swap(p_allocated, s.p_allocated);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
inline void swap(TempCString<R> &a, TempCString<R> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
inline TempCString<R> to_temp_cstr(
|
||||
R input, RemoveCv<RangeValue<R>> *buf, Size bufsize
|
||||
|
|
|
@ -15,61 +15,6 @@
|
|||
|
||||
namespace ostd {
|
||||
|
||||
/* swap */
|
||||
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
auto test_swap(int) -> BoolConstant<IsVoid<
|
||||
decltype(std::declval<T>().swap(std::declval<T &>()))
|
||||
>>;
|
||||
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
|
||||
) noexcept(noexcept(a.swap(b))) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void swap_fb(
|
||||
T &a, T &b, EnableIf<!decltype(test_swap<T>(0))::value, bool> = true
|
||||
) noexcept(IsNothrowMoveConstructible<T> && IsNothrowMoveAssignable<T>) {
|
||||
T c(std::move(a));
|
||||
a = std::move(b);
|
||||
b = std::move(c);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void swap(T &a, T &b) noexcept(noexcept(detail::swap_fb(a, b))) {
|
||||
detail::swap_fb(a, b);
|
||||
}
|
||||
|
||||
template<typename T, Size N>
|
||||
inline void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(ostd::swap(*a, *b))) {
|
||||
for (Size i = 0; i < N; ++i) {
|
||||
ostd::swap(a[i], b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
namespace adl_swap {
|
||||
using ostd::swap;
|
||||
template<typename T>
|
||||
inline void swap_adl(T &a, T &b) noexcept(noexcept(ostd::swap(a, b))) {
|
||||
ostd::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);
|
||||
}
|
||||
}
|
||||
|
||||
/* pair */
|
||||
|
||||
namespace detail {
|
||||
template<typename T, typename U,
|
||||
bool = IsSame<RemoveCv<T>, RemoveCv<U>>,
|
||||
|
|
Loading…
Reference in New Issue