floatfunctorsample.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // spreadsort float functor sorting example.
  2. //
  3. // Copyright Steven Ross 2009.
  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. // See http://www.boost.org/libs/sort for library home page.
  9. // Caution: this file contains Quickbook markup as well as code
  10. // and comments, don't change any of the special comment markups!
  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. //[float_functor_types
  23. #define CAST_TYPE int
  24. #define KEY_TYPE float
  25. //] [/float_functor_types]
  26. //[float_functor_datatypes
  27. struct DATA_TYPE {
  28. KEY_TYPE key;
  29. std::string data;
  30. };
  31. //] [/float_functor_datatypes]
  32. //[float_functor_rightshift
  33. // Casting to an integer before bitshifting
  34. struct rightshift {
  35. int operator()(const DATA_TYPE &x, const unsigned offset) const {
  36. return float_mem_cast<KEY_TYPE, CAST_TYPE>(x.key) >> offset;
  37. }
  38. };
  39. //] [/float_functor_rightshift]
  40. //[float_functor_lessthan
  41. struct lessthan {
  42. bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
  43. return x.key < y.key;
  44. }
  45. };
  46. //] [/float_functor_lessthan]
  47. // Pass in an argument to test std::sort
  48. // Note that this converts NaNs and -0.0 to 0.0, so that sorting results are
  49. // identical every time
  50. int main(int argc, const char ** argv) {
  51. size_t uCount,uSize=sizeof(DATA_TYPE);
  52. bool stdSort = false;
  53. unsigned loopCount = 1;
  54. for (int u = 1; u < argc; ++u) {
  55. if (std::string(argv[u]) == "-std")
  56. stdSort = true;
  57. else
  58. loopCount = atoi(argv[u]);
  59. }
  60. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  61. if (input.fail()) {
  62. printf("input.txt could not be opened\n");
  63. return 1;
  64. }
  65. double total = 0.0;
  66. std::vector<DATA_TYPE> array;
  67. input.seekg (0, std::ios_base::end);
  68. size_t length = input.tellg();
  69. uCount = length/uSize;
  70. //Run multiple loops, if requested
  71. for (unsigned u = 0; u < loopCount; ++u) {
  72. input.seekg (0, std::ios_base::beg);
  73. //Conversion to a vector
  74. array.resize(uCount);
  75. unsigned v = 0;
  76. while (input.good() && v < uCount) {
  77. input.read(reinterpret_cast<char *>(&(array[v].key)),
  78. sizeof(array[v].key));
  79. //Checking for denormalized numbers; float_sort looks too fast on them.
  80. if (!(float_mem_cast<KEY_TYPE, CAST_TYPE>(array[v].key) & 0x7f800000)) {
  81. //Make the top exponent bit high
  82. CAST_TYPE temp = 0x40000000 |
  83. float_mem_cast<KEY_TYPE, CAST_TYPE>(array[v].key);
  84. memcpy(&(array[v].key), &temp, sizeof(KEY_TYPE));
  85. }
  86. //Testcase doesn't sort NaNs; they just cause confusion
  87. if (!(array[v].key < 0.0) && !(0.0 < array[v].key))
  88. array[v].key = 0.0;
  89. //Adding the data, in this case a string
  90. std::stringstream intstr;
  91. intstr << array[v].key;
  92. array[v].data = intstr.str();
  93. ++v;
  94. }
  95. clock_t start, end;
  96. double elapsed;
  97. start = clock();
  98. if (stdSort)
  99. std::sort(array.begin(), array.end(), lessthan());
  100. else
  101. float_sort(array.begin(), array.end(), rightshift(), lessthan());
  102. end = clock();
  103. elapsed = static_cast<double>(end - start) ;
  104. std::ofstream ofile;
  105. if (stdSort)
  106. ofile.open("standard_sort_out.txt", std::ios_base::out |
  107. std::ios_base::binary | std::ios_base::trunc);
  108. else
  109. ofile.open("boost_sort_out.txt", std::ios_base::out |
  110. std::ios_base::binary | std::ios_base::trunc);
  111. if (ofile.good()) {
  112. for (unsigned v = 0; v < array.size(); ++v) {
  113. ofile.write(reinterpret_cast<char *>(&(array[v].key)),
  114. sizeof(array[v].key));
  115. ofile << array[v].data;
  116. }
  117. ofile.close();
  118. }
  119. total += elapsed;
  120. array.clear();
  121. }
  122. if (stdSort)
  123. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  124. else
  125. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  126. return 0;
  127. }