reversestringfunctorsample.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // spreadsort string functor reverse 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. // See http://www.boost.org/libs/sort for library home page.
  9. #include <boost/sort/spreadsort/string_sort.hpp>
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <string>
  18. using std::string;
  19. using namespace boost::sort::spreadsort;
  20. struct DATA_TYPE {
  21. string a;
  22. };
  23. struct greaterthan {
  24. inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
  25. return x.a > y.a;
  26. }
  27. };
  28. struct bracket {
  29. inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
  30. return x.a[offset];
  31. }
  32. };
  33. struct getsize {
  34. inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); }
  35. };
  36. //Pass in an argument to test std::sort
  37. int main(int argc, const char ** argv) {
  38. std::ifstream indata;
  39. std::ofstream outfile;
  40. bool stdSort = false;
  41. unsigned loopCount = 1;
  42. for (int u = 1; u < argc; ++u) {
  43. if (std::string(argv[u]) == "-std")
  44. stdSort = true;
  45. else
  46. loopCount = atoi(argv[u]);
  47. }
  48. double total = 0.0;
  49. //Run multiple loops, if requested
  50. std::vector<DATA_TYPE> array;
  51. for (unsigned u = 0; u < loopCount; ++u) {
  52. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  53. if (!indata) {
  54. printf("input.txt could not be opened\n");
  55. return 1;
  56. }
  57. DATA_TYPE inval;
  58. indata >> inval.a;
  59. while (!indata.eof() ) {
  60. array.push_back(inval);
  61. //Inserting embedded nulls and empty strings
  62. if (!(array.size() % 100)) {
  63. if (inval.a.empty() || !(array.size() % 1000)) {
  64. inval.a = "";
  65. array.push_back(inval);
  66. }
  67. else {
  68. inval.a[0] = '\0';
  69. array.push_back(inval);
  70. }
  71. }
  72. indata >> inval.a;
  73. }
  74. indata.close();
  75. clock_t start, end;
  76. double elapsed;
  77. start = clock();
  78. if (stdSort)
  79. std::sort(array.begin(), array.end(), greaterthan());
  80. else
  81. reverse_string_sort(array.begin(), array.end(), bracket(), getsize(),
  82. greaterthan());
  83. end = clock();
  84. elapsed = static_cast<double>(end - start);
  85. if (stdSort)
  86. outfile.open("standard_sort_out.txt", std::ios_base::out |
  87. std::ios_base::binary | std::ios_base::trunc);
  88. else
  89. outfile.open("boost_sort_out.txt", std::ios_base::out |
  90. std::ios_base::binary | std::ios_base::trunc);
  91. if (outfile.good()) {
  92. for (unsigned u = 0; u < array.size(); ++u)
  93. outfile << array[u].a << "\n";
  94. outfile.close();
  95. }
  96. total += elapsed;
  97. array.clear();
  98. }
  99. if (stdSort)
  100. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  101. else
  102. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  103. return 0;
  104. }