123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- #ifndef __BOOST_SORT_COMMON_SORT_BASIC_HPP
- #define __BOOST_SORT_COMMON_SORT_BASIC_HPP
- #include <ciso646>
- #include <cstdlib>
- #include <functional>
- #include <iterator>
- #include <memory>
- #include <type_traits>
- #include <vector>
- #include <cstddef>
- #include <boost/sort/insert_sort/insert_sort.hpp>
- #include <boost/sort/common/util/traits.hpp>
- #include <boost/sort/common/range.hpp>
- namespace boost
- {
- namespace sort
- {
- namespace common
- {
- using boost::sort::insert_sort;
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- inline Iter_t is_stable_sorted_forward (Iter_t first, Iter_t last,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return first;
- Iter_t it2 = first + 1;
- for (Iter_t it1 = first; it2 != last and not comp(*it2, *it1); it1 = it2++);
- return it2;
- }
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- inline Iter_t is_reverse_stable_sorted_forward(Iter_t first, Iter_t last,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return first;
- Iter_t it2 = first + 1;
- for (Iter_t it1 = first; it2 != last and comp(*it2, *it1); it1 = it2++);
- return it2;
- };
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- size_t number_stable_sorted_forward (Iter_t first, Iter_t last,
- size_t min_process,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return 0;
-
- Iter_t it2 = first + 1;
- for (Iter_t it1 = first; it2 != last and not comp(*it2, *it1); it1 = it2++);
- size_t nsorted = size_t ( it2 - first);
- if ( nsorted != 1)
- return (nsorted >= min_process) ? nsorted: 0;
-
- it2 = first + 1;
- for (Iter_t it1 = first; it2 != last and comp(*it2, *it1); it1 = it2++);
- nsorted = size_t ( it2 - first);
- if ( nsorted < min_process) return 0 ;
- util::reverse ( first , it2);
- return nsorted;
- };
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- inline Iter_t is_stable_sorted_backward(Iter_t first, Iter_t last,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return first;
- Iter_t itaux = last - 1;
- while (itaux != first and not comp(*itaux, *(itaux - 1))) {--itaux; };
- return itaux;
- }
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- inline Iter_t is_reverse_stable_sorted_backward (Iter_t first, Iter_t last,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return first;
- Iter_t itaux = last - 1;
- for (; itaux != first and comp(*itaux, *(itaux - 1)); --itaux);
- return itaux;
- }
- template<class Iter_t, class Compare = std::less<value_iter<Iter_t> > >
- size_t number_stable_sorted_backward (Iter_t first, Iter_t last,
- size_t min_process,
- Compare comp = Compare())
- {
- #ifdef __BS_DEBUG
- assert ( (last- first) >= 0);
- #endif
- if ((last - first) < 2) return 0;
- Iter_t itaux = last - 1;
- while (itaux != first and not comp(*itaux, *(itaux - 1))) {--itaux; };
- size_t nsorted = size_t ( last - itaux);
- if ( nsorted != 1)
- return ( nsorted >= min_process)?nsorted: 0 ;
- itaux = last - 1;
- for (; itaux != first and comp(*itaux, *(itaux - 1)); --itaux);
- nsorted = size_t ( last - itaux);
- if ( nsorted < min_process) return 0 ;
- util::reverse ( itaux, last );
- return nsorted;
- }
- template <class Iter1_t, class Iter2_t, class Compare>
- inline void internal_sort (const range<Iter1_t> &rng1,
- const range<Iter2_t> &rng2,
- Compare comp, uint32_t level, bool even = true)
- {
-
-
-
- typedef value_iter<Iter1_t> value_t;
- typedef value_iter<Iter2_t> value2_t;
- static_assert (std::is_same< value_t, value2_t>::value,
- "Incompatible iterators\n");
-
-
-
- #ifdef __BS_DEBUG
- assert (rng1.size ( ) == rng2.size ( ) );
- #endif
- size_t nelem = (rng1.size() + 1) >> 1;
- range<Iter1_t> rng1_left(rng1.first, rng1.first + nelem),
- rng1_right(rng1.first + nelem, rng1.last);
- range<Iter2_t> rng2_left(rng2.first, rng2.first + nelem),
- rng2_right(rng2.first + nelem, rng2.last);
- if (nelem <= 32 and (level & 1) == even)
- {
- insert_sort(rng1_left.first, rng1_left.last, comp);
- insert_sort(rng1_right.first, rng1_right.last, comp);
- }
- else
- {
- internal_sort(rng2_left, rng1_left, comp, level + 1, even);
- internal_sort(rng2_right, rng1_right, comp, level + 1, even);
- };
- merge(rng2, rng1_left, rng1_right, comp);
- };
- template<class Iter1_t, class Iter2_t, class Compare>
- static void range_sort_data (const range<Iter1_t> & rng_data,
- const range<Iter2_t> & rng_aux, Compare comp)
- {
-
-
-
- typedef value_iter<Iter1_t> value_t;
- typedef value_iter<Iter2_t> value2_t;
- static_assert (std::is_same< value_t, value2_t>::value,
- "Incompatible iterators\n");
-
-
-
- #ifdef __BS_DEBUG
- assert ( rng_data.size() == rng_aux.size());
- #endif
-
- const uint32_t sort_min = 32;
- if (rng_data.size() <= sort_min)
- {
- insert_sort(rng_data.first, rng_data.last, comp);
- return;
- };
- internal_sort(rng_aux, rng_data, comp, 0, true);
- };
- template<class Iter1_t, class Iter2_t, class Compare>
- static void range_sort_buffer(const range<Iter1_t> & rng_data,
- const range<Iter2_t> & rng_aux, Compare comp)
- {
-
-
-
- typedef value_iter<Iter1_t> value_t;
- typedef value_iter<Iter2_t> value2_t;
- static_assert (std::is_same< value_t, value2_t>::value,
- "Incompatible iterators\n");
-
-
-
- #ifdef __BS_DEBUG
- assert ( rng_data.size() == rng_aux.size());
- #endif
-
- const uint32_t sort_min = 32;
- if (rng_data.size() <= sort_min)
- {
- insert_sort(rng_data.first, rng_data.last, comp);
- move_forward(rng_aux, rng_data);
- return;
- };
- internal_sort(rng_data, rng_aux, comp, 0, false);
- };
- };
- };
- };
- #endif
|