diff --git a/Makefile b/Makefile index 3c4328e..0a15997 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,16 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/12 19:45:18 by apommier #+# #+# # -# Updated: 2022/11/19 01:32:23 by apommier ### ########.fr # +# Updated: 2022/11/29 13:23:22 by apommier ### ########.fr # # # # **************************************************************************** # NAME = a.out -SRCS = tests/main.cpp +SRCS = tests/map_tester.cpp\ + tests/vector_tester.cpp\ + tests/stack_tester.cpp\ + tests/main.cpp -INCLUDE = ./containers OBJS = ${SRCS:.cpp=.o} CC = c++ CFLAGS = -g -Wall -Wextra -Werror -std=c++98 @@ -23,7 +25,7 @@ RM = rm -rf $(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o) ${NAME}: ${OBJS} - ${CC} ${LIB} ${OBJS} ${INCLUDE} -o ${NAME} + ${CC} ${LIB} ${OBJS} -o ${NAME} all: ${NAME} diff --git a/containers/map.hpp b/containers/map.hpp index b6996bc..d616dec 100644 --- a/containers/map.hpp +++ b/containers/map.hpp @@ -33,12 +33,15 @@ template< class map { + protected : + struct node; + public : //----------------------------- //---------MEMBER TYPE--------- //----------------------------- - struct node; + typedef Key key_type; typedef node Node; @@ -74,9 +77,6 @@ class map NodePtr _end; size_type _size; - public : - - struct node{ value_type data; NodePtr parent; @@ -91,6 +91,8 @@ class map {} }; + public : + //--------------------------------------- //---------COPLIEN FORM FUNCTION--------- //--------------------------------------- diff --git a/containers/map2.hpp b/containers/map2.hpp deleted file mode 100644 index b8992cf..0000000 --- a/containers/map2.hpp +++ /dev/null @@ -1,1274 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* map.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/11/26 15:23:32 by apommier #+# #+# */ -/* Updated: 2022/11/27 11:46:38 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#pragma once - -#include "./iterators/bidirectionnal_iterator.hpp" -#include "./iterators/pair.hpp" -#include "./iterators/make_pair.hpp" -#include "vector.hpp" - -#define RED 1 -#define BLACK 0 -#define _end 0 - -//typedef typename Alloc::template rebind >::other - -namespace ft -{ - -template< - class Key, - class T, - class Compare = std::less, - class Allocator = std::allocator > > - -class map -{ - public : - - //----------------------------- - //---------MEMBER TYPE--------- - //----------------------------- - struct node; - - typedef Key key_type; - typedef T mapped_type; - typedef ft::pair value_type; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - typedef Compare key_compare; - typedef Allocator allocator_type; - typedef typename Allocator::template rebind::other node_allocator_type; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef typename Allocator::pointer pointer; - typedef typename Allocator::const_pointer const_pointer; - typedef ft::bidirectionnal_iterator iterator; - typedef ft::bidirectionnal_iterator const_iterator; - typedef std::reverse_iterator reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; - - class value_compare; - - protected : - - key_compare _comp; - allocator_type _alloc; - node_allocator_type _node_alloc; - - node *_root; - size_type _size; - - public : - - - struct node{ - value_type data; - node *parent; - node *right; - node *left; - bool color; - - node(key_type const &key, mapped_type const &val) - : data(ft::make_pair(key, val)), parent(0), right(_end), left(_end), color(0) - {} - }; - //----------------------------- - //-----PRIVATE MEMBER TYPE----- - //----------------------------- - - - public : - - //--------------------------------------- - //---------COPLIEN FORM FUNCTION--------- - //--------------------------------------- - - explicit map( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) - { - _size = 0; - } - - template< class InputIt > - map( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) - { - _size = 0; - this->insert(first, last); - } - - map( const map& x) : _comp(x._comp), _alloc(x._alloc), _node_alloc(x._node_alloc) - { - _root = 0; - _size = 0; - insert(x.begin(), x.end()); - //*this = x; - } - - ~map() - { - - } - - map& operator=(const map& x) - { - _comp = x._comp; - _alloc = x._alloc; - _node_alloc = x._node_alloc; - _root = 0; - _size = 0; - insert(x.begin(), x.end()); - // _comp = x._comp; - // _alloc = x._alloc; - // _node_alloc = x._node_alloc; - // _root = x._root; - // _size = x._size; - return (*this); - } - - //---------------------------------- - //---------MEMBER FUNCTION---------- - //---------------------------------- - - //------------------------- - //--------Iterators-------- - //------------------------- - iterator begin() - { - iterator it(_root, _root); - iterator ret(_root, it.minimum(_root)); - - //std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl; - //ret++; - //std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl; - //return iterator(_root, it.minimum(_root)); - return ret; - } - - const_iterator begin() const - { - const_iterator it(_root, _root); - return const_iterator(_root, it.minimum(_root)); - } - - iterator end() - { - return iterator(_root, _end); - } - - const_iterator end() const - { - return const_iterator(_root, _end); - } - - reverse_iterator rbegin() - { - return reverse_iterator(this->end()); - } - - const_reverse_iterator rbegin() const - { - return const_reverse_iterator(this->end()); - } - - reverse_iterator rend() - { - return reverse_iterator(this->begin()); - } - - const_reverse_iterator rend() const - { - return const_reverse_iterator(this->begin()); - } - - - //------------------------ - //--------Capacity-------- - //------------------------ - - bool empty() const - { - if (!_size) - return (1); - return(0); - } - - size_type size() const - { - return (_size); - } - - size_type max_size() const - { - return (_alloc.max_size()); - } - - - //------------------------------ - //--------Element access-------- - //------------------------------ - mapped_type& operator[] (const key_type& k) - { - iterator tmp = this->find(k); - node *pt; - //ft::pair<> new_pair = ft::make_pair(k, mapped_type()); - //value_type new_pair = ft::make_pair(k, mapped_type()); - //std::cout << "base== " << tmp.base() << std::endl; - if (tmp.base() == _end) - { - pt = new_node(k, mapped_type()); - _root = insert_node(_root, pt); - _size++; - return (pt->data.second); - //return (this->insert_node(_root, new_node(k, mapped_type()))->data.second); //?????? - } - else - return ((*tmp).second); - } - - mapped_type& at (const key_type& k) - { - iterator tmp = this->find(k); - - if (tmp->m == _end) - throw (std::out_of_range("ft::map::at")); - else - return (*tmp.pair.second); - } - - const mapped_type& at (const key_type& k) const - { - iterator tmp = this->find(k); - - if (tmp->m == _end) - throw (std::out_of_range("ft::map::at")); - else - return (*tmp.pair.second); - } - - //------------------------- - //--------Modifiers-------- - //------------------------- - ft::pair insert (const value_type& val) - { - node *pt = new_node(val.first, val.second); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - _size++; - } - - iterator insert (iterator position, const value_type& val) - { - (void)position; - node *pt = new_node(val.first, val.second); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - _size++; - } - - template - void insert (InputIterator first, InputIterator last) - { - while (first != last) - { - node *pt = new_node((*first).first, (*first).second); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - first++; - _size++; - } - } - - void erase (iterator position) - { - std::cout << "position"<< std::endl; - //std::cout << "key: " << position->first << " | value: " << position->second << std::endl; - //std::cout << "rrrrrkey: " << _root->data.first << " | rrrrvalue: " << _root->data.second << std::endl; - deleteNode(position.base()); - _size--; - } - - size_type erase (const key_type& k) - { - std::cout << "key== "<< k << std::endl; - iterator test = find(k); - //std::cout << "test-base= " << test.base() << std::endl; - if (test.base()) - { - deleteNode(test.base()); - _size--; - return(1); - } - return (0); - } - - void erase (iterator first, iterator last) - { - std::cout << "range"<< std::endl; - while (last != first) - { - last--; - _size--; - deleteNode(last.base()); - } - } - - void swap (map& x) - { - map tmp; - - tmp->_comp = _comp; - tmp->_alloc = _alloc; - tmp->_node_alloc = _node_alloc; - tmp->_root = _root; - tmp->_size = _size; - - _comp = x->_comp; - _alloc = x->_alloc; - _node_alloc = x->_node_alloc; - _root = x->_root; - _size = x->_size; - - x->_comp = tmp-> _comp; - x->_alloc = tmp->_alloc; - x->_node_alloc = tmp->_node_alloc; - x->_root = tmp->_root; - //x->_end = tmp->_end; - x->_size = tmp->_size; - } - -void printPair(iterator iterator) -{ - std::cout << "key: " << iterator->first << " | value: " << iterator->second << std::endl; -} - -void printReverse() -{ - std::cout << "_________________________in map print______________________" << std::endl; - iterator it = this->end(), ite = this->begin(); - while (it != ite) - { - it--; - printPair(it); - } - std::cout << "_______________________________________________" << std::endl; -} - - void clear() - { - //iterator it = this->begin(); - //iterator ite = this->end(); - - //erase(this->begin(), this->end()); - // printReverse(); - // while (it != ite) - // { - // std::cout << "saesfawf= " << std::endl; - // deleteNode(it.base()); - // printReverse(); - // it++; - // std::cout << "size= " << _size << std::endl; - // _size--; - // } - // // _size = 0; - // _root = 0; - while (_size) - { - //printReverse(); - deleteNode(_root); - _size--; - } - _root = 0; - } - - //------------------------- - //--------Observers-------- - //------------------------- - - key_compare key_comp() const - { - return (_comp); - } - - value_compare value_comp() const - { - return (value_compare(_comp)); - } - - - //------------------------- - //-------Operations-------- - //------------------------- - - iterator find(const key_type& k) - { - node *temp = _root; - while (temp != NULL) - { - if (k < temp->data.first) - { - //if (temp->left == NULL) - // break; - //else - temp = temp->left; - } - else if (k == temp->data.first) - break; - else - { - //if (temp->right == NULL) - // break; - //else - temp = temp->right; - } - } - return iterator(_root, temp); - } - - const_iterator find(const key_type& k) const - { - node *temp = _root; - while (temp != NULL) - { - if (k < temp->data.first) - { - if (temp->left == NULL) - break; - else - temp = temp->left; - } - else if (k == temp->data.first) - break; - else - { - if (temp->right == NULL) - break; - else - temp = temp->right; - } - } - return const_iterator(_root, temp); - } - - // iterator find (const key_type& k) - // { - // node *x = _root; - - // std::cout << "=============k=========== " << k << std::endl; - // while (x != _end && x->data.first != k) - // { - // std::cout << "x data first " << x->data.first << std::endl; - // std::cout << "x data right " << x->right->data.first << std::endl; - // std::cout << "x data left " << x->left->data.first << std::endl; - // if (k > x->data.first) - // x = x->left; - // else - // x = x->right; - // } - // std::cout << "============x============= " << x << std::endl; - // if (x != 0) - // std::cout << "x data first " << x->data.first << std::endl; - // return (iterator(_root, x)); - // } - - // const_iterator find (const key_type& k) const - // { - // node *x = _root; - - // std::cout << "=============k=========== " << k << std::endl; - // while (x != _end && x->data.first != k) - // { - // if (k > x->data.first) - // x = x->left; - // else - // x = x->right; - // } - // std::cout << "============x============= " << x << std::endl; - // return (iterator(_root, x)); - // } - - size_type count (const key_type& k) const - { - if (find(k)->m == _end) - return (0); - return (1); - } - - iterator lower_bound (const key_type& k) - { - iterator it = begin(), ite = end(); - - while (it != ite) - { - if (_comp((*it).first, k) == false) - return (it); - it++; - } - return (it); - } - - const_iterator lower_bound (const key_type& k) const - { - const_iterator it = begin(), ite = end(); - - while (it != ite) - { - if (_comp((*it).first, k) == false) - return (it); - it++; - } - return (it); - } - - iterator upper_bound (const key_type& k) - { - iterator it = begin(), ite = end(); - - while (it != ite) - { - if (_comp(k, (*it).first)) - return (it); - it++; - } - return (it); - } - - const_iterator upper_bound (const key_type& k) const - { - const_iterator it = begin(), ite = end(); - - while (it != ite) - { - if (_comp(k, (*it).first)) - return (it); - it++; - } - return (it); - } - - ft::pair equal_range (const key_type& k) const - { - return (ft::make_pair(lower_bound(k), upper_bound(k))); - } - - ft::pair equal_range (const key_type& k) - { - return (ft::make_pair(lower_bound(k), upper_bound(k))); - } - - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ******************************TREE FUNCTIONS****************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - - private : - - void rotateLeft(node *&pt) - { - node *pt_right = pt->right; - pt->right = pt_right->left; - if (pt->right != NULL) - pt->right->parent = pt; - pt_right->parent = pt->parent; - if (pt->parent == NULL) - _root = pt_right; - else if (pt == pt->parent->left) - pt->parent->left = pt_right; - else - pt->parent->right = pt_right; - pt_right->left = pt; - pt->parent = pt_right; - } - - - void rotateRight(node *&pt) - { - - node *pt_left = pt->left; - pt->left = pt_left->right; - if (pt->left != NULL) - pt->left->parent = pt; - pt_left->parent = pt->parent; - if (pt->parent == NULL) - _root = pt_left; - else if (pt == pt->parent->left) - pt->parent->left = pt_left; - else - pt->parent->right = pt_left; - pt_left->right = pt; - pt->parent = pt_left; - } - - node* insert_node(node* root, node *pt) - { - /* If the tree is empty, return a new node */ - if (root == NULL) - return pt; - else if (pt->data.first == root->data.first) - { - _size--; - return root; - } - /* Otherwise, recur down the tree */ - if (pt->data < root->data) - { - root->left = insert_node(root->left, pt); - root->left->parent = root; - } - else if (pt->data > root->data) - { - root->right = insert_node(root->right, pt); - root->right->parent = root; - } - /* return the (unchanged) node pointer */ - return root; - } - - void fixViolation(node *&root, node *&pt) - { - node *parent_pt = NULL; - node *grand_parent_pt = NULL; - while ((pt != root) && (pt->color != BLACK) && (pt->parent->color == RED)) - { - parent_pt = pt->parent; - grand_parent_pt = pt->parent->parent; - /* Case : A Parent of pt is left child of Grand-parent of pt */ - if (parent_pt == grand_parent_pt->left) - { - node *uncle_pt = grand_parent_pt->right; - /* Case : 1 The uncle of pt is also red Only Recoloring required */ - if (uncle_pt != NULL && uncle_pt->color == RED) - { - grand_parent_pt->color = RED; - parent_pt->color = BLACK; - uncle_pt->color = BLACK; - pt = grand_parent_pt; - } - else - { - /* Case : 2 pt is right child of its parent Left-rotation required */ - if (pt == parent_pt->right) - { - rotateLeft(parent_pt); - pt = parent_pt; - parent_pt = pt->parent; - } - /* Case : 3 pt is left child of its parent Right-rotation required */ - rotateRight(grand_parent_pt); - swapColors(parent_pt, grand_parent_pt); - pt = parent_pt; - } - } - /* Case : B Parent of pt is right child of Grand-parent of pt */ - else - { - node *uncle_pt = grand_parent_pt->left; - /* Case : 1 The uncle of pt is also red Only Recoloring required */ - if ((uncle_pt != NULL) && (uncle_pt->color == RED)) - { - grand_parent_pt->color = RED; - parent_pt->color = BLACK; - uncle_pt->color = BLACK; - pt = grand_parent_pt; - } - else - { - /* Case : 2 | pt is left child of its parent | Right-rotation required */ - if (pt == parent_pt->left) - { - rotateRight(parent_pt); - pt = parent_pt; - parent_pt = pt->parent; - } - /* Case : 3 pt is right child of its parent Left-rotation required */ - rotateLeft(grand_parent_pt); - swapColors(parent_pt, grand_parent_pt); - pt = parent_pt; - } - } - } - root->color = BLACK; - } - - /* ************************************************************************** */ - /* **********************************DELETE********************************** */ - /* ************************************************************************** */ - - - node *uncle(node *x) - { - if (x->parent == NULL || x->parent->parent == NULL) - return NULL; - if (isOnLeft(x->parent)) - return x->parent->parent->right; - else - return x->parent->parent->left; - } - - bool isOnLeft(node *x) - { - if (!x->parent) - return(false); - return x == x->parent->left; - } - - // returns pointer to sibling - node *sibling(node *x) - { - // sibling null if no parent - if (!x) - return NULL; - if (x->parent == NULL) - return NULL; - if (isOnLeft(x)) - return x->parent->right; - return x->parent->left; - } - - // moves node down and moves given node in its place - void moveDown(node *nParent, node *x) - { - if (x->parent != NULL) - { - if (isOnLeft(x)) - x->parent->left = nParent; - else - x->parent->right = nParent; - } - nParent->parent = x->parent; - x->parent = nParent; - } - - bool hasRedChild(node *x) - { - return (x->left != NULL && x->left->color == RED) || (x->right != NULL && x->right->color == RED); - } - - void swapColors(node *x1, node *x2) - { - bool temp; - temp = x1->color; - x1->color = x2->color; - x2->color = temp; - } - - void swapValues(node **u, node **v) - { - //std::cout << "-------------------swap value-------------------\n"; - - //node *test = (*u)->parent; - //node *test2 = (*v)->parent; - - node *tmp = new_node((*u)->data.first, (*u)->data.second); - node *tmp2 = new_node((*v)->data.first, (*v)->data.second); - - tmp->parent = (*v)->parent; - tmp->right = (*v)->right; - tmp->left = (*v)->left; - tmp2->parent = (*u)->parent; - tmp2->right = (*u)->right; - tmp2->left = (*u)->left; - - if (isOnLeft(*u)) - (*u)->parent->left = tmp2; - else if ((*u)->parent) - (*u)->parent->right = tmp2; - - if (isOnLeft(*v)) - (*v)->parent->left = tmp; - else if ((*v)->parent) - (*v)->parent->right = tmp; - - if ((*v)->right) - (*v)->right->parent = tmp; - if ((*v)->left) - (*v)->left->parent = tmp; - - if ((*u)->right) - (*u)->right->parent = tmp2; - if ((*u)->left) - (*u)->left->parent = tmp2; - - //destruct_node(*u); - //destruct_node(*v); - *v = tmp; - *u = tmp2; - //destruct_node(v); - //return (tmp); - - //std::cout << "test= " << test->right << std::endl; - //std::cout << "test= " << test2->right << std::endl; - } - - void fixDoubleBlack(node *x) - { - //std::cout << "x= " << x << std::endl; - if (x == _root) - return; - // Reached root - - node *sibling = this->sibling(x), *parent = x->parent; - if (sibling == NULL) - { - // No sibling, double black pushed up - fixDoubleBlack(parent); - } - else - { - if (sibling->color == RED) - { - // Sibling red - parent->color = RED; - sibling->color = BLACK; - if (isOnLeft(sibling)) - { - // left case - this->rotateRight(parent); - } - else - { - // right case - rotateLeft(parent); - } - fixDoubleBlack(x); - } - else - { - // Sibling black - if (hasRedChild(sibling)) - { - // at least 1 red children - if (sibling->left != NULL && sibling->left->color == RED) - { - if (isOnLeft(sibling)) - { - // left left - sibling->left->color = sibling->color; - sibling->color = parent->color; - rotateRight(parent); - } - else - { - // right left - sibling->left->color = parent->color; - rotateRight(sibling); - rotateLeft(parent); - } - } - else - { - if (isOnLeft(sibling)) - { - // left right - sibling->right->color = parent->color; - rotateLeft(sibling); - rotateRight(parent); - } - else - { - // right right - sibling->right->color = sibling->color; - sibling->color = parent->color; - rotateLeft(parent); - } - } - parent->color = BLACK; - } - else - { - // 2 black children - sibling->color = RED; - if (parent->color == BLACK) - fixDoubleBlack(parent); - else - parent->color = BLACK; - } - } - } - } - - void fixRedRed(node *x) - { - // if x is root color it black and return - if (x == _root) - { - x->color = BLACK; - return; - } - // initialize parent, grandparent, uncle - node *parent = x->parent, *grandparent = parent->parent, - *uncle = x->uncle(); - if (parent->color != BLACK) - { - if (uncle != NULL && uncle->color == RED) - { - // uncle red, perform recoloring and recurse - parent->color = BLACK; - uncle->color = BLACK; - grandparent->color = RED; - fixRedRed(grandparent); - } - else - { - // Else perform LR, LL, RL, RR - if (parent->isOnLeft()) - { - if (x->isOnLeft()) - { - // for left right - swapColors(parent, grandparent); - } - else - { - rotateLeft(parent); - swapColors(x, grandparent); - } - // for left left and left right - rotateRight(grandparent); - } - else - { - if (x->isOnLeft()) - { - // for right left - rotateRight(parent); - swapColors(x, grandparent); - } - else - swapColors(parent, grandparent); - // for right right and right left - rotateLeft(grandparent); - } - } - } - } - - // find node that do not have a left child - // in the subtree of the given node - node *successor(node *x) - { - node *temp = x; - - while (temp->left != NULL) - temp = temp->left; - return temp; - } - - // find node that replaces a deleted node in BST - node *replace_node(node *x) - { - // when node have 2 children - if (x->left != NULL && x->right != NULL) - return successor(x->right); - // when leaf - if (x->left == NULL && x->right == NULL) - return NULL; - // when single child - if (x->left != NULL) - return x->left; - else - return x->right; - } - - // deletes the given node - void deleteNode(node *v) - { - node *u = replace_node(v); - - //std::cout << "root key= " << _root->data.first << std::endl; - // True when u and v are both black - bool uvBlack = ((u == NULL || u->color == BLACK) && (v->color == BLACK)); - node *parent = v->parent; - if (u == NULL) - { - // u is NULL therefore v is leaf - if (v == _root) - _root = NULL;// v is root, making root null - else - { - if (uvBlack) - { - // u and v both black - // v is leaf, fix double black at v - fixDoubleBlack(v); - } - else - { - // u or v is red - if (sibling(v) != NULL) - // sibling is not null, make it red" - sibling(v)->color = RED; - } - // delete v from the tree - if (isOnLeft(v)) - parent->left = NULL; - else - parent->right = NULL; - } - destruct_node(v); - return; - } - if (v->left == NULL || v->right == NULL) - { - // v has 1 child - if (v == _root) - { - // v is root, assign the value of u to v, and delete u - - //swapValues(&u, &v); - //deleteNode(u); - std::cout << "root left " << _root->left << " right " << _root->right << std::endl; - //if (u->parent && isOnLeft(u)) - // u->parent->left = 0; - //else if (u->parent && u->parent->right == u) - // u->parent->right = 0; - node *tmp4 = new_node(u->data.first, u->data.second); - tmp4->parent = 0; - tmp4->right = v->right; - tmp4->left = v->left->left; - _root = tmp4; - - if (v->right) - v->right->parent = _root; - if (v->left) - v->left->parent = _root; - std::cout << "root left " << _root->left << " right " << _root->right << std::endl; - - destruct_node(v); - //destruct_node(u); - - // deleteNode(u); - // node *tmp5 = new_node(v->data.first, v->data.second); - // tmp5->parent = u->parent; - // tmp5->right = u->right; - // tmp5->left = u->left; - // if (isOnLeft(u)) - // u->parent->left = tmp5; - // else if (u->parent) - // u->parent->right = tmp5; - - // destruct_node(u); - // destruct_node(v); - //deleteNode(tmp5); - - - // u->parent->left = 0; - // u->parent = 0; - // u = _root; - - // v->data = u->data; - // v->left = v->right = NULL; - // destruct_node(u); - - //node *tmp = new_node((u)->data.first, (u)->data.second); - - - - - - //swapValues(&u, &v); - - // if (isOnLeft(u)) - // u->parent->left = 0; - // else - // u->parent->right = 0; - - // std::cout << "root key: " << _root->data.first << " | root value: " << _root->data.second << std::endl; - // std::cout << "key: " << u->data.first << " | value: " << u->data.second << std::endl; - // // node *tmp = new_node((u)->data.first, (u)->data.second); - // // tmp->right = v->right; - // // tmp->left = v->left; - // u->parent = 0; - // //u->right = v->right; - // u->left = v->left; - - // //if (u->right) - // // u->right->parent = u; - // //if (u->right) - // // u->left->parent = u; - - // _root = u; - // _root->color = BLACK; - // destruct_node(v); - - // std::cout << "root key: " << _root->data.first << " | root value: " << _root->data.second << std::endl; - // std::cout << "here2\n"; - // std::cout << "here2\n"; - // std::cout << "here2\n"; - // std::cout << "here2\n"; - //_root = u; - //u->right = v->right; - //u->left = v->left; - //v->left = v->right = NULL; - - //destruct_node(u); - } - else - { - // Detach v from tree and move u up - if (isOnLeft(v)) - parent->left = u; - else - parent->right = u; - destruct_node(v); - u->parent = parent; - if (uvBlack) - fixDoubleBlack(u);// u and v both black, fix double black at u - else - u->color = BLACK;// u or v red, color u black - } - return; - } - // v has 2 children, swap values with successor and recurse - //swapValues(&u, &v); - //deleteNode(u); - - std::cout << "here\n"; - std::cout << "here\n"; - std::cout << "here\n"; - - node *tmp5 = new_node(u->data.first, u->data.second); - tmp5->parent = v->parent; - tmp5->right = v->right; - tmp5->left = v->left; - _root = tmp5; - - if (v->right) - v->right->parent = tmp5; - if (v->left) - v->left->parent = tmp5; - - node *tmp6 = new_node(v->data.first, v->data.second); - tmp6->parent = u->parent; - tmp6->right = u->right; - tmp6->left = u->left; - //_root = tmp4; - - if (u->right) - u->right->parent = tmp6; - if (u->left) - u->left->parent = tmp6; - deleteNode(tmp6); - - - - - - - - - - - - - - // node *tmp8 = new_node(u->data.first, u->data.second); - // tmp8->parent = v->parent; - // tmp8->right = v->right; - // tmp8->left = v->left; - // //_root = tmp8; - - // if (v->right) - // v->right->parent = tmp8; - // if (v->left) - // v->left->parent = tmp8; - - - - // deleteNode(u); - - // std::cout << "here\n"; - // std::cout << "here\n"; - // std::cout << "here\n"; - // std::cout << "here\n"; - // std::cout << "here\n"; - // std::cout << "here\n"; - // std::cout << "here\n"; - - // node *tmp2 = new_node(u->data.first, u->data.second); - // tmp2->parent = v->parent; - // tmp2->right = v->right; - // tmp2->left = v->left; - // if (isOnLeft(v)) - // v->parent->left = tmp2; - // else if (v->parent) - // v->parent->right = tmp2; - // else - // _root = tmp2; - - // node *tmp3 = new_node(v->data.first, v->data.second); - // tmp3->parent = u->parent; - // tmp3->right = u->right; - // tmp3->left = u->left; - // if (isOnLeft(u)) - // u->parent->left = tmp3; - // else if (u->parent) - // u->parent->right = tmp3; - - // destruct_node(u); - // destruct_node(v); - // deleteNode(tmp3); - - - //deleteNode(u); - } - - //template - node *new_node(key_type key, mapped_type val) - { - node *ret; - - ret = _node_alloc.allocate(1); - _node_alloc.construct(ret, node(key, val)); - - //ret = _node_alloc::allocate(1); - //_node_alloc::construct(ret, node(key, val)); - return (ret); - } - - void destruct_node(node *x) - { - // if (isOnLeft(x)) - // x->parent->left = _end; - // else if (x->parent) - // x->parent->right = _end; - _node_alloc.destroy(x); - _node_alloc.deallocate(x, 1); - } - -}; //end of map class - -//---------------------------------- -//----------COMPARE CLASS----------- -//---------------------------------- - -template -class map::value_compare //man map::value_compare -{ // in C++98, it is required to inherit binary_function - friend class map; - protected: - Compare comp; - value_compare(Compare c) : comp(c) {} // constructed with map's comparison object - public: - typedef bool result_type; - typedef value_type first_argument_type; - typedef value_type second_argument_type; - bool operator() (const value_type& x, const value_type& y) const - { - return comp(x.first, y.first); - } -}; - -} \ No newline at end of file diff --git a/containers/set.hpp b/containers/set.hpp deleted file mode 100644 index 0073367..0000000 --- a/containers/set.hpp +++ /dev/null @@ -1,776 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* set.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/11/26 15:23:32 by apommier #+# #+# */ -/* Updated: 2022/11/26 17:32:44 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#pragma once - -#include "./iterators/bidirectionnal_iterator.hpp" -#include "./iterators/pair.hpp" -#include "./iterators/make_pair.hpp" - - -#define RED 1 -#define BLACK 0 -#define _end 0 - -//typedef typename Alloc::template rebind >::other - -namespace ft -{ - -template< - class Key, - class Compare = std::less, - class Allocator = std::allocator > - -class set -{ - public : - - //----------------------------- - //---------MEMBER TYPE--------- - //----------------------------- - struct node; - - typedef Key key_type; - typedef Key value_type; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - typedef Compare key_compare; - typedef Allocator allocator_type; - typedef typename Allocator::template rebind::other node_allocator_type; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef typename Allocator::pointer pointer; - typedef typename Allocator::const_pointer const_pointer; - typedef ft::bidirectionnal_iterator iterator; - typedef ft::bidirectionnal_iterator const_iterator; - typedef std::reverse_iterator reverse_iterator; - typedef std::reverse_iterator const_reverse_iterator; - - class value_compare; - - protected : - - key_compare _comp; - allocator_type _alloc; - node_allocator_type _node_alloc; - - node *_root; - size_type _size; - - public : - - - struct node{ - value_type data; - node *parent; - node *right; - node *left; - bool color; - - //node() : parent(0), right(set::_end), left(set::_end), color(0) {} - node(key_type const &key) - : data(key), parent(0), right(_end), left(_end), color(0) - {} - //std::cout << "end in construct= " << _end << std::endl; - }; - //----------------------------- - //-----PRIVATE MEMBER TYPE----- - //----------------------------- - - - public : - - //--------------------------------------- - //---------COPLIEN FORM FUNCTION--------- - //--------------------------------------- - - explicit set( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) - { - //_end = _node_alloc.allocate(1); - //_node_alloc.construct(_end, node()); - _size = 0; - } - - template< class InputIt > - set( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) - { - _size = 0; - //_end = _node_alloc.allocate(1); - //_node_alloc.construct(_end, node()); - this->insert(first, last); - } - - set( const set& x) - { - *this = x; - } - - ~set() - { - - } - - set& operator=(const set& x) - { - _comp = x._comp; - _alloc = x._alloc; - _node_alloc = x._node_alloc; - _root = x._root; - //_end = x._end; - _size = x._size; - return (*this); - } - - //---------------------------------- - //---------MEMBER FUNCTION---------- - //---------------------------------- - - //------------------------- - //--------Iterators-------- - //------------------------- - iterator begin() - { - return iterator(_root, _root); - } - - const_iterator begin() const - { - return const_iterator(_root, _root); - } - - iterator end() - { - return iterator(_root, _end); - } - - const_iterator end() const - { - return const_iterator(_root, _end); - } - - reverse_iterator rbegin() - { - return reverse_iterator(this->end()); - } - - const_reverse_iterator rbegin() const - { - return const_reverse_iterator(this->end()); - } - - reverse_iterator rend() - { - return reverse_iterator(this->begin()); - } - - const_reverse_iterator rend() const - { - return const_reverse_iterator(this->begin()); - } - - - //------------------------ - //--------Capacity-------- - //------------------------ - - bool empty() const - { - if (!_size) - return (1); - return(0); - } - - size_type size() const - { - return (_size); - } - - size_type max_size() const - { - return (_alloc.max_size()); - } - - //------------------------- - //--------Modifiers-------- - //------------------------- - ft::pair insert (const value_type& val) - { - // if (this->insert_node(val.first, val.second)) - // _size++; - node *pt = new_node(val); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - } - - iterator insert (iterator position, const value_type& val) - { - (void)position; - // if (this->insert_node(val.first, val.second)) - // _size++; - node *pt = new_node(val); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - } - - template - void insert (InputIterator first, InputIterator last) - { - while (first != last) - { - // std::cout << "i === " << i++ << std::endl; - // if (this->insert_node(_root, new_node((*first).first, (*first).second))) - // _size++; - // first++; - node *pt = new_node(*first); - _root = insert_node(_root, pt); - fixViolation(_root, pt); - first++; - } - } - - void erase (iterator position) - { - delete_node(position.base()); - } - - size_type erase (const key_type& k) - { - delete_node(find(k).base()); - return(1); - } - - void erase (iterator first, iterator last) - { - while (first != last) - { - delete_node(first.base()); - first++; - } - } - - void swap (set& x) - { - set tmp; - - tmp->_comp = _comp; - tmp->_alloc = _alloc; - tmp->_node_alloc = _node_alloc; - tmp->_root = _root; - //tmp->_end = _end; - tmp->_size = _size; - - _comp = x->_comp; - _alloc = x->_alloc; - _node_alloc = x->_node_alloc; - _root = x->_root; - //_end = x->_end; - _size = x->_size; - - x->_comp = tmp-> _comp; - x->_alloc = tmp->_alloc; - x->_node_alloc = tmp->_node_alloc; - x->_root = tmp->_root; - //x->_end = tmp->_end; - x->_size = tmp->_size; - } - - void clear() - { - - } - - //------------------------- - //--------Observers-------- - //------------------------- - - key_compare key_comp() const - { - return (_comp); - } - - value_compare value_comp() const - { - return (value_compare(_comp)); - } - - - //------------------------- - //-------Operations-------- - //------------------------- - - iterator find (const key_type& k) - { - node *x = _root; - int i = 0; - while (x != _end && x->data.first != k) - { - std::cout << "i === " << i << std::endl; - if (k > x->data.first) - x = x->left; - else - x = x->right; - i++; - } - return (iterator(_root, _end, x)); - } - - const_iterator find (const key_type& k) const - { - node *x = _root; - - while (x != _end && x->data.first != k) - { - if (k > x->data.first) - x = x->left; - else - x = x->right; - } - return (iterator(_root, _end, x)); - } - - size_type count (const key_type& k) const - { - if (find(k)->m == _end) - return (0); - return (1); - } - - iterator lower_bound (const key_type& k) - { - iterator it = begin(), ite = end(); - - while (it != ite && !(_comp((*it), k))) - it++; - return (it); - } - - const_iterator lower_bound (const key_type& k) const - { - const_iterator it = begin(), ite = end(); - - while (it != ite && !(_comp((*it), k))) - it++; - return (it); - } - - iterator upper_bound (const key_type& k) - { - iterator it = begin(), ite = end(); - - while (it != ite && !(_comp((*it), k))) - it++; - return (it); - } - - const_iterator upper_bound (const key_type& k) const - { - const_iterator it = begin(), ite = end(); - - while (it != ite && _comp((*it), k)) - it++; - return (it); - } - - ft::pair equal_range (const key_type& k) const - { - return (ft::make_pair(lower_bound(k), upper_bound(k))); - } - - ft::pair equal_range (const key_type& k) - { - return (ft::make_pair(lower_bound(k), upper_bound(k))); - } - - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ******************************TREE FUNCTIONS****************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - /* ************************************************************************** */ - - private : - - void rotateLeft(node *&root, node *&pt) - { - node *pt_right = pt->right; - pt->right = pt_right->left; - if (pt->right != NULL) - pt->right->parent = pt; - pt_right->parent = pt->parent; - if (pt->parent == NULL) - root = pt_right; - else if (pt == pt->parent->left) - pt->parent->left = pt_right; - else - pt->parent->right = pt_right; - pt_right->left = pt; - pt->parent = pt_right; - } - - - void rotateRight(node *&root, node *&pt) - { - - node *pt_left = pt->left; - pt->left = pt_left->right; - if (pt->left != NULL) - pt->left->parent = pt; - pt_left->parent = pt->parent; - if (pt->parent == NULL) - root = pt_left; - else if (pt == pt->parent->left) - pt->parent->left = pt_left; - else - pt->parent->right = pt_left; - pt_left->right = pt; - pt->parent = pt_left; - } - - node* insert_node(node* root, node *pt) - { - /* If the tree is empty, return a new node */ - if (root == NULL) - return pt; - /* Otherwise, recur down the tree */ - if (pt->data < root->data) - { - root->left = insert_node(root->left, pt); - root->left->parent = root; - } - else if (pt->data > root->data) - { - root->right = insert_node(root->right, pt); - root->right->parent = root; - } - /* return the (unchanged) node pointer */ - return root; - } - - void fixViolation(node *&root, node *&pt) - { - node *parent_pt = NULL; - node *grand_parent_pt = NULL; - while ((pt != root) && (pt->color != BLACK) && (pt->parent->color == RED)) - { - parent_pt = pt->parent; - grand_parent_pt = pt->parent->parent; - /* Case : A Parent of pt is left child of Grand-parent of pt */ - if (parent_pt == grand_parent_pt->left) - { - node *uncle_pt = grand_parent_pt->right; - /* Case : 1 The uncle of pt is also red Only Recoloring required */ - if (uncle_pt != NULL && uncle_pt->color == RED) - { - grand_parent_pt->color = RED; - parent_pt->color = BLACK; - uncle_pt->color = BLACK; - pt = grand_parent_pt; - } - else - { - /* Case : 2 pt is right child of its parent Left-rotation required */ - if (pt == parent_pt->right) - { - rotateLeft(root, parent_pt); - pt = parent_pt; - parent_pt = pt->parent; - } - /* Case : 3 pt is left child of its parent Right-rotation required */ - rotateRight(root, grand_parent_pt); - swapColors(parent_pt, grand_parent_pt); - pt = parent_pt; - } - } - /* Case : B Parent of pt is right child of Grand-parent of pt */ - else - { - node *uncle_pt = grand_parent_pt->left; - /* Case : 1 The uncle of pt is also red Only Recoloring required */ - if ((uncle_pt != NULL) && (uncle_pt->color == RED)) - { - grand_parent_pt->color = RED; - parent_pt->color = BLACK; - uncle_pt->color = BLACK; - pt = grand_parent_pt; - } - else - { - /* Case : 2 | pt is left child of its parent | Right-rotation required */ - if (pt == parent_pt->left) - { - rotateRight(root, parent_pt); - pt = parent_pt; - parent_pt = pt->parent; - } - /* Case : 3 pt is right child of its parent Left-rotation required */ - rotateLeft(root, grand_parent_pt); - swapColors(parent_pt, grand_parent_pt); - pt = parent_pt; - } - } - } - root->color = BLACK; - } - - /* ************************************************************************** */ - /* **********************************DELETE********************************** */ - /* ************************************************************************** */ - - - node *uncle(node *x) - { - if (x->parent == NULL or x->parent->parent == NULL) - return NULL; - if (x->parent->isOnLeft()) - return x->parent->parent->right; - else - return x->parent->parent->left; - } - - bool isOnLeft(node *x) { return this == x->parent->left; } - - // returns pointer to sibling - node *sibling(node *x) - { - // sibling null if no parent - if (x->parent == NULL) - return NULL; - if (isOnLeft()) - return x->parent->right; - return x->parent->left; - } - - // moves node down and moves given node in its place - void moveDown(node *nParent, node *x) - { - if (x->parent != NULL) - { - if (isOnLeft()) - x->parent->left = nParent; - else - x->parent->right = nParent; - } - nParent->parent = x->parent; - x->parent = nParent; - } - - bool hasRedChild(node *x) - { - return (x->left != NULL and x->left->color == RED) or (x->right != NULL and x->right->color == RED); - } - - void swapColors(node *x1, node *x2) - { - bool temp; - temp = x1->color; - x1->color = x2->color; - x2->color = temp; - } - - void swapValues(node *u, node *v) - { - int temp; - temp = u->val; - u->val = v->val; - v->val = temp; - } - - void fixRedRed(node *x) - { - // if x is root color it black and return - if (x == _root) - { - x->color = BLACK; - return; - } - // initialize parent, grandparent, uncle - node *parent = x->parent, *grandparent = parent->parent, - *uncle = x->uncle(); - if (parent->color != BLACK) - { - if (uncle != NULL && uncle->color == RED) - { - // uncle red, perform recoloring and recurse - parent->color = BLACK; - uncle->color = BLACK; - grandparent->color = RED; - fixRedRed(grandparent); - } - else - { - // Else perform LR, LL, RL, RR - if (parent->isOnLeft()) - { - if (x->isOnLeft()) - { - // for left right - swapColors(parent, grandparent); - } - else - { - leftRotate(parent); - swapColors(x, grandparent); - } - // for left left and left right - rightRotate(grandparent); - } - else - { - if (x->isOnLeft()) - { - // for right left - rightRotate(parent); - swapColors(x, grandparent); - } - else - swapColors(parent, grandparent); - // for right right and right left - leftRotate(grandparent); - } - } - } - } - - // find node that do not have a left child - // in the subtree of the given node - node *successor(node *x) - { - node *temp = x; - - while (temp->left != NULL) - temp = temp->left; - return temp; - } - - // find node that replaces a deleted node in BST - node *replace_node(node *x) - { - // when node have 2 children - if (x->left != NULL and x->right != NULL) - return successor(x->right); - // when leaf - if (x->left == NULL and x->right == NULL) - return NULL; - // when single child - if (x->left != NULL) - return x->left; - else - return x->right; - } - - // deletes the given node - void deleteNode(node *v) - { - node *u = replace_node(v); - - // True when u and v are both black - bool uvBlack = ((u == NULL or u->color == BLACK) and (v->color == BLACK)); - node *parent = v->parent; - if (u == NULL) - { - // u is NULL therefore v is leaf - if (v == _root) - _root = NULL;// v is root, making root null - else - { - if (uvBlack) - { - // u and v both black - // v is leaf, fix double black at v - fixDoubleBlack(v); - } - else - { - // u or v is red - if (v->sibling() != NULL) - // sibling is not null, make it red" - v->sibling()->color = RED; - } - // delete v from the tree - if (v->isOnLeft()) - parent->left = NULL; - else - parent->right = NULL; - } - delete v; - return; - } - if (v->left == NULL or v->right == NULL) - { - // v has 1 child - if (v == _root) - { - // v is root, assign the value of u to v, and delete u - v->val = u->val; - v->left = v->right = NULL; - delete u; - } - else - { - // Detach v from tree and move u up - if (v->isOnLeft()) - parent->left = u; - else - parent->right = u; - delete v; - u->parent = parent; - if (uvBlack) - fixDoubleBlack(u);// u and v both black, fix double black at u - else - u->color = BLACK;// u or v red, color u black - } - return; - } - // v has 2 children, swap values with successor and recurse - swapValues(u, v); - deleteNode(u); - } - - //template - node *new_node(key_type key) - { - node *ret; - - ret = _node_alloc.allocate(1); - _node_alloc.construct(ret, node(key)); - - //ret = _node_alloc::allocate(1); - //_node_alloc::construct(ret, node(key)); - return (ret); - } - -}; //end of set class - -//---------------------------------- -//----------COMPARE CLASS----------- -//---------------------------------- - -template -class set::value_compare //man set::value_compare -{ // in C++98, it is required to inherit binary_function - friend class set; - protected: - Compare comp; - value_compare(Compare c) : comp(c) {} // constructed with set's comparison object - public: - typedef bool result_type; - typedef value_type first_argument_type; - typedef value_type second_argument_type; - bool operator() (const value_type& x, const value_type& y) const - { - return comp(x, y); - } -}; - -} \ No newline at end of file diff --git a/containers/stack.hpp b/containers/stack.hpp index fd1c7cc..62b9f95 100644 --- a/containers/stack.hpp +++ b/containers/stack.hpp @@ -6,19 +6,19 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 19:46:30 by apommier #+# #+# */ -/* Updated: 2022/11/28 15:56:04 by apommier ### ########.fr */ +/* Updated: 2022/11/29 13:15:44 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef STACK_HPP # define STACK_HPP -# include +# include "vector.hpp" namespace ft { -template > +template > class stack { public: @@ -30,7 +30,7 @@ class stack typedef T value_type; typedef Container container_type; - private: + protected: container_type c; public: @@ -39,11 +39,14 @@ class stack //---------COPLIEN FORM FUNCTION--------- //--------------------------------------- explicit stack (const container_type& ctnr = container_type()) : c(ctnr){}//default constructor + stack(const stack &other )//copy constructor { *this = other; } + ~stack(void) {}//destructor + stack &operator=( const stack &other )//assigment operator { if (this != &other) diff --git a/containers/vector.hpp b/containers/vector.hpp index 6802470..bbdfb1b 100644 --- a/containers/vector.hpp +++ b/containers/vector.hpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 19:46:32 by apommier #+# #+# */ -/* Updated: 2022/11/29 02:18:37 by apommier ### ########.fr */ +/* Updated: 2022/11/29 14:05:18 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -214,11 +214,6 @@ class vector } else if (n > _size) { - // while (n > _capacity) - // this->reserve(_capacity); - //if (n < _capacity * 2 && _size == _capacity) - - // else if (n > _capacity) { if (n == _capacity + 1 || n > _capacity * 2) @@ -226,10 +221,6 @@ class vector else this->reserve(_capacity * 2); } - // else if (n < _capacity * 2) - // this->reserve(_size * 2); - // else - //this->reserve(n); while (n > _size) { _alloc.construct(_tab + _size, val); @@ -269,19 +260,6 @@ class vector _tab = tmp; _capacity = n; } - // if (n > this->max_size()) - // throw (std::length_error("vector::reserve")); - // else if (n > _capacity) - // { - // value_type *prev_ptr = _tab; - // std::size_t prev_size = _size; - // std::size_t prev_capacity = _capacity; - // _tab = _alloc.allocate(n); - // _capacity = n; - // for(std::size_t i = 0; i < prev_size; i++) - // _alloc.construct(_tab + i, *(prev_ptr + i)); - // _alloc.deallocate(prev_ptr, prev_capacity); - // } } //------------------------------ @@ -455,32 +433,6 @@ class vector temp++; } return (first); - // pointer p_first = &(*first); - // for (; &(*first) != &(*last); first++) - // _alloc.destroy(&(*first)); - // for (int i = 0; i < this->end() - &(*last); i++) - // { - // _alloc.construct(p_first + i, *(&(*last) + i)); - // _alloc.destroy(&(*last) + i); - // } - // //_end -= (&(*last) - p_first); - // return (iterator(p_first)); - - - // difference_type lenght = last - first; - - // int i; - // //if (first == this->begin() && last == this->end()) - // // this->clear(); - // for (i = 0; lenght - i - 1; i++) - // _alloc.destroy(&(*(first + i))); - // for (i = 0; _size - i - 1; i++) - // { - // _alloc.construct(&(*(first + i)), *(&(*last) + i)); - // _alloc.destroy(*(&(*last) + i)); - // } - // _size -= lenght; - // return (first); } void swap (vector& x) @@ -555,10 +507,6 @@ class vector { if (lhs._size != rhs._size) return (false); - // int i = 0; - // while (lhs._size - i - 1 && lhs._tab[i] == rhs._tab[i]) - // i++; - // return (lhs._tab[i] == rhs._tab[i]); return (ft::equal(lhs.begin(), lhs.end(), rhs.begin())); } diff --git a/ft_containers.hpp b/ft_containers.hpp index 2c27c44..d3caf22 100644 --- a/ft_containers.hpp +++ b/ft_containers.hpp @@ -6,22 +6,25 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 19:46:33 by apommier #+# #+# */ -/* Updated: 2022/11/16 19:07:06 by apommier ### ########.fr */ +/* Updated: 2022/11/29 13:22:18 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FT_CONTAINERS_HPP -# define FT_CONTAINERS_HPP +#pragma once # include -//namespace ft { # include "./containers/map.hpp" -# include "./containers/set.hpp" # include "./containers/stack.hpp" # include "./containers/vector.hpp" -//} -# include +# include +# include +# include -#endif \ No newline at end of file +void stack_tester(); +void real_stack_tester(); +void vector_tester(); +void real_vector_tester(); +void map_tester(); +void real_map_tester(); \ No newline at end of file diff --git a/main.cpp b/main.cpp index f84c68c..ab4d4f0 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 19:46:35 by apommier #+# #+# */ -/* Updated: 2022/10/19 18:35:21 by apommier ### ########.fr */ +/* Updated: 2022/11/29 12:38:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ #include namespace ft = std; #else - #include - #include - #include + #include "./containers/map.hpp" + #include "./containers/stack.hpp" + #include "./containers/vector.hpp" #endif #include diff --git a/tests/main.cpp b/tests/main.cpp index e8f089b..4514785 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/19 14:04:37 by apommier #+# #+# */ -/* Updated: 2022/11/16 22:18:48 by apommier ### ########.fr */ +/* Updated: 2022/11/29 13:25:14 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,54 +20,100 @@ long get_time(void) gettimeofday(&time, NULL); return (time.tv_sec * 1000 + time.tv_usec / 1000); } +// void print_vector(ft::vector x) +// { + +// std::cout << "-----print vector----\n"; +// for (size_t i = 0; i < x.size(); i++) +// std::cout << x[i] << std::endl; +// } -void stack_tester() -{ - ft::stack first; - std::cout << "------empty------\n"; - std::cout << first.empty() << std::endl; - std::cout << "------size------\n"; - std::cout << first.size() << std::endl; -} +// void stack_tester() +// { +// ft::stack first; +// std::cout << "------empty------\n"; +// std::cout << first.empty() << std::endl; +// std::cout << "------size------\n"; +// std::cout << first.size() << std::endl; +// } -void print_vector(ft::vector x) -{ - std::cout << "-----print vector----\n"; - for (size_t i = 0; i < x.size(); i++) - std::cout << x[i] << std::endl; -} - -void vector_tester() -{ - ft::vector first(5, 3); - ft::vector second; - ft::vector::iterator it = first.begin(); - std::cout << "------empty------\n"; - std::cout << first.empty() << std::endl; - std::cout << second.empty() << std::endl; - std::cout << "------size------\n"; - std::cout << first.size() << std::endl; - std::cout << "------pushback then size------\n"; - first.push_back(5); - std::cout << first.size() << std::endl; - std::cout << "------for------\n"; - print_vector(first); - std::cout << "-------0-----\n"; - std::cout << first.at(5) << std::endl; +// void vector_tester() +// { +// ft::vector first(5, 3); +// ft::vector second; +// ft::vector::iterator it = first.begin(); +// std::cout << "------empty------\n"; +// std::cout << first.empty() << std::endl; +// std::cout << second.empty() << std::endl; +// std::cout << "------size------\n"; +// std::cout << first.size() << std::endl; +// std::cout << "------pushback then size------\n"; +// first.push_back(5); +// std::cout << first.size() << std::endl; +// std::cout << "------for------\n"; +// print_vector(first); +// std::cout << "-------0-----\n"; +// std::cout << first.at(5) << std::endl; -} +// } + int main() { - std::cout << "------TESTER START-------\n"; - std::cout << "------STACK-------\n"; + long real_time; + long my_time; + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* *****************************FT_CONTAINERS TESTER************************* */\n"; + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* ************************************************************************** */\n"; + + std::cout << " ------------------------------------------------------------\n"; + std::cout << " ---------------------------STACK----------------------------\n"; + std::cout << " ------------------------------------------------------------\n"; + my_time = get_time(); stack_tester(); - std::cout << "------VECTOR-------\n"; + my_time = get_time() - my_time; + std::cout << " --------------------------REAL STACK------------------------\n"; + real_time = get_time(); + real_stack_tester(); + real_time = get_time() - real_time; + std::cout << " --------------------------TIME DIFF-------------------------\n"; + std::cout << " homemade ft containers = " << my_time << "ms\n"; + std::cout << " real std containers = " << real_time << "ms\n"; + + std::cout << " ------------------------------------------------------------\n"; + std::cout << " ---------------------------VECTOR---------------------------\n"; + std::cout << " ------------------------------------------------------------\n"; + my_time = get_time(); vector_tester(); - // std::cout << "------MAP-------\n"; - // map_tester(); - // std::cout << "------SET-------\n"; - // set_tester(); - std::cout << "------End-------\n"; + my_time = get_time() - my_time; + std::cout << " --------------------------REAL VECTOR------------------------\n"; + real_time = get_time(); + real_vector_tester(); + real_time = get_time() - real_time; + std::cout << " --------------------------TIME DIFF-------------------------\n"; + std::cout << " homemade ft containers = " << my_time << "ms\n"; + std::cout << " real std containers = " << real_time << "ms\n"; + + std::cout << " ------------------------------------------------------------\n"; + std::cout << " -----------------------------MAP----------------------------\n"; + std::cout << " ------------------------------------------------------------\n"; + my_time = get_time(); + map_tester(); + my_time = get_time() - my_time; + std::cout << " ---------------------------REAL MAP-------------------------\n"; + real_time = get_time(); + real_map_tester(); + real_time = get_time() - real_time; + std::cout << " --------------------------TIME DIFF-------------------------\n"; + std::cout << " homemade ft containers = " << my_time << "ms\n"; + std::cout << " real std containers = " << real_time << "ms\n"; + + std::cout << "/* ************************************************************************** */\n"; + std::cout << "/* ******************************END OF TESTER******************************* */\n"; + std::cout << "/* ************************************************************************** */\n"; return (0); } \ No newline at end of file diff --git a/tests/map_tester.cpp b/tests/map_tester.cpp new file mode 100644 index 0000000..537480b --- /dev/null +++ b/tests/map_tester.cpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_tester.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/11/29 12:55:17 by apommier #+# #+# */ +/* Updated: 2022/11/29 13:23:51 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_containers.hpp" + +void map_tester() +{ + +} + +void real_map_tester() +{ + +} \ No newline at end of file diff --git a/tests/stack_tester.cpp b/tests/stack_tester.cpp new file mode 100644 index 0000000..52d8646 --- /dev/null +++ b/tests/stack_tester.cpp @@ -0,0 +1,154 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack_tester.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/11/29 12:55:21 by apommier #+# #+# */ +/* Updated: 2022/11/29 13:50:26 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_containers.hpp" + + +template +void printSize(T_STACK &cntr) +{ + std::cout << "empty: " << cntr.empty() << std::endl; + std::cout << "size: " << cntr.size() << std::endl; + std::cout << std::endl << "Content :" << std::endl; + while (cntr.size() != 0) { + std::cout << cntr.top() << std::endl; + cntr.pop(); + } +} + +template +void cmp(const T_STACK &lhs, const T_STACK &rhs) +{ + std::cout << "== : " << (lhs == rhs) << " | != : " << (lhs != rhs) << std::endl; + std::cout << "< : " << (lhs < rhs) << " | <= : " << (lhs <= rhs) << std::endl; + std::cout << "> : " << (lhs > rhs) << " | >= : " << (lhs >= rhs) << std::endl; +} + +void stack_tester() +{ + ft::vector ctnr; + + ctnr.push_back(21); + ctnr.push_back(42); + ctnr.push_back(1337); + ctnr.push_back(19); + ctnr.push_back(0); + ctnr.push_back(183792); + + std::cout << "----container copy constructor----\n"; + ft::stack stack_ctnr(ctnr); + + stack_ctnr.push(1); + stack_ctnr.push(2); + stack_ctnr.push(3); + stack_ctnr.push(4); + stack_ctnr.push(5); + stack_ctnr.push(6); + + printSize(stack_ctnr); + + std::cout << "----default constructor----\n"; + ft::stack stack_ctnr2; + + stack_ctnr2.push(11); + stack_ctnr2.push(21); + stack_ctnr2.push(31); + stack_ctnr2.push(41); + stack_ctnr2.push(51); + stack_ctnr2.push(61); + + printSize(stack_ctnr2); + + std::cout << "----stack copy constructor----\n"; + + stack_ctnr2.push(11); + stack_ctnr2.push(21); + stack_ctnr2.push(31); + stack_ctnr2.push(41); + stack_ctnr2.push(51); + stack_ctnr2.push(61); + + ft::stack stack_ctnr3(stack_ctnr2); + printSize(stack_ctnr3); + + std::cout << "----relationnal ope----\n"; + stack_ctnr3 = stack_ctnr2; + cmp(stack_ctnr2, stack_ctnr2); +} + + + + + + + + + + + + + + + + + +void real_stack_tester() +{ + std::deque ctnr2; + + ctnr2.push_back(21); + ctnr2.push_back(42); + ctnr2.push_back(1337); + ctnr2.push_back(19); + ctnr2.push_back(0); + ctnr2.push_back(183792); + + std::cout << "----container copy constructor----\n"; + std::stack stack_ctnr(ctnr2); + + stack_ctnr.push(1); + stack_ctnr.push(2); + stack_ctnr.push(3); + stack_ctnr.push(4); + stack_ctnr.push(5); + stack_ctnr.push(6); + + printSize(stack_ctnr); + + std::cout << "----default constructor----\n"; + std::stack stack_ctnr2; + + stack_ctnr2.push(11); + stack_ctnr2.push(21); + stack_ctnr2.push(31); + stack_ctnr2.push(41); + stack_ctnr2.push(51); + stack_ctnr2.push(61); + + printSize(stack_ctnr2); + + std::cout << "----stack copy constructor----\n"; + + stack_ctnr2.push(11); + stack_ctnr2.push(21); + stack_ctnr2.push(31); + stack_ctnr2.push(41); + stack_ctnr2.push(51); + stack_ctnr2.push(61); + + std::stack stack_ctnr3(stack_ctnr2); + printSize(stack_ctnr3); + + std::cout << "----relationnal ope----\n"; + stack_ctnr3 = stack_ctnr2; + cmp(stack_ctnr2, stack_ctnr2); +} \ No newline at end of file diff --git a/tests/vector_tester.cpp b/tests/vector_tester.cpp new file mode 100644 index 0000000..23aa9ad --- /dev/null +++ b/tests/vector_tester.cpp @@ -0,0 +1,233 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector_tester.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/11/29 12:55:23 by apommier #+# #+# */ +/* Updated: 2022/11/29 14:45:23 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_containers.hpp" + +template +void cmp(const T_STACK &lhs, const T_STACK &rhs) +{ + std::cout << "== : " << (lhs == rhs) << " | != : " << (lhs != rhs) << std::endl; + std::cout << "< : " << (lhs < rhs) << " | <= : " << (lhs <= rhs) << std::endl; + std::cout << "> : " << (lhs > rhs) << " | >= : " << (lhs >= rhs) << std::endl; +} + +void printSize(ft::vector const &vct) +{ + std::cout << "size: " << vct.size() << std::endl; + std::cout << "capacity: " << vct.capacity() << std::endl; + std::cout << "max_size: " << vct.max_size() << std::endl; + ft::vector::const_iterator it = vct.begin(); + ft::vector::const_iterator ite = vct.end(); + std::cout << std::endl << "Content is:" << std::endl; + for (; it != ite; ++it) + std::cout << "- " << *it << std::endl; +} + +void printSize_real(std::vector const &vct) +{ + std::cout << "size: " << vct.size() << std::endl; + std::cout << "capacity: " << vct.capacity() << std::endl; + std::cout << "max_size: " << vct.max_size() << std::endl; + std::vector::const_iterator it = vct.begin(); + std::vector::const_iterator ite = vct.end(); + std::cout << std::endl << "Content is:" << std::endl; + for (; it != ite; ++it) + std::cout << "- " << *it << std::endl; +} + +void vector_tester() +{ + std::cout << "----default constructor----" << std::endl; + ft::vector ctnr; + printSize(ctnr); + + std::cout << "----assign val----" << std::endl; + ctnr.assign(10, 1); + printSize(ctnr); + + std::cout << "----range constructor----" << std::endl; + ft::vector ctnr2(ctnr.begin(), ctnr.end()); + printSize(ctnr2); + + std::cout << "----range assign----" << std::endl; + ft::vector ctnr3; + ctnr3.assign(ctnr.begin(), ctnr.end()); + printSize(ctnr3); +//////////////////////////////////// + std::cout << "----insert at position + assign constrcutor----" << std::endl; + ft::vector ctnr4(5, 5); + printSize(ctnr4); + + std::cout << "----insert----\n"; + ctnr4.insert(ctnr4.begin() + 3, 100); + printSize(ctnr4); + + std::cout << "----insert fill----\n"; + ctnr4.insert(ctnr4.begin() + 3, 5, 100); + printSize(ctnr4); + + std::cout << "----operator equal----\n"; + ft::vector ctnr5; + ctnr5 = ctnr4; + printSize(ctnr5); + + std::cout << "----insert range----\n"; + ctnr5.insert(ctnr5.begin() + 2, ctnr.begin()++, ctnr.end()--); + printSize(ctnr5); + + std::cout << "----erase----\n"; + ctnr5.erase(ctnr5.end() - 5); + printSize(ctnr5); + + std::cout << "----erase range----\n"; + ctnr5.erase(ctnr5.begin(), ctnr5.end() - 5); + printSize(ctnr5); + + std::cout << "----swap----\n"; + ctnr5.swap(ctnr); + std::cout << "----ctnr----\n"; + printSize(ctnr); + std::cout << "----ctnr5----\n"; + printSize(ctnr5); + + std::cout << "----clear----\n"; + ctnr.clear(); + printSize(ctnr); +/////////////// + std::cout << "----iterator----\n"; + ft::vector::iterator it = ctnr4.begin(); + ft::vector::iterator ite = ctnr4.end(); + + for (int i = 1; it != ite; ++i) + *it++ = i; + printSize(ctnr4); + + it = ctnr4.begin(); + ite = ctnr4.begin(); + + std::cout << *(++ite) << std::endl; + std::cout << *(ite++) << std::endl; + std::cout << *ite++ << std::endl; + std::cout << *++ite << std::endl; + + std::cout << *(++it) << std::endl; + std::cout << *(it++) << std::endl; + std::cout << *it++ << std::endl; + std::cout << *++it << std::endl; + + std::cout << *(--ite) << std::endl; + std::cout << *(ite--) << std::endl; + std::cout << *--ite << std::endl; + std::cout << *ite-- << std::endl; + + std::cout << *(--it) << std::endl; + std::cout << *(it--) << std::endl; + std::cout << *it-- << std::endl; + std::cout << *--it << std::endl; + + std::cout << "----relationnal ope----\n"; + cmp(ctnr, ctnr4); +} + +void real_vector_tester() +{ + std::cout << "----default constructor----" << std::endl; + std::vector ctnr; + printSize_real(ctnr); + + std::cout << "----assign val----" << std::endl; + ctnr.assign(10, 1); + printSize_real(ctnr); + + std::cout << "----range constructor----" << std::endl; + std::vector ctnr2(ctnr.begin(), ctnr.end()); + printSize_real(ctnr2); + + std::cout << "----range assign----" << std::endl; + std::vector ctnr3; + ctnr3.assign(ctnr.begin(), ctnr.end()); + printSize_real(ctnr3); +//////////// + std::cout << "----insert at position + assign constrcutor----" << std::endl; + std::vector ctnr4(5, 5); + printSize_real(ctnr4); + + std::cout << "----insert----\n"; + ctnr4.insert(ctnr4.begin() + 3, 100); + printSize_real(ctnr4); + + std::cout << "----insert fill----\n"; + ctnr4.insert(ctnr4.begin() + 3, 5, 100); + printSize_real(ctnr4); + + std::cout << "----operator equal----\n"; + std::vector ctnr5; + ctnr5 = ctnr4; + printSize_real(ctnr5); + + std::cout << "----insert range----\n"; + ctnr5.insert(ctnr5.begin() + 2, ctnr.begin()++, ctnr.end()--); + printSize_real(ctnr5); + + std::cout << "----erase----\n"; + ctnr5.erase((ctnr5.end()) - 5); + printSize_real(ctnr5); + + std::cout << "----erase range----\n"; + ctnr5.erase(ctnr5.begin(), ctnr5.end() - 5); + printSize_real(ctnr5); + + std::cout << "----swap----\n"; + ctnr5.swap(ctnr); + std::cout << "----ctnr----\n"; + printSize_real(ctnr); + std::cout << "----ctnr5----\n"; + printSize_real(ctnr5); + + std::cout << "----clear----\n"; + ctnr.clear(); + printSize_real(ctnr); + + std::cout << "----iterator----\n"; + std::vector::iterator it = ctnr4.begin(); + std::vector::iterator ite = ctnr4.end(); + + for (int i = 1; it != ite; ++i) + *it++ = i; + printSize_real(ctnr4); + + it = ctnr4.begin(); + ite = ctnr4.begin(); + + std::cout << *(++ite) << std::endl; + std::cout << *(ite++) << std::endl; + std::cout << *ite++ << std::endl; + std::cout << *++ite << std::endl; + + std::cout << *(++it) << std::endl; + std::cout << *(it++) << std::endl; + std::cout << *it++ << std::endl; + std::cout << *++it << std::endl; + + std::cout << *(--ite) << std::endl; + std::cout << *(ite--) << std::endl; + std::cout << *--ite << std::endl; + std::cout << *ite-- << std::endl; + + std::cout << *(--it) << std::endl; + std::cout << *(it--) << std::endl; + std::cout << *it-- << std::endl; + std::cout << *--it << std::endl; + + std::cout << "----relationnal ope----\n"; + cmp(ctnr, ctnr4); +} \ No newline at end of file