algorithms.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <cxxomfort/base.hpp>
  2. #include <cxxomfort/algorithm.hpp>
  3. #include <cxxomfort/functional.hpp>
  4. #include <cxxomfort/iterator.hpp>
  5. #include <cxxomfort/typeindex.hpp>
  6. #include <cxxomfort/random.hpp>
  7. #include <cxxomfort/library/foreach.hpp>
  8. #include <vector>
  9. #include <string>
  10. #include <iostream>
  11. #include <cstdio>
  12. const unsigned LIM=100;
  13. struct modu {
  14. unsigned operator() (unsigned u) const { return '/' + u % ('~'-'/' + 1); }
  15. template <typename Gen>
  16. unsigned operator() (Gen& g) const { return this->operator()(g()); }
  17. // needed for C++03 tr1/bind
  18. template <typename Sig> struct result { typedef unsigned type; };
  19. };
  20. bool pred_z (unsigned char u) {
  21. return ispunct(u);
  22. }
  23. bool sorter (unsigned char n1, unsigned char n2) {
  24. bool uc2 [2] = { (bool)ispunct(n1), (bool)ispunct(n2) };
  25. if (uc2[0]==uc2[1]) {
  26. return n1 < n2;
  27. } else {
  28. return uc2[0];
  29. }
  30. }
  31. typedef std::ranlux24_base Gen_t;
  32. typedef std::vector<unsigned char> VecType;
  33. int main () {
  34. using namespace std;
  35. VecType Ve (LIM);
  36. modu mo;
  37. Gen_t Ge(time(0));
  38. cout<< "Generating "<< LIM<< " values"<< endl;
  39. generate(begin(Ve), end(Ve), bind(mo,Ge));
  40. cout<< "All values: "<< endl;
  41. CXXO_FOREACH(unsigned char u, Ve) { cout<< u<< " "; }
  42. cout<< "\n---"<< endl;
  43. cout<< "Filtering values in the original list with copy_if:"<< endl;
  44. cout<< "* numbers only: ";
  45. copy_if(begin(Ve), end(Ve), ostream_iterator<unsigned char>(cout, " "), ::isdigit );
  46. cout<< endl;
  47. cout<< "* lowercase only: ";
  48. copy_if(begin(Ve), end(Ve), ostream_iterator<unsigned char>(cout), ::islower );
  49. cout<< "\n";
  50. cout<< "\n----\n";
  51. #if (CXXOMFORT_IMPLEMENTS_algorithm_sorted)
  52. cout<< "Testing sorting algorithms via fn:sorter:\n";
  53. VecType VeSorted(Ve);
  54. cout<< "sort, is_sorted"<< endl;
  55. cout<< is_sorted(begin(VeSorted), end(VeSorted), sorter)<< endl;
  56. cout<< "Sorting via sorter..."<< endl;
  57. sort(begin(VeSorted), end(VeSorted), sorter);
  58. cout<< "Testing sorting algorithms via fn:sorter: ";
  59. cout<< is_sorted(begin(VeSorted), end(VeSorted), sorter)<< endl;
  60. CXXO_FOREACH(unsigned char u, VeSorted) { cout<< u<< " "; }
  61. #else
  62. cout<< "C++11 sorting algorithms not implemented"<< endl;
  63. #endif
  64. cout<< "\n----\n";
  65. #if (CXXOMFORT_IMPLEMENTS_algorithm_allanynoneof)
  66. cout<< "Testing amount_of algorithms from C++11:\n";
  67. cout<< "[any|all|none]_of, partition_copy"<< endl;
  68. vector<VecType> splits(2);
  69. // split the list in two
  70. cout<< "Partitioning the original list in two:\n";
  71. partition_copy(begin(Ve), end(Ve)
  72. , back_inserter(splits[true]), back_inserter(splits[false])
  73. , pred_z);
  74. cout<< " Predicate match no: "<< endl;
  75. CXXO_FOREACH(unsigned char u, splits[false]) {
  76. cout<< u<< " ";
  77. }
  78. cout<< "\n---"<< endl;
  79. cout<< " Predicate match yes: "<< endl;
  80. CXXO_FOREACH(unsigned char u, splits[true]) {
  81. cout<< u<< " ";
  82. }
  83. cout<< "\n---"<< endl;
  84. cout<< " Verifying predicate match yes: "<< endl;
  85. cout<< "any_of : "<< any_of(begin(splits[true]), end(splits[true]), pred_z)<< endl;
  86. cout<< "all_of : "<< all_of(begin(splits[true]), end(splits[true]), pred_z)<< endl;
  87. cout<< "none_of : "<< none_of(begin(splits[true]), end(splits[true]), pred_z)<< endl;
  88. cout<< endl;
  89. #else
  90. cout<< "C++11 amount_of predicates not implemented"<< endl;
  91. #endif
  92. }