reversestringsample.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // spreadsort reverse string 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. #define DATA_TYPE string
  21. //Pass in an argument to test std::sort
  22. int main(int argc, const char ** argv) {
  23. std::ifstream indata;
  24. std::ofstream outfile;
  25. bool stdSort = false;
  26. unsigned loopCount = 1;
  27. for (int u = 1; u < argc; ++u) {
  28. if (std::string(argv[u]) == "-std")
  29. stdSort = true;
  30. else
  31. loopCount = atoi(argv[u]);
  32. }
  33. double total = 0.0;
  34. //Run multiple loops, if requested
  35. std::vector<DATA_TYPE> array;
  36. for (unsigned u = 0; u < loopCount; ++u) {
  37. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  38. if (!indata) {
  39. printf("input.txt could not be opened\n");
  40. return 1;
  41. }
  42. DATA_TYPE inval;
  43. indata >> inval;
  44. while (!indata.eof() ) {
  45. //testing substrings
  46. if (!(array.size() % 2))
  47. inval = "prefix" + inval;
  48. else
  49. inval += "suffix";
  50. array.push_back(inval);
  51. //Inserting embedded nulls and empty strings
  52. if (!(array.size() % 100)) {
  53. if (inval.empty() || !(array.size() % 1000))
  54. array.push_back("");
  55. else {
  56. inval[0] = '\0';
  57. array.push_back(inval);
  58. }
  59. }
  60. indata >> inval;
  61. }
  62. indata.close();
  63. clock_t start, end;
  64. double elapsed;
  65. start = clock();
  66. if (stdSort)
  67. //std::sort(&(array[0]), &(array[0]) + uCount);
  68. std::sort(array.begin(), array.end(), std::greater<string>());
  69. else
  70. //string_sort(&(array[0]), &(array[0]) + uCount);
  71. reverse_string_sort(array.begin(), array.end(), std::greater<string>());
  72. end = clock();
  73. elapsed = static_cast<double>(end - start);
  74. if (stdSort)
  75. outfile.open("standard_sort_out.txt", std::ios_base::out |
  76. std::ios_base::binary | std::ios_base::trunc);
  77. else
  78. outfile.open("boost_sort_out.txt", std::ios_base::out |
  79. std::ios_base::binary | std::ios_base::trunc);
  80. if (outfile.good()) {
  81. for (unsigned u = 0; u < array.size(); ++u)
  82. outfile << array[u] << "\n";
  83. outfile.close();
  84. }
  85. total += elapsed;
  86. array.clear();
  87. }
  88. if (stdSort)
  89. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  90. else
  91. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  92. return 0;
  93. }