Advertisement
Guest User

Untitled

a guest
May 14th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 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.     virtual __M_basic_it< _TbasicType >* __copy() = 0;
  34. };
  35.  
  36. template < typename _Titerator, typename _Ttype, typename _TbasicType = _Ttype >
  37. class __M_it : public __M_basic_it< _TbasicType > {
  38.  
  39. private:
  40.     _Titerator          __M_iterator;
  41.     _Titerator          __M_end;
  42.  
  43.     const _Ttype*       __M_cache;
  44.  
  45.  
  46. public:
  47.     typedef _Titerator iterator;
  48.  
  49.     __M_it(const _Titerator &_begin, const _Titerator &_end) :
  50.         __M_iterator(_begin),
  51.         __M_end(_end),
  52.         __M_cache(NULL) {}
  53.  
  54.     _TbasicType operator *() {
  55.         return _TbasicType(*const_cast<_Ttype*>(__M_cache));
  56.     }
  57.  
  58.     ~__M_it() { delete __M_cache; }
  59.  
  60.     bool operator++() {
  61.         if ((__M_iterator) != __M_end) {
  62.             __M_cache = &(*__M_iterator);
  63.             __M_iterator++;
  64.             return true;
  65.         }
  66.         return false;
  67.     }
  68.    
  69.     __M_basic_it< _TbasicType >* __copy() {
  70.         return new __M_it<_Titerator,_Ttype,_TbasicType>(__M_iterator, __M_end);
  71.     }
  72.  
  73. };
  74.  
  75. template < typename T >
  76. class ZrodloDanych {
  77.  
  78. private:
  79.  
  80.     typedef __M_basic_it< T >* __T_iterator;
  81.  
  82.     /* Iterator */
  83.     __T_iterator __M_iterator;
  84.  
  85. public:
  86.  
  87.     /*
  88.      * Konstruktor dla tablicy.
  89.      * @param _array Tablica T.
  90.      */
  91.     template < size_t N >
  92.     ZrodloDanych(T (&_array)[N]) {
  93.         __M_iterator = new __M_it<
  94.                 const T*,
  95.                 T
  96.             >(&_array[0], &_array[N]);
  97.     }
  98.    
  99.     ZrodloDanych(const ZrodloDanych< T > &_orig) :
  100.             __M_iterator(_orig.__M_iterator -> __copy())
  101.                     {}
  102.  
  103.     /**
  104.      * Konstruktor dla kontenera STL'a.
  105.      * @param _orig Kontener STL'a.
  106.      */
  107.     template < typename STL_Container >
  108.     ZrodloDanych(STL_Container &_orig) {
  109.         __M_iterator = new __M_it<
  110.                 typename STL_Container::iterator,
  111.                 typename STL_Container::value_type,
  112.                 T
  113.             >(_orig.begin(), _orig.end());
  114.     }
  115.  
  116.     /**
  117.      * Konstruktor dla stałego kontenera STL'a.
  118.      * @param _orig Kontener STL'a.
  119.      */
  120.     template < typename STL_Container >
  121.     ZrodloDanych(const STL_Container &_orig) {
  122.         __M_iterator = new __M_it<
  123.                 typename STL_Container::const_iterator,
  124.                 typename STL_Container::value_type,
  125.                 T
  126.             >(_orig.begin(), _orig.end());
  127.     }
  128.  
  129.     /**
  130.      * Konstruktor dla dwóch iteratorów STL'a.
  131.      * @param _begin Iterator "początku".
  132.      * @param _end Iterator "końca".
  133.      */
  134.     template < typename STL_Iterator >
  135.     ZrodloDanych(const STL_Iterator &_begin, const STL_Iterator &_end) {
  136.         __M_iterator = new __M_it<
  137.                 STL_Iterator,
  138.                 T
  139.             >(_begin, _end);
  140.     }
  141.  
  142.     /**
  143.      * Konstruktor dla dwóch wskaźników tablicy.
  144.      * @param _ptr1 Wskaźnik początku.
  145.      * @param _ptr2 Wskaźnik końca.
  146.      */
  147.     ZrodloDanych(const T *_ptr1, const T *_ptr2) {
  148.         __M_iterator = new __M_it<
  149.                 const T*,
  150.                 T
  151.             >(_ptr1, _ptr2);
  152.     }
  153.  
  154.     bool nastepnyElement() { return ++(*__M_iterator); }
  155.  
  156.     T aktualnaWartosc() { return *(*__M_iterator); }
  157.  
  158. };
  159.  
  160.  
  161. #endif  /* ZRODLODANYCH_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement