1 //! \brief spreadsort string functor sorting example.
2 //! \file
3 //
4 //  Copyright Steven Ross 2009-2014.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 //    (See accompanying file LICENSE_1_0.txt or copy at
8 //          http://www.boost.org/LICENSE_1_0.txt)
9 
10 //  See http://www.boost.org/libs/sort for library home page.
11 
12 // Caution: this file contains Quickbook markup as well as code
13 // and comments, don't change any of the special comment markups!
14 
15 #include <boost/sort/spreadsort/string_sort.hpp>
16 #include <time.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <algorithm>
20 #include <vector>
21 #include <iostream>
22 #include <fstream>
23 #include <string>
24 using std::string;
25 using namespace boost::sort::spreadsort;
26 
27 struct DATA_TYPE {
28     string a;
29 };
30 
31 //[lessthan_functor
32 
33 struct lessthan {
operator ()lessthan34   inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
35     return x.a < y.a;
36   }
37 };
38 //] [/lessthan_functor]
39 
40 //[bracket_functor
41 struct bracket {
operator ()bracket42   inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
43     return x.a[offset];
44   }
45 };
46 //] [/bracket_functor]
47 
48 //[getsize_functor
49 struct getsize {
operator ()getsize50   inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); }
51 };
52 //] [/getsize_functor]
53 
54 //Pass in an argument to test std::sort
main(int argc,const char ** argv)55 int main(int argc, const char ** argv) {
56   std::ifstream indata;
57   std::ofstream outfile;
58   bool stdSort = false;
59   unsigned loopCount = 1;
60   for (int u = 1; u < argc; ++u) {
61     if (std::string(argv[u]) == "-std")
62       stdSort = true;
63     else
64       loopCount = atoi(argv[u]);
65   }
66   double total = 0.0;
67   //Run multiple loops, if requested
68   std::vector<DATA_TYPE> array;
69   for (unsigned u = 0; u < loopCount; ++u) {
70     indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
71     if (indata.bad()) {
72       printf("input.txt could not be opened\n");
73       return 1;
74     }
75     DATA_TYPE inval;
76     indata >> inval.a;
77     while (!indata.eof() ) {
78       array.push_back(inval);
79       //Inserting embedded nulls and empty strings
80       if (!(array.size() % 100)) {
81         if (inval.a.empty() || !(array.size() % 1000)) {
82           inval.a = "";
83           array.push_back(inval);
84         }
85         else {
86           inval.a[0] = '\0';
87           array.push_back(inval);
88         }
89       }
90       indata >> inval.a;
91     }
92 
93     indata.close();
94     clock_t start, end;
95     double elapsed;
96     start = clock();
97     if (stdSort) {
98       std::sort(array.begin(), array.end(), lessthan());
99     } else {
100 //[stringsort_functors_call
101       string_sort(array.begin(), array.end(), bracket(), getsize(), lessthan());
102 //] [/stringsort_functors_call]
103     }
104     end = clock();
105     elapsed = static_cast<double>(end - start);
106     if (stdSort) {
107       outfile.open("standard_sort_out.txt", std::ios_base::out |
108                    std::ios_base::binary | std::ios_base::trunc);
109     } else {
110       outfile.open("boost_sort_out.txt", std::ios_base::out |
111                    std::ios_base::binary | std::ios_base::trunc);
112     }
113     if (outfile.good()) {
114       for (unsigned u = 0; u < array.size(); ++u)
115         outfile << array[u].a << "\n";
116       outfile.close();
117     }
118     total += elapsed;
119     array.clear();
120   }
121   if (stdSort) {
122     printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
123   } else {
124     printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
125   }
126   return 0;
127 }
128