Boost.Sort: I:/modular-boost/libs/sort/include/boost/sort/spreadsort/float_sort.hpp Source File

Boost.Sort

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

float_sort.hpp
Go to the documentation of this file.
1 //Templated Spreadsort-based implementation of float_sort and float_mem_cast
2
3 // Copyright Steven J. Ross 2001 - 2014.
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_FLOAT_SORT_HPP
18 #define BOOST_FLOAT_SORT_HPP
19 #include <algorithm>
20 #include <vector>
21 #include <cstring>
22 #include <limits>
23 #include <boost/static_assert.hpp>
24 #include <boost/sort/spreadsort/detail/constants.hpp>
25 #include <boost/sort/spreadsort/detail/float_sort.hpp>
26
27 namespace boost {
28 namespace sort {
29
30
31 /*!
32 \brief Casts a float to the specified integer type.
33
34 \tparam Data_type Floating-point IEEE 754/IEC559 type.
35 \tparam Cast_type Integer type (same size) to which to cast.
36
37 \par Example:
38 \code
39 struct rightshift {
40 int operator()(const DATA_TYPE &x, const unsigned offset) const {
41 return float_mem_cast<KEY_TYPE, CAST_TYPE>(x.key) >> offset;
42 }
43 };
44 \endcode
45 */
46 template<class Data_type, class Cast_type>
47 inline Cast_type
48 float_mem_cast(const Data_type & data)
49 {
50 // Only cast IEEE floating-point numbers, and only to a same-sized integer.
51 BOOST_STATIC_ASSERT(sizeof(Cast_type) == sizeof(Data_type));
52 BOOST_STATIC_ASSERT(std::numeric_limits<Data_type>::is_iec559);
53 BOOST_STATIC_ASSERT(std::numeric_limits<Cast_type>::is_integer);
54 Cast_type result;
55 std::memcpy(&result, &data, sizeof(Cast_type));
56 return result;
57 }
58
59
60 /*!
61 \brief @c float_sort with casting to the appropriate size.
62 \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
63
64 \param[in] first Iterator pointer to first element.
65 \param[in] last Iterator pointing to one beyond the end of data.
66
67 Some performance plots of runtime vs. n and log(range) are provided:\n
68 <a href="../../doc/graph/windows_float_sort.htm"> windows_float_sort</a>
69 \n
70 <a href="../../doc/graph/osx_float_sort.htm"> osx_float_sort</a>
71
72
73
74 \par A simple example of sorting some floating-point is:
75 \code
76 vector<float> vec;
77 vec.push_back(1.0);
78 vec.push_back(2.3);
79 vec.push_back(1.3);
80 spreadsort(vec.begin(), vec.end());
81 \endcode
82 \par The sorted vector contains ascending values "1.0 1.3 2.3".
83
84 */
85 template <class RandomAccessIter>
86 inline void float_sort(RandomAccessIter first, RandomAccessIter last)
87 {
88 if (last - first < detail::min_sort_size)
89 std::sort(first, last);
90 else
91 detail::float_sort(first, last);
92 }
93
94 /*!
95 \brief Floating-point sort algorithm using random access iterators with just right-shift functor.
96 \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
97 \tparam Right_shift Functor for right-shift by parameter @c shift bits.
98
99 \param[in] first Iterator pointer to first element.
100 \param[in] last Iterator pointing to one beyond the end of data.
101 \param[in] rshift Number of bits to right-shift (using functor).
102
103 */
104 template <class RandomAccessIter, class Right_shift>
105 inline void float_sort(RandomAccessIter first, RandomAccessIter last,
106 Right_shift rshift)
107 {
108 if (last - first < detail::min_sort_size)
109 std::sort(first, last);
110 else
111 detail::float_sort(first, last, rshift(*first, 0), rshift);
112 }
113
114
115 /*!
116 \brief Float sort algorithm using random access iterators with both right-shift and user-defined comparison operator.
117
118 \tparam RandomAccessIter <a href="http://www.cplusplus.com/reference/iterator/RandomAccessIterator/">Random access iterator</a>
119 \tparam Right_shift functor for right-shift by parameter @c shift bits.
120 \tparam Comp To provide @c operator< for user-defined comparison.
121
122 \param[in] first Iterator pointer to first element.
123 \param[in] last Iterator pointing to one beyond the end of data.
124 \param[in] rshift Number of bits to right-shift (using functor).
125 \param[in] comp comparison functor.
126 */
127
128 template <class RandomAccessIter, class Right_shift, class Compare>
129 inline void float_sort(RandomAccessIter first, RandomAccessIter last,
130 Right_shift rshift, Compare comp)
131 {
132 if (last - first < detail::min_sort_size)
133 std::sort(first, last, comp);
134 else
135 detail::float_sort(first, last, rshift(*first, 0), rshift, comp);
136 }
137 }
138 }
139
140 #endif
boost
Definition: float_sort.hpp:27
boost::sort::float_sort
void float_sort(RandomAccessIter first, RandomAccessIter last, Right_shift rshift, Compare comp)
Float sort algorithm using random access iterators with both right-shift and user-defined comparison ...
Definition: float_sort.hpp:129
boost::sort::float_mem_cast
Cast_type float_mem_cast(const Data_type &data)
Casts a float to the specified integer type.
Definition: float_sort.hpp:48
boost::sort::float_sort
void float_sort(RandomAccessIter first, RandomAccessIter last)
float_sort with casting to the appropriate size.
Definition: float_sort.hpp:86