181 lines
4.5 KiB
C++
181 lines
4.5 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* bidirectionnal_iterator.hpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/11/22 14:50:53 by apommier #+# #+# */
|
|
/* Updated: 2022/11/27 16:57:48 by apommier ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#pragma once
|
|
|
|
#define _end 0
|
|
|
|
namespace ft
|
|
{
|
|
|
|
template<class T, class Node>
|
|
class bidirectionnal_iterator
|
|
{
|
|
public :
|
|
|
|
typedef T value_type;
|
|
typedef std::ptrdiff_t difference_type;
|
|
typedef T* pointer;
|
|
typedef const T* const_pointer;
|
|
typedef T& reference;
|
|
typedef const T& const_reference;
|
|
typedef bidirectionnal_iterator iterator_category;
|
|
typedef Node node_type;
|
|
|
|
private :
|
|
|
|
node_type *_root;
|
|
node_type *_node;
|
|
|
|
public :
|
|
|
|
bidirectionnal_iterator() : _root(NULL), _node(NULL) {}
|
|
bidirectionnal_iterator(node_type *root, node_type *node) : _root(root), _node(node) {}
|
|
bidirectionnal_iterator(bidirectionnal_iterator const &rhs) { *this = rhs; }
|
|
|
|
~bidirectionnal_iterator() {}
|
|
|
|
bidirectionnal_iterator &operator=(bidirectionnal_iterator const &rhs)
|
|
{
|
|
if (this != &rhs)
|
|
{
|
|
this->_root = rhs._root;
|
|
//this->_end = rhs._end;
|
|
this->_node = rhs._node;
|
|
}
|
|
return (*this);
|
|
}
|
|
|
|
operator bidirectionnal_iterator<const T, const Node>() const
|
|
{
|
|
return (bidirectionnal_iterator<const T, const Node>(_root, _node));
|
|
}
|
|
|
|
node_type *base() { return (_node); }
|
|
|
|
friend bool operator==(const bidirectionnal_iterator &rhs, const bidirectionnal_iterator &lhs)
|
|
{
|
|
if (!lhs._node && !rhs._node)
|
|
return (true);
|
|
else if (!lhs._node || !rhs._node)
|
|
return (false);
|
|
return (lhs._node->data == rhs._node->data);
|
|
// if (lhs._node && rhs._node)
|
|
// return (lhs._node->data == rhs._node->data);
|
|
// if (!lhs._node && !rhs._node)
|
|
// return (true);
|
|
// else
|
|
// return (false);
|
|
}
|
|
|
|
friend bool operator!=(const bidirectionnal_iterator &rhs, const bidirectionnal_iterator &lhs)
|
|
{
|
|
if (!lhs._node && !rhs._node)
|
|
return (false);
|
|
else if (!lhs._node || !rhs._node)
|
|
return (true);
|
|
return (lhs._node->data != rhs._node->data);
|
|
// if (lhs._node && rhs._node)
|
|
// return (lhs._node->data != rhs._node->data);
|
|
// if (!lhs._node && !rhs._node)
|
|
// return (true);
|
|
// else
|
|
// return (false);
|
|
}
|
|
|
|
reference operator*() { return (_node->data); }
|
|
const_reference operator*() const { return (_node->data); }
|
|
pointer operator->() { return (&(_node->data)); }
|
|
const_pointer operator->() const { return (&(_node->data)); }
|
|
|
|
bidirectionnal_iterator &operator ++()
|
|
{
|
|
if (_node != NULL)
|
|
_node = successor(_node);
|
|
return (*this);
|
|
}
|
|
|
|
bidirectionnal_iterator operator ++(int)
|
|
{
|
|
bidirectionnal_iterator tmp(*this);
|
|
++(*this);
|
|
return (tmp);
|
|
}
|
|
|
|
bidirectionnal_iterator &operator --()
|
|
{
|
|
if (_node == _end)
|
|
_node = maximum(_root);
|
|
else
|
|
_node = predecessor(_node);
|
|
return (*this);
|
|
}
|
|
|
|
bidirectionnal_iterator operator --(int)
|
|
{
|
|
bidirectionnal_iterator tmp(*this);
|
|
--(*this);
|
|
return (tmp);
|
|
}
|
|
|
|
node_type *maximum(node_type *ptr)
|
|
{
|
|
while (ptr && ptr->right != _end)
|
|
ptr = ptr->right;
|
|
return (ptr);
|
|
}
|
|
|
|
node_type *minimum(node_type *ptr)
|
|
{
|
|
while (ptr && ptr->left != _end)
|
|
ptr = ptr->left;
|
|
return (ptr);
|
|
}
|
|
|
|
private :
|
|
|
|
node_type *predecessor(node_type *x)
|
|
{
|
|
if (x->left != _end)
|
|
{
|
|
return maximum(x->left);
|
|
}
|
|
node_type *y = x->parent;
|
|
while (y != NULL && x == y->left)
|
|
{
|
|
x = y;
|
|
y = y->parent;
|
|
}
|
|
return y;
|
|
}
|
|
|
|
node_type *successor(node_type *x)
|
|
{
|
|
if (!x)
|
|
return (0);
|
|
//std::cout << "_node: " << this->base() << std::endl;
|
|
//std::cout << "succkey: " << x->data.first << " | succvalue: " << x->data.second << std::endl;
|
|
if (x->right != _end)
|
|
return minimum(x->right);
|
|
node_type *y = x->parent;
|
|
while (y != NULL && x == y->right)
|
|
{
|
|
x = y;
|
|
y = y->parent;
|
|
}
|
|
if (y == NULL)
|
|
return _end;
|
|
return y;
|
|
}
|
|
};
|
|
|
|
} |