seq_filt.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Simple test file for cxxomfort
  3. * When compiled and run,
  4. * this will generate a sequence of random numbers and filter it
  5. * according to some criteria.
  6. *
  7. * Interfaces tested in this example:
  8. *
  9. * * algorithms (find_if, transform, cxxomfort:find_last_if)
  10. * * bind
  11. * * cxxomfort:fixed_vector
  12. * * cxxomfort:seq_
  13. * * cxxomfort:CXXO_FOREACH
  14. * * cxxomfort:constant_iterator
  15. * * cxxomfort:transparent_functional
  16. *
  17. * A list of SIZ1 numbers will be generated, with type 'uint16_t'.
  18. * The first and last numbers greater than LIM1 will be selected as pivots, if any.
  19. * Then, all positions in the list between the first and last found
  20. * will be reduced modulo LIM2 via bind() and the std::modulus() transparent functor.
  21. *
  22. * This and other examples assume cxxomfort is installed in
  23. * include library path; if otherwise, compile with
  24. * -I /path/to/cxxomfort .
  25. */
  26. #include <cxxomfort/cxxomfort.hpp>
  27. #include <cxxomfort/library/fixed_vector.hpp>
  28. #include <cxxomfort/library/foreach.hpp>
  29. #include <cxxomfort/library/iteratorfn.hpp>
  30. #include <cxxomfort/library/algorithmfn.hpp>
  31. #include <cstdlib>
  32. #include <ctime>
  33. #include <vector>
  34. #include <forward_list>
  35. #include <algorithm>
  36. const unsigned SIZ1 = 400;
  37. const unsigned LIM1 = 19999;
  38. const unsigned LIM2 = 100;
  39. typedef uint16_t TT;
  40. typedef cxxomfort::fixed_vector<TT> seq1_t_TT;
  41. typedef std::vector<TT> seq2_t_TT;
  42. template <typename Seq, typename S>
  43. void outp_sequence (Seq const& ss, S& os);
  44. int main () {
  45. using namespace std;
  46. cxxomfort::output_info(cout);
  47. cout<< endl;
  48. // A list of SIZ1 numbers will be generated, with type 'uint16_t'.
  49. cxxomfort::fixed_vector<TT> origin(SIZ1);
  50. #if (CXXOMFORT_COMPILER_ID == CXXO_VALUE_COMPILER_GCC)
  51. srand48( time(0) );
  52. generate (begin(origin), end(origin), mrand48);
  53. #else
  54. srand( time(0) );
  55. generate (begin(origin), end(origin), rand);
  56. #endif
  57. outp_sequence(origin, cout);
  58. cout<< " -end of sequence."<< endl;
  59. // The first and last numbers greater than LIM1 will be selected as pivots, if any.
  60. cxxomfort::fixed_vector<TT>::iterator findth = find_if(begin(origin), end(origin),
  61. bind(greater<void>(), placeholders::_1, LIM1));
  62. if (findth == end(origin)) {
  63. cout<< "No element in {origin} fulfills threshold 'x>"<< LIM1<< "'."<< endl;
  64. exit(0);
  65. }
  66. cout<< "first larger: "<< *findth<< endl;
  67. using cxxomfort::algorithm::find_last_if;
  68. cxxomfort::fixed_vector<TT>::iterator findlast = find_last_if(findth, end(origin),
  69. bind(greater<void>(), placeholders::_1, LIM1));
  70. cout<< "last larger: "<< *findlast<< endl;
  71. if (findlast != end(origin)) ++findlast; // transform requires the "after the end" of range.
  72. // Then, all positions in the list between the first and last found
  73. // will be reduced modulo LIM2 via bind() and the std::modulus() transparent functor.
  74. cout<< "Filtering numbers larger than "<< LIM1<< " to be % "<< LIM2<< "."<< endl;
  75. const uint16_t vlim2 = LIM2;
  76. typedef cxxomfort::iterator::fake_iterator<uint16_t> CIter_t;
  77. CIter_t clim2 (vlim2);
  78. transform(findth, findlast, clim2, findth
  79. , bind(std::modulus<void>(), placeholders::_1, placeholders::_2) );
  80. cout<< endl;
  81. outp_sequence(origin, cout);
  82. cout<< " -end of sequence."<< endl;
  83. cout<< endl;
  84. }
  85. template <typename Seq, typename S>
  86. void outp_sequence (Seq const& ss, S& os) {
  87. CXXO_FOREACH( typename Seq::value_type const& x, ss) {
  88. os<< x<< ", ";
  89. }
  90. }