#ifndef MAP_ITERATOR_HPP # define MAP_ITERATOR_HPP # include // NULL, ptrdiff_t # include // iterator_tag # include "pair.hpp" # include "map_node.hpp" namespace ft { template < typename Key, typename T, typename Compare, typename Allocator > class map_iterator { private: typedef map_iterator Self; public: typedef std::bidirectional_iterator_tag iterator_category; typedef pair value_type; typedef std::ptrdiff_t difference_type; typedef value_type* pointer; typedef value_type& reference; map_iterator() : _node(), _sentinel() {} map_iterator(node* n, node_sentinel* sentinel) : _node(n), _sentinel(sentinel) {} //map_iterator(const map_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit reference operator*() const { return _node->value; } pointer operator->() const { return &_node->value; } Self& operator++() { if (_node == NULL) _node = _sentinel->child->min(); else if (_node->right) _node = _node->right->min(); else { node* up = _node->up; while (up != NULL && _node == up->right) { _node = up; up = up->up; } _node = up; } return *this; } Self& operator--() { if (_node == NULL) _node = _sentinel->child->max(); else if (_node->left) _node = _node->left->max(); else { node* up = _node->up; while (up != NULL && _node == up->left) { _node = up; up = up->up; } _node = up; } return *this; } Self operator++(int) { //Self old(*this); Self old = *this; ++(*this); return old; } Self operator--(int) { //Self old(*this); Self old = *this; --(*this); return old; } node* getNode() { return _node; } const node* getNode() const { return _node; } const node_sentinel* getSentinel() const { return _sentinel; } // TODO : friend Non-member functions syntaxe pas clair. friend bool operator==(const Self &lhs, const Self &rhs) { return lhs._node == rhs._node; } friend bool operator!=(const Self &lhs, const Self &rhs) { return !(lhs._node == rhs._node); } private: node* _node; node_sentinel* _sentinel; }; template < typename Key, typename T, typename Compare, typename Allocator > class map_const_iterator { private: typedef map_const_iterator Self; public: typedef std::bidirectional_iterator_tag iterator_category; typedef pair value_type; typedef std::ptrdiff_t difference_type; typedef const value_type* pointer; typedef const value_type& reference; map_const_iterator() : _node(), _sentinel() {} map_const_iterator(const node* node, const node_sentinel* sentinel) : _node(node), _sentinel(sentinel) {} //map_const_iterator(const map_const_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit map_const_iterator(const map_iterator& src) : _node(src.getNode()), _sentinel(src.getSentinel()) {} reference operator*() const { return _node->value; } pointer operator->() const { return &_node->value; } Self& operator++() { if (_node == NULL) _node = _sentinel->child->min(); else if (_node->right) _node = _node->right->min(); else { node* up = _node->up; while (up != NULL && _node == up->right) { _node = up; up = up->up; } _node = up; } return *this; } Self& operator--() { if (_node == NULL) _node = _sentinel->child->max(); else if (_node->left) _node = _node->left->max(); else { node* up = _node->up; while (up != NULL && _node == up->left) { _node = up; up = up->up; } _node = up; } return *this; } Self operator++(int) { //Self old(*this); Self old = *this; ++(*this); return old; } Self operator--(int) { //Self old(*this); Self old = *this; --(*this); return old; } node* getNode() const { return _node; } friend bool operator==(const Self &lhs, const Self &rhs) { return lhs._node == rhs._node; } friend bool operator!=(const Self &lhs, const Self &rhs) { return !(lhs._node == rhs._node); } private: const node* _node; const node_sentinel* _sentinel; }; } // namespace ft #endif