]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/sort/example/mostlysorted.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / mostlysorted.cpp
CommitLineData
3a9019d9
FG
1// spreadsort on a mostly sorted array 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 <sstream>\r
20#include <iostream>\r
21using namespace boost::sort::spreadsort;\r
22\r
23#define DATA_TYPE int\r
24\r
25unsigned\r
26get_index(unsigned count)\r
27{\r
28 unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16));\r
29 if (result >= count || result < 0)\r
30 result = count - 1;\r
31 return result;\r
32}\r
33\r
34//Pass in an argument to test std::sort\r
35int main(int argc, const char ** argv) {\r
36 srand(1);\r
37 size_t uCount,uSize=sizeof(DATA_TYPE);\r
38 bool stdSort = false;\r
39 unsigned loopCount = 1;\r
40 for (int u = 1; u < argc; ++u) {\r
41 if (std::string(argv[u]) == "-std")\r
42 stdSort = true;\r
43 else\r
44 loopCount = atoi(argv[u]);\r
45 }\r
46 //Sorts the data once, then times sorting of already-sorted data\r
47 loopCount += 1;\r
48 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);\r
49 if (input.fail()) {\r
50 printf("input.txt could not be opened\n");\r
51 return 1;\r
52 }\r
53 double total = 0.0;\r
54 std::vector<DATA_TYPE> array;\r
55 input.seekg (0, std::ios_base::end);\r
56 size_t length = input.tellg();\r
57 uCount = length/uSize;\r
58 input.seekg (0, std::ios_base::beg);\r
59 //Conversion to a vector\r
60 array.resize(uCount);\r
61 unsigned v = 0;\r
62 while (input.good() && v < uCount) // EOF or failure stops the reading\r
63 input.read(reinterpret_cast<char *>(&(array[v++])), uSize );\r
64 //Run multiple loops, if requested\r
65 for (unsigned u = 0; u < loopCount; ++u) {\r
66 for (unsigned v = 0; v < uCount/10; ++v)\r
67 std::swap(array[get_index(uCount)], array[get_index(uCount)]);\r
68 clock_t start, end;\r
69 double elapsed;\r
70 start = clock();\r
71 if (stdSort)\r
72 //std::sort(&(array[0]), &(array[0]) + uCount);\r
73 std::sort(array.begin(), array.end());\r
74 else\r
75 //integer_sort(&(array[0]), &(array[0]) + uCount);\r
76 integer_sort(array.begin(), array.end());\r
77 end = clock();\r
78 elapsed = static_cast<double>(end - start) ;\r
79 std::ofstream ofile;\r
80 if (stdSort)\r
81 ofile.open("standard_sort_out.txt", std::ios_base::out |\r
82 std::ios_base::binary | std::ios_base::trunc);\r
83 else\r
84 ofile.open("boost_sort_out.txt", std::ios_base::out |\r
85 std::ios_base::binary | std::ios_base::trunc);\r
86 if (ofile.good()) {\r
87 for (unsigned v = 0; v < array.size(); ++v) {\r
88 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );\r
89 }\r
90 ofile.close();\r
91 }\r
92 if (u)\r
93 total += elapsed;\r
94 }\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