1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns=
"http://www.w3.org/1999/xhtml">
4 <meta http-equiv=
"Content-Type" content=
"text/xhtml;charset=UTF-8"/>
5 <meta http-equiv=
"X-UA-Compatible" content=
"IE=9"/>
6 <meta name=
"generator" content=
"Doxygen 1.8.9.1"/>
7 <title>Boost.Sort: I:/modular-boost/libs/sort/include/boost/sort/spreadsort/integer_sort.hpp Source File
</title>
8 <link href=
"tabs.css" rel=
"stylesheet" type=
"text/css"/>
9 <script type=
"text/javascript" src=
"jquery.js"></script>
10 <script type=
"text/javascript" src=
"dynsections.js"></script>
11 <link href=
"search/search.css" rel=
"stylesheet" type=
"text/css"/>
12 <script type=
"text/javascript" src=
"search/searchdata.js"></script>
13 <script type=
"text/javascript" src=
"search/search.js"></script>
14 <script type=
"text/javascript">
15 $(document).ready(function() { init_search(); });
17 <link href=
"doxygen.css" rel=
"stylesheet" type=
"text/css" />
20 <div id=
"top"><!-- do not remove this div, it is closed by doxygen! -->
22 <table cellspacing=
"0" cellpadding=
"0">
24 <tr style=
"height: 56px;">
25 <td style=
"padding-left: 0.5em;">
26 <div id=
"projectname">Boost.Sort
33 <!-- end header part -->
34 <!-- Generated by Doxygen 1.8.9.1 -->
35 <script type=
"text/javascript">
36 var searchBox = new SearchBox(
"searchBox",
"search",false,'Search');
38 <div id=
"navrow1" class=
"tabs">
40 <li><a href=
"index.html"><span>Main
 Page
</span></a></li>
41 <li><a href=
"namespaces.html"><span>Namespaces
</span></a></li>
42 <li><a href=
"annotated.html"><span>Classes
</span></a></li>
43 <li class=
"current"><a href=
"files.html"><span>Files
</span></a></li>
45 <div id=
"MSearchBox" class=
"MSearchBoxInactive">
47 <img id=
"MSearchSelect" src=
"search/mag_sel.png"
48 onmouseover=
"return searchBox.OnSearchSelectShow()"
49 onmouseout=
"return searchBox.OnSearchSelectHide()"
51 <input type=
"text" id=
"MSearchField" value=
"Search" accesskey=
"S"
52 onfocus=
"searchBox.OnSearchFieldFocus(true)"
53 onblur=
"searchBox.OnSearchFieldFocus(false)"
54 onkeyup=
"searchBox.OnSearchFieldChange(event)"/>
55 </span><span class=
"right">
56 <a id=
"MSearchClose" href=
"javascript:searchBox.CloseResultsWindow()"><img id=
"MSearchCloseImg" border=
"0" src=
"search/close.png" alt=
""/></a>
62 <div id=
"navrow2" class=
"tabs2">
64 <li><a href=
"files.html"><span>File
 List
</span></a></li>
65 <li><a href=
"globals.html"><span>File
 Members
</span></a></li>
68 <!-- window showing the filter options -->
69 <div id=
"MSearchSelectWindow"
70 onmouseover=
"return searchBox.OnSearchSelectShow()"
71 onmouseout=
"return searchBox.OnSearchSelectHide()"
72 onkeydown=
"return searchBox.OnSearchSelectKey(event)">
75 <!-- iframe showing the search results (closed by default) -->
76 <div id=
"MSearchResultsWindow">
77 <iframe src=
"javascript:void(0)" frameborder=
"0"
78 name=
"MSearchResults" id=
"MSearchResults">
82 <div id=
"nav-path" class=
"navpath">
84 <li class=
"navelem"><a class=
"el" href=
"dir_d44c64559bbebec7f509842c48db8b23.html">include
</a></li><li class=
"navelem"><a class=
"el" href=
"dir_1878a3f4746a95c6aad317458cc7ef80.html">boost
</a></li><li class=
"navelem"><a class=
"el" href=
"dir_0dcad0da4f36218cbabb216021de9867.html">sort
</a></li><li class=
"navelem"><a class=
"el" href=
"dir_4aa236a92b75d6f514e733718a475329.html">spreadsort
</a></li> </ul>
88 <div class=
"headertitle">
89 <div class=
"title">integer_sort.hpp
</div> </div>
91 <div class=
"contents">
92 <a href=
"integer__sort_8hpp.html">Go to the documentation of this file.
</a><div class=
"fragment"><div class=
"line"><a name=
"l00001"></a><span class=
"lineno"> 1</span> <span class=
"comment">//Templated Spreadsort-based implementation of integer_sort
</span></div>
93 <div class=
"line"><a name=
"l00002"></a><span class=
"lineno"> 2</span> </div>
94 <div class=
"line"><a name=
"l00003"></a><span class=
"lineno"> 3</span> <span class=
"comment">// Copyright Steven J. Ross
2001 -
2014.
</span></div>
95 <div class=
"line"><a name=
"l00004"></a><span class=
"lineno"> 4</span> <span class=
"comment">// Distributed under the Boost Software License, Version
1.0.
</span></div>
96 <div class=
"line"><a name=
"l00005"></a><span class=
"lineno"> 5</span> <span class=
"comment">// (See accompanying file LICENSE_1_0.txt or copy at
</span></div>
97 <div class=
"line"><a name=
"l00006"></a><span class=
"lineno"> 6</span> <span class=
"comment">// http://www.boost.org/LICENSE_1_0.txt)
</span></div>
98 <div class=
"line"><a name=
"l00007"></a><span class=
"lineno"> 7</span> </div>
99 <div class=
"line"><a name=
"l00008"></a><span class=
"lineno"> 8</span> <span class=
"comment">// See http://www.boost.org/libs/sort/ for library home page.
</span></div>
100 <div class=
"line"><a name=
"l00009"></a><span class=
"lineno"> 9</span> </div>
101 <div class=
"line"><a name=
"l00010"></a><span class=
"lineno"> 10</span> <span class=
"comment">/*
</span></div>
102 <div class=
"line"><a name=
"l00011"></a><span class=
"lineno"> 11</span> <span class=
"comment">Some improvements suggested by:
</span></div>
103 <div class=
"line"><a name=
"l00012"></a><span class=
"lineno"> 12</span> <span class=
"comment">Phil Endecott and Frank Gennari
</span></div>
104 <div class=
"line"><a name=
"l00013"></a><span class=
"lineno"> 13</span> <span class=
"comment"></span></div>
105 <div class=
"line"><a name=
"l00014"></a><span class=
"lineno"> 14</span> <span class=
"comment">Doxygen comments by Paul A. Bristow Jan
2015</span></div>
106 <div class=
"line"><a name=
"l00015"></a><span class=
"lineno"> 15</span> <span class=
"comment"></span></div>
107 <div class=
"line"><a name=
"l00016"></a><span class=
"lineno"> 16</span> <span class=
"comment">*/
</span></div>
108 <div class=
"line"><a name=
"l00017"></a><span class=
"lineno"> 17</span> </div>
109 <div class=
"line"><a name=
"l00018"></a><span class=
"lineno"> 18</span> <span class=
"preprocessor">#ifndef BOOST_INTEGER_SORT_HPP
</span></div>
110 <div class=
"line"><a name=
"l00019"></a><span class=
"lineno"> 19</span> <span class=
"preprocessor">#define BOOST_INTEGER_SORT_HPP
</span></div>
111 <div class=
"line"><a name=
"l00020"></a><span class=
"lineno"> 20</span> <span class=
"preprocessor">#include
<algorithm
></span></div>
112 <div class=
"line"><a name=
"l00021"></a><span class=
"lineno"> 21</span> <span class=
"preprocessor">#include
<vector
></span></div>
113 <div class=
"line"><a name=
"l00022"></a><span class=
"lineno"> 22</span> <span class=
"preprocessor">#include
<cstring
></span></div>
114 <div class=
"line"><a name=
"l00023"></a><span class=
"lineno"> 23</span> <span class=
"preprocessor">#include
<limits
></span></div>
115 <div class=
"line"><a name=
"l00024"></a><span class=
"lineno"> 24</span> <span class=
"preprocessor">#include
<boost/static_assert.hpp
></span></div>
116 <div class=
"line"><a name=
"l00025"></a><span class=
"lineno"> 25</span> <span class=
"preprocessor">#include
<boost/sort/spreadsort/detail/constants.hpp
></span></div>
117 <div class=
"line"><a name=
"l00026"></a><span class=
"lineno"> 26</span> <span class=
"preprocessor">#include
<boost/sort/spreadsort/detail/integer_sort.hpp
></span></div>
118 <div class=
"line"><a name=
"l00027"></a><span class=
"lineno"> 27</span> </div>
119 <div class=
"line"><a name=
"l00028"></a><span class=
"lineno"> 28</span> <span class=
"keyword">namespace
</span><a class=
"code" href=
"namespaceboost.html">boost
</a> {
</div>
120 <div class=
"line"><a name=
"l00029"></a><span class=
"lineno"> 29</span> <span class=
"keyword">namespace
</span>sort {
</div>
121 <div class=
"line"><a name=
"l00030"></a><span class=
"lineno"> 30</span>  <span class=
"comment">//Top-level sorting call for integers.
</span></div>
122 <div class=
"line"><a name=
"l00031"></a><span class=
"lineno"> 31</span> </div>
123 <div class=
"line"><a name=
"l00032"></a><span class=
"lineno"> 32</span> <span class=
"comment"></span></div>
124 <div class=
"line"><a name=
"l00033"></a><span class=
"lineno"> 33</span> <span class=
"comment">/*! \brief Integer sort algorithm using random access iterators.
</span></div>
125 <div class=
"line"><a name=
"l00034"></a><span class=
"lineno"> 34</span> <span class=
"comment"> (All variants fall back to @c std::sort if the data size is too small,
< @c detail::min_sort_size).
</span></div>
126 <div class=
"line"><a name=
"l00035"></a><span class=
"lineno"> 35</span> <span class=
"comment"></span></div>
127 <div class=
"line"><a name=
"l00036"></a><span class=
"lineno"> 36</span> <span class=
"comment"> \details @c integer_sort is a fast templated in-place hybrid radix/comparison algorithm,
</span></div>
128 <div class=
"line"><a name=
"l00037"></a><span class=
"lineno"> 37</span> <span class=
"comment">which in testing tends to be roughly
50% to
2X faster than @c std::sort for large tests (
>=
100kB).\n
</span></div>
129 <div class=
"line"><a name=
"l00038"></a><span class=
"lineno"> 38</span> <span class=
"comment">Worst-case performance is
<em
> O(N * (lg(range)/s + s))
</em
>,
</span></div>
130 <div class=
"line"><a name=
"l00039"></a><span class=
"lineno"> 39</span> <span class=
"comment">so @c integer_sort is asymptotically faster
</span></div>
131 <div class=
"line"><a name=
"l00040"></a><span class=
"lineno"> 40</span> <span class=
"comment">than pure comparison-based algorithms. @c s is @c max_splits, which defaults to
11,
</span></div>
132 <div class=
"line"><a name=
"l00041"></a><span class=
"lineno"> 41</span> <span class=
"comment">so its worst-case with default settings for
32-bit integers is
</span></div>
133 <div class=
"line"><a name=
"l00042"></a><span class=
"lineno"> 42</span> <span class=
"comment"><em
> O(N * ((
32/
11)
</em
> slow radix-based iterations fast comparison-based iterations).\n\n
</span></div>
134 <div class=
"line"><a name=
"l00043"></a><span class=
"lineno"> 43</span> <span class=
"comment">Some performance plots of runtime vs. n and log(range) are provided:\n
</span></div>
135 <div class=
"line"><a name=
"l00044"></a><span class=
"lineno"> 44</span> <span class=
"comment"> <a href=
"../../doc/graph/windows_integer_sort.htm
"> windows_integer_sort
</a
></span></div>
136 <div class=
"line"><a name=
"l00045"></a><span class=
"lineno"> 45</span> <span class=
"comment"> \n
</span></div>
137 <div class=
"line"><a name=
"l00046"></a><span class=
"lineno"> 46</span> <span class=
"comment"> <a href=
"../../doc/graph/osx_integer_sort.htm
"> osx_integer_sort
</a
></span></div>
138 <div class=
"line"><a name=
"l00047"></a><span class=
"lineno"> 47</span> <span class=
"comment"></span></div>
139 <div class=
"line"><a name=
"l00048"></a><span class=
"lineno"> 48</span> <span class=
"comment"></span></div>
140 <div class=
"line"><a name=
"l00049"></a><span class=
"lineno"> 49</span> <span class=
"comment"> \tparam RandomAccessIter
<a href=
"http://www.cplusplus.com/reference/iterator/RandomAccessIterator/
">Random access iterator
</a
></span></div>
141 <div class=
"line"><a name=
"l00050"></a><span class=
"lineno"> 50</span> <span class=
"comment"> \param[in] first Iterator pointer to first element.
</span></div>
142 <div class=
"line"><a name=
"l00051"></a><span class=
"lineno"> 51</span> <span class=
"comment"> \param[in] last Iterator pointing to one beyond the end of data.
</span></div>
143 <div class=
"line"><a name=
"l00052"></a><span class=
"lineno"> 52</span> <span class=
"comment"></span></div>
144 <div class=
"line"><a name=
"l00053"></a><span class=
"lineno"> 53</span> <span class=
"comment"> \pre [@c first, @c last) is a valid range.
</span></div>
145 <div class=
"line"><a name=
"l00054"></a><span class=
"lineno"> 54</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is mutable.
</span></div>
146 <div class=
"line"><a name=
"l00055"></a><span class=
"lineno"> 55</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is
<a href=
"http://en.cppreference.com/w/cpp/concept/LessThanComparable
">LessThanComparable
</a
></span></div>
147 <div class=
"line"><a name=
"l00056"></a><span class=
"lineno"> 56</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type supports the @c operator
>>,
</span></div>
148 <div class=
"line"><a name=
"l00057"></a><span class=
"lineno"> 57</span> <span class=
"comment"> which returns an integer-type right-shifted a specified number of bits.
</span></div>
149 <div class=
"line"><a name=
"l00058"></a><span class=
"lineno"> 58</span> <span class=
"comment"> \post The elements in the range [@c first, @c last) are sorted in ascending order.
</span></div>
150 <div class=
"line"><a name=
"l00059"></a><span class=
"lineno"> 59</span> <span class=
"comment"></span></div>
151 <div class=
"line"><a name=
"l00060"></a><span class=
"lineno"> 60</span> <span class=
"comment"> \throws std::exception Propagates exceptions if any of the element comparisons, the element swaps (or moves),
</span></div>
152 <div class=
"line"><a name=
"l00061"></a><span class=
"lineno"> 61</span> <span class=
"comment"> the right shift, subtraction of right-shifted elements, functors, or any operations on iterators throw.
</span></div>
153 <div class=
"line"><a name=
"l00062"></a><span class=
"lineno"> 62</span> <span class=
"comment"></span></div>
154 <div class=
"line"><a name=
"l00063"></a><span class=
"lineno"> 63</span> <span class=
"comment"> \warning Throwing an exception may cause data loss. This will also throw if a small vector resize throws, in which case there will be no data loss.
</span></div>
155 <div class=
"line"><a name=
"l00064"></a><span class=
"lineno"> 64</span> <span class=
"comment"> \warning Invalid arguments cause undefined behaviour.
</span></div>
156 <div class=
"line"><a name=
"l00065"></a><span class=
"lineno"> 65</span> <span class=
"comment"> \note @c spreadsort function provides a wrapper that calls the fastest sorting algorithm available for a data type,
</span></div>
157 <div class=
"line"><a name=
"l00066"></a><span class=
"lineno"> 66</span> <span class=
"comment"> enabling faster generic-programming.
</span></div>
158 <div class=
"line"><a name=
"l00067"></a><span class=
"lineno"> 67</span> <span class=
"comment"></span></div>
159 <div class=
"line"><a name=
"l00068"></a><span class=
"lineno"> 68</span> <span class=
"comment"> \remark The lesser of
<em
> O(N*log(N))
</em
> comparisons and
<em
> O(N*log(K/S + S))
</em
>operations worst-case, where:
</span></div>
160 <div class=
"line"><a name=
"l00069"></a><span class=
"lineno"> 69</span> <span class=
"comment"> \remark * N is @c last - @c first,
</span></div>
161 <div class=
"line"><a name=
"l00070"></a><span class=
"lineno"> 70</span> <span class=
"comment"> \remark * K is the log of the range in bits (
32 for
32-bit integers using their full range),
</span></div>
162 <div class=
"line"><a name=
"l00071"></a><span class=
"lineno"> 71</span> <span class=
"comment"> \remark * S is a constant called max_splits, defaulting to
11 (except for strings where it is the log of the character size).
</span></div>
163 <div class=
"line"><a name=
"l00072"></a><span class=
"lineno"> 72</span> <span class=
"comment"></span></div>
164 <div class=
"line"><a name=
"l00073"></a><span class=
"lineno"> 73</span> <span class=
"comment">*/
</span></div>
165 <div class=
"line"><a name=
"l00074"></a><span class=
"lineno"> 74</span>  <span class=
"keyword">template
</span> <<span class=
"keyword">class
</span> RandomAccessIter
></div>
166 <div class=
"line"><a name=
"l00075"></a><span class=
"lineno"><a class=
"line" href=
"namespaceboost_1_1sort.html#ae6ffbcf932699589fd2b93879f209013"> 75</a></span>  <span class=
"keyword">inline
</span> <span class=
"keywordtype">void
</span> <a class=
"code" href=
"namespaceboost_1_1sort.html#ae6ffbcf932699589fd2b93879f209013">integer_sort
</a>(RandomAccessIter first, RandomAccessIter last)
</div>
167 <div class=
"line"><a name=
"l00076"></a><span class=
"lineno"> 76</span>  {
</div>
168 <div class=
"line"><a name=
"l00077"></a><span class=
"lineno"> 77</span>  <span class=
"comment">// Don
't sort if it
's too small to optimize.
</span></div>
169 <div class=
"line"><a name=
"l00078"></a><span class=
"lineno"> 78</span>  <span class=
"keywordflow">if
</span> (last - first
< detail::min_sort_size)
</div>
170 <div class=
"line"><a name=
"l00079"></a><span class=
"lineno"> 79</span>  std::sort(first, last);
</div>
171 <div class=
"line"><a name=
"l00080"></a><span class=
"lineno"> 80</span>  <span class=
"keywordflow">else
</span></div>
172 <div class=
"line"><a name=
"l00081"></a><span class=
"lineno"> 81</span>  <a class=
"code" href=
"namespaceboost_1_1sort.html#ae50349854aad811f67a540d9b3aa4d4a">detail::integer_sort
</a>(first, last, *first
>> 0);
</div>
173 <div class=
"line"><a name=
"l00082"></a><span class=
"lineno"> 82</span>  }
</div>
174 <div class=
"line"><a name=
"l00083"></a><span class=
"lineno"> 83</span> <span class=
"comment"></span></div>
175 <div class=
"line"><a name=
"l00084"></a><span class=
"lineno"> 84</span> <span class=
"comment">/*! \brief Integer sort algorithm using random access iterators with both right-shift and user-defined comparison operator.
</span></div>
176 <div class=
"line"><a name=
"l00085"></a><span class=
"lineno"> 85</span> <span class=
"comment"> (All variants fall back to @c std::sort if the data size is too small,
< @c detail::min_sort_size).
</span></div>
177 <div class=
"line"><a name=
"l00086"></a><span class=
"lineno"> 86</span> <span class=
"comment"></span></div>
178 <div class=
"line"><a name=
"l00087"></a><span class=
"lineno"> 87</span> <span class=
"comment"> \details @c integer_sort is a fast templated in-place hybrid radix/comparison algorithm,
</span></div>
179 <div class=
"line"><a name=
"l00088"></a><span class=
"lineno"> 88</span> <span class=
"comment">which in testing tends to be roughly
50% to
2X faster than @c std::sort for large tests (
>=
100kB).\n
</span></div>
180 <div class=
"line"><a name=
"l00089"></a><span class=
"lineno"> 89</span> <span class=
"comment">Worst-case performance is
<em
> O(N * (lg(range)/s + s))
</em
>,
</span></div>
181 <div class=
"line"><a name=
"l00090"></a><span class=
"lineno"> 90</span> <span class=
"comment">so @c integer_sort is asymptotically faster
</span></div>
182 <div class=
"line"><a name=
"l00091"></a><span class=
"lineno"> 91</span> <span class=
"comment">than pure comparison-based algorithms. @c s is @c max_splits, which defaults to
11,
</span></div>
183 <div class=
"line"><a name=
"l00092"></a><span class=
"lineno"> 92</span> <span class=
"comment">so its worst-case with default settings for
32-bit integers is
</span></div>
184 <div class=
"line"><a name=
"l00093"></a><span class=
"lineno"> 93</span> <span class=
"comment"><em
> O(N * ((
32/
11)
</em
> slow radix-based iterations fast comparison-based iterations).\n\n
</span></div>
185 <div class=
"line"><a name=
"l00094"></a><span class=
"lineno"> 94</span> <span class=
"comment">Some performance plots of runtime vs. n and log(range) are provided:\n
</span></div>
186 <div class=
"line"><a name=
"l00095"></a><span class=
"lineno"> 95</span> <span class=
"comment"> <a href=
"../../doc/graph/windows_integer_sort.htm
"> windows_integer_sort
</a
></span></div>
187 <div class=
"line"><a name=
"l00096"></a><span class=
"lineno"> 96</span> <span class=
"comment"> \n
</span></div>
188 <div class=
"line"><a name=
"l00097"></a><span class=
"lineno"> 97</span> <span class=
"comment"> <a href=
"../../doc/graph/osx_integer_sort.htm
"> osx_integer_sort
</a
></span></div>
189 <div class=
"line"><a name=
"l00098"></a><span class=
"lineno"> 98</span> <span class=
"comment"></span></div>
190 <div class=
"line"><a name=
"l00099"></a><span class=
"lineno"> 99</span> <span class=
"comment"> \tparam RandomAccessIter
<a href=
"http://www.cplusplus.com/reference/iterator/RandomAccessIterator/
">Random access iterator
</a
></span></div>
191 <div class=
"line"><a name=
"l00100"></a><span class=
"lineno"> 100</span> <span class=
"comment"> \tparam Right_shift functor for right-shift by parameter @c shift bits.
</span></div>
192 <div class=
"line"><a name=
"l00101"></a><span class=
"lineno"> 101</span> <span class=
"comment"> \tparam Comp To provide @c operator
< for user-defined comparison.
</span></div>
193 <div class=
"line"><a name=
"l00102"></a><span class=
"lineno"> 102</span> <span class=
"comment"></span></div>
194 <div class=
"line"><a name=
"l00103"></a><span class=
"lineno"> 103</span> <span class=
"comment"> \param[in] first Iterator pointer to first element.
</span></div>
195 <div class=
"line"><a name=
"l00104"></a><span class=
"lineno"> 104</span> <span class=
"comment"> \param[in] last Iterator pointing to one beyond the end of data.
</span></div>
196 <div class=
"line"><a name=
"l00105"></a><span class=
"lineno"> 105</span> <span class=
"comment"> \param[in] shift Number of bits to right-shift (using functor).
</span></div>
197 <div class=
"line"><a name=
"l00106"></a><span class=
"lineno"> 106</span> <span class=
"comment"> \param[in] comp comparison functor.
</span></div>
198 <div class=
"line"><a name=
"l00107"></a><span class=
"lineno"> 107</span> <span class=
"comment"></span></div>
199 <div class=
"line"><a name=
"l00108"></a><span class=
"lineno"> 108</span> <span class=
"comment"> \pre [@c first, @c last) is a valid range.
</span></div>
200 <div class=
"line"><a name=
"l00109"></a><span class=
"lineno"> 109</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is mutable.
</span></div>
201 <div class=
"line"><a name=
"l00110"></a><span class=
"lineno"> 110</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is
<a href=
"http://en.cppreference.com/w/cpp/concept/LessThanComparable
">LessThanComparable
</a
></span></div>
202 <div class=
"line"><a name=
"l00111"></a><span class=
"lineno"> 111</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type supports the @c operator
>>,
</span></div>
203 <div class=
"line"><a name=
"l00112"></a><span class=
"lineno"> 112</span> <span class=
"comment"> which returns an integer-type right-shifted a specified number of bits.
</span></div>
204 <div class=
"line"><a name=
"l00113"></a><span class=
"lineno"> 113</span> <span class=
"comment"> \post The elements in the range [@c first, @c last) are sorted in ascending order.
</span></div>
205 <div class=
"line"><a name=
"l00114"></a><span class=
"lineno"> 114</span> <span class=
"comment"></span></div>
206 <div class=
"line"><a name=
"l00115"></a><span class=
"lineno"> 115</span> <span class=
"comment"> \return @c void.
</span></div>
207 <div class=
"line"><a name=
"l00116"></a><span class=
"lineno"> 116</span> <span class=
"comment"></span></div>
208 <div class=
"line"><a name=
"l00117"></a><span class=
"lineno"> 117</span> <span class=
"comment"> \throws std::exception Propagates exceptions if any of the element comparisons, the element swaps (or moves),
</span></div>
209 <div class=
"line"><a name=
"l00118"></a><span class=
"lineno"> 118</span> <span class=
"comment"> the right shift, subtraction of right-shifted elements, functors,
</span></div>
210 <div class=
"line"><a name=
"l00119"></a><span class=
"lineno"> 119</span> <span class=
"comment"> or any operations on iterators throw.
</span></div>
211 <div class=
"line"><a name=
"l00120"></a><span class=
"lineno"> 120</span> <span class=
"comment"></span></div>
212 <div class=
"line"><a name=
"l00121"></a><span class=
"lineno"> 121</span> <span class=
"comment"> \warning Throwing an exception may cause data loss. This will also throw if a small vector resize throws, in which case there will be no data loss.
</span></div>
213 <div class=
"line"><a name=
"l00122"></a><span class=
"lineno"> 122</span> <span class=
"comment"> \warning Invalid arguments cause undefined behaviour.
</span></div>
214 <div class=
"line"><a name=
"l00123"></a><span class=
"lineno"> 123</span> <span class=
"comment"> \note @c spreadsort function provides a wrapper that calls the fastest sorting algorithm available for a data type,
</span></div>
215 <div class=
"line"><a name=
"l00124"></a><span class=
"lineno"> 124</span> <span class=
"comment"> enabling faster generic-programming.
</span></div>
216 <div class=
"line"><a name=
"l00125"></a><span class=
"lineno"> 125</span> <span class=
"comment"></span></div>
217 <div class=
"line"><a name=
"l00126"></a><span class=
"lineno"> 126</span> <span class=
"comment"> \remark The lesser of
<em
> O(N*log(N))
</em
> comparisons and
<em
> O(N*log(K/S + S))
</em
>operations worst-case, where:
</span></div>
218 <div class=
"line"><a name=
"l00127"></a><span class=
"lineno"> 127</span> <span class=
"comment"> \remark * N is @c last - @c first,
</span></div>
219 <div class=
"line"><a name=
"l00128"></a><span class=
"lineno"> 128</span> <span class=
"comment"> \remark * K is the log of the range in bits (
32 for
32-bit integers using their full range),
</span></div>
220 <div class=
"line"><a name=
"l00129"></a><span class=
"lineno"> 129</span> <span class=
"comment"> \remark * S is a constant called max_splits, defaulting to
11 (except for strings where it is the log of the character size).
</span></div>
221 <div class=
"line"><a name=
"l00130"></a><span class=
"lineno"> 130</span> <span class=
"comment">*/
</span></div>
222 <div class=
"line"><a name=
"l00131"></a><span class=
"lineno"> 131</span>  <span class=
"keyword">template
</span> <<span class=
"keyword">class
</span> RandomAccessIter,
<span class=
"keyword">class
</span> Right_shift,
<span class=
"keyword">class
</span> Compare
></div>
223 <div class=
"line"><a name=
"l00132"></a><span class=
"lineno"><a class=
"line" href=
"namespaceboost_1_1sort.html#aa4ebb2541be58f9f0fecd8d7c108b817"> 132</a></span>  <span class=
"keyword">inline
</span> <span class=
"keywordtype">void
</span> <a class=
"code" href=
"namespaceboost_1_1sort.html#ae6ffbcf932699589fd2b93879f209013">integer_sort
</a>(RandomAccessIter first, RandomAccessIter last,
</div>
224 <div class=
"line"><a name=
"l00133"></a><span class=
"lineno"> 133</span>  Right_shift shift, Compare comp) {
</div>
225 <div class=
"line"><a name=
"l00134"></a><span class=
"lineno"> 134</span>  <span class=
"keywordflow">if
</span> (last - first
< detail::min_sort_size)
</div>
226 <div class=
"line"><a name=
"l00135"></a><span class=
"lineno"> 135</span>  std::sort(first, last, comp);
</div>
227 <div class=
"line"><a name=
"l00136"></a><span class=
"lineno"> 136</span>  <span class=
"keywordflow">else
</span></div>
228 <div class=
"line"><a name=
"l00137"></a><span class=
"lineno"> 137</span>  <a class=
"code" href=
"namespaceboost_1_1sort.html#ae50349854aad811f67a540d9b3aa4d4a">detail::integer_sort
</a>(first, last, shift(*first,
0), shift, comp);
</div>
229 <div class=
"line"><a name=
"l00138"></a><span class=
"lineno"> 138</span>  }
</div>
230 <div class=
"line"><a name=
"l00139"></a><span class=
"lineno"> 139</span> <span class=
"comment"></span></div>
231 <div class=
"line"><a name=
"l00140"></a><span class=
"lineno"> 140</span> <span class=
"comment">/*! \brief Integer sort algorithm using random access iterators with just right-shift functor.
</span></div>
232 <div class=
"line"><a name=
"l00141"></a><span class=
"lineno"> 141</span> <span class=
"comment"> (All variants fall back to @c std::sort if the data size is too small,
< @c detail::min_sort_size).
</span></div>
233 <div class=
"line"><a name=
"l00142"></a><span class=
"lineno"> 142</span> <span class=
"comment"></span></div>
234 <div class=
"line"><a name=
"l00143"></a><span class=
"lineno"> 143</span> <span class=
"comment"> \details @c integer_sort is a fast templated in-place hybrid radix/comparison algorithm,
</span></div>
235 <div class=
"line"><a name=
"l00144"></a><span class=
"lineno"> 144</span> <span class=
"comment">which in testing tends to be roughly
50% to
2X faster than @c std::sort for large tests (
>=
100kB).\n
</span></div>
236 <div class=
"line"><a name=
"l00145"></a><span class=
"lineno"> 145</span> <span class=
"comment"></span></div>
237 <div class=
"line"><a name=
"l00146"></a><span class=
"lineno"> 146</span> <span class=
"comment">\par Performance:
</span></div>
238 <div class=
"line"><a name=
"l00147"></a><span class=
"lineno"> 147</span> <span class=
"comment">Worst-case performance is
<em
> O(N * (lg(range)/s + s))
</em
>,
</span></div>
239 <div class=
"line"><a name=
"l00148"></a><span class=
"lineno"> 148</span> <span class=
"comment">so @c integer_sort is asymptotically faster
</span></div>
240 <div class=
"line"><a name=
"l00149"></a><span class=
"lineno"> 149</span> <span class=
"comment">than pure comparison-based algorithms. @c s is @c max_splits, which defaults to
11,
</span></div>
241 <div class=
"line"><a name=
"l00150"></a><span class=
"lineno"> 150</span> <span class=
"comment">so its worst-case with default settings for
32-bit integers is
</span></div>
242 <div class=
"line"><a name=
"l00151"></a><span class=
"lineno"> 151</span> <span class=
"comment"><em
> O(N * ((
32/
11)
</em
> slow radix-based iterations fast comparison-based iterations).\n\n
</span></div>
243 <div class=
"line"><a name=
"l00152"></a><span class=
"lineno"> 152</span> <span class=
"comment">Some performance plots of runtime vs. n and log(range) are provided:\n
</span></div>
244 <div class=
"line"><a name=
"l00153"></a><span class=
"lineno"> 153</span> <span class=
"comment"> *
<a href=
"../../doc/graph/windows_integer_sort.htm
"> windows_integer_sort
</a
>\n
</span></div>
245 <div class=
"line"><a name=
"l00154"></a><span class=
"lineno"> 154</span> <span class=
"comment"> *
<a href=
"../../doc/graph/osx_integer_sort.htm
"> osx_integer_sort
</a
></span></div>
246 <div class=
"line"><a name=
"l00155"></a><span class=
"lineno"> 155</span> <span class=
"comment"></span></div>
247 <div class=
"line"><a name=
"l00156"></a><span class=
"lineno"> 156</span> <span class=
"comment"> \tparam RandomAccessIter
<a href=
"http://www.cplusplus.com/reference/iterator/RandomAccessIterator/
">Random access iterator
</a
></span></div>
248 <div class=
"line"><a name=
"l00157"></a><span class=
"lineno"> 157</span> <span class=
"comment"> \tparam Right_shift functor for right-shift by parameter @c shift bits.
</span></div>
249 <div class=
"line"><a name=
"l00158"></a><span class=
"lineno"> 158</span> <span class=
"comment"></span></div>
250 <div class=
"line"><a name=
"l00159"></a><span class=
"lineno"> 159</span> <span class=
"comment"> \param[in] first Iterator pointer to first element.
</span></div>
251 <div class=
"line"><a name=
"l00160"></a><span class=
"lineno"> 160</span> <span class=
"comment"> \param[in] last Iterator pointing to one beyond the end of data.
</span></div>
252 <div class=
"line"><a name=
"l00161"></a><span class=
"lineno"> 161</span> <span class=
"comment"> \param[in] shift Number of bits to right-shift (using functor).
</span></div>
253 <div class=
"line"><a name=
"l00162"></a><span class=
"lineno"> 162</span> <span class=
"comment"></span></div>
254 <div class=
"line"><a name=
"l00163"></a><span class=
"lineno"> 163</span> <span class=
"comment"> \pre [@c first, @c last) is a valid range.
</span></div>
255 <div class=
"line"><a name=
"l00164"></a><span class=
"lineno"> 164</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is mutable.
</span></div>
256 <div class=
"line"><a name=
"l00165"></a><span class=
"lineno"> 165</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type is
<a href=
"http://en.cppreference.com/w/cpp/concept/LessThanComparable
">LessThanComparable
</a
></span></div>
257 <div class=
"line"><a name=
"l00166"></a><span class=
"lineno"> 166</span> <span class=
"comment"> \pre @c RandomAccessIter @c value_type supports the @c operator
>>,
</span></div>
258 <div class=
"line"><a name=
"l00167"></a><span class=
"lineno"> 167</span> <span class=
"comment"> which returns an integer-type right-shifted a specified number of bits.
</span></div>
259 <div class=
"line"><a name=
"l00168"></a><span class=
"lineno"> 168</span> <span class=
"comment"> \post The elements in the range [@c first, @c last) are sorted in ascending order.
</span></div>
260 <div class=
"line"><a name=
"l00169"></a><span class=
"lineno"> 169</span> <span class=
"comment"></span></div>
261 <div class=
"line"><a name=
"l00170"></a><span class=
"lineno"> 170</span> <span class=
"comment"> \throws std::exception Propagates exceptions if any of the element comparisons, the element swaps (or moves),
</span></div>
262 <div class=
"line"><a name=
"l00171"></a><span class=
"lineno"> 171</span> <span class=
"comment"> the right shift, subtraction of right-shifted elements, functors,
</span></div>
263 <div class=
"line"><a name=
"l00172"></a><span class=
"lineno"> 172</span> <span class=
"comment"> or any operations on iterators throw.
</span></div>
264 <div class=
"line"><a name=
"l00173"></a><span class=
"lineno"> 173</span> <span class=
"comment"></span></div>
265 <div class=
"line"><a name=
"l00174"></a><span class=
"lineno"> 174</span> <span class=
"comment"> \warning Throwing an exception may cause data loss. This will also throw if a small vector resize throws, in which case there will be no data loss.
</span></div>
266 <div class=
"line"><a name=
"l00175"></a><span class=
"lineno"> 175</span> <span class=
"comment"> \warning Invalid arguments cause undefined behaviour.
</span></div>
267 <div class=
"line"><a name=
"l00176"></a><span class=
"lineno"> 176</span> <span class=
"comment"> \note @c spreadsort function provides a wrapper that calls the fastest sorting algorithm available for a data type,
</span></div>
268 <div class=
"line"><a name=
"l00177"></a><span class=
"lineno"> 177</span> <span class=
"comment"> enabling faster generic-programming.
</span></div>
269 <div class=
"line"><a name=
"l00178"></a><span class=
"lineno"> 178</span> <span class=
"comment"></span></div>
270 <div class=
"line"><a name=
"l00179"></a><span class=
"lineno"> 179</span> <span class=
"comment"> \remark The lesser of
<em
> O(N*log(N))
</em
> comparisons and
<em
> O(N*log(K/S + S))
</em
>operations worst-case, where:
</span></div>
271 <div class=
"line"><a name=
"l00180"></a><span class=
"lineno"> 180</span> <span class=
"comment"> \remark * N is @c last - @c first,
</span></div>
272 <div class=
"line"><a name=
"l00181"></a><span class=
"lineno"> 181</span> <span class=
"comment"> \remark * K is the log of the range in bits (
32 for
32-bit integers using their full range),
</span></div>
273 <div class=
"line"><a name=
"l00182"></a><span class=
"lineno"> 182</span> <span class=
"comment"> \remark * S is a constant called max_splits, defaulting to
11 (except for strings where it is the log of the character size).
</span></div>
274 <div class=
"line"><a name=
"l00183"></a><span class=
"lineno"> 183</span> <span class=
"comment"></span></div>
275 <div class=
"line"><a name=
"l00184"></a><span class=
"lineno"> 184</span> <span class=
"comment">*/
</span></div>
276 <div class=
"line"><a name=
"l00185"></a><span class=
"lineno"> 185</span>  <span class=
"keyword">template
</span> <<span class=
"keyword">class
</span> RandomAccessIter,
<span class=
"keyword">class
</span> Right_shift
></div>
277 <div class=
"line"><a name=
"l00186"></a><span class=
"lineno"><a class=
"line" href=
"namespaceboost_1_1sort.html#ae50349854aad811f67a540d9b3aa4d4a"> 186</a></span>  <span class=
"keyword">inline
</span> <span class=
"keywordtype">void
</span> <a class=
"code" href=
"namespaceboost_1_1sort.html#ae6ffbcf932699589fd2b93879f209013">integer_sort
</a>(RandomAccessIter first, RandomAccessIter last,
</div>
278 <div class=
"line"><a name=
"l00187"></a><span class=
"lineno"> 187</span>  Right_shift shift) {
</div>
279 <div class=
"line"><a name=
"l00188"></a><span class=
"lineno"> 188</span>  <span class=
"keywordflow">if
</span> (last - first
< detail::min_sort_size)
</div>
280 <div class=
"line"><a name=
"l00189"></a><span class=
"lineno"> 189</span>  std::sort(first, last);
</div>
281 <div class=
"line"><a name=
"l00190"></a><span class=
"lineno"> 190</span>  <span class=
"keywordflow">else
</span></div>
282 <div class=
"line"><a name=
"l00191"></a><span class=
"lineno"> 191</span>  <a class=
"code" href=
"namespaceboost_1_1sort.html#ae50349854aad811f67a540d9b3aa4d4a">detail::integer_sort
</a>(first, last, shift(*first,
0), shift);
</div>
283 <div class=
"line"><a name=
"l00192"></a><span class=
"lineno"> 192</span>  }
</div>
284 <div class=
"line"><a name=
"l00193"></a><span class=
"lineno"> 193</span> }
</div>
285 <div class=
"line"><a name=
"l00194"></a><span class=
"lineno"> 194</span> }
</div>
286 <div class=
"line"><a name=
"l00195"></a><span class=
"lineno"> 195</span> </div>
287 <div class=
"line"><a name=
"l00196"></a><span class=
"lineno"> 196</span> <span class=
"preprocessor">#endif
</span></div>
288 <div class=
"line"><a name=
"l00197"></a><span class=
"lineno"> 197</span> </div>
289 <div class=
"ttc" id=
"namespaceboost_1_1sort_html_ae6ffbcf932699589fd2b93879f209013"><div class=
"ttname"><a href=
"namespaceboost_1_1sort.html#ae6ffbcf932699589fd2b93879f209013">boost::sort::integer_sort
</a></div><div class=
"ttdeci">void integer_sort(RandomAccessIter first, RandomAccessIter last)
</div><div class=
"ttdoc">Integer sort algorithm using random access iterators. (All variants fall back to std::sort if the dat...
</div><div class=
"ttdef"><b>Definition:
</b> integer_sort.hpp:
75</div></div>
290 <div class=
"ttc" id=
"namespaceboost_html"><div class=
"ttname"><a href=
"namespaceboost.html">boost
</a></div><div class=
"ttdef"><b>Definition:
</b> float_sort.hpp:
27</div></div>
291 <div class=
"ttc" id=
"namespaceboost_1_1sort_html_ae50349854aad811f67a540d9b3aa4d4a"><div class=
"ttname"><a href=
"namespaceboost_1_1sort.html#ae50349854aad811f67a540d9b3aa4d4a">boost::sort::integer_sort
</a></div><div class=
"ttdeci">void integer_sort(RandomAccessIter first, RandomAccessIter last, Right_shift shift)
</div><div class=
"ttdoc">Integer sort algorithm using random access iterators with just right-shift functor. (All variants fall back to std::sort if the data size is too small, < detail::min_sort_size).
</div><div class=
"ttdef"><b>Definition:
</b> integer_sort.hpp:
186</div></div>
292 </div><!-- fragment --></div><!-- contents -->
293 <!-- start footer part -->
294 <hr class=
"footer"/><address class=
"footer"><small>
295 Generated on Fri Jan
9 2015 14:
20:
24 for Boost.Sort by
 <a href=
"http://www.doxygen.org/index.html">
296 <img class=
"footer" src=
"doxygen.png" alt=
"doxygen"/>