stringfunctorsample.cpp 3.3 KB

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