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