//======================================================================= // Copyright Baptiste Wicht 2013-2018. // Distributed under the terms of the MIT License. // (See accompanying file LICENSE or copy at // http://www.opensource.org/licenses/MIT) //======================================================================= #ifndef ITERATOR_HPP #define ITERATOR_HPP #include #include #include #include namespace std { template size_t distance(Iterator it, Iterator end) { // For now, we only have random access iterator return end - it; } /*! * \brief Reverse iterator adapter */ template struct reverse_iterator { using iterator_type = Iterator; ///< The iterator type using value_type = typename std::iterator_traits::value_type; ///< The value type using difference_type = typename std::iterator_traits::difference_type; ///< The difference type using pointer = typename std::iterator_traits::pointer; ///< The pointer type using reference = typename std::iterator_traits::reference; ///< The reference type reverse_iterator(Iterator it) : it(it) { //Nothing else } /*! * \brief Returns a reference to the pointed element */ reference operator*() { return *it; } reverse_iterator& operator++() { --it; return *this; } reverse_iterator& operator--() { ++it; return *this; } bool operator==(const reverse_iterator& rhs) { return it == rhs.it; } bool operator!=(const reverse_iterator& rhs) { return it != rhs.it; } private: iterator_type it; }; template struct back_insert_iterator { using container_type = Container; ///< The container type using value_type = void; ///< The value type using difference_type = void; ///< The difference type using reference = void; ///< The reference type using const_reference = void; ///< The const reference type container_type& container; explicit back_insert_iterator(container_type& container) : container(container) {} back_insert_iterator& operator=(const typename container_type::value_type& value) { container.push_back(value); return *this; } back_insert_iterator& operator=(typename container_type::value_type&& value) { container.push_back(std::move(value)); return *this; } back_insert_iterator& operator*() { return *this; } back_insert_iterator& operator++() { return *this; } back_insert_iterator& operator++(int) { return *this; } }; template struct front_insert_iterator { using container_type = Container; ///< The container type using value_type = void; ///< The value type using difference_type = void; ///< The difference type using reference = void; ///< The reference type using const_reference = void; ///< The const reference type container_type& container; explicit front_insert_iterator(container_type& container) : container(container) {} front_insert_iterator& operator=(const typename container_type::value_type& value) { container.push_front(value); return *this; } front_insert_iterator& operator=(typename container_type::value_type&& value) { container.push_front(std::move(value)); return *this; } front_insert_iterator& operator*() { return *this; } front_insert_iterator& operator++() { return *this; } front_insert_iterator& operator++(int) { return *this; } }; template std::back_insert_iterator back_inserter(Container& c) { return std::back_insert_iterator(c); } template std::front_insert_iterator front_inserter(Container& c) { return std::front_insert_iterator(c); } } //end of namespace std #endif