]>
Commit | Line | Data |
---|---|---|
3a9019d9 FG |
1 | //! \file\r |
2 | //! \brief integer sort with a rightshift functor reverse sorting example.\r | |
3 | //\r | |
4 | // Copyright Steven Ross 2009-2014.\r | |
5 | //\r | |
6 | // Distributed under the Boost Software License, Version 1.0.\r | |
7 | // (See accompanying file LICENSE_1_0.txt or copy at\r | |
8 | // http://www.boost.org/LICENSE_1_0.txt)\r | |
9 | \r | |
10 | // See http://www.boost.org/libs/sort for library home page.\r | |
11 | \r | |
12 | // Caution: this file contains Quickbook markup as well as code\r | |
13 | // and comments, don't change any of the special comment markups!\r | |
14 | \r | |
15 | #include <boost/sort/spreadsort/spreadsort.hpp>\r | |
16 | #include <time.h>\r | |
17 | #include <stdio.h>\r | |
18 | #include <stdlib.h>\r | |
19 | #include <algorithm>\r | |
20 | #include <vector>\r | |
21 | #include <string>\r | |
22 | #include <fstream>\r | |
23 | #include <iostream>\r | |
24 | #include <functional>\r | |
25 | using namespace boost::sort::spreadsort;\r | |
26 | \r | |
27 | #define DATA_TYPE int\r | |
28 | \r | |
29 | struct negrightshift {\r | |
30 | inline int operator()(const int &x, const unsigned offset) {\r | |
31 | return -(x >> offset);\r | |
32 | }\r | |
33 | };\r | |
34 | \r | |
35 | //Pass in an argument to test std::sort\r | |
36 | int main(int argc, const char ** argv) {\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 | std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);\r | |
47 | if (input.fail()) {\r | |
48 | printf("input.txt could not be opened\n");\r | |
49 | return 1;\r | |
50 | }\r | |
51 | double total = 0.0;\r | |
52 | std::vector<DATA_TYPE> array;\r | |
53 | input.seekg (0, std::ios_base::end);\r | |
54 | size_t length = input.tellg();\r | |
55 | uCount = length/uSize;\r | |
56 | //Run multiple loops, if requested\r | |
57 | for (unsigned u = 0; u < loopCount; ++u) {\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)\r | |
63 | input.read(reinterpret_cast<char *>(&(array[v++])), uSize );\r | |
64 | if (v < uCount)\r | |
65 | array.resize(v);\r | |
66 | clock_t start, end;\r | |
67 | double elapsed;\r | |
68 | start = clock();\r | |
69 | if (stdSort)\r | |
70 | \r | |
71 | //[reverse_int_1\r | |
72 | std::sort(array.begin(), array.end(), std::greater<DATA_TYPE>());\r | |
73 | //] [/reverse_int_1]\r | |
74 | else\r | |
75 | //[reverse_int_2\r | |
76 | integer_sort(array.begin(), array.end(), negrightshift(), std::greater<DATA_TYPE>());\r | |
77 | //] [/reverse_int_2]\r | |
78 | end = clock();\r | |
79 | elapsed = static_cast<double>(end - start) ;\r | |
80 | std::ofstream ofile;\r | |
81 | if (stdSort)\r | |
82 | ofile.open("standard_sort_out.txt", std::ios_base::out |\r | |
83 | std::ios_base::binary | std::ios_base::trunc);\r | |
84 | else\r | |
85 | ofile.open("boost_sort_out.txt", std::ios_base::out |\r | |
86 | std::ios_base::binary | std::ios_base::trunc);\r | |
87 | if (ofile.good()) {\r | |
88 | for (unsigned v = 0; v < array.size(); ++v) {\r | |
89 | ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]));\r | |
90 | }\r | |
91 | ofile.close();\r | |
92 | }\r | |
93 | total += elapsed;\r | |
94 | array.clear();\r | |
95 | }\r | |
96 | if (stdSort)\r | |
97 | printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);\r | |
98 | else\r | |
99 | printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);\r | |
100 | return 0;\r | |
101 | }\r |