]>
Commit | Line | Data |
---|---|---|
3a9019d9 FG |
1 | // spreadsort reverse string 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/string_sort.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 <iostream>\r | |
18 | #include <fstream>\r | |
19 | #include <string>\r | |
20 | using std::string;\r | |
21 | using namespace boost::sort::spreadsort;\r | |
22 | \r | |
23 | #define DATA_TYPE string\r | |
24 | \r | |
25 | //Pass in an argument to test std::sort\r | |
26 | int main(int argc, const char ** argv) {\r | |
27 | std::ifstream indata;\r | |
28 | std::ofstream outfile;\r | |
29 | bool stdSort = false;\r | |
30 | unsigned loopCount = 1;\r | |
31 | for (int u = 1; u < argc; ++u) {\r | |
32 | if (std::string(argv[u]) == "-std")\r | |
33 | stdSort = true;\r | |
34 | else\r | |
35 | loopCount = atoi(argv[u]);\r | |
36 | }\r | |
37 | double total = 0.0;\r | |
38 | //Run multiple loops, if requested\r | |
39 | std::vector<DATA_TYPE> array;\r | |
40 | for (unsigned u = 0; u < loopCount; ++u) {\r | |
41 | indata.open("input.txt", std::ios_base::in | std::ios_base::binary); \r | |
42 | if (!indata) {\r | |
43 | printf("input.txt could not be opened\n");\r | |
44 | return 1;\r | |
45 | }\r | |
46 | DATA_TYPE inval;\r | |
47 | indata >> inval;\r | |
48 | while (!indata.eof() ) {\r | |
49 | //testing substrings\r | |
50 | if (!(array.size() % 2))\r | |
51 | inval = "prefix" + inval;\r | |
52 | else\r | |
53 | inval += "suffix";\r | |
54 | array.push_back(inval);\r | |
55 | //Inserting embedded nulls and empty strings\r | |
56 | if (!(array.size() % 100)) {\r | |
57 | if (inval.empty() || !(array.size() % 1000))\r | |
58 | array.push_back("");\r | |
59 | else {\r | |
60 | inval[0] = '\0';\r | |
61 | array.push_back(inval);\r | |
62 | }\r | |
63 | }\r | |
64 | indata >> inval;\r | |
65 | }\r | |
66 | \r | |
67 | indata.close();\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(), std::greater<string>());\r | |
74 | else\r | |
75 | //string_sort(&(array[0]), &(array[0]) + uCount);\r | |
76 | reverse_string_sort(array.begin(), array.end(), std::greater<string>());\r | |
77 | end = clock();\r | |
78 | elapsed = static_cast<double>(end - start);\r | |
79 | if (stdSort)\r | |
80 | outfile.open("standard_sort_out.txt", std::ios_base::out |\r | |
81 | std::ios_base::binary | std::ios_base::trunc);\r | |
82 | else\r | |
83 | outfile.open("boost_sort_out.txt", std::ios_base::out |\r | |
84 | std::ios_base::binary | std::ios_base::trunc);\r | |
85 | if (outfile.good()) {\r | |
86 | for (unsigned u = 0; u < array.size(); ++u)\r | |
87 | outfile << array[u] << "\n";\r | |
88 | outfile.close();\r | |
89 | }\r | |
90 | total += elapsed;\r | |
91 | array.clear();\r | |
92 | }\r | |
93 | if (stdSort)\r | |
94 | printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);\r | |
95 | else\r | |
96 | printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);\r | |
97 | return 0;\r | |
98 | }\r |