123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- //----------------------------------------------------------------------------
- /// @file sort_basic.hpp
- /// @brief Spin Sort algorithm
- ///
- /// @author Copyright (c) 2016 Francisco José Tapia ([email protected] )\n
- /// Distributed under the Boost Software License, Version 1.0.\n
- /// ( See accompanying file LICENSE_1_0.txt or copy at
- /// http://www.boost.org/LICENSE_1_0.txt )
- /// @version 0.1
- ///
- /// @remarks
- //-----------------------------------------------------------------------------
- #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 SENTENCES
- //----------------------------------------------------------------------------
- using boost::sort::insert_sort;
- //-----------------------------------------------------------------------------
- // function : is_stable_sorted_forward
- /// @brief examine the elements in the range first, last if they are stable
- /// sorted, and return an iterator to the first element not sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @return iterator to the first element not stable sorted. The number of
- /// elements sorted is the iterator returned minus first
- //-----------------------------------------------------------------------------
- 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;
- }
- //-----------------------------------------------------------------------------
- // function : is_reverse_stable_sorted_forward
- /// @brief examine the elements in the range first, last if they are reverse
- /// stable sorted, and return an iterator to the first element not
- /// reverse stable sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @return iterator to the first element not reverse stable sorted. The number
- /// of elements sorted is the iterator returned minus first
- //-----------------------------------------------------------------------------
- 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;
- };
- //-----------------------------------------------------------------------------
- // function : number_stable_sorted_forward
- /// @brief examine the elements in the range first, last if they are stable
- /// sorted, and return the number of elements sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @param min_process : minimal number of elements to be consideer
- /// @return number of element sorted. I f the number is lower than min_process
- /// return 0
- //-----------------------------------------------------------------------------
- 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;
- // sorted elements
- 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;
- // reverse sorted elements
- 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;
- };
- //-----------------------------------------------------------------------------
- // function : is_stable_sorted_backward
- /// @brief examine the elements in the range first, last beginning at end, and
- /// if they are stablesorted, and return an iterator to the last element
- /// sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @return iterator to the last element stable sorted. The number of
- /// elements sorted is the last minus the iterator returned
- //-----------------------------------------------------------------------------
- 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;
- }
- //-----------------------------------------------------------------------------
- // function : is_reverse_stable_sorted_backward
- /// @brief examine the elements in the range first, last beginning at end, and
- /// if they are stablesorted, and return an iterator to the last element
- /// sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @return iterator to the last element stable sorted. The number of
- /// elements sorted is the last minus the iterator returned
- //-----------------------------------------------------------------------------
- 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;
- }
- //-----------------------------------------------------------------------------
- // function : number_stable_sorted_backward
- /// @brief examine the elements in the range first, last if they are stable
- /// sorted, and return the number of elements sorted
- /// @param first : iterator to the first element in the range
- /// @param last : ierator after the last element of the range
- /// @param comp : object for to compare two elements
- /// @param min_process : minimal number of elements to be consideer
- /// @return number of element sorted. I f the number is lower than min_process
- /// return 0
- //-----------------------------------------------------------------------------
- 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;
- }
- //-----------------------------------------------------------------------------
- // function : internal_sort
- /// @brief this function divide r_input in two parts, sort it,and merge moving
- /// the elements to range_buf
- /// @param range_input : range with the elements to sort
- /// @param range_buffer : range with the elements sorted
- /// @param comp : object for to compare two elements
- /// @param level : when is 1, sort with the insertionsort algorithm
- /// if not make a recursive call splitting the ranges
- //
- //-----------------------------------------------------------------------------
- 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)
- {
- //-----------------------------------------------------------------------
- // metaprogram
- //-----------------------------------------------------------------------
- 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");
- //-----------------------------------------------------------------------
- // program
- //-----------------------------------------------------------------------
- #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);
- };
- //-----------------------------------------------------------------------------
- // function : range_sort_data
- /// @brief this sort elements using the range_sort function and receiving a
- /// buffer of initialized memory
- /// @param rng_data : range with the elements to sort
- /// @param rng_aux : range of at least the same memory than rng_data used as
- /// auxiliary memory in the sorting
- /// @param comp : object for to compare two elements
- //-----------------------------------------------------------------------------
- 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)
- {
- //-----------------------------------------------------------------------
- // metaprogram
- //-----------------------------------------------------------------------
- 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");
- //------------------------------------------------------------------------
- // program
- //------------------------------------------------------------------------
- #ifdef __BS_DEBUG
- assert ( rng_data.size() == rng_aux.size());
- #endif
- // minimal number of element before to jump to insertionsort
- 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);
- };
- //-----------------------------------------------------------------------------
- // function : range_sort_buffer
- /// @brief this sort elements using the range_sort function and receiving a
- /// buffer of initialized memory
- /// @param rng_data : range with the elements to sort
- /// @param rng_aux : range of at least the same memory than rng_data used as
- /// auxiliary memory in the sorting
- /// @param comp : object for to compare two elements
- //-----------------------------------------------------------------------------
- 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)
- {
- //-----------------------------------------------------------------------
- // metaprogram
- //-----------------------------------------------------------------------
- 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");
- //------------------------------------------------------------------------
- // program
- //------------------------------------------------------------------------
- #ifdef __BS_DEBUG
- assert ( rng_data.size() == rng_aux.size());
- #endif
- // minimal number of element before to jump to insertionsort
- 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);
- };
- //****************************************************************************
- };// End namespace common
- };// End namespace sort
- };// End namepspace boost
- //****************************************************************************
- //
- #endif
|