2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _RSTL_ALGORITHM
|
|
|
|
#define _RSTL_ALGORITHM
|
2022-08-13 12:57:38 +00:00
|
|
|
|
2022-12-05 21:35:31 +00:00
|
|
|
#include "rstl/functional.hpp"
|
|
|
|
#include "rstl/pair.hpp"
|
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;
|
|
|
|
|
2022-11-28 15:00:16 +00:00
|
|
|
It t1 = next - 1;
|
|
|
|
It t2 = next;
|
|
|
|
while (first < t2 && cmp(value, *t1)) {
|
|
|
|
*t2-- = *t1--;
|
2022-11-27 03:04:19 +00:00
|
|
|
}
|
2022-11-28 15:00:16 +00:00
|
|
|
*t2 = value;
|
2022-11-27 03:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-12-05 21:35:31 +00:00
|
|
|
int count = last - first; // use distance?
|
2022-11-27 03:04:19 +00:00
|
|
|
if (count > 1) {
|
|
|
|
if (count <= 20) {
|
|
|
|
__insertion_sort(first, last, cmp);
|
|
|
|
} else {
|
2022-11-28 16:03:59 +00:00
|
|
|
It pivot = first + count / 2;
|
2022-11-28 00:40:31 +00:00
|
|
|
It end = last;
|
2022-11-28 16:03:59 +00:00
|
|
|
__sort3(*first, *pivot, *--end, cmp);
|
|
|
|
typename iterator_traits< It >::value_type value = *pivot;
|
2022-11-27 03:04:19 +00:00
|
|
|
It it = first + 1;
|
2022-11-28 00:40:31 +00:00
|
|
|
--end;
|
2022-11-27 03:04:19 +00:00
|
|
|
while (true) {
|
2022-11-27 21:58:19 +00:00
|
|
|
while (cmp(*it, value))
|
|
|
|
++it;
|
2022-11-28 00:40:31 +00:00
|
|
|
while (cmp(value, *end))
|
|
|
|
--end;
|
|
|
|
if (it >= end)
|
2022-11-27 03:04:19 +00:00
|
|
|
break;
|
2022-11-28 16:03:59 +00:00
|
|
|
iter_swap(it++, end--);
|
2022-11-27 03:04:19 +00:00
|
|
|
}
|
|
|
|
sort(first, it, cmp);
|
|
|
|
sort(it, last, cmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:35:31 +00:00
|
|
|
template < typename It, typename T, typename Cmp >
|
|
|
|
It lower_bound(It start, It end, const T& value, Cmp cmp) {
|
|
|
|
int dist = distance(start, end);
|
|
|
|
It it = start;
|
|
|
|
while (dist > 0) {
|
|
|
|
int halfDist = dist / 2;
|
2022-12-07 16:46:34 +00:00
|
|
|
it = start;
|
2022-12-05 21:35:31 +00:00
|
|
|
advance(it, halfDist);
|
|
|
|
if (cmp(*it, value)) {
|
|
|
|
start = it;
|
|
|
|
++start;
|
|
|
|
dist = (dist - halfDist) - 1;
|
|
|
|
} else {
|
|
|
|
dist = halfDist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename It, typename T, typename Cmp >
|
2022-12-06 12:05:36 +00:00
|
|
|
inline It binary_find(It start, It end, const T& value, Cmp cmp) {
|
2022-12-05 21:35:31 +00:00
|
|
|
It lower = lower_bound(start, end, value, cmp);
|
|
|
|
bool found = false;
|
|
|
|
if (lower != end && !cmp(value, *lower)) {
|
|
|
|
found = true;
|
|
|
|
}
|
2022-12-09 18:00:01 +00:00
|
|
|
return found ? lower : end;
|
2022-12-05 21:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T, typename Cmp >
|
|
|
|
class pair_sorter_finder;
|
|
|
|
|
|
|
|
template < typename K, typename V, typename Cmp >
|
|
|
|
class pair_sorter_finder< pair< K, V >, Cmp > {
|
|
|
|
public:
|
|
|
|
typedef K key_type;
|
|
|
|
Cmp cmp;
|
|
|
|
pair_sorter_finder(const Cmp& cmp) : cmp(cmp) {}
|
|
|
|
|
|
|
|
bool operator()(const K& a, const pair< K, V >& b) const;
|
|
|
|
/* {
|
|
|
|
return cmp(a, b.first);
|
|
|
|
}*/
|
2022-12-06 12:05:36 +00:00
|
|
|
|
2022-12-05 21:35:31 +00:00
|
|
|
bool operator()(const pair< K, V >& a, const K& b) const;
|
|
|
|
/* {
|
|
|
|
return cmp(a.first, b);
|
|
|
|
}*/
|
|
|
|
};
|
|
|
|
|
2022-12-09 17:05:49 +00:00
|
|
|
template <typename T>
|
|
|
|
inline pair_sorter_finder< T::value_type, less< select1st< typename T::value_type >::value_type > > default_pair_sorter_finder()
|
|
|
|
{
|
|
|
|
less< select1st< typename T::value_type >::value_type > l;
|
|
|
|
pair_sorter_finder< T::value_type, less< select1st< typename T::value_type >::value_type > > a(l);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:35:31 +00:00
|
|
|
template < typename K, typename V, typename Cmp >
|
2022-12-06 12:05:36 +00:00
|
|
|
inline bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const K& a,
|
|
|
|
const pair< K, V >& b) const {
|
2022-12-05 21:35:31 +00:00
|
|
|
return cmp(a, b.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename K, typename V, typename Cmp >
|
2022-12-06 12:05:36 +00:00
|
|
|
inline bool pair_sorter_finder< pair< K, V >, Cmp >::operator()(const pair< K, V >& a,
|
|
|
|
const K& b) const {
|
2022-12-05 21:35:31 +00:00
|
|
|
return cmp(a.first, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
typename T::const_iterator
|
|
|
|
find_by_key(const T& container,
|
|
|
|
const typename select1st< typename T::value_type >::value_type& key);
|
|
|
|
|
|
|
|
template < typename T >
|
2022-12-06 12:05:36 +00:00
|
|
|
typename T::const_iterator inline find_by_key(
|
|
|
|
const T& container, const typename select1st< typename T::value_type >::value_type& key) {
|
|
|
|
return binary_find(container.begin(), container.end(), key,
|
2022-12-09 17:05:49 +00:00
|
|
|
default_pair_sorter_finder<T>());
|
2022-12-06 12:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
typename T::iterator
|
|
|
|
find_by_key_nc(T& container, const typename select1st< typename T::value_type >::value_type& key);
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
typename T::iterator inline find_by_key_nc(
|
|
|
|
T& container, const typename select1st< typename T::value_type >::value_type& key) {
|
2022-12-05 21:35:31 +00:00
|
|
|
return binary_find(container.begin(), container.end(), key,
|
2022-12-09 17:05:49 +00:00
|
|
|
default_pair_sorter_finder<T>());
|
2022-12-05 21:35:31 +00:00
|
|
|
}
|
|
|
|
|
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
|