| 1 | // spreadsort string functor reverse sorting example. |
| 2 | // |
| 3 | // Copyright Steven Ross 2009-2014. |
| 4 | // |
| 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) |
| 8 | |
| 9 | // See http://www.boost.org/libs/sort for library home page. |
| 10 | |
| 11 | #include <boost/sort/spreadsort/string_sort.hpp> |
| 12 | #include <time.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <algorithm> |
| 16 | #include <vector> |
| 17 | #include <iostream> |
| 18 | #include <fstream> |
| 19 | #include <string> |
| 20 | using std::string; |
| 21 | using namespace boost::sort::spreadsort; |
| 22 | |
| 23 | struct DATA_TYPE { |
| 24 | string a; |
| 25 | }; |
| 26 | |
| 27 | struct greaterthan { |
| 28 | inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const { |
| 29 | return x.a > y.a; |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | struct bracket { |
| 34 | inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const { |
| 35 | return x.a[offset]; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | struct getsize { |
| 40 | inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); } |
| 41 | }; |
| 42 | |
| 43 | //Pass in an argument to test std::sort |
| 44 | int main(int argc, const char ** argv) { |
| 45 | std::ifstream indata; |
| 46 | std::ofstream outfile; |
| 47 | bool stdSort = false; |
| 48 | unsigned loopCount = 1; |
| 49 | for (int u = 1; u < argc; ++u) { |
| 50 | if (std::string(argv[u]) == "-std" ) |
| 51 | stdSort = true; |
| 52 | else |
| 53 | loopCount = atoi(nptr: argv[u]); |
| 54 | } |
| 55 | double total = 0.0; |
| 56 | //Run multiple loops, if requested |
| 57 | std::vector<DATA_TYPE> array; |
| 58 | for (unsigned u = 0; u < loopCount; ++u) { |
| 59 | indata.open(s: "input.txt" , mode: std::ios_base::in | std::ios_base::binary); |
| 60 | if (!indata) { |
| 61 | printf(format: "input.txt could not be opened\n" ); |
| 62 | return 1; |
| 63 | } |
| 64 | DATA_TYPE inval; |
| 65 | indata >> inval.a; |
| 66 | while (!indata.eof() ) { |
| 67 | array.push_back(x: inval); |
| 68 | //Inserting embedded nulls and empty strings |
| 69 | if (!(array.size() % 100)) { |
| 70 | if (inval.a.empty() || !(array.size() % 1000)) { |
| 71 | inval.a = "" ; |
| 72 | array.push_back(x: inval); |
| 73 | } |
| 74 | else { |
| 75 | inval.a[0] = '\0'; |
| 76 | array.push_back(x: inval); |
| 77 | } |
| 78 | } |
| 79 | indata >> inval.a; |
| 80 | } |
| 81 | |
| 82 | indata.close(); |
| 83 | clock_t start, end; |
| 84 | double elapsed; |
| 85 | start = clock(); |
| 86 | if (stdSort) |
| 87 | std::sort(first: array.begin(), last: array.end(), comp: greaterthan()); |
| 88 | else |
| 89 | reverse_string_sort(first: array.begin(), last: array.end(), get_character: bracket(), length: getsize(), |
| 90 | comp: greaterthan()); |
| 91 | end = clock(); |
| 92 | elapsed = static_cast<double>(end - start); |
| 93 | if (stdSort) |
| 94 | outfile.open(s: "standard_sort_out.txt" , mode: std::ios_base::out | |
| 95 | std::ios_base::binary | std::ios_base::trunc); |
| 96 | else |
| 97 | outfile.open(s: "boost_sort_out.txt" , mode: std::ios_base::out | |
| 98 | std::ios_base::binary | std::ios_base::trunc); |
| 99 | if (outfile.good()) { |
| 100 | for (unsigned u = 0; u < array.size(); ++u) |
| 101 | outfile << array[u].a << "\n" ; |
| 102 | outfile.close(); |
| 103 | } |
| 104 | total += elapsed; |
| 105 | array.clear(); |
| 106 | } |
| 107 | if (stdSort) |
| 108 | printf(format: "std::sort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 109 | else |
| 110 | printf(format: "spreadsort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 111 | return 0; |
| 112 | } |
| 113 | |