]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/keyplusdatasample.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / keyplusdatasample.cpp
1 // spreadsort key and data 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/spreadsort.hpp>
12 #include <time.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <algorithm>
16 #include <vector>
17 #include <string>
18 #include <fstream>
19 #include <sstream>
20 #include <iostream>
21 using namespace boost::sort::spreadsort;
22
23 struct DATA_TYPE {
24 int key;
25 std::string data;
26 };
27 //functor example
28 struct lessthan {
29 inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
30 return x.key < y.key;
31 }
32 };
33
34 struct rightshift {
35 inline int operator()(const DATA_TYPE &x, const unsigned offset) {
36 return x.key >> offset;
37 }
38 };
39
40 //Pass in an argument to test std::sort
41 int main(int argc, const char ** argv) {
42 size_t uSize = sizeof(int);
43 bool stdSort = false;
44 unsigned loopCount = 1;
45 for (int u = 1; u < argc; ++u) {
46 if (std::string(argv[u]) == "-std")
47 stdSort = true;
48 else
49 loopCount = atoi(argv[u]);
50 }
51 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
52 if (input.fail()) {
53 printf("input.txt could not be opened\n");
54 return 1;
55 }
56 input.seekg (0, std::ios_base::end);
57 size_t length = input.tellg();
58 double total = 0.0;
59 std::vector<DATA_TYPE> array;
60 array.reserve(length/uSize);
61 unsigned uCount = length/uSize;
62 //Run multiple loops, if requested
63 for (unsigned u = 0; u < loopCount; ++u) {
64 input.seekg (0, std::ios_base::beg);
65 unsigned v = 0;
66 while (input.good() && v++ < uCount) { // EOF or failure stops the reading
67 DATA_TYPE element;
68 input.read(reinterpret_cast<char *>(&(element.key)), sizeof(element.key));
69 std::stringstream intstr;
70 intstr << element.key;
71 element.data = intstr.str();
72 array.push_back(element);
73 }
74 clock_t start, end;
75 double elapsed;
76 start = clock();
77 if (stdSort)
78 std::sort(array.begin(), array.end(), lessthan());
79 else
80 integer_sort(array.begin(), array.end(), rightshift(), lessthan());
81 end = clock();
82 elapsed = static_cast<double>(end - start) ;
83 std::ofstream ofile;
84 if (stdSort)
85 ofile.open("standard_sort_out.txt", std::ios_base::out |
86 std::ios_base::binary | std::ios_base::trunc);
87 else
88 ofile.open("boost_sort_out.txt", std::ios_base::out |
89 std::ios_base::binary | std::ios_base::trunc);
90 if (ofile.good()) {
91 for (unsigned v = 0; v < array.size(); ++v) {
92 ofile.write(reinterpret_cast<char *>(&(array[v].key)),
93 sizeof(array[v].key));
94 ofile << array[v].data;
95 }
96 ofile.close();
97 }
98 total += elapsed;
99 array.clear();
100 }
101 input.close();
102 if (stdSort)
103 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
104 else
105 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
106 return 0;
107 }