Advertisement
Guest User

Untitled

a guest
May 13th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. // Michał Garapich
  2.  
  3. #ifndef ZRODLODANYCH_H
  4. #define ZRODLODANYCH_H
  5.  
  6.  
  7. #include <iostream>
  8. #include <iterator>
  9. #include <vector>
  10.  
  11.  
  12. /**
  13.  * Podstawowy iterator.
  14.  * @param _Ttype typ wartości, na której operuje iterator.
  15.  */
  16. template < typename _TbasicType >
  17. class __M_basic_it {
  18.  
  19. public:
  20.  
  21.     /**
  22.      * Metoda, która zwraca wartośc, na którą w danym momencie wskazuje iterator.
  23.      * @return Wartość elementu, na który wskazuje iterator.
  24.      */
  25.     virtual _TbasicType operator *() = 0;
  26.  
  27.     /**
  28.      * Iterator przechodzi dalej. W cache'u mamy ostatnio poznaną wartość.
  29.      * @return True, jeżeli udało się przejść dalej. W przeciwnym wypadku false.
  30.      */
  31.     virtual bool operator++()  = 0;
  32. };
  33.  
  34. template < typename _Titerator, typename _Ttype, typename _TbasicType = _Ttype >
  35. class __M_it : public __M_basic_it< _TbasicType > {
  36.  
  37. private:
  38.     _Titerator          __M_iterator;
  39.     _Titerator          __M_end;
  40.  
  41.     const _Ttype*       __M_cache;
  42.  
  43.  
  44. public:
  45.     typedef _Titerator iterator;
  46.  
  47.     __M_it(const _Titerator &_begin, const _Titerator &_end) :
  48.         __M_iterator(_begin),
  49.         __M_end(_end),
  50.         __M_cache(NULL) {}
  51.  
  52.     _TbasicType operator *() {
  53.         return _TbasicType(*const_cast<_Ttype*>(__M_cache));
  54.     }
  55.  
  56.     ~__M_it() { delete __M_cache; }
  57.  
  58.     bool operator++() {
  59.         if ((__M_iterator) != __M_end) {
  60.             __M_cache = &(*__M_iterator);
  61.             __M_iterator++;
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.  
  67. };
  68.  
  69. template < typename T >
  70. class ZrodloDanych {
  71.  
  72. private:
  73.  
  74.     typedef __M_basic_it< T >* __T_iterator;
  75.  
  76.     /* Iterator */
  77.     __T_iterator __M_iterator;
  78.  
  79. public:
  80.  
  81.     /*
  82.      * Konstruktor dla tablicy.
  83.      * @param _array Tablica T.
  84.      */
  85.     template < size_t N >
  86.     ZrodloDanych(T (&_array)[N]) {
  87.         __M_iterator = new __M_it<
  88.                 const T*,
  89.                 T
  90.             >(&_array[0], &_array[N]);
  91.     }
  92.  
  93.     /**
  94.      * Konstruktor dla kontenera STL'a.
  95.      * @param _orig Kontener STL'a.
  96.      */
  97.     template < typename STL_Container >
  98.     ZrodloDanych(STL_Container &_orig) {
  99.         __M_iterator = new __M_it<
  100.                 typename STL_Container::iterator,
  101.                 typename STL_Container::value_type,
  102.                 T
  103.             >(_orig.begin(), _orig.end());
  104.     }
  105.  
  106.     /**
  107.      * Konstruktor dla stałego kontenera STL'a.
  108.      * @param _orig Kontener STL'a.
  109.      */
  110.     template < typename STL_Container >
  111.     ZrodloDanych(const STL_Container &_orig) {
  112.         __M_iterator = new __M_it<
  113.                 typename STL_Container::const_iterator,
  114.                 typename STL_Container::value_type,
  115.                 T
  116.             >(_orig.begin(), _orig.end());
  117.     }
  118.  
  119.     /**
  120.      * Konstruktor dla dwóch iteratorów STL'a.
  121.      * @param _begin Iterator "początku".
  122.      * @param _end Iterator "końca".
  123.      */
  124.     template < typename STL_Iterator >
  125.     ZrodloDanych(const STL_Iterator &_begin, const STL_Iterator &_end) {
  126.         __M_iterator = new __M_it<
  127.                 STL_Iterator,
  128.                 T
  129.             >(_begin, _end);
  130.     }
  131.  
  132.     /**
  133.      * Konstruktor dla dwóch wskaźników tablicy.
  134.      * @param _ptr1 Wskaźnik początku.
  135.      * @param _ptr2 Wskaźnik końca.
  136.      */
  137.     ZrodloDanych(const T *_ptr1, const T *_ptr2) {
  138.         __M_iterator = new __M_it<
  139.                 const T*,
  140.                 T
  141.             >(_ptr1, _ptr2);
  142.     }
  143.  
  144.     bool nastepnyElement() { return ++(*__M_iterator); }
  145.  
  146.     T aktualnaWartosc() { return *(*__M_iterator); }
  147.  
  148. };
  149.  
  150.  
  151. #endif  /* ZRODLODANYCH_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement