| 1 | //! \brief Integer sort with a rightshift 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/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 | using namespace boost::sort::spreadsort; |
| 25 | |
| 26 | #define DATA_TYPE int |
| 27 | |
| 28 | //[rightshift_int_functor |
| 29 | struct rightshift { |
| 30 | inline int operator()(DATA_TYPE x, unsigned offset) { return x >> offset; } |
| 31 | }; |
| 32 | //] [/rightshift_int_functor] |
| 33 | |
| 34 | //Pass in an argument to test std::sort |
| 35 | int main(int argc, const char ** argv) { |
| 36 | size_t uCount,uSize=sizeof(DATA_TYPE); |
| 37 | bool stdSort = false; |
| 38 | unsigned loopCount = 1; |
| 39 | for (int u = 1; u < argc; ++u) { |
| 40 | if (std::string(argv[u]) == "-std" ) |
| 41 | stdSort = true; |
| 42 | else |
| 43 | loopCount = atoi(nptr: argv[u]); |
| 44 | } |
| 45 | std::ifstream input("input.txt" , std::ios_base::in | std::ios_base::binary); |
| 46 | if (input.fail()) { |
| 47 | printf(format: "input.txt could not be opened\n" ); |
| 48 | return 1; |
| 49 | } |
| 50 | double total = 0.0; |
| 51 | std::vector<DATA_TYPE> array; |
| 52 | input.seekg (0, std::ios_base::end); |
| 53 | size_t length = input.tellg(); |
| 54 | uCount = length/uSize; |
| 55 | //Run multiple loops, if requested |
| 56 | for (unsigned u = 0; u < loopCount; ++u) { |
| 57 | input.seekg (0, std::ios_base::beg); |
| 58 | //Conversion to a vector |
| 59 | array.resize(new_size: uCount); |
| 60 | unsigned v = 0; |
| 61 | while (input.good() && v < uCount) |
| 62 | input.read(s: reinterpret_cast<char *>(&(array[v++])), n: uSize ); |
| 63 | if (v < uCount) |
| 64 | array.resize(new_size: v); |
| 65 | clock_t start, end; |
| 66 | double elapsed; |
| 67 | start = clock(); |
| 68 | if (stdSort) { |
| 69 | std::sort(first: array.begin(), last: array.end()); |
| 70 | } else { |
| 71 | //[rightshift_1 |
| 72 | integer_sort(first: array.begin(), last: array.end(), shift: rightshift()); |
| 73 | //] [/rightshift_1] |
| 74 | } |
| 75 | end = clock(); |
| 76 | elapsed = static_cast<double>(end - start) ; |
| 77 | std::ofstream ofile; |
| 78 | if (stdSort) |
| 79 | ofile.open(s: "standard_sort_out.txt" , mode: std::ios_base::out | |
| 80 | std::ios_base::binary | std::ios_base::trunc); |
| 81 | else |
| 82 | ofile.open(s: "boost_sort_out.txt" , mode: std::ios_base::out | |
| 83 | std::ios_base::binary | std::ios_base::trunc); |
| 84 | if (ofile.good()) { |
| 85 | for (unsigned v = 0; v < array.size(); ++v) { |
| 86 | ofile.write(s: reinterpret_cast<char *>(&(array[v])), n: sizeof(array[v]) ); |
| 87 | } |
| 88 | ofile.close(); |
| 89 | } |
| 90 | total += elapsed; |
| 91 | array.clear(); |
| 92 | } |
| 93 | if (stdSort) |
| 94 | printf(format: "std::sort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 95 | else |
| 96 | printf(format: "spreadsort elapsed time %f\n" , total / CLOCKS_PER_SEC); |
| 97 | return 0; |
| 98 | } |
| 99 | |