fork download
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template<class T>
  7. struct iota
  8. {
  9. T first;
  10. T last;
  11.  
  12. iota(T first, T last)
  13. : first(first), last(last) {}
  14.  
  15. T front() const {
  16. return first;
  17. }
  18.  
  19. void popFront() {
  20. ++first;
  21. }
  22.  
  23. bool empty() const {
  24. return first == last;
  25. }
  26.  
  27. struct iterator
  28. {
  29. iota *_iota;
  30.  
  31. bool operator!=(const iterator& that) const {
  32. assert(_iota == that._iota);
  33. return !_iota->empty();
  34. }
  35.  
  36. void operator++() {
  37. _iota->popFront();
  38. }
  39.  
  40. T operator*() const {
  41. return _iota->front();
  42. }
  43. };
  44.  
  45. iterator begin() { return iterator{this}; }
  46. iterator end() { return iterator{this}; }
  47.  
  48. template<template<class _T, class S> class R, class... Args>
  49. R<T, iota<T>> to(Args... args) {
  50. return R<T, iota<T>>(args..., this);
  51. }
  52. };
  53.  
  54. template<class T, class R>
  55. struct take
  56. {
  57. R* r;
  58. int count;
  59.  
  60. take(int count, R* r) : r(r), count(count){}
  61.  
  62. T front() const {
  63. return r->front();
  64. }
  65.  
  66. void popFront() {
  67. --count;
  68. r->popFront();
  69. }
  70.  
  71. bool empty() const {
  72. return r->empty() || count == 0;
  73. }
  74.  
  75. struct iterator
  76. {
  77. take *_take;
  78.  
  79. bool operator!=(const iterator& that) const {
  80. assert(_take == that._take);
  81. return !_take->empty();
  82. }
  83.  
  84. void operator++() {
  85. _take->popFront();
  86. }
  87.  
  88. T operator*() const {
  89. return _take->front();
  90. }
  91. };
  92.  
  93. iterator begin() { return iterator{this}; }
  94. iterator end() { return iterator{this}; }
  95. };
  96.  
  97. int main(int argc, const char * argv[])
  98. {
  99. for(int x : iota<int>(0, 5).to<take>(3))
  100. printf("%d ", x);
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty