fork download
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <type_traits>
  5. #include <functional>
  6. using namespace std;
  7.  
  8. template<class RangeType>
  9. struct range_iterator
  10. {
  11. RangeType* _range;
  12.  
  13. bool operator!=(const range_iterator<RangeType>& that) const {
  14. assert(_range == that._range);
  15. return !_range->empty();
  16. }
  17.  
  18. void operator++() {
  19. _range->popFront();
  20. }
  21.  
  22. typename RangeType::value_type operator*() const {
  23. return _range->front();
  24. }
  25. };
  26.  
  27. template<class T>
  28. struct iota
  29. {
  30. typedef T value_type;
  31.  
  32. T first;
  33. T last;
  34.  
  35. iota(T first, T last)
  36. : first(first), last(last) {}
  37.  
  38. T front() const {
  39. return first;
  40. }
  41.  
  42. void popFront() {
  43. ++first;
  44. }
  45.  
  46. bool empty() const {
  47. return first == last;
  48. }
  49.  
  50. typedef range_iterator<iota<T>> iterator;
  51. iterator begin() { return iterator{this}; }
  52. iterator end() { return iterator{this}; }
  53.  
  54. template<template<class InputRange> class RTy, class... Args>
  55. RTy<iota<T>> to(Args... args) {
  56. return RTy<iota<T>>(*this, args...);
  57. }
  58. };
  59.  
  60. template<class R>
  61. struct take
  62. {
  63. typedef typename R::value_type value_type;
  64.  
  65. R r;
  66. int count;
  67.  
  68. take(R r, int count)
  69. : r(r), count(count){}
  70.  
  71. value_type front() const {
  72. return r.front();
  73. }
  74.  
  75. void popFront() {
  76. --count;
  77. r.popFront();
  78. }
  79.  
  80. bool empty() const {
  81. return r.empty() || count == 0;
  82. }
  83.  
  84. typedef range_iterator<take<R>> iterator;
  85. iterator begin() { return iterator{this}; }
  86. iterator end() { return iterator{this}; }
  87.  
  88. template<template<class InputRange> class RTy, class... Args>
  89. RTy<take<R>> to(Args... args) {
  90. return RTy<take<R>>(*this, args...);
  91. }
  92. };
  93.  
  94. template<class R>
  95. struct filter
  96. {
  97. typedef typename R::value_type value_type;
  98.  
  99. R r;
  100. function<bool(value_type)> pred;
  101.  
  102. filter(R r, const function<bool(value_type)> &pred)
  103. : r(r), pred(pred)
  104. {
  105. while(!r.empty() && !pred(r.front())) {
  106. r.popFront();
  107. }
  108. }
  109.  
  110. value_type front() const {
  111. return r.front();
  112. }
  113.  
  114. void popFront() {
  115. r.popFront();
  116.  
  117. while(!r.empty() && !pred(r.front())) {
  118. r.popFront();
  119. }
  120. }
  121.  
  122. bool empty() const {
  123. return r.empty();
  124. }
  125.  
  126. typedef range_iterator<filter<R>> iterator;
  127. iterator begin() { return iterator{this}; }
  128. iterator end() { return iterator{this}; }
  129. };
  130.  
  131. int main(int argc, const char * argv[])
  132. {
  133. auto r = iota<int>(0, 10)
  134. .to<take>(6)
  135. .to<filter>([](int x){ return !(x % 2); });
  136.  
  137. for(int x : r)
  138. printf("%d ", x);
  139.  
  140. return 0;
  141. }
  142.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
0 2 4