forked from OctaForge/libostd
add algorithm::partition
parent
35424dd248
commit
cd0b6fc2fc
|
@ -10,6 +10,18 @@
|
||||||
#include "octa/range.h"
|
#include "octa/range.h"
|
||||||
|
|
||||||
namespace octa {
|
namespace octa {
|
||||||
|
template<typename R, typename U>
|
||||||
|
R partition(R range, U pred) {
|
||||||
|
R ret = range;
|
||||||
|
for (; !range.empty(); range.pop_first()) {
|
||||||
|
if (pred(range.first())) {
|
||||||
|
swap(range.first(), ret.first());
|
||||||
|
ret.pop_first();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename R, typename C>
|
template<typename R, typename C>
|
||||||
void insertion_sort(R range, C compare) {
|
void insertion_sort(R range, C compare) {
|
||||||
size_t rlen = range.length();
|
size_t rlen = range.length();
|
||||||
|
@ -28,7 +40,6 @@ namespace octa {
|
||||||
insertion_sort(range, Less<typename RangeTraits<R>::value>());
|
insertion_sort(range, Less<typename RangeTraits<R>::value>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename R, typename C>
|
template<typename R, typename C>
|
||||||
void sort(R range, C compare) {
|
void sort(R range, C compare) {
|
||||||
insertion_sort(range, compare);
|
insertion_sort(range, compare);
|
||||||
|
|
|
@ -38,17 +38,6 @@ namespace std {
|
||||||
namespace octa {
|
namespace octa {
|
||||||
template<typename T> using InitializerList = std::initializer_list<T>;
|
template<typename T> using InitializerList = std::initializer_list<T>;
|
||||||
|
|
||||||
template<typename T> void swap(T &a, T &b) {
|
|
||||||
T c(move(a));
|
|
||||||
a = move(b);
|
|
||||||
b = move(c);
|
|
||||||
}
|
|
||||||
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N]) {
|
|
||||||
for (size_t i = 0; i < N; ++i) {
|
|
||||||
swap(a[i], b[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* aliased in traits.h later */
|
/* aliased in traits.h later */
|
||||||
namespace internal {
|
namespace internal {
|
||||||
template<typename T> struct RemoveReference { typedef T type; };
|
template<typename T> struct RemoveReference { typedef T type; };
|
||||||
|
@ -153,6 +142,17 @@ namespace octa {
|
||||||
internal::MemFn<R, T> mem_fn(R T:: *ptr) {
|
internal::MemFn<R, T> mem_fn(R T:: *ptr) {
|
||||||
return internal::MemFn<R, T>(ptr);
|
return internal::MemFn<R, T>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T> void swap(T &a, T &b) {
|
||||||
|
T c(move(a));
|
||||||
|
a = move(b);
|
||||||
|
b = move(c);
|
||||||
|
}
|
||||||
|
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N]) {
|
||||||
|
for (size_t i = 0; i < N; ++i) {
|
||||||
|
swap(a[i], b[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue