Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Michał Garapich
- #ifndef ZRODLODANYCH_H
- #define ZRODLODANYCH_H
- #include <iostream>
- #include <iterator>
- #include <vector>
- /**
- * Podstawowy iterator.
- * @param _Ttype typ wartości, na której operuje iterator.
- */
- template < typename _TbasicType >
- class __M_basic_it {
- public:
- /**
- * Metoda, która zwraca wartośc, na którą w danym momencie wskazuje iterator.
- * @return Wartość elementu, na który wskazuje iterator.
- */
- virtual _TbasicType operator *() = 0;
- /**
- * Iterator przechodzi dalej. W cache'u mamy ostatnio poznaną wartość.
- * @return True, jeżeli udało się przejść dalej. W przeciwnym wypadku false.
- */
- virtual bool operator++() = 0;
- };
- template < typename _Titerator, typename _Ttype, typename _TbasicType = _Ttype >
- class __M_it : public __M_basic_it< _TbasicType > {
- private:
- _Titerator __M_iterator;
- _Titerator __M_end;
- const _Ttype* __M_cache;
- public:
- typedef _Titerator iterator;
- __M_it(const _Titerator &_begin, const _Titerator &_end) :
- __M_iterator(_begin),
- __M_end(_end),
- __M_cache(NULL) {}
- _TbasicType operator *() {
- return _TbasicType(*const_cast<_Ttype*>(__M_cache));
- }
- ~__M_it() { delete __M_cache; }
- bool operator++() {
- if ((__M_iterator) != __M_end) {
- __M_cache = &(*__M_iterator);
- __M_iterator++;
- return true;
- }
- return false;
- }
- };
- template < typename T >
- class ZrodloDanych {
- private:
- typedef __M_basic_it< T >* __T_iterator;
- /* Iterator */
- __T_iterator __M_iterator;
- public:
- /*
- * Konstruktor dla tablicy.
- * @param _array Tablica T.
- */
- template < size_t N >
- ZrodloDanych(T (&_array)[N]) {
- __M_iterator = new __M_it<
- const T*,
- T
- >(&_array[0], &_array[N]);
- }
- /**
- * Konstruktor dla kontenera STL'a.
- * @param _orig Kontener STL'a.
- */
- template < typename STL_Container >
- ZrodloDanych(STL_Container &_orig) {
- __M_iterator = new __M_it<
- typename STL_Container::iterator,
- typename STL_Container::value_type,
- T
- >(_orig.begin(), _orig.end());
- }
- /**
- * Konstruktor dla stałego kontenera STL'a.
- * @param _orig Kontener STL'a.
- */
- template < typename STL_Container >
- ZrodloDanych(const STL_Container &_orig) {
- __M_iterator = new __M_it<
- typename STL_Container::const_iterator,
- typename STL_Container::value_type,
- T
- >(_orig.begin(), _orig.end());
- }
- /**
- * Konstruktor dla dwóch iteratorów STL'a.
- * @param _begin Iterator "początku".
- * @param _end Iterator "końca".
- */
- template < typename STL_Iterator >
- ZrodloDanych(const STL_Iterator &_begin, const STL_Iterator &_end) {
- __M_iterator = new __M_it<
- STL_Iterator,
- T
- >(_begin, _end);
- }
- /**
- * Konstruktor dla dwóch wskaźników tablicy.
- * @param _ptr1 Wskaźnik początku.
- * @param _ptr2 Wskaźnik końca.
- */
- ZrodloDanych(const T *_ptr1, const T *_ptr2) {
- __M_iterator = new __M_it<
- const T*,
- T
- >(_ptr1, _ptr2);
- }
- bool nastepnyElement() { return ++(*__M_iterator); }
- T aktualnaWartosc() { return *(*__M_iterator); }
- };
- #endif /* ZRODLODANYCH_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement