reverseintsample.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // See http://www.boost.org/libs/sort for library home page.
  10. // Caution: this file contains Quickbook markup as well as code
  11. // and comments, don't change any of the special comment markups!
  12. #include <boost/sort/spreadsort/spreadsort.hpp>
  13. #include <time.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <algorithm>
  17. #include <vector>
  18. #include <string>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <functional>
  22. using namespace boost::sort::spreadsort;
  23. #define DATA_TYPE int
  24. struct negrightshift {
  25. inline int operator()(const int &x, const unsigned offset) {
  26. return -(x >> offset);
  27. }
  28. };
  29. //Pass in an argument to test std::sort
  30. int main(int argc, const char ** argv) {
  31. size_t uCount,uSize=sizeof(DATA_TYPE);
  32. bool stdSort = false;
  33. unsigned loopCount = 1;
  34. for (int u = 1; u < argc; ++u) {
  35. if (std::string(argv[u]) == "-std")
  36. stdSort = true;
  37. else
  38. loopCount = atoi(argv[u]);
  39. }
  40. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  41. if (input.fail()) {
  42. printf("input.txt could not be opened\n");
  43. return 1;
  44. }
  45. double total = 0.0;
  46. std::vector<DATA_TYPE> array;
  47. input.seekg (0, std::ios_base::end);
  48. size_t length = input.tellg();
  49. uCount = length/uSize;
  50. //Run multiple loops, if requested
  51. for (unsigned u = 0; u < loopCount; ++u) {
  52. input.seekg (0, std::ios_base::beg);
  53. //Conversion to a vector
  54. array.resize(uCount);
  55. unsigned v = 0;
  56. while (input.good() && v < uCount)
  57. input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
  58. if (v < uCount)
  59. array.resize(v);
  60. clock_t start, end;
  61. double elapsed;
  62. start = clock();
  63. if (stdSort)
  64. //[reverse_int_1
  65. std::sort(array.begin(), array.end(), std::greater<DATA_TYPE>());
  66. //] [/reverse_int_1]
  67. else
  68. //[reverse_int_2
  69. integer_sort(array.begin(), array.end(), negrightshift(), std::greater<DATA_TYPE>());
  70. //] [/reverse_int_2]
  71. end = clock();
  72. elapsed = static_cast<double>(end - start) ;
  73. std::ofstream ofile;
  74. if (stdSort)
  75. ofile.open("standard_sort_out.txt", std::ios_base::out |
  76. std::ios_base::binary | std::ios_base::trunc);
  77. else
  78. ofile.open("boost_sort_out.txt", std::ios_base::out |
  79. std::ios_base::binary | std::ios_base::trunc);
  80. if (ofile.good()) {
  81. for (unsigned v = 0; v < array.size(); ++v) {
  82. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]));
  83. }
  84. ofile.close();
  85. }
  86. total += elapsed;
  87. array.clear();
  88. }
  89. if (stdSort)
  90. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  91. else
  92. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  93. return 0;
  94. }