Guest User

Untitled

a guest
Apr 4th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. #define show(X) cout << #X << ": " << X << endl;
  8.  
  9. #define MAX_DEPTH 10
  10.  
  11. template<class T>
  12. struct lazy {
  13.     lazy() = default;
  14.    
  15.     using num_type = function<T()>;
  16.    
  17.     num_type number;
  18.     int depth = 1;
  19.  
  20.  
  21.     lazy(const T &in) {
  22.        
  23.         number = [in] () -> T {
  24.             return in;
  25.         };
  26.  
  27.     }
  28.  
  29.     lazy(num_type f, int d) : number(f), depth(d + 1) {
  30.  
  31.         if(depth >= MAX_DEPTH) {
  32.             collapse();
  33.         }
  34.        
  35.     }
  36.  
  37.     auto operator+(const lazy& other) -> lazy {
  38.  
  39.         auto f = [other, n = number] () -> T {
  40.             return other.number() + n();
  41.         };
  42.  
  43.         return lazy { f, depth };
  44.     }
  45.  
  46.     inline void collapse() {
  47.         auto result = number();
  48.  
  49.         depth = 1;
  50.  
  51.         if(depth - 1) {
  52.             number = [result] () -> T {
  53.                 return result;
  54.             };
  55.         }
  56.     }
  57.  
  58.     operator T() {
  59.         collapse();
  60.         return number();
  61.     }
  62.  
  63. };
  64.  
  65.  
  66. int main() {
  67.     using lint = lazy<int>;
  68.    
  69.     lint a = 3;
  70.     lint b = 3234;
  71.     while(true) {
  72.         a = a + b;
  73.     }
  74.  
  75.  
  76. }
Add Comment
Please, Sign In to add comment