]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/floatfunctorsample.cpp
1 // spreadsort float functor sorting example.
3 // Copyright Steven Ross 2009.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/sort for library home page.
11 // Caution: this file contains Quickbook markup as well as code
12 // and comments, don't change any of the special comment markups!
14 #include <boost/sort/spreadsort/spreadsort.hpp>
25 using namespace boost::sort::spreadsort
;
27 //[float_functor_types
29 #define KEY_TYPE float
30 //] [/float_functor_types]
33 //[float_functor_datatypes
38 //] [/float_functor_datatypes]
41 //[float_functor_rightshift
42 // Casting to an integer before bitshifting
44 int operator()(const DATA_TYPE
&x
, const unsigned offset
) const {
45 return float_mem_cast
<KEY_TYPE
, CAST_TYPE
>(x
.key
) >> offset
;
48 //] [/float_functor_rightshift]
50 //[float_functor_lessthan
52 bool operator()(const DATA_TYPE
&x
, const DATA_TYPE
&y
) const {
56 //] [/float_functor_lessthan]
58 // Pass in an argument to test std::sort
59 // Note that this converts NaNs and -0.0 to 0.0, so that sorting results are
60 // identical every time
61 int main(int argc
, const char ** argv
) {
62 size_t uCount
,uSize
=sizeof(DATA_TYPE
);
64 unsigned loopCount
= 1;
65 for (int u
= 1; u
< argc
; ++u
) {
66 if (std::string(argv
[u
]) == "-std")
69 loopCount
= atoi(argv
[u
]);
71 std::ifstream
input("input.txt", std::ios_base::in
| std::ios_base::binary
);
73 printf("input.txt could not be opened\n");
77 std::vector
<DATA_TYPE
> array
;
78 input
.seekg (0, std::ios_base::end
);
79 size_t length
= input
.tellg();
80 uCount
= length
/uSize
;
81 //Run multiple loops, if requested
82 for (unsigned u
= 0; u
< loopCount
; ++u
) {
83 input
.seekg (0, std::ios_base::beg
);
84 //Conversion to a vector
87 while (input
.good() && v
< uCount
) {
88 input
.read(reinterpret_cast<char *>(&(array
[v
].key
)),
89 sizeof(array
[v
].key
));
90 //Checking for denormalized numbers; float_sort looks too fast on them.
91 if (!(float_mem_cast
<KEY_TYPE
, CAST_TYPE
>(array
[v
].key
) & 0x7f800000)) {
92 //Make the top exponent bit high
93 CAST_TYPE temp
= 0x40000000 |
94 float_mem_cast
<KEY_TYPE
, CAST_TYPE
>(array
[v
].key
);
95 memcpy(&(array
[v
].key
), &temp
, sizeof(KEY_TYPE
));
97 //Testcase doesn't sort NaNs; they just cause confusion
98 if (!(array
[v
].key
< 0.0) && !(0.0 < array
[v
].key
))
100 //Adding the data, in this case a string
101 std::stringstream intstr
;
102 intstr
<< array
[v
].key
;
103 array
[v
].data
= intstr
.str();
110 std::sort(array
.begin(), array
.end(), lessthan());
112 float_sort(array
.begin(), array
.end(), rightshift(), lessthan());
114 elapsed
= static_cast<double>(end
- start
) ;
117 ofile
.open("standard_sort_out.txt", std::ios_base::out
|
118 std::ios_base::binary
| std::ios_base::trunc
);
120 ofile
.open("boost_sort_out.txt", std::ios_base::out
|
121 std::ios_base::binary
| std::ios_base::trunc
);
123 for (unsigned v
= 0; v
< array
.size(); ++v
) {
124 ofile
.write(reinterpret_cast<char *>(&(array
[v
].key
)),
125 sizeof(array
[v
].key
));
126 ofile
<< array
[v
].data
;
134 printf("std::sort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);
136 printf("spreadsort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);