3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Float Sort</title>
5 <link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../../../index.html" title="Boost.Sort">
8 <link rel="up" href="../sort_hpp.html" title="Spreadsort">
9 <link rel="prev" href="integer_sort.html" title="Integer Sort">
10 <link rel="next" href="string_sort.html" title="String Sort">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="integer_sort.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sort_hpp.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="string_sort.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h5 class="title">
27 <a name="sort.single_thread.spreadsort.sort_hpp.float_sort"></a><a class="link" href="float_sort.html" title="Float Sort">Float
29 </h5></div></div></div>
31 <code class="literal"><code class="computeroutput"><a class="link" href="../../../../boost/sort/spreadsort/float_so_idm46048204416688.html" title="Function template float_sort">float_sort</a></code></code>
32 is a fast templated in-place hybrid radix/comparison algorithm much like
33 <code class="literal"><code class="computeroutput"><a class="link" href="../../../../boost/sort/spreadsort/integer__idm46048203222928.html" title="Function template integer_sort">integer_sort</a></code></code>,
34 but sorts IEEE floating-point numbers (positive, zero, NaN, and negative)
35 into ascending order by casting them to integers. This works because
36 positive IEEE floating-point numbers sort like integers with the same
37 bits, and negative IEEE floating-point numbers sort in the reverse order
38 of integers with the same bits. float_sort is roughly 2X as fast as std::sort.
41 -0.0 vs. 0.0 and NaN are given definitive ordered positions by the radix-based
42 portion of this algorithm, where comparison-based sorting does not guarantee
43 their relative position. The included tests avoid creating NaN and -0.0
44 so that results match std::sort, which is not consistent in how it handles
45 these numbers, as they compare as equal to numbers with different values.
48 float_sort checks the size of the data type and whether it is castable,
49 picking an integer type to cast to, if a casting functor isn't provided
53 float_mem_cast casts IEEE floating-point numbers (positive, zero, NaN,
54 and negative) into integers. This is an essential utility for creating
55 a custom rightshift functor for float_sort, when one is needed. Only
56 IEEE floating-point numbers of the same size as the integer type being
57 cast to should be used in this specialized method call. Worst-case performance
58 is <span class="emphasis"><em>𝑶(N * (log2(range)/s + s))</em></span>, so <code class="literal"><code class="computeroutput"><a class="link" href="../../../../boost/sort/spreadsort/float_so_idm46048204416688.html" title="Function template float_sort">float_sort</a></code></code>
59 is asymptotically faster than pure comparison-based algorithms. <span class="emphasis"><em>s</em></span>
60 is <span class="emphasis"><em>max_splits</em></span>, which defaults to 11, so its worst-case
61 with default settings for 32-bit integers is <span class="emphasis"><em>𝑶(N * ((32/11)</em></span>
62 slow radix-based iterations + 11 fast comparison-based iterations).
65 Some performance plots of runtime vs. n and log2(range) are provided
69 <a href="../../../../../../doc/graph/windows_float_sort.htm" target="_top">Windows Float Sort</a>
72 <a href="../../../../../../doc/graph/osx_float_sort.htm" target="_top">OSX Float Sort</a>
75 <div class="titlepage"><div><div><h6 class="title">
76 <a name="sort.single_thread.spreadsort.sort_hpp.float_sort.floatsort_examples"></a><a class="link" href="float_sort.html#sort.single_thread.spreadsort.sort_hpp.float_sort.floatsort_examples" title="Float Sort Examples">Float
78 </h6></div></div></div>
80 See <a href="../../../../../../example/floatfunctorsample.cpp" target="_top">floatfunctorsample.cpp</a>
81 for a working example of how to sort structs with a float key:
83 <pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">CAST_TYPE</span> <span class="keyword">int</span>
84 <span class="preprocessor">#define</span> <span class="identifier">KEY_TYPE</span> <span class="keyword">float</span>
86 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">DATA_TYPE</span> <span class="special">{</span>
87 <span class="identifier">KEY_TYPE</span> <span class="identifier">key</span><span class="special">;</span>
88 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">data</span><span class="special">;</span>
89 <span class="special">};</span>
94 <pre class="programlisting"><span class="comment">// Casting to an integer before bitshifting</span>
95 <span class="keyword">struct</span> <span class="identifier">rightshift</span> <span class="special">{</span>
96 <span class="keyword">int</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">DATA_TYPE</span> <span class="special">&</span><span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">offset</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
97 <span class="keyword">return</span> <span class="identifier">float_mem_cast</span><span class="special"><</span><span class="identifier">KEY_TYPE</span><span class="special">,</span> <span class="identifier">CAST_TYPE</span><span class="special">>(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">key</span><span class="special">)</span> <span class="special">>></span> <span class="identifier">offset</span><span class="special">;</span>
98 <span class="special">}</span>
99 <span class="special">};</span>
102 Comparison lessthan <code class="computeroutput">operator<</code> functor:
104 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">lessthan</span> <span class="special">{</span>
105 <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">DATA_TYPE</span> <span class="special">&</span><span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">DATA_TYPE</span> <span class="special">&</span><span class="identifier">y</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
106 <span class="keyword">return</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">key</span> <span class="special"><</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">key</span><span class="special">;</span>
107 <span class="special">}</span>
108 <span class="special">};</span>
114 <a href="../../../../../../example/double.cpp" target="_top">Sort doubles.</a>
117 <a href="../../../../../../example/shiftfloatsample.cpp" target="_top">Sort floats using a
118 rightshift functor.</a>
122 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
123 <td align="left"></td>
124 <td align="right"><div class="copyright-footer">Copyright © 2014-2017 Steven
125 Ross, Francisco Tapia, Orson Peters<p>
126 Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
127 Software License, Version 1.0</a>.
132 <div class="spirit-nav">
133 <a accesskey="p" href="integer_sort.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sort_hpp.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="string_sort.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>