]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/sort/example/floatsample.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / floatsample.cpp
CommitLineData
3a9019d9
FG
1// spreadsort float sorting example.\r
2//\r
3// Copyright Steven Ross 2009-2014.\r
4//\r
5// Distributed under the Boost Software License, Version 1.0.\r
6// (See accompanying file LICENSE_1_0.txt or copy at\r
7// http://www.boost.org/LICENSE_1_0.txt)\r
8\r
9// See http://www.boost.org/libs/sort for library home page.\r
10\r
11#include <boost/sort/spreadsort/spreadsort.hpp>\r
12#include <time.h>\r
13#include <stdio.h>\r
14#include <stdlib.h>\r
15#include <algorithm>\r
16#include <vector>\r
17#include <string>\r
18#include <fstream>\r
19#include <iostream>\r
20using namespace boost::sort::spreadsort;\r
21\r
22#define DATA_TYPE float\r
23#define CAST_TYPE int\r
24\r
25//Pass in an argument to test std::sort\r
26//Note that this converts NaNs and -0.0 to 0.0, so that sorting results are\r
27//identical every time\r
28int main(int argc, const char ** argv) {\r
29 size_t uCount,uSize=sizeof(DATA_TYPE);\r
30 bool stdSort = false;\r
31 unsigned loopCount = 1;\r
32 for (int u = 1; u < argc; ++u) {\r
33 if (std::string(argv[u]) == "-std")\r
34 stdSort = true;\r
35 else\r
36 loopCount = atoi(argv[u]);\r
37 }\r
38 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);\r
39 if (input.fail()) {\r
40 printf("input.txt could not be opened\n");\r
41 return 1;\r
42 }\r
43 double total = 0.0;\r
44 std::vector<DATA_TYPE> array;\r
45 input.seekg (0, std::ios_base::end);\r
46 size_t length = input.tellg();\r
47 uCount = length/uSize;\r
48 //Run multiple loops, if requested\r
49 for (unsigned u = 0; u < loopCount; ++u) {\r
50 input.seekg (0, std::ios_base::beg);\r
51 //Conversion to a vector\r
52 array.resize(uCount);\r
53 unsigned v = 0;\r
54 while (input.good() && v < uCount) {\r
55 input.read(reinterpret_cast<char *>(&(array[v])), uSize );\r
56 //Checking for denormalized numbers\r
57 if (!(float_mem_cast<float, int>(array[v]) & 0x7f800000)) {\r
58 //Make the top exponent bit high\r
59 CAST_TYPE temp = 0x40000000 | float_mem_cast<float, int>(array[v]);\r
60 memcpy(&(array[v]), &temp, sizeof(DATA_TYPE));\r
61 }\r
62 //Testcase doesn't sort NaNs; they just cause confusion\r
63 if (!(array[v] < 0.0) && !(0.0 < array[v]))\r
64 array[v] = 0.0;\r
65 ++v;\r
66 }\r
67 clock_t start, end;\r
68 double elapsed;\r
69 start = clock();\r
70 if (stdSort)\r
71 //std::sort(&(array[0]), &(array[0]) + uCount);\r
72 std::sort(array.begin(), array.end());\r
73 else\r
74 //boost::sort::spreadsort::spreadsort(&(array[0]), &(array[0]) + uCount);\r
75 boost::sort::spreadsort::spreadsort(array.begin(), array.end());\r
76 end = clock();\r
77 elapsed = static_cast<double>(end - start) ;\r
78 std::ofstream ofile;\r
79 if (stdSort)\r
80 ofile.open("standard_sort_out.txt", std::ios_base::out |\r
81 std::ios_base::binary | std::ios_base::trunc);\r
82 else\r
83 ofile.open("boost_sort_out.txt", std::ios_base::out |\r
84 std::ios_base::binary | std::ios_base::trunc);\r
85 if (ofile.good()) {\r
86 for (unsigned v = 0; v < array.size(); ++v) {\r
87 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );\r
88 }\r
89 ofile.close();\r
90 }\r
91 total += elapsed;\r
92 array.clear();\r
93 }\r
94 input.close();\r
95 if (stdSort)\r
96 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
97 else\r
98 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
99 return 0;\r
100}\r