Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <vector>
  5.  
  6. template <class T>
  7. class Range {
  8. public:
  9. Range(const T upper) : Range(0, upper, 1) {}
  10. Range(const T lower, const T upper) : Range(lower, upper, 1) {}
  11. Range(const T lower, const T upper, const T step)
  12. : lower_(lower), upper_(upper), step_(step) {}
  13.  
  14. class iterator {
  15. public:
  16. iterator(const Range &r, const T val) : owner_(r), val_(val) {}
  17. T operator*() const { return val_; }
  18. bool operator!=(const iterator &other) const { return val_ < other.val_; }
  19. iterator operator++() {
  20. val_ += owner_.step_;
  21. return *this;
  22. }
  23.  
  24. iterator reset() {
  25. val_ = lower_;
  26. return *this;
  27. }
  28.  
  29. private:
  30. const Range &owner_;
  31. T val_;
  32. };
  33.  
  34. iterator begin() const { return iterator(*this, lower_); }
  35. iterator end() const { return iterator(*this, upper_); }
  36.  
  37. private:
  38. T lower_, upper_, step_;
  39. };
  40.  
  41. template <class T>
  42. Range<T> range(T low, T up, T step) {
  43. return Range<T>(low, up, step);
  44. }
  45.  
  46. template <class T>
  47. class Range3D {
  48. public:
  49. Range3D(const T max) : Range3D({max, max}, {max, max}, {max, max}) {}
  50. Range3D(std::pair<T, T> xbounds, std::pair<T, T> ybounds,
  51. std::pair<T, T> zbounds)
  52. : xbounds_{xbounds}, ybounds_{ybounds}, zbounds_{zbounds} {}
  53.  
  54. class iterator {
  55. public:
  56. iterator(const Range3D &r, const T x, const T y, const T z)
  57. : owner_{r}, x_{x}, y_{y}, z_{z} {}
  58. const iterator &operator*() const { return *this; }
  59. bool operator!=(const iterator &other) const {
  60. return x_ != other.x_ && y_ != other.y_ && z_ != other.z_;
  61. }
  62. iterator operator++() {
  63. ++z_;
  64. if (z_ == owner_.zbounds_.second) {
  65. z_ = owner_.zbounds_.first;
  66. ++y_;
  67. if (y_ == owner_.ybounds_.second) {
  68. y_ = owner_.ybounds_.first;
  69. ++x_;
  70. }
  71. }
  72. return *this;
  73. }
  74.  
  75. T x() const { return x_; }
  76. T y() const { return y_; }
  77. T z() const { return z_; }
  78.  
  79. private:
  80. const Range3D &owner_;
  81. T x_, y_, z_;
  82. };
  83.  
  84. iterator begin() const {
  85. return iterator{*this, xbounds_.first, ybounds_.first, zbounds_.first};
  86. }
  87. iterator end() const {
  88. return iterator{*this, xbounds_.second, ybounds_.second, zbounds_.second};
  89. }
  90.  
  91. private:
  92. std::pair<T, T> xbounds_, ybounds_, zbounds_;
  93. };
  94.  
  95. template <class T, class U>
  96. class Enumeration {
  97. public:
  98. Enumeration(const T &begin, const T &end) : Enumeration(begin, end, 0) {}
  99. Enumeration(const T &begin, const T &end, int start_idx)
  100. : begin_{begin}, end_{end}, start_idx_{start_idx} {};
  101.  
  102. class iterator {
  103. public:
  104. iterator(const Enumeration &e, const T val)
  105. : owner_(e), val_(val), idx_(e.start_idx_) {}
  106. iterator operator*() const { return *this; }
  107. bool operator!=(const iterator &other) const { return val_ != other.val_; }
  108. iterator operator++() {
  109. ++val_;
  110. ++idx_;
  111. return *this;
  112. }
  113.  
  114. int idx() const { return idx_; }
  115. U value() const { return *val_; }
  116.  
  117. private:
  118. const Enumeration &owner_;
  119. T val_;
  120. int idx_;
  121. };
  122.  
  123. iterator begin() { return iterator{*this, begin_}; }
  124. iterator end() { return iterator{*this, end_}; }
  125.  
  126. private:
  127. T begin_, end_;
  128. int start_idx_;
  129. };
  130.  
  131. template <class T>
  132. auto enumerate(const T &begin, const T &end, int i0 = 0) {
  133. return Enumeration<T, decltype(*begin)>(begin, end, i0);
  134. }
  135.  
  136. int main() {
  137. std::vector<int> x{10, 20, 30, 40, 50};
  138. for (const auto &e : enumerate(x.begin(), x.end())) {
  139. std::cout << e.idx() << " " << e.value() << std::endl;
  140. }
  141.  
  142. std::cout << std::endl;
  143.  
  144. std::set<int> y{10, 20, 3, 3, 40, 10, 50};
  145. for (const auto &e : enumerate(y.begin(), y.end())) {
  146. std::cout << e.idx() << " " << e.value() << std::endl;
  147. }
  148.  
  149. std::cout << std::endl;
  150.  
  151. std::string s{"The cake is a lie!"};
  152. for (const auto &e : enumerate(s.begin(), s.end())) {
  153. std::cout << e.idx() << " " << e.value() << std::endl;
  154. }
  155.  
  156. for(const auto &i : Range3D<int>({0, 3}, {0, 3}, {0, 3})) {
  157. std::cout << i.x() << " " << i.y() << " " << i.z() << std::endl;
  158. }
  159.  
  160. return 0;
  161. }
Add Comment
Please, Sign In to add comment