Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <stdlib.h>
  5. #include <type_traits>
  6. #include <stdexcept>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. template <typename T, T B = std::numeric_limits<T>::min(), T E = std::numeric_limits<T>::max()>
  12. class Range
  13. {
  14.     T value;
  15.  
  16.     template <typename T3, T3 B3, T3 E3>
  17.     friend class Range;
  18.  
  19. public:
  20.     typedef T Type;
  21.     Range(T value) : value(value)
  22.     {
  23.         if (value < B || value > E)
  24.             throw std::out_of_range("Value is out of range");
  25.     }
  26.  
  27.     template<typename T2, T2 BO, T2 EO>
  28.     Range(const Range<T2, BO, EO> &o)
  29.     {
  30.         if (o.value < B || o.value > E)
  31.             throw std::out_of_range("Value is out of range");
  32.  
  33.         value = o.value;
  34.     }
  35.  
  36.     template<typename T2, T2 BO, T2 EO>
  37.     Range& operator=(const Range<T2, BO, EO> &o)
  38.     {
  39.         if (o.value < B || o.value > E)
  40.             throw std::out_of_range("Value is out of range");
  41.  
  42.         value = o.value;
  43.     }
  44.  
  45.     template<typename To, To Bo, To Eo>
  46.     Range<T, B + Bo, E + Eo> operator+(const Range<To, Bo, Eo> &o)
  47.     {
  48.         return { value + o.value };
  49.     }
  50.  
  51.     explicit operator T() const { return value; }
  52.  
  53.     static constexpr T min() { return B; }
  54.     static constexpr T max() { return E; }
  55.     static constexpr T length() { return E - B; }
  56.  
  57.     T distanceFromMin() { return value - min(); }
  58. };
  59.  
  60. template<typename T, T B, T E>
  61. std::ostream& operator<<(std::ostream &os, const Range<T, B, E> &range)
  62. {
  63.     cout << T(range) << " in [" << range.min() << " .. " << range.max() << "]";
  64. }
  65.  
  66. template<int a, int b>
  67. using IntRange = Range<int, a, b>;
  68.  
  69. template <typename Index, typename Value>
  70. class Array
  71. {
  72.     Value m_data[Index::length()];
  73. public:
  74.     static constexpr typename Index::Type size() { return Index::length(); }
  75.  
  76.     Value& operator[](Index idx)
  77.     {
  78.         return m_data[idx.distanceFromMin()];
  79.     }
  80.  
  81.     Value operator[](Index idx) const
  82.     {
  83.         return m_data[idx.distanceFromMin()];
  84.     }
  85.  
  86.     Value *begin()
  87.     {
  88.         return std::begin(m_data);
  89.     }
  90.  
  91.     Value *end()
  92.     {
  93.         return std::end(m_data);
  94.     }
  95. };
  96.  
  97. template<typename T, T a, T b>
  98. Range<T, T(std::sqrt(std::min<T>(0, a))), T(std::sqrt(b))> sqrt(const Range<T, a, b>& r)
  99. {
  100.     return std::sqrt(T(r));
  101. }
  102.  
  103. void tryprint(auto f)
  104. {
  105.     try
  106.     {
  107.         cout << f() << endl;
  108.     }
  109.     catch (const std::exception &e)
  110.     {
  111.         cout << e.what() << endl;
  112.     }
  113. }
  114.  
  115. auto f(auto a, auto b, auto c)
  116. {
  117.     return sqrt(sqrt(a + b) + c);
  118. }
  119.  
  120. int main()
  121. {
  122.     IntRange<-25, 33> a = -5;
  123.     IntRange<25, 144> b = 55;
  124.     Range c = a + b;
  125.     cout << "a = " << a << endl;
  126.     cout << "b = " << b << endl;
  127.     cout << "c = " << c << endl;
  128.     cout << "sqrt(b) = " << sqrt(b) << endl;
  129.     cout << "f(a,b,c) = " << f(a, b, c) << endl;
  130.  
  131.    
  132.     Range e = -5234;
  133.     cout << "e = " << e << endl;
  134.  
  135.     Array<IntRange<5, 13>, int> arr;
  136.     cout << "Arr size: " << arr.size() << endl;
  137.     for (int i = 5; i <= 13; i++) arr[i] = i * 2;
  138.  
  139.     cout << "Arr elems: ";
  140.     for (int i : arr)
  141.         cout << i << " ";
  142.     cout << endl;
  143.  
  144.     cout << "arr[7] = "; tryprint([&]() { return arr[7]; });
  145.     cout << "arr[1] = "; tryprint([&]() { return arr[1]; });
  146.  
  147.     cout << "d = ";  tryprint([&]() {
  148.         IntRange<0, 10> d = c;
  149.         return d;
  150.     });
  151.  
  152.     char _;
  153.     cin >> _;
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement