charstringsample.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Example of sorting structs with a string key and indexing functors.
  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. inline bool operator<(const DATA_TYPE &y) const { return a < y.a;}
  23. };
  24. struct bracket {
  25. inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
  26. return x.a[offset];
  27. }
  28. };
  29. struct getsize {
  30. inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); }
  31. };
  32. //Pass in an argument to test std::sort
  33. int main(int argc, const char ** argv) {
  34. std::ifstream indata;
  35. bool stdSort = false;
  36. unsigned loopCount = 1;
  37. for (int u = 1; u < argc; ++u) {
  38. if (std::string(argv[u]) == "-std")
  39. stdSort = true;
  40. else
  41. loopCount = atoi(argv[u]);
  42. }
  43. double total = 0.0;
  44. //Run multiple loops, if requested
  45. std::vector<DATA_TYPE> array;
  46. for (unsigned u = 0; u < loopCount; ++u) {
  47. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  48. if (indata.bad()) {
  49. printf("input.txt could not be opened\n");
  50. return 1;
  51. }
  52. DATA_TYPE inval;
  53. indata >> inval.a;
  54. while (!indata.eof() ) {
  55. array.push_back(inval);
  56. indata >> inval.a;
  57. }
  58. indata.close();
  59. clock_t start, end;
  60. double elapsed;
  61. start = clock();
  62. if (stdSort) {
  63. std::sort(array.begin(), array.end());
  64. } else {
  65. //[bracket_1
  66. string_sort(array.begin(), array.end(), bracket(), getsize());
  67. //] [/bracket_1]
  68. }
  69. end = clock();
  70. elapsed = static_cast<double>(end - start);
  71. std::ofstream ofile;
  72. if (stdSort)
  73. ofile.open("standard_sort_out.txt", std::ios_base::out |
  74. std::ios_base::binary | std::ios_base::trunc);
  75. else
  76. ofile.open("boost_sort_out.txt", std::ios_base::out |
  77. std::ios_base::binary | std::ios_base::trunc);
  78. if (ofile.good()) {
  79. for (unsigned u = 0; u < array.size(); ++u)
  80. ofile << array[u].a << "\n";
  81. ofile.close();
  82. }
  83. total += elapsed;
  84. array.clear();
  85. }
  86. if (stdSort)
  87. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  88. else
  89. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  90. return 0;
  91. }