2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _RSTL_ALGORITHM
|
|
|
|
#define _RSTL_ALGORITHM
|
2022-08-13 12:57:38 +00:00
|
|
|
|
2022-09-18 05:55:13 +00:00
|
|
|
#include "rstl/pointer_iterator.hpp"
|
|
|
|
|
2022-08-13 12:57:38 +00:00
|
|
|
namespace rstl {
|
2022-10-04 00:00:46 +00:00
|
|
|
template < class It, class T >
|
|
|
|
inline It find(It first, It last, const T& val) {
|
2022-09-18 05:55:13 +00:00
|
|
|
while (first != last && !(*first == val))
|
|
|
|
++first;
|
|
|
|
return first;
|
2022-08-13 12:57:38 +00:00
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
inline void swap(T& a, T& b) {
|
|
|
|
T tmp(a);
|
|
|
|
a = b;
|
|
|
|
b = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename I1, typename I2 >
|
|
|
|
inline void iter_swap(I1 a, I2 b) {
|
|
|
|
swap(*a, *b);
|
|
|
|
}
|
|
|
|
|
2022-11-27 03:04:19 +00:00
|
|
|
template < typename It, class Cmp >
|
|
|
|
void __insertion_sort(It first, It last, Cmp cmp);
|
|
|
|
|
|
|
|
template < class T, class Cmp >
|
|
|
|
void __sort3(T& a, T& b, T& c, Cmp comp); // TODO
|
|
|
|
|
|
|
|
template < typename It, class Cmp >
|
|
|
|
void sort(It first, It last, Cmp cmp); // TODO
|
|
|
|
|
|
|
|
// Implementations
|
2022-10-04 00:00:46 +00:00
|
|
|
|
|
|
|
template < typename It, class Cmp >
|
2022-11-27 03:04:19 +00:00
|
|
|
void __insertion_sort(It first, It last, Cmp cmp) {
|
|
|
|
for (It next = first + 1; next < last; ++next) {
|
|
|
|
typename iterator_traits< It >::value_type value = *next;
|
|
|
|
|
|
|
|
It t1 = next;
|
|
|
|
It t2 = next;
|
|
|
|
while (first < t1 && cmp(value, *(t2 - 1))) {
|
|
|
|
*t1 = *(t2 - 1);
|
|
|
|
--t1;
|
|
|
|
--t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
*t1 = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T, class Cmp >
|
|
|
|
void __sort3(T& a, T& b, T& c, Cmp comp) {
|
|
|
|
if (comp(b, a)) {
|
|
|
|
swap(a, b);
|
|
|
|
}
|
|
|
|
if (comp(c, b)) {
|
|
|
|
T tmp(c);
|
|
|
|
c = b;
|
|
|
|
if (comp(tmp, a)) {
|
|
|
|
b = a;
|
|
|
|
a = tmp;
|
|
|
|
} else {
|
|
|
|
b = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename It, class Cmp >
|
|
|
|
void sort(It first, It last, Cmp cmp) {
|
|
|
|
int count = last - first;
|
|
|
|
if (count > 1) {
|
|
|
|
if (count <= 20) {
|
|
|
|
__insertion_sort(first, last, cmp);
|
|
|
|
} else {
|
|
|
|
It beforeLast = last - 1;
|
|
|
|
__sort3(*first, *(first + count / 2), *(last - 1), cmp);
|
|
|
|
|
|
|
|
It middle = first + count / 2;
|
|
|
|
It it = first + 1;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
for (; cmp(*it, *middle); ++it)
|
|
|
|
;
|
|
|
|
for (; cmp(*middle, *beforeLast); --beforeLast)
|
|
|
|
;
|
|
|
|
if (!cmp(*it, *beforeLast))
|
|
|
|
break;
|
|
|
|
rstl::swap(*it, *beforeLast);
|
|
|
|
++it;
|
|
|
|
--beforeLast;
|
|
|
|
}
|
|
|
|
sort(first, it, cmp);
|
|
|
|
sort(it, last, cmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 05:55:13 +00:00
|
|
|
} // namespace rstl
|
2022-10-04 00:00:46 +00:00
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _RSTL_ALGORITHM
|