caseinsensitive.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Example of sorting a struct using a case-insensitive string key.
  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/algorithm/string.hpp>
  10. #include <boost/sort/spreadsort/string_sort.hpp>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <algorithm>
  15. #include <vector>
  16. #include <iostream>
  17. #include <fstream>
  18. #include <string>
  19. using std::string;
  20. using namespace boost::sort::spreadsort;
  21. struct DATA_TYPE {
  22. string a;
  23. };
  24. struct lessthan {
  25. inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
  26. return boost::algorithm::ilexicographical_compare(x.a, y.a);
  27. }
  28. };
  29. struct bracket {
  30. inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
  31. return toupper(x.a[offset]);
  32. }
  33. };
  34. struct getsize {
  35. inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); }
  36. };
  37. //Pass in an argument to test std::sort
  38. int main(int argc, const char ** argv) {
  39. std::ifstream indata;
  40. std::ofstream outfile;
  41. bool stdSort = false;
  42. unsigned loopCount = 1;
  43. for (int u = 1; u < argc; ++u) {
  44. if (std::string(argv[u]) == "-std")
  45. stdSort = true;
  46. else
  47. loopCount = atoi(argv[u]);
  48. }
  49. double total = 0.0;
  50. //Run multiple loops, if requested
  51. std::vector<DATA_TYPE> array;
  52. for (unsigned u = 0; u < loopCount; ++u) {
  53. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  54. if (indata.bad()) {
  55. printf("input.txt could not be opened\n");
  56. return 1;
  57. }
  58. DATA_TYPE inval;
  59. indata >> inval.a;
  60. while (!indata.eof() ) {
  61. array.push_back(inval);
  62. indata >> inval.a;
  63. }
  64. indata.close();
  65. clock_t start, end;
  66. double elapsed;
  67. start = clock();
  68. if (stdSort)
  69. std::sort(array.begin(), array.end(), lessthan());
  70. else
  71. string_sort(array.begin(), array.end(), bracket(), getsize(), lessthan());
  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].a << "\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. }