wstringsample.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // spreadsort wstring 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::wstring;
  19. using namespace boost::sort::spreadsort;
  20. #define DATA_TYPE wstring
  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.bad()) {
  39. printf("input.txt could not be opened\n");
  40. return 1;
  41. }
  42. unsigned short inval;
  43. DATA_TYPE current;
  44. while (indata.good()) {
  45. indata.read(reinterpret_cast<char *>(&inval), sizeof(inval));
  46. current.push_back(inval);
  47. //32 characters is a moderately long string
  48. if (static_cast<int>(current.size()) > inval || current.size() >= 32) {
  49. array.push_back(current);
  50. current.clear();
  51. }
  52. }
  53. //adding the last string
  54. if (!current.empty())
  55. array.push_back(current);
  56. indata.close();
  57. clock_t start, end;
  58. double elapsed;
  59. start = clock();
  60. wchar_t cast_type = 0;
  61. if (stdSort)
  62. //std::sort(&(array[0]), &(array[0]) + uCount);
  63. std::sort(array.begin(), array.end());
  64. else
  65. //string_sort(&(array[0]), &(array[0]) + uCount, cast_type);
  66. string_sort(array.begin(), array.end(), cast_type);
  67. end = clock();
  68. elapsed = static_cast<double>(end - start);
  69. if (stdSort)
  70. outfile.open("standard_sort_out.txt", std::ios_base::out |
  71. std::ios_base::binary | std::ios_base::trunc);
  72. else
  73. outfile.open("boost_sort_out.txt", std::ios_base::out |
  74. std::ios_base::binary | std::ios_base::trunc);
  75. if (outfile.good()) {
  76. for (unsigned u = 0; u < array.size(); ++u){
  77. for (unsigned v = 0; v < array[u].size(); ++v)
  78. outfile << array[u][v];
  79. outfile << "\n";
  80. }
  81. outfile.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. }