]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/reverseintsample.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / reverseintsample.cpp
1 //! \file
2 //! \brief integer sort with a rightshift functor reverse sorting example.
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/spreadsort.hpp>
16 #include <time.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <algorithm>
20 #include <vector>
21 #include <string>
22 #include <fstream>
23 #include <iostream>
24 #include <functional>
25 using namespace boost::sort::spreadsort;
26
27 #define DATA_TYPE int
28
29 struct negrightshift {
30 inline int operator()(const int &x, const unsigned offset) {
31 return -(x >> offset);
32 }
33 };
34
35 //Pass in an argument to test std::sort
36 int main(int argc, const char ** argv) {
37 size_t uCount,uSize=sizeof(DATA_TYPE);
38 bool stdSort = false;
39 unsigned loopCount = 1;
40 for (int u = 1; u < argc; ++u) {
41 if (std::string(argv[u]) == "-std")
42 stdSort = true;
43 else
44 loopCount = atoi(argv[u]);
45 }
46 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
47 if (input.fail()) {
48 printf("input.txt could not be opened\n");
49 return 1;
50 }
51 double total = 0.0;
52 std::vector<DATA_TYPE> array;
53 input.seekg (0, std::ios_base::end);
54 size_t length = input.tellg();
55 uCount = length/uSize;
56 //Run multiple loops, if requested
57 for (unsigned u = 0; u < loopCount; ++u) {
58 input.seekg (0, std::ios_base::beg);
59 //Conversion to a vector
60 array.resize(uCount);
61 unsigned v = 0;
62 while (input.good() && v < uCount)
63 input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
64 if (v < uCount)
65 array.resize(v);
66 clock_t start, end;
67 double elapsed;
68 start = clock();
69 if (stdSort)
70
71 //[reverse_int_1
72 std::sort(array.begin(), array.end(), std::greater<DATA_TYPE>());
73 //] [/reverse_int_1]
74 else
75 //[reverse_int_2
76 integer_sort(array.begin(), array.end(), negrightshift(), std::greater<DATA_TYPE>());
77 //] [/reverse_int_2]
78 end = clock();
79 elapsed = static_cast<double>(end - start) ;
80 std::ofstream ofile;
81 if (stdSort)
82 ofile.open("standard_sort_out.txt", std::ios_base::out |
83 std::ios_base::binary | std::ios_base::trunc);
84 else
85 ofile.open("boost_sort_out.txt", std::ios_base::out |
86 std::ios_base::binary | std::ios_base::trunc);
87 if (ofile.good()) {
88 for (unsigned v = 0; v < array.size(); ++v) {
89 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]));
90 }
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 }