Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T, int N>
  8. class ScalarIterator;
  9.  
  10. template <typename T, int N>
  11. class Sequence
  12. {
  13. private:
  14.     vector<vector<T> > seq;
  15.     friend class ScalarIterator<T, N>;
  16. public:
  17.     Sequence(){}
  18.  
  19.     void add(vector<T> x)
  20.     {
  21.         if (x.size() != N) throw "incorrect dimencity";
  22.         seq.emplace_back(x);
  23.     }
  24.  
  25.     ScalarIterator<T, N> begin();
  26.     ScalarIterator<T, N> end();
  27.  
  28.     vector<T>& operator[] (int n)
  29.     {
  30.         return seq[n];
  31.     }
  32.  
  33.     const vector<T>& operator[] (int n) const
  34.     {
  35.         return seq[n];
  36.     }
  37. };
  38.  
  39. template <typename T, int N>
  40. class ScalarIterator:
  41.     public iterator<
  42.         bidirectional_iterator_tag,
  43.         T,
  44.         ptrdiff_t,
  45.         const T*,
  46.         const T
  47.     >
  48. {
  49. private:
  50.     Sequence<T, N> *s;
  51.     bool is_default;
  52.     int ind;
  53.  
  54.     bool is_end() const {
  55.         return ind >= s->seq.size();
  56.     }
  57.  
  58. public:
  59.         ScalarIterator(): is_default(true) {}
  60.         ScalarIterator(Sequence<T, N> &obj): ScalarIterator<T, N>(obj, 0) {};
  61.         ScalarIterator(Sequence<T, N> &obj, int ind): s(&obj), ind(ind), is_default(false) {}
  62.  
  63.     bool operator== (const ScalarIterator<T, N> &other) const
  64.     {
  65.         return (is_default && other.is_default) ||
  66.             (is_default && other.is_end()) ||
  67.             (is_end() && other.is_default) ||
  68.             (s == other.s && ind == other.ind);
  69.     }
  70.  
  71.     bool operator!= (const ScalarIterator<T, N> &other) const
  72.     {
  73.         return !(*this == other);
  74.     }
  75.  
  76.     const T operator* ()
  77.     {
  78.         if (is_default) throw "not initialized iterator";
  79.         T res = 0;
  80.         for (int i = 0; i < N; i++)
  81.             res += (s->seq[ind])[i] * (s->seq[ind+1])[i];
  82.         return res;
  83.     }
  84.  
  85.     ScalarIterator<T, N>& operator++()
  86.     {
  87.         if (is_default) throw "not initialized iterator";
  88.         ind++;
  89.         return *this;
  90.     }
  91.  
  92.     ScalarIterator<T, N> operator++(int)
  93.     {
  94.         ScalarIterator<T, N> tmp (*this);
  95.         ind++;
  96.         return tmp;
  97.     }
  98.  
  99.     ScalarIterator<T, N>& operator--()
  100.     {
  101.         if (is_default) throw "not initialized iterator";
  102.         ind--;
  103.         return *this;
  104.     }
  105.  
  106.     ScalarIterator<T, N> operator--(int)
  107.     {
  108.         ScalarIterator<T, N> tmp(*this);
  109.         ind--;
  110.         return tmp;
  111.     }
  112. };
  113.  
  114. template<typename T, int N>
  115. ScalarIterator<T, N> Sequence<T, N>::begin()
  116. {
  117.     return ScalarIterator<T, N>(*this);
  118. }
  119.  
  120. template<typename T, int N>
  121. ScalarIterator<T, N> Sequence<T, N>::end()
  122. {
  123.     return ScalarIterator<T, N>(*this, this->seq.size()-1);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement