]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/charstringsample.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / sort / example / charstringsample.cpp
1 // Example of sorting structs with a string key and indexing functors.
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 inline bool operator<(const DATA_TYPE &y) const { return a < y.a;}
26 };
27
28
29 struct bracket {
30 inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
31 return x.a[offset];
32 }
33 };
34
35 struct getsize {
36 inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); }
37 };
38
39
40
41 //Pass in an argument to test std::sort
42 int main(int argc, const char ** argv) {
43 std::ifstream indata;
44 bool stdSort = false;
45 unsigned loopCount = 1;
46 for (int u = 1; u < argc; ++u) {
47 if (std::string(argv[u]) == "-std")
48 stdSort = true;
49 else
50 loopCount = atoi(argv[u]);
51 }
52 double total = 0.0;
53 //Run multiple loops, if requested
54 std::vector<DATA_TYPE> array;
55 for (unsigned u = 0; u < loopCount; ++u) {
56 indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
57 if (indata.bad()) {
58 printf("input.txt could not be opened\n");
59 return 1;
60 }
61 DATA_TYPE inval;
62 indata >> inval.a;
63 while (!indata.eof() ) {
64 array.push_back(inval);
65 indata >> inval.a;
66 }
67
68 indata.close();
69 clock_t start, end;
70 double elapsed;
71 start = clock();
72 if (stdSort) {
73 std::sort(array.begin(), array.end());
74 } else {
75 //[bracket_1
76 string_sort(array.begin(), array.end(), bracket(), getsize());
77 //] [/bracket_1]
78 }
79 end = clock();
80 elapsed = static_cast<double>(end - start);
81 std::ofstream ofile;
82 if (stdSort)
83 ofile.open("standard_sort_out.txt", std::ios_base::out |
84 std::ios_base::binary | std::ios_base::trunc);
85 else
86 ofile.open("boost_sort_out.txt", std::ios_base::out |
87 std::ios_base::binary | std::ios_base::trunc);
88 if (ofile.good()) {
89 for (unsigned u = 0; u < array.size(); ++u)
90 ofile << array[u].a << "\n";
91 ofile.close();
92 }
93 total += elapsed;
94 array.clear();
95 }
96 if (stdSort)
97 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
98 else
99 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
100 return 0;
101 }