Boost.Sort
spreadsort.hpp
Go to the documentation of this file.
1 // Templated generic hybrid sorting
2 
3 // Copyright Steven J. Ross 2001 - 2009.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
8 // See http://www.boost.org/libs/sort/ for library home page.
9 
10 /*
11 Some improvements suggested by:
12 Phil Endecott and Frank Gennari
13 float_mem_cast fix provided by:
14 Scott McMurray
15 */
16 
17 #ifndef BOOST_SORT_SPREADSORT_HPP
18 #define BOOST_SORT_SPREADSORT_HPP
19 #include <algorithm>
20 #include <vector>
21 #include <cstring>
22 #include <string>
23 #include <limits>
24 #include <boost/type_traits.hpp>
28 
29 namespace boost {
30 
31 /*! Namespace for spreadsort sort variants for different data types.
32 \note Use hyperlinks (coloured) to get detailed information about functions.
33 */
34 namespace sort {
35 
36  /*!
37  \brief Generic @c spreadsort variant detecting integer-type elements so call to @c integer_sort.
38  \details If the data type provided is an integer, @c integer_sort is used.
39  \note Sorting other data types requires picking between @c integer_sort, @c float_sort and @c string_sort directly,
40  as @c spreadsort won't accept types that don't have the appropriate @c type_traits.
41  \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
42  \param[in] first Iterator pointer to first element.
43  \param[in] last Iterator pointing to one beyond the end of data.
44 
45  \pre [@c first, @c last) is a valid range.
46  \pre @c RandomAccessIter @c value_type is mutable.
47  \pre @c RandomAccessIter @c value_type is <a href="http://en.cppreference.com/w/cpp/concept/LessThanComparable">LessThanComparable</a>
48  \pre @c RandomAccessIter @c value_type supports the @c operator>>,
49  which returns an integer-type right-shifted a specified number of bits.
50  \post The elements in the range [@c first, @c last) are sorted in ascending order.
51  */
52 
53  template <class RandomAccessIter>
54  inline typename boost::enable_if_c< std::numeric_limits<
55  typename std::iterator_traits<RandomAccessIter>::value_type >::is_integer,
56  void >::type
57  spreadsort(RandomAccessIter first, RandomAccessIter last)
58  {
59  integer_sort(first, last);
60  }
61 
62  /*!
63  \brief Generic @c spreadsort variant detecting float element type so call to @c float_sort.
64  \details If the data type provided is a float or castable-float, @c float_sort is used.
65  \note Sorting other data types requires picking between @c integer_sort, @c float_sort and @c string_sort directly,
66  as @c spreadsort won't accept types that don't have the appropriate @c type_traits.
67 
68  \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
69  \param[in] first Iterator pointer to first element.
70  \param[in] last Iterator pointing to one beyond the end of data.
71 
72  \pre [@c first, @c last) is a valid range.
73  \pre @c RandomAccessIter @c value_type is mutable.
74  \pre @c RandomAccessIter @c value_type is <a href="http://en.cppreference.com/w/cpp/concept/LessThanComparable">LessThanComparable</a>
75  \pre @c RandomAccessIter @c value_type supports the @c operator>>,
76  which returns an integer-type right-shifted a specified number of bits.
77  \post The elements in the range [@c first, @c last) are sorted in ascending order.
78  */
79 
80  template <class RandomAccessIter>
81  inline typename boost::enable_if_c< !std::numeric_limits<
82  typename std::iterator_traits<RandomAccessIter>::value_type >::is_integer
83  && std::numeric_limits<
84  typename std::iterator_traits<RandomAccessIter>::value_type >::is_iec559,
85  void >::type
86  spreadsort(RandomAccessIter first, RandomAccessIter last)
87  {
88  float_sort(first, last);
89  }
90 
91  /*!
92  \brief Generic @c spreadsort variant detecting string element type so call to @c string_sort for @c std::strings and @c std::wstrings.
93  \details If the data type provided is a string or wstring, @c string_sort is used.
94  \note Sorting other data types requires picking between @c integer_sort, @c float_sort and @c string_sort directly,
95  as @c spreadsort won't accept types that don't have the appropriate @c type_traits.
96 
97  \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
98  \param[in] first Iterator pointer to first element.
99  \param[in] last Iterator pointing to one beyond the end of data.
100 
101  \pre [@c first, @c last) is a valid range.
102  \pre @c RandomAccessIter @c value_type is mutable.
103  \pre @c RandomAccessIter @c value_type is <a href="http://en.cppreference.com/w/cpp/concept/LessThanComparable">LessThanComparable</a>
104  \pre @c RandomAccessIter @c value_type supports the @c operator>>,
105  which returns an integer-type right-shifted a specified number of bits.
106  \post The elements in the range [@c first, @c last) are sorted in ascending order.
107  */
108 
109  template <class RandomAccessIter>
110  inline typename boost::enable_if_c<
111  is_same<typename std::iterator_traits<RandomAccessIter>::value_type,
112  typename std::string>::value ||
113  is_same<typename std::iterator_traits<RandomAccessIter>::value_type,
114  typename std::wstring>::value, void >::type
115  spreadsort(RandomAccessIter first, RandomAccessIter last)
116  {
117  string_sort(first, last);
118  }
119 } // namespace sort
120 } // namespace boost
121 
122 #endif
void integer_sort(RandomAccessIter first, RandomAccessIter last)
Integer sort algorithm using random access iterators. (All variants fall back to std::sort if the dat...
Definition: integer_sort.hpp:75
Definition: float_sort.hpp:27
boost::enable_if_c< std::numeric_limits< typename std::iterator_traits< RandomAccessIter >::value_type >::is_integer, void >::type spreadsort(RandomAccessIter first, RandomAccessIter last)
Generic spreadsort variant detecting integer-type elements so call to integer_sort.
Definition: spreadsort.hpp:57
void string_sort(RandomAccessIter first, RandomAccessIter last, Unsigned_char_type unused)
String sort algorithm using random access iterators, allowing character-type overloads. (All variants fall back to std::sort if the data size is too small, < detail::min_sort_size).
Definition: string_sort.hpp:73
void float_sort(RandomAccessIter first, RandomAccessIter last)
float_sort with casting to the appropriate size.
Definition: float_sort.hpp:86