diff options
Add sem03
1 files changed, 205 insertions, 0 deletions
| -rw-r--r-- | Seminar03/src/dynamic-array/iterator/iterator.hpp | 205 |
diff --git a/Seminar03/src/dynamic-array/iterator/iterator.hpp b/Seminar03/src/dynamic-array/iterator/iterator.hpp new file mode 100644 index 0000000..4086bf7 --- /dev/null +++ b/Seminar03/src/dynamic-array/iterator/iterator.hpp @@ -0,0 +1,205 @@ +#pragma once +#include <stddef.h> + +template <class T> +class const_vector_iterator +{ + public: + const_vector_iterator(T* passedVal) + : memPointer{passedVal} + {} + const_vector_iterator(T* passedVal, size_t _push) + : memPointer{passedVal + _push} + {} + + const_vector_iterator<T> operator+(int off) const + { + return {memPointer + off}; + } + + const_vector_iterator<T> operator-(int off) const + { + return {memPointer - off}; + } + + int operator-(const_vector_iterator<T> other) const + { + return memPointer - other.memPointer; + } + + const T* operator->() const noexcept + { + return memPointer; + } + + T& operator*() const noexcept + { + return *(memPointer); + } + + bool operator==(const const_vector_iterator& it) const + { + return (memPointer == it.memPointer); + } + + bool operator!=(const const_vector_iterator& it) const + { + return !(memPointer == it.memPointer); + } + + private: + T* memPointer; +}; + +template <class T> +class vector_iterator +{ + public: + vector_iterator(T* passedVal) + : memPointer{passedVal} {}; + vector_iterator(T* passedVal, size_t _push) + : memPointer{passedVal + _push} {}; + + vector_iterator<T>& operator++() + { + memPointer++; + return *this; + } + + vector_iterator<T> operator++(int) const + { + vector_iterator it = *this; + ++(*this); + return it; + } + + vector_iterator<T>& operator--() + { + memPointer--; + return *this; + } + + vector_iterator<T> operator--(int) const + { + vector_iterator it = *this; + --(*this); + return it; + } + + operator const_vector_iterator<T>() const + { + return const_vector_iterator(memPointer); + } + + vector_iterator<T> operator+(int off) const + { + return {memPointer + off}; + } + + vector_iterator<T> operator-(int off) const + { + return {memPointer - off}; + } + + T* operator->() + { + return memPointer; + } + + const T* operator->() const + { + return memPointer; + } + + T& operator*() + { + return *(memPointer); + } + + bool operator==(const vector_iterator<T>& it) const + { + return (memPointer == it.memPointer); + } + + bool operator!=(const vector_iterator<T>& it) const + { + return !(memPointer == it.memPointer); + } + + private: + T* memPointer; +}; + +template <class T> +class reverse_vector_iterator +{ + public: + reverse_vector_iterator(T* passedVal) + : memPointer{passedVal} {}; + reverse_vector_iterator(T* passedVal, size_t _push) + : memPointer{passedVal + _push} {}; + + reverse_vector_iterator<T>& operator++() + { + memPointer--; + return *this; + } + + reverse_vector_iterator<T> operator++(int) const + { + reverse_vector_iterator it = *this; + ++(*this); + return it; + } + + reverse_vector_iterator<T>& operator--() + { + memPointer++; + return *this; + } + + reverse_vector_iterator<T> operator--(int) const + { + reverse_vector_iterator it = *this; + --(*this); + return it; + } + + reverse_vector_iterator<T> operator+(int off) const + { + return {memPointer - off}; + } + + reverse_vector_iterator<T> operator-(int off) const + { + return {memPointer + off}; + } + + T* operator->() + { + return memPointer; + } + + const T* operator->() const + { + return memPointer; + } + + T& operator*() + { + return *(memPointer); + } + + bool operator==(const reverse_vector_iterator<T>& it) const + { + return (memPointer == it.memPointer); + } + + bool operator!=(const reverse_vector_iterator<T>& it) const + { + return !(memPointer == it.memPointer); + } + + private: + T* memPointer; +};
\ No newline at end of file |
