stack almost good
This commit is contained in:
93
Makefile
93
Makefile
@@ -31,6 +31,8 @@ RESET = "\e[0m"
|
|||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
NAME = containers
|
NAME = containers
|
||||||
|
NAME_FT = containers_ft
|
||||||
|
NAME_STL = containers_stl
|
||||||
|
|
||||||
CC = g++
|
CC = g++
|
||||||
EXT = cpp
|
EXT = cpp
|
||||||
@@ -39,6 +41,11 @@ CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
|
|||||||
CFLAGS += -std=c++98
|
CFLAGS += -std=c++98
|
||||||
CFLAGS += -g3
|
CFLAGS += -g3
|
||||||
|
|
||||||
|
CFLAGS_STL = -Wall -Wextra -Werror $(INCLUDES)
|
||||||
|
CFLAGS_STL += -D STL
|
||||||
|
CFLAGS_STL += -std=c++98
|
||||||
|
CFLAGS_STL += -g3
|
||||||
|
|
||||||
VPATH = $(D_SRCS)
|
VPATH = $(D_SRCS)
|
||||||
|
|
||||||
LIBS =
|
LIBS =
|
||||||
@@ -52,14 +59,20 @@ INCLUDES = -I$(D_HEADERS) \
|
|||||||
|
|
||||||
D_SRCS = ./tests
|
D_SRCS = ./tests
|
||||||
#SRCS = main42.cpp
|
#SRCS = main42.cpp
|
||||||
SRCS = main.cpp \
|
#SRCS = main_map_1.cpp
|
||||||
tests_definitions.cpp \
|
#SRCS = main_map_2.cpp
|
||||||
\
|
SRCS = main_stack_1.cpp
|
||||||
tests_vector.cpp \
|
#SRCS = \
|
||||||
tests_map.cpp
|
# main.cpp \
|
||||||
|
# tests_definitions.cpp \
|
||||||
|
# \
|
||||||
|
# tests_vector.cpp \
|
||||||
|
# tests_map.cpp \
|
||||||
|
# tests_stack.cpp
|
||||||
|
|
||||||
D_HEADERS = ./headers
|
D_HEADERS = ./headers
|
||||||
HEADERS = colors.h \
|
HEADERS = \
|
||||||
|
colors.h \
|
||||||
\
|
\
|
||||||
enable_if.hpp \
|
enable_if.hpp \
|
||||||
iterator_traits.hpp \
|
iterator_traits.hpp \
|
||||||
@@ -71,30 +84,23 @@ HEADERS = colors.h \
|
|||||||
map.hpp \
|
map.hpp \
|
||||||
map_node.hpp \
|
map_node.hpp \
|
||||||
map_iterator.hpp \
|
map_iterator.hpp \
|
||||||
vector.hpp
|
vector.hpp \
|
||||||
# map.hpp
|
stack.hpp
|
||||||
# bst.hpp
|
|
||||||
# bst_node.hpp
|
|
||||||
# bst_iterator.hpp
|
|
||||||
|
|
||||||
D_TEMPLATES = ./templates
|
D_TEMPLATES = ./templates
|
||||||
TEMPLATES = bst.tpp \
|
TEMPLATES = \
|
||||||
\
|
vector.tpp \
|
||||||
vector.tpp
|
map.tpp
|
||||||
# map.tpp
|
|
||||||
|
|
||||||
D_TESTS = ./tests/includes
|
D_TESTS = ./tests/includes
|
||||||
TESTS = main.hpp \
|
TESTS = \
|
||||||
|
main.hpp \
|
||||||
tests_utils.hpp
|
tests_utils.hpp
|
||||||
|
|
||||||
D_OBJS = builds
|
D_OBJS_FT = builds_ft
|
||||||
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
|
OBJS_FT = $(SRCS:%.$(EXT)=$(D_OBJS_FT)/%.o)
|
||||||
|
D_OBJS_STL = builds_stl
|
||||||
ifeq "$(D_OBJS)" "."
|
OBJS_STL = $(SRCS:%.$(EXT)=$(D_OBJS_STL)/%.o)
|
||||||
RM_OBJS = rm -f $(OBJS)
|
|
||||||
else
|
|
||||||
RM_OBJS = rm -rf $(D_OBJS)
|
|
||||||
endif
|
|
||||||
|
|
||||||
|
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
@@ -103,37 +109,42 @@ endif
|
|||||||
# . @recipe (silent) . $^ : all prerequisites #
|
# . @recipe (silent) . $^ : all prerequisites #
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME_FT) $(NAME_STL)
|
||||||
|
|
||||||
stl: CFLAGS += -D STL
|
stl: $(NAME_STL)
|
||||||
stl: re
|
ft: $(NAME_FT)
|
||||||
ft: re
|
|
||||||
|
|
||||||
leakstl: CFLAGS += -D STL
|
$(D_OBJS_FT)/%.o: %.$(EXT) | $(D_OBJS_FT)
|
||||||
leakstl: fclean leaks
|
|
||||||
leakft: fclean leaks
|
|
||||||
|
|
||||||
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
|
|
||||||
@echo $(CYAN)"compilation " $@ $(RESET)
|
@echo $(CYAN)"compilation " $@ $(RESET)
|
||||||
@$(CC) $(CFLAGS) -c $< -o $@
|
@$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
$(D_OBJS_STL)/%.o: %.$(EXT) | $(D_OBJS_STL)
|
||||||
|
@echo $(CYAN)"compilation -D STL" $@ $(RESET)
|
||||||
|
@$(CC) $(CFLAGS) -D STL -c $< -o $@
|
||||||
|
|
||||||
$(D_OBJS):
|
$(D_OBJS_FT) $(D_OBJS_STL):
|
||||||
mkdir $@
|
mkdir $@
|
||||||
|
|
||||||
$(OBJS): $(F_INCLUDES)
|
$(OBJS): $(F_INCLUDES)
|
||||||
|
|
||||||
$(NAME): $(OBJS)
|
# https://stackoverflow.com/questions/19259108/makefile-same-rule-for-multiple-targets
|
||||||
@echo $(CYAN)"linkage (link objects.o) :"$(RESET)
|
$(NAME_FT): $(OBJS_FT)
|
||||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
$(NAME_STL): $(OBJS_STL)
|
||||||
|
$(NAME_FT) $(NAME_STL):
|
||||||
|
@echo $(CYAN)"linkage (link objects.o)"$(RESET)
|
||||||
|
@$(CC) $^ -o $@ $(LIBS)
|
||||||
|
|
||||||
leaks: $(NAME)
|
leaks: leaksft
|
||||||
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
|
leaksft: $(NAME_FT)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$<
|
||||||
|
leakstl: $(NAME_STL)
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./$<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM_OBJS)
|
rm -rf $(D_OBJS_FT)
|
||||||
|
rm -rf $(D_OBJS_STL)
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -f $(NAME)
|
rm -f $(NAME_FT) $(NAME_STL)
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
|
|||||||
BIN
containers_ft
Executable file
BIN
containers_ft
Executable file
Binary file not shown.
@@ -21,8 +21,8 @@ template <
|
|||||||
typename T,
|
typename T,
|
||||||
typename Compare = std::less<Key>,
|
typename Compare = std::less<Key>,
|
||||||
typename Allocator = std::allocator< ft::pair<const Key, T> >
|
typename Allocator = std::allocator< ft::pair<const Key, T> >
|
||||||
> class Bst
|
> class Bst {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
typedef Key key_type;
|
typedef Key key_type;
|
||||||
typedef T mapped_type;
|
typedef T mapped_type;
|
||||||
@@ -32,60 +32,93 @@ template <
|
|||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
|
||||||
typedef typename Allocator::pointer pointer;
|
|
||||||
typedef typename Allocator::const_pointer const_pointer;
|
|
||||||
typedef typename Allocator::reference reference;
|
|
||||||
typedef typename Allocator::const_reference const_reference;
|
|
||||||
|
|
||||||
|
|
||||||
typedef Bst_iterator<Key, T, Compare, Allocator> iterator;
|
typedef Bst_iterator<Key, T, Compare, Allocator> iterator;
|
||||||
typedef Bst_const_iterator<Key, T, Compare, Allocator> const_iterator;
|
typedef Bst_const_iterator<Key, T, Compare, Allocator> const_iterator;
|
||||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
|
||||||
|
|
||||||
// Member functions
|
/************
|
||||||
explicit Bst(const Compare& comp = Compare(), const Allocator& alloc = Allocator() );
|
* copliens :
|
||||||
|
************/
|
||||||
|
// constructors ------------------------------
|
||||||
|
explicit Bst(
|
||||||
|
const key_compare& comp = Compare(),
|
||||||
|
const Allocator& alloc = Allocator()
|
||||||
|
);
|
||||||
template < typename InputIt >
|
template < typename InputIt >
|
||||||
Bst(InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator());
|
Bst(
|
||||||
|
InputIt first, InputIt last,
|
||||||
|
const key_compare& comp = Compare(),
|
||||||
|
const allocator_type& alloc = Allocator()
|
||||||
|
);
|
||||||
Bst(const Bst& src);
|
Bst(const Bst& src);
|
||||||
|
// destructor --------------------------------
|
||||||
~Bst();
|
~Bst();
|
||||||
|
// operator= ---------------------------------
|
||||||
Bst& operator=(const Bst& rhs);
|
Bst& operator=(const Bst& rhs);
|
||||||
|
|
||||||
// Element access
|
|
||||||
T& operator[](const Key& key);
|
|
||||||
|
|
||||||
// Iterators
|
/*************
|
||||||
|
* iterators :
|
||||||
|
*************/
|
||||||
|
// begin -------------------------------------
|
||||||
iterator begin();
|
iterator begin();
|
||||||
const_iterator begin() const;
|
const_iterator begin() const;
|
||||||
|
// end ---------------------------------------
|
||||||
iterator end();
|
iterator end();
|
||||||
const_iterator end() const;
|
const_iterator end() const;
|
||||||
|
// rbegin ------------------------------------
|
||||||
reverse_iterator rbegin();
|
reverse_iterator rbegin();
|
||||||
const_reverse_iterator rbegin() const;
|
const_reverse_iterator rbegin() const;
|
||||||
|
// rend --------------------------------------
|
||||||
reverse_iterator rend();
|
reverse_iterator rend();
|
||||||
const_reverse_iterator rend() const;
|
const_reverse_iterator rend() const;
|
||||||
|
|
||||||
// Capacity
|
|
||||||
|
/************
|
||||||
|
* capacity :
|
||||||
|
************/
|
||||||
|
// empty -------------------------------------
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
// size --------------------------------------
|
||||||
size_type size() const;
|
size_type size() const;
|
||||||
|
// max_size ----------------------------------
|
||||||
size_type max_size() const;
|
size_type max_size() const;
|
||||||
|
|
||||||
// Modifiers
|
|
||||||
void clear();
|
/******************
|
||||||
|
* element access :
|
||||||
|
******************/
|
||||||
|
// operator[] --------------------------------
|
||||||
|
mapped_type& operator[](const key_type& key);
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* modifiers :
|
||||||
|
*************/
|
||||||
|
// insert ------------------------------------
|
||||||
pair<iterator, bool> insert(const value_type& value);
|
pair<iterator, bool> insert(const value_type& value);
|
||||||
iterator insert(iterator hint, const value_type& value);
|
iterator insert(iterator hint, const value_type& value);
|
||||||
template < typename InputIt >
|
template < typename InputIt >
|
||||||
void insert(InputIt first, InputIt last);
|
void insert(InputIt first, InputIt last);
|
||||||
|
// erase -------------------------------------
|
||||||
void erase(iterator pos);
|
void erase(iterator pos);
|
||||||
|
size_type erase(const key_type& key);
|
||||||
void erase(iterator first, iterator last);
|
void erase(iterator first, iterator last);
|
||||||
size_type erase(const Key& key);
|
// swap --------------------------------------
|
||||||
void swap(Bst& other);
|
void swap(Bst& other);
|
||||||
|
// clear -------------------------------------
|
||||||
|
void clear();
|
||||||
|
|
||||||
// Lookup
|
/**************
|
||||||
iterator find(const Key& key);
|
* operations :
|
||||||
const_iterator find(const Key& key) const;
|
**************/
|
||||||
size_type count(const Key& key) const;
|
// find --------------------------------------
|
||||||
|
iterator find(const key_type& key);
|
||||||
|
const_iterator find(const key_type& key) const;
|
||||||
|
// count -------------------------------------
|
||||||
|
size_type count(const key_type& key) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_type _size;
|
size_type _size;
|
||||||
@@ -94,21 +127,27 @@ template <
|
|||||||
Compare _comp;
|
Compare _comp;
|
||||||
Allocator _allocator;
|
Allocator _allocator;
|
||||||
|
|
||||||
// TODO : rebind syntaxe pas clair.
|
// https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
|
||||||
typename Allocator::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
typename Allocator::template
|
||||||
typename Allocator::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe
|
rebind< node<value_type> >::other _allocator_node;
|
||||||
|
typename Allocator::template
|
||||||
|
rebind< node_sentinel<value_type> >::other _allocator_node_sentinel;
|
||||||
|
|
||||||
void _init_sentinel();
|
void _init_sentinel();
|
||||||
pair<iterator, bool> _insert(const value_type& value);
|
pair<iterator, bool> _insert(const value_type& value);
|
||||||
node<value_type>* _erase(iterator pos);
|
node<value_type>* _erase(iterator pos);
|
||||||
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
node<value_type>* _subtree_shift(
|
||||||
|
node<value_type>* st_old,
|
||||||
|
node<value_type>* st_new
|
||||||
|
);
|
||||||
|
|
||||||
// AVL Balancing
|
// BBST
|
||||||
|
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||||
void _insert_rebalancing(node<value_type>* n);
|
void _insert_rebalancing(node<value_type>* n);
|
||||||
void _erase_rebalancing(node<value_type>* n);
|
void _erase_rebalancing(node<value_type>* n);
|
||||||
|
|
||||||
short _compute_height(node<value_type>* n);
|
short _compute_height(node<value_type>* n);
|
||||||
short _bf(node<value_type>* n); // balance factor
|
short _balance_factor(node<value_type>* n);
|
||||||
node<value_type>* _rotate_left(node<value_type>* n);
|
node<value_type>* _rotate_left(node<value_type>* n);
|
||||||
node<value_type>* _rotate_right(node<value_type>* n);
|
node<value_type>* _rotate_right(node<value_type>* n);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#ifndef MAP_HPP
|
#ifndef MAP_HPP
|
||||||
# define MAP_HPP
|
# define MAP_HPP
|
||||||
|
|
||||||
# include "colors.h"
|
|
||||||
# include <memory> // std::allocator
|
# include <memory> // std::allocator
|
||||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||||
# include <functional> // std::less, std::binary_function
|
# include <functional> // std::less, std::binary_function
|
||||||
@@ -29,11 +28,6 @@ public:
|
|||||||
typedef Compare key_compare;
|
typedef Compare key_compare;
|
||||||
typedef Alloc allocator_type;
|
typedef Alloc allocator_type;
|
||||||
|
|
||||||
// typedef typename allocator_type::reference reference;
|
|
||||||
// typedef typename allocator_type::const_reference const_reference;
|
|
||||||
// typedef typename allocator_type::pointer pointer;
|
|
||||||
// typedef typename allocator_type::const_pointer const_pointer;
|
|
||||||
|
|
||||||
typedef Bst<Key,T,Compare,Alloc> bst_map;
|
typedef Bst<Key,T,Compare,Alloc> bst_map;
|
||||||
|
|
||||||
typedef typename bst_map::iterator iterator;
|
typedef typename bst_map::iterator iterator;
|
||||||
@@ -203,5 +197,11 @@ template< class Key, class T, class Compare, class Alloc > void swap
|
|||||||
|
|
||||||
# include "map.tpp"
|
# include "map.tpp"
|
||||||
|
|
||||||
|
// banlanced binary search tree :
|
||||||
|
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||||
|
|
||||||
|
// entinel node :
|
||||||
|
// https://en.wikipedia.org/wiki/Sentinel_node
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
195
headers/map.hpp
195
headers/map.hpp
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
# include <memory> // std::allocator
|
# include <memory> // std::allocator
|
||||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||||
# include <algorithm> // max()
|
# include <algorithm> // std::max()
|
||||||
# include <functional> // std::less, std::binary_function
|
# include <functional> // std::less, std::binary_function
|
||||||
|
|
||||||
# include "reverse_iterator.hpp"
|
# include "reverse_iterator.hpp"
|
||||||
@@ -22,27 +22,29 @@ template <
|
|||||||
class T, // map::mapped_type
|
class T, // map::mapped_type
|
||||||
class Compare = std::less<Key>, // map::key_compare
|
class Compare = std::less<Key>, // map::key_compare
|
||||||
class Alloc = std::allocator< ft::pair<const Key, T> > // map::allocator_type
|
class Alloc = std::allocator< ft::pair<const Key, T> > // map::allocator_type
|
||||||
> class map
|
> class map {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef Key key_type;
|
typedef Key key_type;
|
||||||
typedef T mapped_type;
|
typedef T mapped_type;
|
||||||
typedef pair<const Key, T> value_type;
|
typedef pair<const Key, T> value_type;
|
||||||
typedef Compare key_compare;
|
|
||||||
typedef Alloc allocator_type;
|
|
||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
typedef Compare key_compare;
|
||||||
// typedef typename Alloc::pointer pointer;
|
typedef Alloc allocator_type;
|
||||||
// typedef typename Alloc::const_pointer const_pointer;
|
|
||||||
// typedef typename Alloc::reference reference;
|
|
||||||
// typedef typename Alloc::const_reference const_reference;
|
|
||||||
|
|
||||||
typedef map_iterator<Key, T, Compare, Alloc> iterator;
|
typedef map_iterator<Key, T, Compare, Alloc> iterator;
|
||||||
typedef map_const_iterator<Key, T, Compare, Alloc> const_iterator;
|
typedef map_const_iterator<Key, T, Compare, Alloc> const_iterator;
|
||||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
|
||||||
|
|
||||||
|
/****************
|
||||||
|
* member class :
|
||||||
|
****************/
|
||||||
|
// https://en.cppreference.com/w/cpp/container/map/value_compare
|
||||||
|
// https://stackoverflow.com/questions/4571355/why-would-one-use-nested-classes-in-c
|
||||||
class value_compare : public std::binary_function<value_type, value_type, bool> {
|
class value_compare : public std::binary_function<value_type, value_type, bool> {
|
||||||
|
|
||||||
friend class map;
|
friend class map;
|
||||||
@@ -54,48 +56,110 @@ template <
|
|||||||
{ return comp(x.first, y.first); }
|
{ return comp(x.first, y.first); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Member functions
|
|
||||||
explicit map(const Compare& comp = Compare(), const Alloc& alloc = Alloc() );
|
/************
|
||||||
template < typename InputIt >
|
* copliens :
|
||||||
map(InputIt first, InputIt last, const Compare& comp = Compare(), const Alloc& alloc = Alloc());
|
************/
|
||||||
map(const map& src);
|
// constructors ------------------------------
|
||||||
|
explicit map (const key_compare& comp = key_compare(),
|
||||||
|
const allocator_type& alloc = allocator_type());
|
||||||
|
template <class InputIterator>
|
||||||
|
map (InputIterator first, InputIterator last,
|
||||||
|
const key_compare& comp = key_compare(),
|
||||||
|
const allocator_type& alloc = allocator_type());
|
||||||
|
map(const map& x);
|
||||||
|
// destructor --------------------------------
|
||||||
~map();
|
~map();
|
||||||
|
// operator= ---------------------------------
|
||||||
|
map& operator= (const map& x);
|
||||||
|
|
||||||
map& operator=(const map& rhs);
|
|
||||||
|
|
||||||
// Element access
|
/*************
|
||||||
T& operator[](const Key& key);
|
* iterators :
|
||||||
|
*************/
|
||||||
// Iterators
|
// begin -------------------------------------
|
||||||
iterator begin();
|
iterator begin();
|
||||||
const_iterator begin() const;
|
const_iterator begin() const;
|
||||||
|
// end ---------------------------------------
|
||||||
iterator end();
|
iterator end();
|
||||||
const_iterator end() const;
|
const_iterator end() const;
|
||||||
|
// rbegin ------------------------------------
|
||||||
reverse_iterator rbegin();
|
reverse_iterator rbegin();
|
||||||
const_reverse_iterator rbegin() const;
|
const_reverse_iterator rbegin() const;
|
||||||
|
// rend --------------------------------------
|
||||||
reverse_iterator rend();
|
reverse_iterator rend();
|
||||||
const_reverse_iterator rend() const;
|
const_reverse_iterator rend() const;
|
||||||
|
|
||||||
// Capacity
|
|
||||||
|
/************
|
||||||
|
* capacity :
|
||||||
|
************/
|
||||||
|
// empty -------------------------------------
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
// size --------------------------------------
|
||||||
size_type size() const;
|
size_type size() const;
|
||||||
|
// max_size ----------------------------------
|
||||||
size_type max_size() const;
|
size_type max_size() const;
|
||||||
|
|
||||||
// Modifiers
|
|
||||||
void clear();
|
|
||||||
pair<iterator, bool> insert(const value_type& value);
|
|
||||||
iterator insert(iterator hint, const value_type& value);
|
|
||||||
template < typename InputIt >
|
|
||||||
void insert(InputIt first, InputIt last);
|
|
||||||
void erase(iterator pos);
|
|
||||||
void erase(iterator first, iterator last);
|
|
||||||
size_type erase(const Key& key);
|
|
||||||
void swap(map& other);
|
|
||||||
|
|
||||||
// Lookup
|
/******************
|
||||||
iterator find(const Key& key);
|
* element access :
|
||||||
const_iterator find(const Key& key) const;
|
******************/
|
||||||
size_type count(const Key& key) const;
|
// operator[] --------------------------------
|
||||||
|
mapped_type & operator[] (const key_type& k);
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* modifiers :
|
||||||
|
*************/
|
||||||
|
// insert ------------------------------------
|
||||||
|
pair<iterator,bool> insert (const value_type& val);
|
||||||
|
iterator insert (iterator position, const value_type& val);
|
||||||
|
template <class InputIterator>
|
||||||
|
void insert (InputIterator first, InputIterator last);
|
||||||
|
// erase -------------------------------------
|
||||||
|
void erase (iterator position);
|
||||||
|
size_type erase (const key_type& k);
|
||||||
|
void erase (iterator first, iterator last);
|
||||||
|
// swap --------------------------------------
|
||||||
|
void swap (map& x);
|
||||||
|
// clear -------------------------------------
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* observers :
|
||||||
|
*************/
|
||||||
|
// key_comp ----------------------------------
|
||||||
|
key_compare key_comp() const;
|
||||||
|
// value_comp --------------------------------
|
||||||
|
value_compare value_comp() const;
|
||||||
|
|
||||||
|
|
||||||
|
/**************
|
||||||
|
* operations :
|
||||||
|
**************/
|
||||||
|
// find --------------------------------------
|
||||||
|
iterator find (const key_type& k);
|
||||||
|
const_iterator find (const key_type& k) const;
|
||||||
|
// count -------------------------------------
|
||||||
|
size_type count (const key_type& k) const;
|
||||||
|
// lower_bound -------------------------------
|
||||||
|
iterator lower_bound (const key_type& k);
|
||||||
|
const_iterator lower_bound (const key_type& k) const;
|
||||||
|
// upper_bound -------------------------------
|
||||||
|
iterator upper_bound (const key_type& k);
|
||||||
|
const_iterator upper_bound (const key_type& k) const;
|
||||||
|
// equal_range -------------------------------
|
||||||
|
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
|
||||||
|
pair<iterator,iterator> equal_range (const key_type& k);
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* allocator :
|
||||||
|
*************/
|
||||||
|
// get_allocator -----------------------------
|
||||||
|
allocator_type get_allocator() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_type _size;
|
size_type _size;
|
||||||
@@ -104,55 +168,60 @@ template <
|
|||||||
Compare _comp;
|
Compare _comp;
|
||||||
Alloc _allocator;
|
Alloc _allocator;
|
||||||
|
|
||||||
// TODO : rebind syntaxe pas clair.
|
// https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
|
||||||
typename Alloc::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
typename Alloc::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
||||||
typename Alloc::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe
|
typename Alloc::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe // SENTINELL
|
||||||
|
|
||||||
void _init_sentinel();
|
void _init_sentinel();
|
||||||
pair<iterator, bool> _insert(const value_type& value);
|
pair<iterator, bool> _insert(const value_type& value);
|
||||||
node<value_type>* _erase(iterator pos);
|
node<value_type>* _erase(iterator pos);
|
||||||
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
||||||
|
|
||||||
// AVL Balancing
|
// BBST
|
||||||
void _insert_rebalancing(node<value_type>* n);
|
void _insert_rebalancing(node<value_type>* n);
|
||||||
void _erase_rebalancing(node<value_type>* n);
|
void _erase_rebalancing(node<value_type>* n);
|
||||||
|
|
||||||
short _compute_height(node<value_type>* n);
|
short _compute_height(node<value_type>* n);
|
||||||
short _bf(node<value_type>* n); // balance factor
|
short _balance_factor(node<value_type>* n);
|
||||||
node<value_type>* _rotate_left(node<value_type>* n);
|
node<value_type>* _rotate_left(node<value_type>* n);
|
||||||
node<value_type>* _rotate_right(node<value_type>* n);
|
node<value_type>* _rotate_right(node<value_type>* n);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Non-member functions
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator==(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator!=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator<(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator>(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator<=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
|
||||||
bool operator>=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
|
||||||
|
|
||||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
/************************
|
||||||
void swap(map<Key,T,Compare,Alloc>& lhs, map<Key,T,Compare,Alloc>& rhs);
|
* non-member functions :
|
||||||
|
************************/
|
||||||
|
// operator == -------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator==
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// operator != -------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator!=
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// operator < --------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator<
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// operator <= -------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator<=
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// operator > --------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator>
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// operator >= -------------------------------
|
||||||
|
template< class K, class T, class Comp, class Alloc > bool operator>=
|
||||||
|
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||||
|
// swap (map) -----------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > void swap
|
||||||
|
( std::map<Key,T,Compare,Alloc>& lhs, std::map<Key,T,Compare,Alloc>& rhs );
|
||||||
|
|
||||||
} // namespace ft
|
} // namespace ft
|
||||||
|
|
||||||
# include "bst.tpp"
|
# include "map.tpp"
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Binary_search_tree
|
// banlanced binary search tree :
|
||||||
// https://en.wikipedia.org/wiki/AVL_tree
|
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||||
|
|
||||||
// https://fr.wikipedia.org/wiki/Arbre_binaire_de_recherche
|
|
||||||
// https://fr.wikipedia.org/wiki/Arbre_AVL
|
|
||||||
|
|
||||||
// https://visualgo.net/en/bst
|
|
||||||
// https://visualgo.net/en/bst?slide=14-8 // --> to 14-13
|
|
||||||
|
|
||||||
|
// entinel node :
|
||||||
|
// https://en.wikipedia.org/wiki/Sentinel_node
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ template <
|
|||||||
typedef value_type& reference;
|
typedef value_type& reference;
|
||||||
|
|
||||||
map_iterator() : _node(), _sentinel() {}
|
map_iterator() : _node(), _sentinel() {}
|
||||||
map_iterator(node<value_type>* n, node_sentinel<value_type>* sentinel) : _node(n), _sentinel(sentinel) {}
|
map_iterator(node<value_type>* n, node_sentinel<value_type>* sentinel) : _node(n), _sentinel(sentinel) {} // SENTINELL
|
||||||
//map_iterator(const map_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
|
//map_iterator(node<value_type>* n, node<value_type>* sentinel) : _node(n), _sentinel(sentinel) {}
|
||||||
|
|
||||||
reference operator*() const
|
reference operator*() const
|
||||||
{ return _node->value; }
|
{ return _node->value; }
|
||||||
@@ -38,7 +38,8 @@ template <
|
|||||||
Self& operator++()
|
Self& operator++()
|
||||||
{
|
{
|
||||||
if (_node == NULL)
|
if (_node == NULL)
|
||||||
_node = _sentinel->child->min();
|
_node = _sentinel->child->min(); // SENTINELL
|
||||||
|
//_node = _sentinel->min();
|
||||||
else if (_node->right)
|
else if (_node->right)
|
||||||
_node = _node->right->min();
|
_node = _node->right->min();
|
||||||
else
|
else
|
||||||
@@ -57,7 +58,8 @@ template <
|
|||||||
Self& operator--()
|
Self& operator--()
|
||||||
{
|
{
|
||||||
if (_node == NULL)
|
if (_node == NULL)
|
||||||
_node = _sentinel->child->max();
|
_node = _sentinel->child->max(); // SENTINELL
|
||||||
|
//_node = _sentinel->max();
|
||||||
else if (_node->left)
|
else if (_node->left)
|
||||||
_node = _node->left->max();
|
_node = _node->left->max();
|
||||||
else
|
else
|
||||||
@@ -93,7 +95,8 @@ template <
|
|||||||
{ return _node; }
|
{ return _node; }
|
||||||
const node<value_type>* getNode() const
|
const node<value_type>* getNode() const
|
||||||
{ return _node; }
|
{ return _node; }
|
||||||
const node_sentinel<value_type>* getSentinel() const
|
const node_sentinel<value_type>* getSentinel() const // SENTINELL
|
||||||
|
//const node<value_type>* getSentinel() const
|
||||||
{ return _sentinel; }
|
{ return _sentinel; }
|
||||||
|
|
||||||
// TODO : friend Non-member functions syntaxe pas clair.
|
// TODO : friend Non-member functions syntaxe pas clair.
|
||||||
@@ -104,7 +107,8 @@ template <
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
node<value_type>* _node;
|
node<value_type>* _node;
|
||||||
node_sentinel<value_type>* _sentinel;
|
node_sentinel<value_type>* _sentinel; // SENTINELL
|
||||||
|
//node<value_type>* _sentinel;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <
|
template <
|
||||||
@@ -125,8 +129,8 @@ template <
|
|||||||
typedef const value_type& reference;
|
typedef const value_type& reference;
|
||||||
|
|
||||||
map_const_iterator() : _node(), _sentinel() {}
|
map_const_iterator() : _node(), _sentinel() {}
|
||||||
map_const_iterator(const node<value_type>* node, const node_sentinel<value_type>* sentinel) : _node(node), _sentinel(sentinel) {}
|
map_const_iterator(const node<value_type>* node, const node_sentinel<value_type>* sentinel) : _node(node), _sentinel(sentinel) {} // SENTINELL
|
||||||
//map_const_iterator(const map_const_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
|
//map_const_iterator(const node<value_type>* nodee, const node<value_type>* sentinel) : _node(nodee), _sentinel(sentinel) {}
|
||||||
map_const_iterator(const map_iterator<Key, T, Compare, Allocator>& src) : _node(src.getNode()), _sentinel(src.getSentinel()) {}
|
map_const_iterator(const map_iterator<Key, T, Compare, Allocator>& src) : _node(src.getNode()), _sentinel(src.getSentinel()) {}
|
||||||
|
|
||||||
reference operator*() const
|
reference operator*() const
|
||||||
@@ -137,7 +141,8 @@ template <
|
|||||||
Self& operator++()
|
Self& operator++()
|
||||||
{
|
{
|
||||||
if (_node == NULL)
|
if (_node == NULL)
|
||||||
_node = _sentinel->child->min();
|
_node = _sentinel->child->min(); // SENTINELL
|
||||||
|
//_node = _sentinel->min();
|
||||||
else if (_node->right)
|
else if (_node->right)
|
||||||
_node = _node->right->min();
|
_node = _node->right->min();
|
||||||
else
|
else
|
||||||
@@ -156,7 +161,8 @@ template <
|
|||||||
Self& operator--()
|
Self& operator--()
|
||||||
{
|
{
|
||||||
if (_node == NULL)
|
if (_node == NULL)
|
||||||
_node = _sentinel->child->max();
|
_node = _sentinel->child->max(); // SENTINELL
|
||||||
|
//_node = _sentinel->max();
|
||||||
else if (_node->left)
|
else if (_node->left)
|
||||||
_node = _node->left->max();
|
_node = _node->left->max();
|
||||||
else
|
else
|
||||||
@@ -174,7 +180,6 @@ template <
|
|||||||
|
|
||||||
Self operator++(int)
|
Self operator++(int)
|
||||||
{
|
{
|
||||||
//Self old(*this);
|
|
||||||
Self old = *this;
|
Self old = *this;
|
||||||
++(*this);
|
++(*this);
|
||||||
return old;
|
return old;
|
||||||
@@ -182,7 +187,6 @@ template <
|
|||||||
|
|
||||||
Self operator--(int)
|
Self operator--(int)
|
||||||
{
|
{
|
||||||
//Self old(*this);
|
|
||||||
Self old = *this;
|
Self old = *this;
|
||||||
--(*this);
|
--(*this);
|
||||||
return old;
|
return old;
|
||||||
@@ -198,7 +202,8 @@ template <
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const node<value_type>* _node;
|
const node<value_type>* _node;
|
||||||
const node_sentinel<value_type>* _sentinel;
|
const node_sentinel<value_type>* _sentinel; // SENTINELL
|
||||||
|
//const node<value_type>* _sentinel;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ft
|
} // namespace ft
|
||||||
|
|||||||
@@ -7,18 +7,24 @@
|
|||||||
namespace ft {
|
namespace ft {
|
||||||
|
|
||||||
template < typename ValueType >
|
template < typename ValueType >
|
||||||
struct node
|
struct node {
|
||||||
{
|
|
||||||
ValueType value;
|
ValueType value;
|
||||||
node *up;
|
node *up;
|
||||||
node *left;
|
node *left;
|
||||||
node *right;
|
node *right;
|
||||||
short height;
|
short height;
|
||||||
|
|
||||||
node(const ValueType& val) : value(val), up(NULL), left(NULL), right(NULL), height(1) {}
|
node(const ValueType& val)
|
||||||
|
: value(val)
|
||||||
|
, up(NULL)
|
||||||
|
, left(NULL)
|
||||||
|
, right(NULL)
|
||||||
|
, height(1)
|
||||||
|
{}
|
||||||
|
|
||||||
|
node* min() {
|
||||||
|
|
||||||
node* min()
|
|
||||||
{
|
|
||||||
node* n = this;
|
node* n = this;
|
||||||
|
|
||||||
while (n->left)
|
while (n->left)
|
||||||
@@ -26,8 +32,8 @@ struct node
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
node* max()
|
node* max() {
|
||||||
{
|
|
||||||
node* n = this;
|
node* n = this;
|
||||||
|
|
||||||
while (n->right)
|
while (n->right)
|
||||||
@@ -37,8 +43,8 @@ struct node
|
|||||||
};
|
};
|
||||||
|
|
||||||
template < typename ValueType >
|
template < typename ValueType >
|
||||||
struct node_sentinel
|
struct node_sentinel {
|
||||||
{
|
|
||||||
node<ValueType> *child;
|
node<ValueType> *child;
|
||||||
|
|
||||||
node_sentinel() : child(NULL) {}
|
node_sentinel() : child(NULL) {}
|
||||||
|
|||||||
94
headers/stack.hpp
Normal file
94
headers/stack.hpp
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
|
||||||
|
#ifndef STACK_HPP
|
||||||
|
# define STACK_HPP
|
||||||
|
|
||||||
|
# include "vector.hpp"
|
||||||
|
|
||||||
|
namespace ft {
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename T,
|
||||||
|
typename Container = ft::vector<T>
|
||||||
|
> class stack
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef Container container_type;
|
||||||
|
typedef typename Container::value_type value_type;
|
||||||
|
typedef typename Container::size_type size_type;
|
||||||
|
|
||||||
|
|
||||||
|
/************
|
||||||
|
* copliens :
|
||||||
|
************/
|
||||||
|
// constructors ------------------------------
|
||||||
|
explicit stack(const container_type& cont = Container()) : c(cont) {}
|
||||||
|
explicit stack(stack const &other): c(other.c) {}
|
||||||
|
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* overload functions :
|
||||||
|
**********************/
|
||||||
|
// empty -------------------------------------
|
||||||
|
bool empty() const { return c.empty(); }
|
||||||
|
// size --------------------------------------
|
||||||
|
size_type size() const { return c.size(); }
|
||||||
|
// top ---------------------------------------
|
||||||
|
value_type& top() { return c.back(); }
|
||||||
|
const value_type& top() const { return c.back(); }
|
||||||
|
// push --------------------------------------
|
||||||
|
void push(const value_type& value) { c.push_back(value); }
|
||||||
|
// pop ---------------------------------------
|
||||||
|
void pop() { c.pop_back(); }
|
||||||
|
|
||||||
|
// Relational Operators (friend)
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator==(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator!=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator<(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator>(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator<=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
template < typename T2, typename C2 >
|
||||||
|
friend bool operator>=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Container c;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/************************
|
||||||
|
* non-member functions :
|
||||||
|
************************/
|
||||||
|
// operator == -------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator==(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c == rhs.c; }
|
||||||
|
// operator != -------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator!=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c != rhs.c; }
|
||||||
|
// operator < --------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator<(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c < rhs.c; }
|
||||||
|
// operator > --------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator>(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c > rhs.c; }
|
||||||
|
// operator <= -------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator<=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c <= rhs.c; }
|
||||||
|
// operator >= -------------------------------
|
||||||
|
template < typename T, typename Container >
|
||||||
|
bool operator>=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||||
|
{ return lhs.c >= rhs.c; }
|
||||||
|
|
||||||
|
} // namespace ft
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -8,55 +8,49 @@ namespace ft {
|
|||||||
//////////////////////
|
//////////////////////
|
||||||
// Member functions //
|
// Member functions //
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE BST::
|
||||||
BST::
|
Bst(const Compare& comp, const Allocator& alloc)
|
||||||
Bst(const Compare& comp, const Allocator& alloc) :
|
: _size(0)
|
||||||
_size(0),
|
, _root(NULL)
|
||||||
_root(NULL),
|
, _comp(comp)
|
||||||
_comp(comp),
|
, _allocator(alloc) {
|
||||||
_allocator(alloc)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
_init_sentinel();
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE template < typename InputIt > BST::
|
||||||
template < typename InputIt >
|
Bst(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc)
|
||||||
BST::
|
: _size(0)
|
||||||
Bst(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc) :
|
, _root(NULL)
|
||||||
_size(0),
|
, _comp(comp)
|
||||||
_root(NULL),
|
, _allocator(alloc) {
|
||||||
_comp(comp),
|
|
||||||
_allocator(alloc)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
_init_sentinel();
|
||||||
insert(first, last);
|
insert(first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE BST::
|
||||||
BST::
|
Bst(const Bst& src)
|
||||||
Bst(const Bst& src) :
|
: _size(0)
|
||||||
_size(0),
|
, _root(NULL)
|
||||||
_root(NULL),
|
, _comp(src._comp)
|
||||||
_comp(src._comp),
|
, _allocator(src._allocator) {
|
||||||
_allocator(src._allocator)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
_init_sentinel();
|
||||||
*this = src;
|
*this = src;
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE BST::
|
||||||
BST::
|
~Bst() {
|
||||||
~Bst()
|
|
||||||
{
|
|
||||||
clear();
|
clear();
|
||||||
_allocator_node_sentinel.destroy(_sentinel);
|
_allocator_node_sentinel.destroy(_sentinel);
|
||||||
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE BST& BST::
|
||||||
BST& BST::
|
operator=(const Bst& rhs) {
|
||||||
operator=(const Bst& rhs)
|
|
||||||
{
|
|
||||||
if (this == &rhs)
|
if (this == &rhs)
|
||||||
return (*this);
|
return (*this);
|
||||||
Bst new_bst(rhs.begin(), rhs.end());
|
Bst new_bst(rhs.begin(), rhs.end());
|
||||||
@@ -68,10 +62,9 @@ BST& BST::
|
|||||||
////////////////////
|
////////////////////
|
||||||
// Element access //
|
// Element access //
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE T& BST::
|
||||||
T& BST::
|
operator[](const Key& key) {
|
||||||
operator[](const Key& key)
|
|
||||||
{
|
|
||||||
node<value_type>* n = _root;
|
node<value_type>* n = _root;
|
||||||
//node<value_type>* prev = NULL;
|
//node<value_type>* prev = NULL;
|
||||||
|
|
||||||
@@ -96,64 +89,53 @@ T& BST::
|
|||||||
///////////////
|
///////////////
|
||||||
// Iterators //
|
// Iterators //
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::iterator BST::
|
||||||
typename BST::iterator BST::
|
begin() {
|
||||||
begin()
|
|
||||||
{
|
|
||||||
if (_root)
|
if (_root)
|
||||||
return iterator(_root->min(), _sentinel);
|
return iterator(_root->min(), _sentinel);
|
||||||
else
|
else
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::const_iterator BST::
|
||||||
typename BST::const_iterator BST::
|
begin() const {
|
||||||
begin() const
|
|
||||||
{
|
|
||||||
if (_root)
|
if (_root)
|
||||||
return const_iterator(_root->min(), _sentinel);
|
return const_iterator(_root->min(), _sentinel);
|
||||||
else
|
else
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::iterator BST::
|
||||||
typename BST::iterator BST::
|
|
||||||
end() { return iterator(NULL, _sentinel); }
|
end() { return iterator(NULL, _sentinel); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::const_iterator BST::
|
||||||
typename BST::const_iterator BST::
|
|
||||||
end() const { return const_iterator(NULL, _sentinel); }
|
end() const { return const_iterator(NULL, _sentinel); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::reverse_iterator BST::
|
||||||
typename BST::reverse_iterator BST::
|
|
||||||
rbegin() { return reverse_iterator(end()); }
|
rbegin() { return reverse_iterator(end()); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::const_reverse_iterator BST::
|
||||||
typename BST::const_reverse_iterator BST::
|
|
||||||
rbegin() const { return const_reverse_iterator(end()); }
|
rbegin() const { return const_reverse_iterator(end()); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::reverse_iterator BST::
|
||||||
typename BST::reverse_iterator BST::
|
|
||||||
rend() { return reverse_iterator(begin()); }
|
rend() { return reverse_iterator(begin()); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::const_reverse_iterator BST::
|
||||||
typename BST::const_reverse_iterator BST::
|
|
||||||
rend() const { return const_reverse_iterator(begin()); }
|
rend() const { return const_reverse_iterator(begin()); }
|
||||||
|
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
// Capacity //
|
// Capacity //
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE bool BST::
|
||||||
bool BST::
|
|
||||||
empty() const { return (_size == 0); }
|
empty() const { return (_size == 0); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::size_type BST::
|
||||||
typename BST::size_type BST::
|
|
||||||
size() const { return (_size); }
|
size() const { return (_size); }
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE typename BST::size_type BST::
|
||||||
typename BST::size_type BST::
|
|
||||||
max_size() const
|
max_size() const
|
||||||
{
|
{
|
||||||
return ( _allocator_node.max_size() );
|
return ( _allocator_node.max_size() );
|
||||||
@@ -419,19 +401,19 @@ void BST::
|
|||||||
{
|
{
|
||||||
n->height = _compute_height(n);
|
n->height = _compute_height(n);
|
||||||
|
|
||||||
if (_bf(n) > 1) // Left Heavy
|
if (_balance_factor(n) > 1) // Left Heavy
|
||||||
{
|
{
|
||||||
parent = n->up;
|
parent = n->up;
|
||||||
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
|
if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1)
|
||||||
n->left = _rotate_left(n->left);
|
n->left = _rotate_left(n->left);
|
||||||
// Left-Left Case
|
// Left-Left Case
|
||||||
n = _rotate_right(n);
|
n = _rotate_right(n);
|
||||||
old_n = n->right;
|
old_n = n->right;
|
||||||
}
|
}
|
||||||
else if (_bf(n) < -1) // Right Heavy
|
else if (_balance_factor(n) < -1) // Right Heavy
|
||||||
{
|
{
|
||||||
parent = n->up;
|
parent = n->up;
|
||||||
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
|
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
|
||||||
n->right = _rotate_right(n->right);
|
n->right = _rotate_right(n->right);
|
||||||
// Right-Right Case
|
// Right-Right Case
|
||||||
n = _rotate_left(n);
|
n = _rotate_left(n);
|
||||||
@@ -468,19 +450,19 @@ void BST::
|
|||||||
{
|
{
|
||||||
n->height = _compute_height(n);
|
n->height = _compute_height(n);
|
||||||
|
|
||||||
if (_bf(n) > 1) // Left Heavy
|
if (_balance_factor(n) > 1) // Left Heavy
|
||||||
{
|
{
|
||||||
parent = n->up;
|
parent = n->up;
|
||||||
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
|
if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1)
|
||||||
n->left = _rotate_left(n->left);
|
n->left = _rotate_left(n->left);
|
||||||
// Left-Left Case
|
// Left-Left Case
|
||||||
n = _rotate_right(n);
|
n = _rotate_right(n);
|
||||||
old_n = n->right;
|
old_n = n->right;
|
||||||
}
|
}
|
||||||
else if (_bf(n) < -1) // Right Heavy
|
else if (_balance_factor(n) < -1) // Right Heavy
|
||||||
{
|
{
|
||||||
parent = n->up;
|
parent = n->up;
|
||||||
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
|
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
|
||||||
n->right = _rotate_right(n->right);
|
n->right = _rotate_right(n->right);
|
||||||
// Right-Right Case
|
// Right-Right Case
|
||||||
n = _rotate_left(n);
|
n = _rotate_left(n);
|
||||||
@@ -514,10 +496,9 @@ short BST::
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE short BST::
|
||||||
short BST::
|
_balance_factor(node<value_type>* n) {
|
||||||
_bf(node<value_type>* n) // optimisation possible if assume n have at least one child ?
|
|
||||||
{
|
|
||||||
if (n->left && n->right)
|
if (n->left && n->right)
|
||||||
return n->left->height - n->right->height;
|
return n->left->height - n->right->height;
|
||||||
else if (n->left)
|
else if (n->left)
|
||||||
@@ -528,10 +509,9 @@ short BST::
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE node<typename BST::value_type>* BST::
|
||||||
node<typename BST::value_type>* BST::
|
_rotate_left(node<value_type>* n) {
|
||||||
_rotate_left(node<value_type>* n) // assume n->right != NULL
|
|
||||||
{
|
|
||||||
node<value_type>* ori_right = n->right;
|
node<value_type>* ori_right = n->right;
|
||||||
|
|
||||||
ori_right->up = n->up;
|
ori_right->up = n->up;
|
||||||
@@ -554,10 +534,9 @@ node<typename BST::value_type>* BST::
|
|||||||
return ori_right; // return new sub-tree root
|
return ori_right; // return new sub-tree root
|
||||||
}
|
}
|
||||||
|
|
||||||
BST_TEMPLATE
|
BST_TEMPLATE node<typename BST::value_type>* BST::
|
||||||
node<typename BST::value_type>* BST::
|
_rotate_right(node<value_type>* n) {
|
||||||
_rotate_right(node<value_type>* n) // assume n->left != NULL
|
|
||||||
{
|
|
||||||
node<value_type>* ori_left = n->left;
|
node<value_type>* ori_left = n->left;
|
||||||
|
|
||||||
ori_left->up = n->up;
|
ori_left->up = n->up;
|
||||||
268
templates/bak__map.tpp
Normal file
268
templates/bak__map.tpp
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
|
||||||
|
#define MP_TPL template < typename Key, typename T, typename Compare, typename Alloc >
|
||||||
|
#define MP map<Key, T, Compare, Alloc>
|
||||||
|
|
||||||
|
namespace ft {
|
||||||
|
|
||||||
|
|
||||||
|
/************
|
||||||
|
* copliens :
|
||||||
|
************/
|
||||||
|
// constructors ------------------------------
|
||||||
|
MP_TPL MP::
|
||||||
|
map (const key_compare & comp, const allocator_type & alloc)
|
||||||
|
: _bst()
|
||||||
|
, _allocator(alloc)
|
||||||
|
, _comp(comp) {
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MP_TPL template <class InputIt> MP::
|
||||||
|
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
|
||||||
|
: _bst(first, last)
|
||||||
|
, _allocator(alloc)
|
||||||
|
, _comp(comp) {
|
||||||
|
}
|
||||||
|
MP_TPL MP::
|
||||||
|
map (const map& x)
|
||||||
|
: _bst()
|
||||||
|
, _allocator(x._allocator)
|
||||||
|
, _comp(x._comp) {
|
||||||
|
|
||||||
|
*this = x;
|
||||||
|
}
|
||||||
|
// destructor --------------------------------
|
||||||
|
MP_TPL MP::
|
||||||
|
~map() { clear(); }
|
||||||
|
// operator= ---------------------------------
|
||||||
|
MP_TPL MP& MP::
|
||||||
|
operator= (const map& x) {
|
||||||
|
|
||||||
|
if (this == &x)
|
||||||
|
return (*this);
|
||||||
|
|
||||||
|
map new_map(x.begin(), x.end());
|
||||||
|
swap(new_map);
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* iterators :
|
||||||
|
*************/
|
||||||
|
// begin -------------------------------------
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
begin() { return (_bst.begin()); }
|
||||||
|
MP_TPL typename MP::const_iterator MP::
|
||||||
|
begin() const { return (_bst.begin()); }
|
||||||
|
// end ---------------------------------------
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
end() { return (_bst.end()); }
|
||||||
|
MP_TPL typename MP::const_iterator MP::
|
||||||
|
end() const { return (_bst.end()); }
|
||||||
|
// rbegin ------------------------------------
|
||||||
|
MP_TPL typename MP::reverse_iterator MP::
|
||||||
|
rbegin() { return (_bst.rbegin()); }
|
||||||
|
MP_TPL typename MP::const_reverse_iterator MP::
|
||||||
|
rbegin() const { return (_bst.rbegin()); }
|
||||||
|
// rend --------------------------------------
|
||||||
|
MP_TPL typename MP::reverse_iterator MP::
|
||||||
|
rend() { return (_bst.rend()); }
|
||||||
|
MP_TPL typename MP::const_reverse_iterator MP::
|
||||||
|
rend() const { return (_bst.rend()); }
|
||||||
|
|
||||||
|
|
||||||
|
/************
|
||||||
|
* capacity :
|
||||||
|
************/
|
||||||
|
// empty -------------------------------------
|
||||||
|
MP_TPL bool MP::
|
||||||
|
empty() const { return (_bst.empty()); }
|
||||||
|
// size --------------------------------------
|
||||||
|
MP_TPL typename MP::size_type MP::
|
||||||
|
size() const { return (_bst.size()); }
|
||||||
|
// max_size ----------------------------------
|
||||||
|
MP_TPL typename MP::size_type MP::
|
||||||
|
max_size() const { return (_bst.max_size()); }
|
||||||
|
|
||||||
|
|
||||||
|
/******************
|
||||||
|
* element access :
|
||||||
|
******************/
|
||||||
|
// operator[] --------------------------------
|
||||||
|
MP_TPL typename MP::mapped_type& MP::
|
||||||
|
operator[] (const key_type& k) { return _bst[k]; }
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* modifiers :
|
||||||
|
*************/
|
||||||
|
// insert ------------------------------------
|
||||||
|
MP_TPL pair<typename MP::iterator, bool> MP::
|
||||||
|
insert (const value_type& val) { return (_bst.insert(val)); }
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
insert (iterator pos, const value_type& val) { return (_bst.insert(pos, val)); }
|
||||||
|
MP_TPL template <class InputIt> void MP::
|
||||||
|
insert (InputIt first, InputIt last) { return (_bst.insert(first, last)); }
|
||||||
|
// erase -------------------------------------
|
||||||
|
MP_TPL void MP::
|
||||||
|
erase (iterator pos) { return (_bst.erase(pos)); }
|
||||||
|
MP_TPL typename MP::size_type MP::
|
||||||
|
erase (const key_type& k) { return (_bst.erase(k)); }
|
||||||
|
MP_TPL void MP::
|
||||||
|
erase (iterator first, iterator last) { return (_bst.erase(first, last)); }
|
||||||
|
// swap --------------------------------------
|
||||||
|
MP_TPL void MP::
|
||||||
|
swap (map& x) {
|
||||||
|
|
||||||
|
bst_map tmp;
|
||||||
|
|
||||||
|
tmp.swap(_bst);
|
||||||
|
_bst.swap(x._bst);
|
||||||
|
x._bst.swap(tmp);
|
||||||
|
}
|
||||||
|
// clear -------------------------------------
|
||||||
|
MP_TPL void MP::
|
||||||
|
clear() {
|
||||||
|
|
||||||
|
_bst.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* observers :
|
||||||
|
*************/
|
||||||
|
// key_comp ----------------------------------
|
||||||
|
MP_TPL typename MP::key_compare MP::
|
||||||
|
key_comp() const { return (value_compare(_comp).comp); }
|
||||||
|
// value_comp --------------------------------
|
||||||
|
MP_TPL typename MP::value_compare MP::
|
||||||
|
value_comp() const { return (value_compare(_comp)); }
|
||||||
|
|
||||||
|
|
||||||
|
/**************
|
||||||
|
* operations :
|
||||||
|
**************/
|
||||||
|
// find --------------------------------------
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
find (const key_type& k) { return (_bst.find(k)); }
|
||||||
|
MP_TPL typename MP::const_iterator MP::
|
||||||
|
find (const key_type& k) const { return (_bst.find(k)); }
|
||||||
|
// count -------------------------------------
|
||||||
|
MP_TPL typename MP::size_type MP::
|
||||||
|
count (const key_type& k) const { return (_bst.count(k)); }
|
||||||
|
// lower_bound -------------------------------
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
lower_bound (const key_type& k) {
|
||||||
|
|
||||||
|
iterator it = begin();
|
||||||
|
iterator it_end = end();
|
||||||
|
|
||||||
|
while (it != it_end)
|
||||||
|
{
|
||||||
|
if (_comp(it->first, k) == false)
|
||||||
|
return (it);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
return (it_end);
|
||||||
|
}
|
||||||
|
MP_TPL typename MP::const_iterator MP::
|
||||||
|
lower_bound (const key_type& k) const {
|
||||||
|
|
||||||
|
const_iterator it = begin();
|
||||||
|
const_iterator it_end = end();
|
||||||
|
|
||||||
|
while (it != it_end)
|
||||||
|
{
|
||||||
|
if (_comp(it->first, k) == false)
|
||||||
|
return (it);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
return (it_end);
|
||||||
|
}
|
||||||
|
// upper_bound -------------------------------
|
||||||
|
MP_TPL typename MP::iterator MP::
|
||||||
|
upper_bound (const key_type& k) {
|
||||||
|
|
||||||
|
iterator it = begin();
|
||||||
|
iterator it_end = end();
|
||||||
|
|
||||||
|
while (it != it_end)
|
||||||
|
{
|
||||||
|
if (_comp(k, it->first))
|
||||||
|
return (it);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
return (it_end);
|
||||||
|
}
|
||||||
|
MP_TPL typename MP::const_iterator MP::
|
||||||
|
upper_bound (const key_type& k) const {
|
||||||
|
|
||||||
|
const_iterator it = begin();
|
||||||
|
const_iterator it_end = end();
|
||||||
|
|
||||||
|
while (it != it_end)
|
||||||
|
{
|
||||||
|
if (_comp(k, it->first))
|
||||||
|
return (it);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
return (it_end);
|
||||||
|
}
|
||||||
|
// equal_range -------------------------------
|
||||||
|
MP_TPL pair<typename MP::const_iterator, typename MP::const_iterator> MP::
|
||||||
|
equal_range (const key_type& k) const {
|
||||||
|
|
||||||
|
return ft::make_pair( lower_bound(k), upper_bound(k) );
|
||||||
|
}
|
||||||
|
MP_TPL pair<typename MP::iterator, typename MP::iterator> MP::
|
||||||
|
equal_range (const key_type& k) {
|
||||||
|
|
||||||
|
return ft::make_pair( lower_bound(k), upper_bound(k) );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* allocator :
|
||||||
|
*************/
|
||||||
|
// get_allocator -----------------------------
|
||||||
|
MP_TPL typename MP::allocator_type MP::
|
||||||
|
get_allocator() const { return (_allocator); }
|
||||||
|
|
||||||
|
|
||||||
|
/************************
|
||||||
|
* non-member functions :
|
||||||
|
************************/
|
||||||
|
// operator == -------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator==
|
||||||
|
( const MP& lhs, const MP& rhs ) {
|
||||||
|
|
||||||
|
return (lhs._bst == rhs._bst);
|
||||||
|
}
|
||||||
|
// operator < --------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator<
|
||||||
|
( const MP& lhs, const MP& rhs ) {
|
||||||
|
|
||||||
|
return (lhs._bst < rhs._bst);
|
||||||
|
}
|
||||||
|
// operator != -------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator!=
|
||||||
|
( const MP& lhs, const MP& rhs ) { return !(lhs == rhs); }
|
||||||
|
// operator <= -------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator<=
|
||||||
|
( const MP& lhs, const MP& rhs ) { return !(lhs > rhs); }
|
||||||
|
// operator > --------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator>
|
||||||
|
( const MP& lhs, const MP& rhs ) { return (rhs < lhs); }
|
||||||
|
// operator >= -------------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > bool operator>=
|
||||||
|
( const MP& lhs, const MP& rhs ) { return !(lhs < rhs); }
|
||||||
|
// swap (map) -----------------------------
|
||||||
|
template< class Key, class T, class Compare, class Alloc > void swap
|
||||||
|
( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }
|
||||||
|
|
||||||
|
} // namespace ft
|
||||||
|
|
||||||
|
#undef VT
|
||||||
|
#undef VT_TPL
|
||||||
|
|
||||||
@@ -1,628 +0,0 @@
|
|||||||
|
|
||||||
#define BST_TEMPLATE template < typename Key, typename T, typename Compare, typename Allocator >
|
|
||||||
#define BST map<Key, T, Compare, Allocator>
|
|
||||||
|
|
||||||
namespace ft {
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////
|
|
||||||
// Member functions //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
BST::
|
|
||||||
map(const Compare& comp, const Allocator& alloc) :
|
|
||||||
_size(0),
|
|
||||||
_root(NULL),
|
|
||||||
_comp(comp),
|
|
||||||
_allocator(alloc)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
template < typename InputIt >
|
|
||||||
BST::
|
|
||||||
map(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc) :
|
|
||||||
_size(0),
|
|
||||||
_root(NULL),
|
|
||||||
_comp(comp),
|
|
||||||
_allocator(alloc)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
|
||||||
insert(first, last);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
BST::
|
|
||||||
map(const map& src) :
|
|
||||||
_size(0),
|
|
||||||
_root(NULL),
|
|
||||||
_comp(src._comp),
|
|
||||||
_allocator(src._allocator)
|
|
||||||
{
|
|
||||||
_init_sentinel();
|
|
||||||
*this = src;
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
BST::
|
|
||||||
~map()
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
_allocator_node_sentinel.destroy(_sentinel);
|
|
||||||
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
BST& BST::
|
|
||||||
operator=(const map& rhs)
|
|
||||||
{
|
|
||||||
if (this == &rhs)
|
|
||||||
return (*this);
|
|
||||||
map new_bst(rhs.begin(), rhs.end());
|
|
||||||
swap(new_bst);
|
|
||||||
return (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////
|
|
||||||
// Element access //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
T& BST::
|
|
||||||
operator[](const Key& key)
|
|
||||||
{
|
|
||||||
node<value_type>* n = _root;
|
|
||||||
//node<value_type>* prev = NULL;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
//prev = n;
|
|
||||||
if (_comp(key, n->value.first))
|
|
||||||
n = n->left;
|
|
||||||
else if (_comp(n->value.first, key))
|
|
||||||
n = n->right;
|
|
||||||
else
|
|
||||||
return (n->value.second);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO : Call insert with hint (prev)
|
|
||||||
n = insert( ft::make_pair(key, mapped_type()) ).first.getNode();
|
|
||||||
|
|
||||||
return (n->value.second);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////
|
|
||||||
// Iterators //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::iterator BST::
|
|
||||||
begin()
|
|
||||||
{
|
|
||||||
if (_root)
|
|
||||||
return iterator(_root->min(), _sentinel);
|
|
||||||
else
|
|
||||||
return end();
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::const_iterator BST::
|
|
||||||
begin() const
|
|
||||||
{
|
|
||||||
if (_root)
|
|
||||||
return const_iterator(_root->min(), _sentinel);
|
|
||||||
else
|
|
||||||
return end();
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::iterator BST::
|
|
||||||
end() { return iterator(NULL, _sentinel); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::const_iterator BST::
|
|
||||||
end() const { return const_iterator(NULL, _sentinel); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::reverse_iterator BST::
|
|
||||||
rbegin() { return reverse_iterator(end()); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::const_reverse_iterator BST::
|
|
||||||
rbegin() const { return const_reverse_iterator(end()); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::reverse_iterator BST::
|
|
||||||
rend() { return reverse_iterator(begin()); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::const_reverse_iterator BST::
|
|
||||||
rend() const { return const_reverse_iterator(begin()); }
|
|
||||||
|
|
||||||
|
|
||||||
//////////////
|
|
||||||
// Capacity //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool BST::
|
|
||||||
empty() const { return (_size == 0); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::size_type BST::
|
|
||||||
size() const { return (_size); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::size_type BST::
|
|
||||||
max_size() const
|
|
||||||
{
|
|
||||||
return ( _allocator_node.max_size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////
|
|
||||||
// Modifiers //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
clear()
|
|
||||||
{
|
|
||||||
// TODO : optimisation jouable ?
|
|
||||||
erase(begin(), end());
|
|
||||||
//_size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
pair<typename BST::iterator, bool> BST::
|
|
||||||
insert(const value_type& value)
|
|
||||||
{
|
|
||||||
pair<typename BST::iterator, bool> ret;
|
|
||||||
|
|
||||||
ret = _insert(value);
|
|
||||||
if (ret.second == true)
|
|
||||||
_insert_rebalancing(ret.first.getNode()->up);
|
|
||||||
return (ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::iterator BST::
|
|
||||||
insert(iterator hint, const value_type& value)
|
|
||||||
{
|
|
||||||
// TODO : optimise with hint
|
|
||||||
(void)hint;
|
|
||||||
return insert(value).first;
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
template < typename InputIt >
|
|
||||||
void BST::
|
|
||||||
insert(InputIt first, InputIt last)
|
|
||||||
{
|
|
||||||
//static int i = 0; // Debug
|
|
||||||
while (first != last)
|
|
||||||
{
|
|
||||||
insert(*first);
|
|
||||||
++first;
|
|
||||||
//std::cout << "c|" << i << "\n";
|
|
||||||
//++i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
erase(iterator pos)
|
|
||||||
{
|
|
||||||
node<value_type>* delete_point;
|
|
||||||
delete_point = _erase(pos);
|
|
||||||
_erase_rebalancing(delete_point);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
erase(iterator first, iterator last)
|
|
||||||
{
|
|
||||||
while (first != last)
|
|
||||||
erase(first++);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::size_type BST::
|
|
||||||
erase(const Key& key)
|
|
||||||
{
|
|
||||||
iterator pos = find(key);
|
|
||||||
if (pos == end())
|
|
||||||
return (0);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
erase(pos);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
swap(map& other)
|
|
||||||
{
|
|
||||||
node<value_type>* tmp_root = _root;
|
|
||||||
node_sentinel<value_type>* tmp_sentinel = _sentinel;
|
|
||||||
size_type tmp_size = _size;
|
|
||||||
|
|
||||||
_root = other._root;
|
|
||||||
_sentinel = other._sentinel;
|
|
||||||
_size = other._size;
|
|
||||||
|
|
||||||
other._root = tmp_root;
|
|
||||||
other._sentinel = tmp_sentinel;
|
|
||||||
other._size = tmp_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////
|
|
||||||
// Lookup //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::iterator BST::
|
|
||||||
find(const Key& key)
|
|
||||||
{
|
|
||||||
node<value_type>* n = _root;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
if (_comp(key, n->value.first))
|
|
||||||
n = n->left;
|
|
||||||
else if (_comp(n->value.first, key))
|
|
||||||
n = n->right;
|
|
||||||
else
|
|
||||||
return (iterator(n, _sentinel));
|
|
||||||
}
|
|
||||||
return (end());
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::const_iterator BST::
|
|
||||||
find(const Key& key) const
|
|
||||||
{
|
|
||||||
node<value_type>* n = _root;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
if (_comp(key, n->value.first))
|
|
||||||
n = n->left;
|
|
||||||
else if (_comp(n->value.first, key))
|
|
||||||
n = n->right;
|
|
||||||
else
|
|
||||||
return (const_iterator(n, _sentinel));
|
|
||||||
}
|
|
||||||
return (end());
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
typename BST::size_type BST::
|
|
||||||
count(const Key& key) const
|
|
||||||
{
|
|
||||||
if (find(key) != end())
|
|
||||||
return (1);
|
|
||||||
else
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////
|
|
||||||
// Private functions //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
_init_sentinel()
|
|
||||||
{
|
|
||||||
_sentinel = _allocator_node_sentinel.allocate(1);
|
|
||||||
_allocator_node_sentinel.construct(_sentinel, node_sentinel<value_type>());
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
pair<typename BST::iterator, bool> BST::
|
|
||||||
_insert(const value_type& value)
|
|
||||||
{
|
|
||||||
node<value_type>* n = _root;
|
|
||||||
node<value_type>* prev = NULL;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
prev = n;
|
|
||||||
if (_comp(value.first, n->value.first))
|
|
||||||
n = n->left;
|
|
||||||
else if (_comp(n->value.first, value.first))
|
|
||||||
n = n->right;
|
|
||||||
else
|
|
||||||
return ft::make_pair(iterator(n, _sentinel), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = _allocator_node.allocate(1);
|
|
||||||
_allocator_node.construct(n, node<value_type>(value));
|
|
||||||
if (_root == NULL) // if (_size == 0)
|
|
||||||
{
|
|
||||||
_root = n;
|
|
||||||
_sentinel->child = _root;
|
|
||||||
}
|
|
||||||
else if (_comp(value.first, prev->value.first))
|
|
||||||
prev->left = n;
|
|
||||||
else
|
|
||||||
prev->right = n;
|
|
||||||
n->up = prev;
|
|
||||||
++_size;
|
|
||||||
return ft::make_pair(iterator(n, _sentinel), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
node<typename BST::value_type>* BST::
|
|
||||||
_erase(iterator pos)
|
|
||||||
{
|
|
||||||
node<value_type>* n = pos.getNode();
|
|
||||||
node<value_type>* delete_point = NULL;
|
|
||||||
|
|
||||||
if (n->left && n->right) // 2 child
|
|
||||||
{
|
|
||||||
node<value_type>* next = n->right->min();
|
|
||||||
|
|
||||||
if (next->up != n)
|
|
||||||
{
|
|
||||||
_subtree_shift(next, next->right);
|
|
||||||
next->right = n->right;
|
|
||||||
next->right->up = next;
|
|
||||||
}
|
|
||||||
delete_point = _subtree_shift(n, next);
|
|
||||||
next->left = n->left;
|
|
||||||
next->left->up = next;
|
|
||||||
}
|
|
||||||
else if (!n->left && !n->right) // no child (leaf)
|
|
||||||
delete_point = _subtree_shift(n, NULL); // bug ?
|
|
||||||
else if (n->left) // 1 child
|
|
||||||
delete_point = _subtree_shift(n, n->left);
|
|
||||||
else if (n->right) // 1 child
|
|
||||||
delete_point = _subtree_shift(n, n->right); // bug ?
|
|
||||||
|
|
||||||
_allocator_node.destroy(n);
|
|
||||||
_allocator_node.deallocate(n, 1);
|
|
||||||
--_size;
|
|
||||||
return (delete_point);
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
node<typename BST::value_type>* BST::
|
|
||||||
_subtree_shift(node<value_type>* st_old, node<value_type>* st_new)
|
|
||||||
{
|
|
||||||
node<value_type>* p = st_old->up;
|
|
||||||
|
|
||||||
if (st_old == _root)
|
|
||||||
{
|
|
||||||
_root = st_new;
|
|
||||||
_sentinel->child = _root;
|
|
||||||
}
|
|
||||||
else if (st_old == p->left)
|
|
||||||
p->left = st_new;
|
|
||||||
else
|
|
||||||
p->right = st_new;
|
|
||||||
|
|
||||||
if (st_new == NULL)
|
|
||||||
return (p); // return deletion point
|
|
||||||
st_new->up = p;
|
|
||||||
return (st_new); // return deletion point
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
_insert_rebalancing(node<value_type>* n)
|
|
||||||
{
|
|
||||||
node<value_type>* old_n;
|
|
||||||
node<value_type>* parent = NULL;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
n->height = _compute_height(n);
|
|
||||||
|
|
||||||
if (_bf(n) > 1) // Left Heavy
|
|
||||||
{
|
|
||||||
parent = n->up;
|
|
||||||
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
|
|
||||||
n->left = _rotate_left(n->left);
|
|
||||||
// Left-Left Case
|
|
||||||
n = _rotate_right(n);
|
|
||||||
old_n = n->right;
|
|
||||||
}
|
|
||||||
else if (_bf(n) < -1) // Right Heavy
|
|
||||||
{
|
|
||||||
parent = n->up;
|
|
||||||
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
|
|
||||||
n->right = _rotate_right(n->right);
|
|
||||||
// Right-Right Case
|
|
||||||
n = _rotate_left(n);
|
|
||||||
old_n = n->left;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parent)
|
|
||||||
{
|
|
||||||
if (parent->left == old_n)
|
|
||||||
parent->left = n;
|
|
||||||
else
|
|
||||||
parent->right = n;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
n = n->up;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
n->height = _compute_height(n);
|
|
||||||
n = n->up;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void BST::
|
|
||||||
_erase_rebalancing(node<value_type>* n)
|
|
||||||
{
|
|
||||||
node<value_type>* old_n;
|
|
||||||
node<value_type>* parent = NULL;
|
|
||||||
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
n->height = _compute_height(n);
|
|
||||||
|
|
||||||
if (_bf(n) > 1) // Left Heavy
|
|
||||||
{
|
|
||||||
parent = n->up;
|
|
||||||
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
|
|
||||||
n->left = _rotate_left(n->left);
|
|
||||||
// Left-Left Case
|
|
||||||
n = _rotate_right(n);
|
|
||||||
old_n = n->right;
|
|
||||||
}
|
|
||||||
else if (_bf(n) < -1) // Right Heavy
|
|
||||||
{
|
|
||||||
parent = n->up;
|
|
||||||
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
|
|
||||||
n->right = _rotate_right(n->right);
|
|
||||||
// Right-Right Case
|
|
||||||
n = _rotate_left(n);
|
|
||||||
old_n = n->left;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parent)
|
|
||||||
{
|
|
||||||
if (parent->left == old_n)
|
|
||||||
parent->left = n;
|
|
||||||
else
|
|
||||||
parent->right = n;
|
|
||||||
parent = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
n = n->up;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
short BST::
|
|
||||||
_compute_height(node<value_type>* n)
|
|
||||||
{
|
|
||||||
if (n->left && n->right)
|
|
||||||
return std::max(n->left->height, n->right->height) + 1;
|
|
||||||
else if (n->left)
|
|
||||||
return n->left->height + 1;
|
|
||||||
else if (n->right)
|
|
||||||
return n->right->height + 1;
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
short BST::
|
|
||||||
_bf(node<value_type>* n) // optimisation possible if assume n have at least one child ?
|
|
||||||
{
|
|
||||||
if (n->left && n->right)
|
|
||||||
return n->left->height - n->right->height;
|
|
||||||
else if (n->left)
|
|
||||||
return n->left->height;
|
|
||||||
else if (n->right)
|
|
||||||
return (-(n->right->height));
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
node<typename BST::value_type>* BST::
|
|
||||||
_rotate_left(node<value_type>* n) // assume n->right != NULL
|
|
||||||
{
|
|
||||||
node<value_type>* ori_right = n->right;
|
|
||||||
|
|
||||||
ori_right->up = n->up;
|
|
||||||
n->up = ori_right;
|
|
||||||
|
|
||||||
n->right = ori_right->left;
|
|
||||||
if (n->right != NULL)
|
|
||||||
n->right->up = n;
|
|
||||||
ori_right->left = n;
|
|
||||||
|
|
||||||
n->height = _compute_height(n);
|
|
||||||
ori_right->height = _compute_height(ori_right);
|
|
||||||
|
|
||||||
if (n == _root)
|
|
||||||
{
|
|
||||||
_root = ori_right;
|
|
||||||
_sentinel->child = _root;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ori_right; // return new sub-tree root
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
node<typename BST::value_type>* BST::
|
|
||||||
_rotate_right(node<value_type>* n) // assume n->left != NULL
|
|
||||||
{
|
|
||||||
node<value_type>* ori_left = n->left;
|
|
||||||
|
|
||||||
ori_left->up = n->up;
|
|
||||||
n->up = ori_left;
|
|
||||||
|
|
||||||
n->left = ori_left->right;
|
|
||||||
if (n->left != NULL)
|
|
||||||
n->left->up = n;
|
|
||||||
ori_left->right = n;
|
|
||||||
|
|
||||||
n->height = _compute_height(n);
|
|
||||||
ori_left->height = _compute_height(ori_left);
|
|
||||||
|
|
||||||
if (n == _root)
|
|
||||||
{
|
|
||||||
_root = ori_left;
|
|
||||||
_sentinel->child = _root;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ori_left; // return new sub-tree root
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////
|
|
||||||
// Non-member functions //
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator==(const BST& lhs, const BST& rhs)
|
|
||||||
{
|
|
||||||
if (lhs.size() != rhs.size())
|
|
||||||
return false;
|
|
||||||
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator!=(const BST& lhs, const BST& rhs)
|
|
||||||
{ return !(lhs == rhs); }
|
|
||||||
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator<(const BST& lhs, const BST& rhs)
|
|
||||||
{
|
|
||||||
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator>(const BST& lhs, const BST& rhs)
|
|
||||||
{ return (rhs < lhs); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator<=(const BST& lhs, const BST& rhs)
|
|
||||||
{ return !(lhs > rhs); }
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
bool operator>=(const BST& lhs, const BST& rhs)
|
|
||||||
{ return !(lhs < rhs); }
|
|
||||||
|
|
||||||
|
|
||||||
BST_TEMPLATE
|
|
||||||
void swap(BST& lhs, BST& rhs)
|
|
||||||
{ lhs.swap(rhs); }
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace ft
|
|
||||||
|
|
||||||
#undef BST
|
|
||||||
#undef BST_TEMPLATE
|
|
||||||
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
|
|
||||||
|
#define MP_TPL template < typename Key, typename T, typename Compare, typename Allocator >
|
||||||
#define MP_TPL template < typename Key, typename T, typename Compare, typename Alloc >
|
#define MP map<Key, T, Compare, Allocator>
|
||||||
#define MP map<Key, T, Compare, Alloc>
|
|
||||||
|
|
||||||
|
|
||||||
namespace ft {
|
namespace ft {
|
||||||
|
|
||||||
@@ -13,38 +11,49 @@ namespace ft {
|
|||||||
// constructors ------------------------------
|
// constructors ------------------------------
|
||||||
MP_TPL MP::
|
MP_TPL MP::
|
||||||
map (const key_compare & comp, const allocator_type & alloc)
|
map (const key_compare & comp, const allocator_type & alloc)
|
||||||
: _bst()
|
: _size(0)
|
||||||
, _allocator(alloc)
|
, _root(NULL)
|
||||||
, _comp(comp) {
|
, _comp(comp)
|
||||||
|
, _allocator(alloc) {
|
||||||
|
|
||||||
return;
|
_init_sentinel();
|
||||||
}
|
}
|
||||||
MP_TPL template <class InputIt> MP::
|
MP_TPL template < typename InputIt > MP::
|
||||||
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
|
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
|
||||||
: _bst(first, last)
|
: _size(0)
|
||||||
, _allocator(alloc)
|
, _root(NULL)
|
||||||
, _comp(comp) {
|
, _comp(comp)
|
||||||
|
, _allocator(alloc) {
|
||||||
|
|
||||||
|
_init_sentinel();
|
||||||
|
insert(first, last);
|
||||||
}
|
}
|
||||||
MP_TPL MP::
|
MP_TPL MP::
|
||||||
map (const map& x)
|
map(const map& src)
|
||||||
: _bst()
|
: _size(0)
|
||||||
, _allocator(x._allocator)
|
, _root(NULL)
|
||||||
, _comp(x._comp) {
|
, _comp(src._comp)
|
||||||
|
, _allocator(src._allocator) {
|
||||||
|
|
||||||
*this = x;
|
_init_sentinel();
|
||||||
|
*this = src;
|
||||||
}
|
}
|
||||||
// destructor --------------------------------
|
// destructor --------------------------------
|
||||||
MP_TPL MP::
|
MP_TPL MP::
|
||||||
~map() { clear(); }
|
~map() {
|
||||||
|
|
||||||
|
clear();
|
||||||
|
_allocator_node_sentinel.destroy(_sentinel);
|
||||||
|
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
||||||
|
}
|
||||||
// operator= ---------------------------------
|
// operator= ---------------------------------
|
||||||
MP_TPL MP& MP::
|
MP_TPL MP& MP::
|
||||||
operator= (const map& x) {
|
operator=(const map& rhs) {
|
||||||
|
|
||||||
if (this == &x)
|
if (this == &rhs)
|
||||||
return (*this);
|
return (*this);
|
||||||
|
map new_bst(rhs.begin(), rhs.end());
|
||||||
map new_map(x.begin(), x.end());
|
swap(new_bst);
|
||||||
swap(new_map);
|
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,24 +63,36 @@ MP_TPL MP& MP::
|
|||||||
*************/
|
*************/
|
||||||
// begin -------------------------------------
|
// begin -------------------------------------
|
||||||
MP_TPL typename MP::iterator MP::
|
MP_TPL typename MP::iterator MP::
|
||||||
begin() { return (_bst.begin()); }
|
begin() {
|
||||||
|
|
||||||
|
if (_root)
|
||||||
|
return iterator(_root->min(), _sentinel);
|
||||||
|
else
|
||||||
|
return end();
|
||||||
|
}
|
||||||
MP_TPL typename MP::const_iterator MP::
|
MP_TPL typename MP::const_iterator MP::
|
||||||
begin() const { return (_bst.begin()); }
|
begin() const {
|
||||||
|
|
||||||
|
if (_root)
|
||||||
|
return const_iterator(_root->min(), _sentinel);
|
||||||
|
else
|
||||||
|
return end();
|
||||||
|
}
|
||||||
// end ---------------------------------------
|
// end ---------------------------------------
|
||||||
MP_TPL typename MP::iterator MP::
|
MP_TPL typename MP::iterator MP::
|
||||||
end() { return (_bst.end()); }
|
end() { return iterator(NULL, _sentinel); }
|
||||||
MP_TPL typename MP::const_iterator MP::
|
MP_TPL typename MP::const_iterator MP::
|
||||||
end() const { return (_bst.end()); }
|
end() const { return const_iterator(NULL, _sentinel); }
|
||||||
// rbegin ------------------------------------
|
// rbegin ------------------------------------
|
||||||
MP_TPL typename MP::reverse_iterator MP::
|
MP_TPL typename MP::reverse_iterator MP::
|
||||||
rbegin() { return (_bst.rbegin()); }
|
rbegin() { return reverse_iterator(end()); }
|
||||||
MP_TPL typename MP::const_reverse_iterator MP::
|
MP_TPL typename MP::const_reverse_iterator MP::
|
||||||
rbegin() const { return (_bst.rbegin()); }
|
rbegin() const { return const_reverse_iterator(end()); }
|
||||||
// rend --------------------------------------
|
// rend --------------------------------------
|
||||||
MP_TPL typename MP::reverse_iterator MP::
|
MP_TPL typename MP::reverse_iterator MP::
|
||||||
rend() { return (_bst.rend()); }
|
rend() { return reverse_iterator(begin()); }
|
||||||
MP_TPL typename MP::const_reverse_iterator MP::
|
MP_TPL typename MP::const_reverse_iterator MP::
|
||||||
rend() const { return (_bst.rend()); }
|
rend() const { return const_reverse_iterator(begin()); }
|
||||||
|
|
||||||
|
|
||||||
/************
|
/************
|
||||||
@@ -79,13 +100,13 @@ MP_TPL typename MP::const_reverse_iterator MP::
|
|||||||
************/
|
************/
|
||||||
// empty -------------------------------------
|
// empty -------------------------------------
|
||||||
MP_TPL bool MP::
|
MP_TPL bool MP::
|
||||||
empty() const { return (_bst.empty()); }
|
empty() const { return (_size == 0); }
|
||||||
// size --------------------------------------
|
// size --------------------------------------
|
||||||
MP_TPL typename MP::size_type MP::
|
MP_TPL typename MP::size_type MP::
|
||||||
size() const { return (_bst.size()); }
|
size() const { return (_size); }
|
||||||
// max_size ----------------------------------
|
// max_size ----------------------------------
|
||||||
MP_TPL typename MP::size_type MP::
|
MP_TPL typename MP::size_type MP::
|
||||||
max_size() const { return (_bst.max_size()); }
|
max_size() const { return ( _allocator_node.max_size() ); }
|
||||||
|
|
||||||
|
|
||||||
/******************
|
/******************
|
||||||
@@ -93,7 +114,24 @@ MP_TPL typename MP::size_type MP::
|
|||||||
******************/
|
******************/
|
||||||
// operator[] --------------------------------
|
// operator[] --------------------------------
|
||||||
MP_TPL typename MP::mapped_type& MP::
|
MP_TPL typename MP::mapped_type& MP::
|
||||||
operator[] (const key_type& k) { return _bst[k]; }
|
operator[](const Key& key) {
|
||||||
|
|
||||||
|
node<value_type>* n = _root;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
if (_comp(key, n->value.first))
|
||||||
|
n = n->left;
|
||||||
|
else if (_comp(n->value.first, key))
|
||||||
|
n = n->right;
|
||||||
|
else
|
||||||
|
return (n->value.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
n = insert( ft::make_pair(key, mapped_type()) ).first.getNode();
|
||||||
|
|
||||||
|
return (n->value.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*************
|
/*************
|
||||||
@@ -101,35 +139,75 @@ MP_TPL typename MP::mapped_type& MP::
|
|||||||
*************/
|
*************/
|
||||||
// insert ------------------------------------
|
// insert ------------------------------------
|
||||||
MP_TPL pair<typename MP::iterator, bool> MP::
|
MP_TPL pair<typename MP::iterator, bool> MP::
|
||||||
insert (const value_type& val) { return (_bst.insert(val)); }
|
insert(const value_type& value) {
|
||||||
|
|
||||||
|
pair<typename MP::iterator, bool> ret;
|
||||||
|
|
||||||
|
ret = _insert(value);
|
||||||
|
if (ret.second == true)
|
||||||
|
_insert_rebalancing(ret.first.getNode()->up);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
MP_TPL typename MP::iterator MP::
|
MP_TPL typename MP::iterator MP::
|
||||||
insert (iterator pos, const value_type& val) { return (_bst.insert(pos, val)); }
|
insert(iterator hint, const value_type& value) {
|
||||||
MP_TPL template <class InputIt> void MP::
|
|
||||||
insert (InputIt first, InputIt last) { return (_bst.insert(first, last)); }
|
(void)hint;
|
||||||
|
return insert(value).first;
|
||||||
|
}
|
||||||
|
MP_TPL template < typename InputIt > void MP::
|
||||||
|
insert(InputIt first, InputIt last) {
|
||||||
|
|
||||||
|
while (first != last)
|
||||||
|
{
|
||||||
|
insert(*first);
|
||||||
|
++first;
|
||||||
|
}
|
||||||
|
}
|
||||||
// erase -------------------------------------
|
// erase -------------------------------------
|
||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
erase (iterator pos) { return (_bst.erase(pos)); }
|
erase(iterator pos) {
|
||||||
MP_TPL typename MP::size_type MP::
|
|
||||||
erase (const key_type& k) { return (_bst.erase(k)); }
|
node<value_type>* delete_point;
|
||||||
|
delete_point = _erase(pos);
|
||||||
|
_erase_rebalancing(delete_point);
|
||||||
|
}
|
||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
erase (iterator first, iterator last) { return (_bst.erase(first, last)); }
|
erase(iterator first, iterator last) {
|
||||||
|
|
||||||
|
while (first != last)
|
||||||
|
erase(first++);
|
||||||
|
}
|
||||||
|
MP_TPL typename MP::size_type MP::
|
||||||
|
erase(const Key& key) {
|
||||||
|
|
||||||
|
iterator pos = find(key);
|
||||||
|
if (pos == end())
|
||||||
|
return (0);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
erase(pos);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
// swap --------------------------------------
|
// swap --------------------------------------
|
||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
swap (map& x) {
|
swap(map& other) {
|
||||||
|
|
||||||
bst_map tmp;
|
node<value_type>* tmp_root = _root;
|
||||||
|
node_sentinel<value_type>* tmp_sentinel = _sentinel;
|
||||||
|
size_type tmp_size = _size;
|
||||||
|
|
||||||
tmp.swap(_bst);
|
_root = other._root;
|
||||||
_bst.swap(x._bst);
|
_sentinel = other._sentinel;
|
||||||
x._bst.swap(tmp);
|
_size = other._size;
|
||||||
|
|
||||||
|
other._root = tmp_root;
|
||||||
|
other._sentinel = tmp_sentinel;
|
||||||
|
other._size = tmp_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear -------------------------------------
|
// clear -------------------------------------
|
||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
clear() {
|
clear() { erase(begin(), end()); }
|
||||||
|
|
||||||
_bst.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*************
|
/*************
|
||||||
@@ -148,12 +226,46 @@ MP_TPL typename MP::value_compare MP::
|
|||||||
**************/
|
**************/
|
||||||
// find --------------------------------------
|
// find --------------------------------------
|
||||||
MP_TPL typename MP::iterator MP::
|
MP_TPL typename MP::iterator MP::
|
||||||
find (const key_type& k) { return (_bst.find(k)); }
|
find(const Key& key) {
|
||||||
|
|
||||||
|
node<value_type>* n = _root;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
if (_comp(key, n->value.first))
|
||||||
|
n = n->left;
|
||||||
|
else if (_comp(n->value.first, key))
|
||||||
|
n = n->right;
|
||||||
|
else
|
||||||
|
return (iterator(n, _sentinel));
|
||||||
|
}
|
||||||
|
return (end());
|
||||||
|
}
|
||||||
MP_TPL typename MP::const_iterator MP::
|
MP_TPL typename MP::const_iterator MP::
|
||||||
find (const key_type& k) const { return (_bst.find(k)); }
|
find(const Key& key) const {
|
||||||
|
|
||||||
|
node<value_type>* n = _root;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
if (_comp(key, n->value.first))
|
||||||
|
n = n->left;
|
||||||
|
else if (_comp(n->value.first, key))
|
||||||
|
n = n->right;
|
||||||
|
else
|
||||||
|
return (const_iterator(n, _sentinel));
|
||||||
|
}
|
||||||
|
return (end());
|
||||||
|
}
|
||||||
// count -------------------------------------
|
// count -------------------------------------
|
||||||
MP_TPL typename MP::size_type MP::
|
MP_TPL typename MP::size_type MP::
|
||||||
count (const key_type& k) const { return (_bst.count(k)); }
|
count(const Key& key) const {
|
||||||
|
|
||||||
|
if (find(key) != end())
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
// lower_bound -------------------------------
|
// lower_bound -------------------------------
|
||||||
MP_TPL typename MP::iterator MP::
|
MP_TPL typename MP::iterator MP::
|
||||||
lower_bound (const key_type& k) {
|
lower_bound (const key_type& k) {
|
||||||
@@ -233,39 +345,300 @@ MP_TPL typename MP::allocator_type MP::
|
|||||||
get_allocator() const { return (_allocator); }
|
get_allocator() const { return (_allocator); }
|
||||||
|
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* private functions :
|
||||||
|
*********************/
|
||||||
|
MP_TPL void MP::
|
||||||
|
_init_sentinel() {
|
||||||
|
|
||||||
|
_sentinel = _allocator_node_sentinel.allocate(1);
|
||||||
|
_allocator_node_sentinel.construct(_sentinel, node_sentinel<value_type>());
|
||||||
|
}
|
||||||
|
MP_TPL pair<typename MP::iterator, bool> MP::
|
||||||
|
_insert(const value_type& value) {
|
||||||
|
|
||||||
|
node<value_type>* n = _root;
|
||||||
|
node<value_type>* prev = NULL;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
prev = n;
|
||||||
|
if (_comp(value.first, n->value.first))
|
||||||
|
n = n->left;
|
||||||
|
else if (_comp(n->value.first, value.first))
|
||||||
|
n = n->right;
|
||||||
|
else
|
||||||
|
return ft::make_pair(iterator(n, _sentinel), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
n = _allocator_node.allocate(1);
|
||||||
|
_allocator_node.construct(n, node<value_type>(value));
|
||||||
|
if (_root == NULL)
|
||||||
|
{
|
||||||
|
_root = n;
|
||||||
|
_sentinel->child = _root;
|
||||||
|
}
|
||||||
|
else if (_comp(value.first, prev->value.first))
|
||||||
|
prev->left = n;
|
||||||
|
else
|
||||||
|
prev->right = n;
|
||||||
|
n->up = prev;
|
||||||
|
++_size;
|
||||||
|
return ft::make_pair(iterator(n, _sentinel), true);
|
||||||
|
}
|
||||||
|
MP_TPL node<typename MP::value_type>* MP::
|
||||||
|
_erase(iterator pos) {
|
||||||
|
|
||||||
|
node<value_type>* n = pos.getNode();
|
||||||
|
node<value_type>* delete_point = NULL;
|
||||||
|
|
||||||
|
if (n->left && n->right)
|
||||||
|
{
|
||||||
|
node<value_type>* next = n->right->min();
|
||||||
|
|
||||||
|
if (next->up != n)
|
||||||
|
{
|
||||||
|
_subtree_shift(next, next->right);
|
||||||
|
next->right = n->right;
|
||||||
|
next->right->up = next;
|
||||||
|
}
|
||||||
|
delete_point = _subtree_shift(n, next);
|
||||||
|
next->left = n->left;
|
||||||
|
next->left->up = next;
|
||||||
|
}
|
||||||
|
else if (!n->left && !n->right)
|
||||||
|
delete_point = _subtree_shift(n, NULL);
|
||||||
|
else if (n->left)
|
||||||
|
delete_point = _subtree_shift(n, n->left);
|
||||||
|
else if (n->right)
|
||||||
|
delete_point = _subtree_shift(n, n->right);
|
||||||
|
|
||||||
|
_allocator_node.destroy(n);
|
||||||
|
_allocator_node.deallocate(n, 1);
|
||||||
|
--_size;
|
||||||
|
return (delete_point);
|
||||||
|
}
|
||||||
|
MP_TPL node<typename MP::value_type>* MP::
|
||||||
|
_subtree_shift(node<value_type>* st_old, node<value_type>* st_new) {
|
||||||
|
|
||||||
|
node<value_type>* p = st_old->up;
|
||||||
|
|
||||||
|
if (st_old == _root)
|
||||||
|
{
|
||||||
|
_root = st_new;
|
||||||
|
_sentinel->child = _root;
|
||||||
|
}
|
||||||
|
else if (st_old == p->left)
|
||||||
|
p->left = st_new;
|
||||||
|
else
|
||||||
|
p->right = st_new;
|
||||||
|
|
||||||
|
if (st_new == NULL)
|
||||||
|
return (p);
|
||||||
|
st_new->up = p;
|
||||||
|
return (st_new);
|
||||||
|
}
|
||||||
|
MP_TPL void MP::
|
||||||
|
_insert_rebalancing(node<value_type>* n) {
|
||||||
|
|
||||||
|
node<value_type>* old_n;
|
||||||
|
node<value_type>* parent = NULL;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
n->height = _compute_height(n);
|
||||||
|
|
||||||
|
if (_balance_factor(n) > 1) // Left Heavy
|
||||||
|
{
|
||||||
|
parent = n->up;
|
||||||
|
if (_balance_factor(n->left) < 0) // Left-Right Case
|
||||||
|
n->left = _rotate_left(n->left);
|
||||||
|
// Left-Left Case
|
||||||
|
n = _rotate_right(n);
|
||||||
|
old_n = n->right;
|
||||||
|
}
|
||||||
|
else if (_balance_factor(n) < -1) // Right Heavy
|
||||||
|
{
|
||||||
|
parent = n->up;
|
||||||
|
if (_balance_factor(n->right) > 0) // Right-Left Case
|
||||||
|
n->right = _rotate_right(n->right);
|
||||||
|
// Right-Right Case
|
||||||
|
n = _rotate_left(n);
|
||||||
|
old_n = n->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent)
|
||||||
|
{
|
||||||
|
if (parent->left == old_n)
|
||||||
|
parent->left = n;
|
||||||
|
else
|
||||||
|
parent->right = n;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = n->up;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
n->height = _compute_height(n);
|
||||||
|
n = n->up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MP_TPL void MP::
|
||||||
|
_erase_rebalancing(node<value_type>* n) {
|
||||||
|
|
||||||
|
node<value_type>* old_n;
|
||||||
|
node<value_type>* parent = NULL;
|
||||||
|
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
n->height = _compute_height(n);
|
||||||
|
|
||||||
|
if (_balance_factor(n) > 1) // Left Heavy
|
||||||
|
{
|
||||||
|
parent = n->up;
|
||||||
|
if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1)
|
||||||
|
n->left = _rotate_left(n->left);
|
||||||
|
// Left-Left Case
|
||||||
|
n = _rotate_right(n);
|
||||||
|
old_n = n->right;
|
||||||
|
}
|
||||||
|
else if (_balance_factor(n) < -1) // Right Heavy
|
||||||
|
{
|
||||||
|
parent = n->up;
|
||||||
|
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
|
||||||
|
n->right = _rotate_right(n->right);
|
||||||
|
// Right-Right Case
|
||||||
|
n = _rotate_left(n);
|
||||||
|
old_n = n->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent)
|
||||||
|
{
|
||||||
|
if (parent->left == old_n)
|
||||||
|
parent->left = n;
|
||||||
|
else
|
||||||
|
parent->right = n;
|
||||||
|
parent = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = n->up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MP_TPL short MP::
|
||||||
|
_compute_height(node<value_type>* n) {
|
||||||
|
|
||||||
|
if (n->left && n->right)
|
||||||
|
return std::max(n->left->height, n->right->height) + 1;
|
||||||
|
else if (n->left)
|
||||||
|
return n->left->height + 1;
|
||||||
|
else if (n->right)
|
||||||
|
return n->right->height + 1;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
MP_TPL short MP::
|
||||||
|
_balance_factor(node<value_type>* n) {
|
||||||
|
|
||||||
|
if (n->left && n->right)
|
||||||
|
return n->left->height - n->right->height;
|
||||||
|
else if (n->left)
|
||||||
|
return n->left->height;
|
||||||
|
else if (n->right)
|
||||||
|
return (-(n->right->height));
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
MP_TPL node<typename MP::value_type>* MP::
|
||||||
|
_rotate_left(node<value_type>* n) {
|
||||||
|
|
||||||
|
node<value_type>* ori_right = n->right;
|
||||||
|
|
||||||
|
ori_right->up = n->up;
|
||||||
|
n->up = ori_right;
|
||||||
|
|
||||||
|
n->right = ori_right->left;
|
||||||
|
if (n->right != NULL)
|
||||||
|
n->right->up = n;
|
||||||
|
ori_right->left = n;
|
||||||
|
|
||||||
|
n->height = _compute_height(n);
|
||||||
|
ori_right->height = _compute_height(ori_right);
|
||||||
|
|
||||||
|
if (n == _root)
|
||||||
|
{
|
||||||
|
_root = ori_right;
|
||||||
|
_sentinel->child = _root;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ori_right;
|
||||||
|
}
|
||||||
|
MP_TPL node<typename MP::value_type>* MP::
|
||||||
|
_rotate_right(node<value_type>* n) {
|
||||||
|
|
||||||
|
node<value_type>* ori_left = n->left;
|
||||||
|
|
||||||
|
ori_left->up = n->up;
|
||||||
|
n->up = ori_left;
|
||||||
|
|
||||||
|
n->left = ori_left->right;
|
||||||
|
if (n->left != NULL)
|
||||||
|
n->left->up = n;
|
||||||
|
ori_left->right = n;
|
||||||
|
|
||||||
|
n->height = _compute_height(n);
|
||||||
|
ori_left->height = _compute_height(ori_left);
|
||||||
|
|
||||||
|
if (n == _root)
|
||||||
|
{
|
||||||
|
_root = ori_left;
|
||||||
|
_sentinel->child = _root;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ori_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/************************
|
/************************
|
||||||
* non-member functions :
|
* non-member functions :
|
||||||
************************/
|
************************/
|
||||||
// operator == -------------------------------
|
// operator == -------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator==
|
MP_TPL bool operator== (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) {
|
|
||||||
|
|
||||||
return (lhs._bst == rhs._bst);
|
if (lhs.size() != rhs.size())
|
||||||
|
return false;
|
||||||
|
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||||
}
|
}
|
||||||
// operator < --------------------------------
|
// operator < --------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator<
|
MP_TPL bool operator< (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) {
|
|
||||||
|
|
||||||
return (lhs._bst < rhs._bst);
|
return ft::lexicographical_compare(
|
||||||
|
lhs.begin(),
|
||||||
|
lhs.end(),
|
||||||
|
rhs.begin(),
|
||||||
|
rhs.end()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// operator != -------------------------------
|
// operator != -------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator!=
|
MP_TPL bool operator!= (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) { return !(lhs == rhs); }
|
return !(lhs == rhs); }
|
||||||
// operator <= -------------------------------
|
// operator <= -------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator<=
|
MP_TPL bool operator<= (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) { return !(lhs > rhs); }
|
return !(lhs > rhs); }
|
||||||
// operator > --------------------------------
|
// operator > --------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator>
|
MP_TPL bool operator> (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) { return (rhs < lhs); }
|
return (rhs < lhs); }
|
||||||
// operator >= -------------------------------
|
// operator >= -------------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > bool operator>=
|
MP_TPL bool operator>= (const MP& lhs, const MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) { return !(lhs < rhs); }
|
return !(lhs < rhs); }
|
||||||
// swap (map) -----------------------------
|
// swap (map) -----------------------------
|
||||||
template< class Key, class T, class Compare, class Alloc > void swap
|
MP_TPL void swap(MP& lhs, MP& rhs) {
|
||||||
( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }
|
lhs.swap(rhs); }
|
||||||
|
|
||||||
|
|
||||||
} // namespace ft
|
} // namespace ft
|
||||||
|
|
||||||
#undef VT
|
#undef MP
|
||||||
#undef VT_TPL
|
#undef MP_TPL
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,8 @@ void tests_map_equal_range();
|
|||||||
void tests_map_get_allocator();
|
void tests_map_get_allocator();
|
||||||
void tests_map_relational_operators();
|
void tests_map_relational_operators();
|
||||||
void tests_map_swap_non_member();
|
void tests_map_swap_non_member();
|
||||||
|
// stack
|
||||||
|
void tests_stack_constructor();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
22
tests/includes/tests_mutant_stack.hpp
Normal file
22
tests/includes/tests_mutant_stack.hpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#ifndef TESTS_MUTANT_STACK_HPP
|
||||||
|
# define TESTS_MUTANT_STACK_HPP
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class MutantStack : public std::stack<T> {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef typename std::stack<T>::container_type::iterator iterator;
|
||||||
|
typedef typename std::stack<T>::container_type::const_iterator const_iterator;
|
||||||
|
|
||||||
|
iterator begin() {return this->c.begin();}
|
||||||
|
iterator end() {return this->c.end();}
|
||||||
|
const_iterator begin() const {return this->c.begin();}
|
||||||
|
const_iterator end() const {return this->c.end();}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -10,17 +10,20 @@
|
|||||||
#include <iterator> // std::reverse_iterator
|
#include <iterator> // std::reverse_iterator
|
||||||
#include <utility> // std::make_pair
|
#include <utility> // std::make_pair
|
||||||
#include <sstream> // std::stringstream
|
#include <sstream> // std::stringstream
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
|
||||||
// toogle between test ft and stl
|
// toogle between test ft and stl
|
||||||
// *************************
|
// *************************
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
#ifdef STL
|
#ifdef STL
|
||||||
namespace ft = std;
|
namespace ft = std;
|
||||||
#else
|
#else
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
|
#include "stack.hpp"
|
||||||
#include "reverse_iterator.hpp"
|
#include "reverse_iterator.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -66,15 +69,18 @@ extern std::vector< mystruct* > mem_list;
|
|||||||
|
|
||||||
// adding each test to the list
|
// adding each test to the list
|
||||||
// ***************************
|
// ***************************
|
||||||
|
#define TEST(f_name) TEST_V(f_name)
|
||||||
|
/*
|
||||||
|
add_to_list(#f_name, "char", new(s_ ## f_name <char>));\
|
||||||
|
add_to_list(#f_name, "std::string", new(s_ ## f_name <std::string>));\
|
||||||
|
add_to_list(#f_name, "mystruct*", new(s_ ## f_name <mystruct*>));\
|
||||||
|
*/
|
||||||
#define TEST_V(f_name) \
|
#define TEST_V(f_name) \
|
||||||
template <class T> struct s_ ## f_name : public A_test\
|
template <class T> struct s_ ## f_name : public A_test\
|
||||||
{ void func(); };\
|
{ void func(); };\
|
||||||
void f_name () {\
|
void f_name () {\
|
||||||
add_to_list("", "", NULL);\
|
add_to_list("", "", NULL);\
|
||||||
add_to_list(#f_name, "int", new(s_ ## f_name <int>));\
|
add_to_list(#f_name, "int", new(s_ ## f_name <int>));\
|
||||||
add_to_list(#f_name, "char", new(s_ ## f_name <char>));\
|
|
||||||
add_to_list(#f_name, "std::string", new(s_ ## f_name <std::string>));\
|
|
||||||
add_to_list(#f_name, "mystruct*", new(s_ ## f_name <mystruct*>));\
|
|
||||||
}\
|
}\
|
||||||
template <class T>\
|
template <class T>\
|
||||||
void s_ ## f_name <T>::func()
|
void s_ ## f_name <T>::func()
|
||||||
@@ -84,13 +90,6 @@ extern std::vector< mystruct* > mem_list;
|
|||||||
{ void func(); };\
|
{ void func(); };\
|
||||||
void f_name () {\
|
void f_name () {\
|
||||||
add_to_list("", "", NULL);\
|
add_to_list("", "", NULL);\
|
||||||
add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name <char, mystruct*>));\
|
|
||||||
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
|
|
||||||
}\
|
|
||||||
template <class T, class U>\
|
|
||||||
void s_ ## f_name <T, U>::func()
|
|
||||||
|
|
||||||
/*
|
|
||||||
add_to_list(#f_name, "char, int", new(s_ ## f_name <char, int>));\
|
add_to_list(#f_name, "char, int", new(s_ ## f_name <char, int>));\
|
||||||
add_to_list(#f_name, "char, char", new(s_ ## f_name <char, char>));\
|
add_to_list(#f_name, "char, char", new(s_ ## f_name <char, char>));\
|
||||||
add_to_list(#f_name, "char, std::string", new(s_ ## f_name <char, std::string>));\
|
add_to_list(#f_name, "char, std::string", new(s_ ## f_name <char, std::string>));\
|
||||||
@@ -99,12 +98,17 @@ extern std::vector< mystruct* > mem_list;
|
|||||||
add_to_list(#f_name, "int, char", new(s_ ## f_name <int, char>));\
|
add_to_list(#f_name, "int, char", new(s_ ## f_name <int, char>));\
|
||||||
add_to_list(#f_name, "int, std::string", new(s_ ## f_name <int, std::string>));\
|
add_to_list(#f_name, "int, std::string", new(s_ ## f_name <int, std::string>));\
|
||||||
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
|
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
|
||||||
|
}\
|
||||||
|
template <class T, class U>\
|
||||||
|
void s_ ## f_name <T, U>::func()
|
||||||
|
|
||||||
|
/*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// templates print
|
// templates print
|
||||||
// *****************************************
|
// *****************************************
|
||||||
template <class T>
|
template <class T>
|
||||||
void print(ft::vector<T> vec, std::string name) {
|
void print(ft::vector<T>& vec, std::string name) {
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
typename ft::vector<T>::iterator it;
|
typename ft::vector<T>::iterator it;
|
||||||
@@ -116,7 +120,7 @@ template <class T>
|
|||||||
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
|
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
|
||||||
}
|
}
|
||||||
template <class T, class U>
|
template <class T, class U>
|
||||||
void print(ft::map<T, U> mp, std::string name) {
|
void print(ft::map<T, U>& mp, std::string name) {
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
typename ft::map<T, U>::iterator it;
|
typename ft::map<T, U>::iterator it;
|
||||||
@@ -127,6 +131,14 @@ template <class T, class U>
|
|||||||
std::cout << "[" << i << "]" << it->first << ":" << it->second << " ";
|
std::cout << "[" << i << "]" << it->first << ":" << it->second << " ";
|
||||||
std::cout << "\nsize:" << mp.size() << "\n";
|
std::cout << "\nsize:" << mp.size() << "\n";
|
||||||
}
|
}
|
||||||
|
template <class T, class cont>
|
||||||
|
void print(ft::stack<T,cont>& st, std::string name) {
|
||||||
|
|
||||||
|
std::cout << "\n" << name << ":(map)\n";
|
||||||
|
for (int i = st.size(); i > 0 ; i--, st.pop())
|
||||||
|
std::cout << "[" << i << "]" << st.top() << " ";
|
||||||
|
std::cout << "\nsize:" << st.size() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
// templates get value
|
// templates get value
|
||||||
// *************************************
|
// *************************************
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
// VECTOR
|
// VECTOR
|
||||||
@@ -34,35 +33,35 @@ int main() {
|
|||||||
// tests_vector_reverse_iterators();
|
// tests_vector_reverse_iterators();
|
||||||
|
|
||||||
// MAP
|
// MAP
|
||||||
tests_map_simple();
|
// tests_map_simple();
|
||||||
tests_map_constructor();
|
// tests_map_constructor();
|
||||||
tests_map_operator_assignation();
|
// tests_map_operator_assignation();
|
||||||
tests_map_begin();
|
// tests_map_begin();
|
||||||
tests_map_end();
|
// tests_map_end();
|
||||||
tests_map_rbegin();
|
// tests_map_rbegin();
|
||||||
tests_map_rend();
|
// tests_map_rend();
|
||||||
tests_map_empty();
|
// tests_map_empty();
|
||||||
tests_map_size();
|
// tests_map_size();
|
||||||
tests_map_max_size();
|
// tests_map_max_size();
|
||||||
tests_map_operator_access();
|
// tests_map_operator_access();
|
||||||
tests_map_insert();
|
// tests_map_insert();
|
||||||
tests_map_erase();
|
// tests_map_erase();
|
||||||
tests_map_swap();
|
// tests_map_swap();
|
||||||
tests_map_clear();
|
// tests_map_clear();
|
||||||
// tests_map_key_comp();
|
// tests_map_key_comp();
|
||||||
// tests_map_value_comp();
|
// tests_map_value_comp();
|
||||||
tests_map_find();
|
// tests_map_find();
|
||||||
tests_map_count();
|
// tests_map_count();
|
||||||
// tests_map_lower_bound();
|
// tests_map_lower_bound();
|
||||||
// tests_map_upper_bound();
|
// tests_map_upper_bound();
|
||||||
// tests_map_equal_range();
|
// tests_map_equal_range();
|
||||||
// tests_map_get_allocator();
|
// tests_map_get_allocator();
|
||||||
// tests_map_relational_operators();
|
// tests_map_relational_operators();
|
||||||
tests_map_swap_non_member();
|
// tests_map_swap_non_member();
|
||||||
|
|
||||||
|
|
||||||
// STACK
|
// STACK
|
||||||
// tests_stack_constructor();
|
tests_stack_constructor();
|
||||||
// tests_stack_operator_assignation();
|
// tests_stack_operator_assignation();
|
||||||
// tests_stack_begin();
|
// tests_stack_begin();
|
||||||
// tests_stack_end();
|
// tests_stack_end();
|
||||||
|
|||||||
741
tests/main_map_1.cpp
Normal file
741
tests/main_map_1.cpp
Normal file
@@ -0,0 +1,741 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <iomanip> // std::setw()
|
||||||
|
#include <iterator> // std::reverse_iterator
|
||||||
|
#include <utility> // std::make_pair
|
||||||
|
#include <sstream> // std::stringstream
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
// toogle ft in stl
|
||||||
|
#ifdef STL
|
||||||
|
namespace ft = std;
|
||||||
|
#else
|
||||||
|
#include "map.hpp"
|
||||||
|
#include "reverse_iterator.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// defines
|
||||||
|
# define TEST_M(s) template <class T, class U> void s ()
|
||||||
|
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
|
||||||
|
# define VALT(n) val<T>(n)
|
||||||
|
# define VALU(n) val<U>(n)
|
||||||
|
# define TOI(n) toi<T>(n)
|
||||||
|
# define PRINT(n) print<>(n, #n);
|
||||||
|
# define DELETE delete_structs();
|
||||||
|
|
||||||
|
// colors
|
||||||
|
# define GRAY "\e[0;30m"
|
||||||
|
# define RED "\e[0;31m"
|
||||||
|
# define GREEN "\e[0;32m"
|
||||||
|
# define YELLOW "\e[0;33m"
|
||||||
|
# define BLUE "\e[0;34m"
|
||||||
|
# define PURPLE "\e[0;35m"
|
||||||
|
# define CYAN "\e[0;36m"
|
||||||
|
# define WHITE "\e[0;37m"
|
||||||
|
|
||||||
|
# define B_GRAY "\e[1;30m"
|
||||||
|
# define B_RED "\e[1;31m"
|
||||||
|
# define B_GREEN "\e[1;32m"
|
||||||
|
# define B_YELLOW "\e[1;33m"
|
||||||
|
# define B_BLUE "\e[1;34m"
|
||||||
|
# define B_PURPLE "\e[1;35m"
|
||||||
|
# define B_CYAN "\e[1;36m"
|
||||||
|
# define B_WHITE "\e[1;37m"
|
||||||
|
|
||||||
|
# define RESET "\e[0m"
|
||||||
|
|
||||||
|
// mystruct declaration
|
||||||
|
struct mystruct {
|
||||||
|
public:
|
||||||
|
mystruct(int data = 0);
|
||||||
|
~mystruct();
|
||||||
|
int * get_data() const;
|
||||||
|
private:
|
||||||
|
int * _val;
|
||||||
|
};
|
||||||
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs);
|
||||||
|
|
||||||
|
// mystruct definition
|
||||||
|
mystruct::mystruct(int data)
|
||||||
|
{_val = new int[2]; _val[0] = data; _val[1] = data;}
|
||||||
|
mystruct::~mystruct()
|
||||||
|
{delete[] _val;}
|
||||||
|
int * mystruct::get_data() const
|
||||||
|
{return _val;}
|
||||||
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs) {
|
||||||
|
if (rhs != NULL)
|
||||||
|
o << (*rhs).get_data()[0] << "," << (*rhs).get_data()[1];
|
||||||
|
else
|
||||||
|
o << "NULL";
|
||||||
|
return (o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// global variable for mem gestion of mystruct
|
||||||
|
std::vector< mystruct* > mem_list;
|
||||||
|
|
||||||
|
// template print function
|
||||||
|
template <class T, class U>
|
||||||
|
void print(ft::map<T, U> mp, std::string name) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
typename ft::map<T, U>::iterator it;
|
||||||
|
typename ft::map<T, U>::iterator it_end = mp.end();
|
||||||
|
|
||||||
|
std::cout << "\n" << name << ":(map)\n";
|
||||||
|
for (it = mp.begin(); it != it_end; ++it, i++)
|
||||||
|
std::cout << "[" << i << "]" << it->first << ":" << it->second << " ";
|
||||||
|
std::cout << "\nsize:" << mp.size() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// template val() for instanciation of values for types :
|
||||||
|
// int, char, std::string, and mystruct*
|
||||||
|
template <class T>
|
||||||
|
T val(int n) { (void)n; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(int n) { return (n); }
|
||||||
|
template <>
|
||||||
|
inline char val(int n) {
|
||||||
|
|
||||||
|
if (n <= 126 && n >= 33)
|
||||||
|
return n;
|
||||||
|
return (n % 94 + 33);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline std::string val(int n) {
|
||||||
|
|
||||||
|
std::string str;
|
||||||
|
std::stringstream stream;
|
||||||
|
|
||||||
|
stream << n;
|
||||||
|
stream >> str;
|
||||||
|
stream.clear();
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(int n) {
|
||||||
|
|
||||||
|
mystruct *s = new mystruct(n);
|
||||||
|
mem_list.push_back(s);
|
||||||
|
return ( s );
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
T val(std::string str) { (void)str; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(std::string str) { int i = str[0]; return (val<int>(i)); }
|
||||||
|
template <>
|
||||||
|
inline char val(std::string str) { int i = str[0]; return (val<char>(i)); }
|
||||||
|
template <>
|
||||||
|
inline std::string val(std::string str) { return (str); }
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(std::string str) { int i = str[0]; return (val<mystruct*>(i)); }
|
||||||
|
|
||||||
|
// delete function for mem gestion of mystruct*
|
||||||
|
void delete_structs() {
|
||||||
|
|
||||||
|
std::vector<mystruct*>::iterator it;
|
||||||
|
std::vector<mystruct*>::iterator it_end = mem_list.end();
|
||||||
|
|
||||||
|
for (it = mem_list.begin(); it != it_end; ++it)
|
||||||
|
delete *it;
|
||||||
|
mem_list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// all tests
|
||||||
|
TEST_M(tests_map_operator_assignation)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> first;
|
||||||
|
ft::map<T,U> second;
|
||||||
|
|
||||||
|
first[VALT('x')]=VALU(8);
|
||||||
|
first[VALT('y')]=VALU(16);
|
||||||
|
first[VALT('z')]=VALU(32);
|
||||||
|
|
||||||
|
PRINT(first)
|
||||||
|
PRINT(second)
|
||||||
|
|
||||||
|
second=first; // second now contains 3 ints
|
||||||
|
first=ft::map<T,U>(); // and first is now empty
|
||||||
|
|
||||||
|
std::cout << "Size of first: " << first.size() << '\n';
|
||||||
|
std::cout << "Size of second: " << second.size() << '\n';
|
||||||
|
|
||||||
|
PRINT(first)
|
||||||
|
PRINT(second)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_begin)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('b')] = VALU(100);
|
||||||
|
mymap[VALT('a')] = VALU(200);
|
||||||
|
mymap[VALT('c')] = VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_end)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('b')] = VALU(100);
|
||||||
|
mymap[VALT('a')] = VALU(200);
|
||||||
|
mymap[VALT('c')] = VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_rbegin)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')] = VALU(100);
|
||||||
|
mymap[VALT('y')] = VALU(200);
|
||||||
|
mymap[VALT('z')] = VALU(300);
|
||||||
|
|
||||||
|
// show content:
|
||||||
|
typename ft::map<T,U>::reverse_iterator rit;
|
||||||
|
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
|
||||||
|
std::cout << rit->first << " => " << rit->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_rend)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')] = VALU(100);
|
||||||
|
mymap[VALT('y')] = VALU(200);
|
||||||
|
mymap[VALT('z')] = VALU(300);
|
||||||
|
|
||||||
|
// show content:
|
||||||
|
typename ft::map<T,U>::reverse_iterator rit;
|
||||||
|
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
|
||||||
|
std::cout << rit->first << " => " << rit->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_empty)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
|
||||||
|
while (!mymap.empty())
|
||||||
|
{
|
||||||
|
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
|
||||||
|
mymap.erase(mymap.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_size)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
mymap[VALT('a')]=VALU(101);
|
||||||
|
mymap[VALT('b')]=VALU(202);
|
||||||
|
mymap[VALT('c')]=VALU(302);
|
||||||
|
|
||||||
|
std::cout << "mymap.size() is " << mymap.size() << '\n';
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_max_size)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
int i;
|
||||||
|
std::map<T,U> mymap;
|
||||||
|
|
||||||
|
if (mymap.max_size()>1000)
|
||||||
|
{
|
||||||
|
for (i=0; i<1000; i++) mymap[i]=VALU(0);
|
||||||
|
std::cout << "The map contains 1000 elements.\n";
|
||||||
|
}
|
||||||
|
else std::cout << "The map could not hold 1000 elements.\n";
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_operator_access)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU("An element");
|
||||||
|
mymap[VALT('b')]=VALU("another element");
|
||||||
|
mymap[VALT('c')]=mymap[VALT('b')];
|
||||||
|
|
||||||
|
std::cout << "mymap['a'] is " << mymap[VALT('a')] << '\n';
|
||||||
|
std::cout << "mymap['b'] is " << mymap[VALT('b')] << '\n';
|
||||||
|
std::cout << "mymap['c'] is " << mymap[VALT('c')] << '\n';
|
||||||
|
std::cout << "mymap['d'] is " << mymap[VALT('d')] << '\n';
|
||||||
|
|
||||||
|
std::cout << "mymap now contains " << mymap.size() << " elements.\n";
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_insert)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
// first insert function version (single parameter):
|
||||||
|
mymap.insert ( ft::pair<T,U>(VALT('a'),VALU(100)) );
|
||||||
|
mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(200)) );
|
||||||
|
|
||||||
|
ft::pair<typename ft::map<T,U>::iterator, bool> ret;
|
||||||
|
ret = mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(500)) );
|
||||||
|
if (ret.second==false) {
|
||||||
|
std::cout << "element 'z' already existed";
|
||||||
|
std::cout << " with a value of " << ret.first->second << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// second insert function version (with hint position):
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
mymap.insert (it, ft::pair<T,U>(VALT('b'),VALU(300))); // max efficiency inserting
|
||||||
|
mymap.insert (it, ft::pair<T,U>(VALT('c'),VALU(400))); // no max efficiency inserting
|
||||||
|
|
||||||
|
// third insert function version (range insertion):
|
||||||
|
ft::map<T,U> anothermap;
|
||||||
|
anothermap.insert(mymap.begin(),mymap.find('c'));
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
PRINT(anothermap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_erase)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator it;
|
||||||
|
|
||||||
|
// insert some values:
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
mymap[VALT('d')]=VALU(40);
|
||||||
|
mymap[VALT('e')]=VALU(50);
|
||||||
|
mymap[VALT('f')]=VALU(60);
|
||||||
|
|
||||||
|
it=mymap.find(VALT('b'));
|
||||||
|
mymap.erase (it); // erasing by iterator
|
||||||
|
|
||||||
|
mymap.erase (VALT('c')); // erasing by key
|
||||||
|
|
||||||
|
it=mymap.find (VALT('e'));
|
||||||
|
mymap.erase ( it, mymap.end() ); // erasing by range
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_swap)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> foo,bar;
|
||||||
|
|
||||||
|
foo[VALT('x')]=VALU(100);
|
||||||
|
foo[VALT('y')]=VALU(200);
|
||||||
|
|
||||||
|
bar[VALT('a')]=VALU(11);
|
||||||
|
bar[VALT('b')]=VALU(22);
|
||||||
|
bar[VALT('c')]=VALU(33);
|
||||||
|
|
||||||
|
foo.swap(bar);
|
||||||
|
|
||||||
|
PRINT(foo)
|
||||||
|
PRINT(bar)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_clear)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')]=VALU(100);
|
||||||
|
mymap[VALT('y')]=VALU(200);
|
||||||
|
mymap[VALT('z')]=VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
mymap.clear();
|
||||||
|
mymap[VALT('a')]=VALU(1101);
|
||||||
|
mymap[VALT('b')]=VALU(2202);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_key_comp)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
typename ft::map<T,U>::key_compare mycomp = mymap.key_comp();
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(100);
|
||||||
|
mymap[VALT('b')]=VALU(200);
|
||||||
|
mymap[VALT('c')]=VALU(300);
|
||||||
|
|
||||||
|
std::cout << "mymap contains:\n";
|
||||||
|
|
||||||
|
T highest = mymap.rbegin()->first; // key value of last element
|
||||||
|
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
do {
|
||||||
|
std::cout << it->first << " => " << it->second << '\n';
|
||||||
|
} while ( mycomp((*it++).first, highest) );
|
||||||
|
|
||||||
|
std::cout << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_value_comp)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')]=VALU(1001);
|
||||||
|
mymap[VALT('y')]=VALU(2002);
|
||||||
|
mymap[VALT('z')]=VALU(3003);
|
||||||
|
|
||||||
|
std::cout << "mymap contains:\n";
|
||||||
|
|
||||||
|
ft::pair<T,U> highest = *mymap.rbegin(); // last element
|
||||||
|
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
do {
|
||||||
|
std::cout << it->first << " => " << it->second << '\n';
|
||||||
|
} while ( mymap.value_comp()(*it++, highest) );
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_find)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator it;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(50);
|
||||||
|
mymap[VALT('b')]=VALU(100);
|
||||||
|
mymap[VALT('c')]=VALU(150);
|
||||||
|
mymap[VALT('d')]=VALU(200);
|
||||||
|
|
||||||
|
it = mymap.find(VALT('b'));
|
||||||
|
if (it != mymap.end())
|
||||||
|
mymap.erase (it);
|
||||||
|
|
||||||
|
// print content:
|
||||||
|
std::cout << "elements in mymap:" << '\n';
|
||||||
|
std::cout << "a => " << mymap.find(VALT('a'))->second << '\n';
|
||||||
|
std::cout << "c => " << mymap.find(VALT('c'))->second << '\n';
|
||||||
|
std::cout << "d => " << mymap.find(VALT('d'))->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_count)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
T c;
|
||||||
|
|
||||||
|
mymap [VALT('a')]=VALU(101);
|
||||||
|
mymap [VALT('c')]=VALU(202);
|
||||||
|
mymap [VALT('f')]=VALU(303);
|
||||||
|
|
||||||
|
// to do this test with T as a 'string' or 'mystruct*' we should add overload
|
||||||
|
for (c=VALT('a'); c<VALT('h'); c++)
|
||||||
|
{
|
||||||
|
std::cout << c;
|
||||||
|
if (mymap.count(c)>0)
|
||||||
|
std::cout << " is an element of mymap.\n";
|
||||||
|
else
|
||||||
|
std::cout << " is not an element of mymap.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_lower_bound)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator itlow,itup;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(20);
|
||||||
|
mymap[VALT('b')]=VALU(40);
|
||||||
|
mymap[VALT('c')]=VALU(60);
|
||||||
|
mymap[VALT('d')]=VALU(80);
|
||||||
|
mymap[VALT('e')]=VALU(100);
|
||||||
|
|
||||||
|
itlow=mymap.lower_bound (VALT('b')); // itlow points to b
|
||||||
|
itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
|
||||||
|
|
||||||
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_upper_bound)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator itlow,itup;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(20);
|
||||||
|
mymap[VALT('b')]=VALU(40);
|
||||||
|
mymap[VALT('c')]=VALU(60);
|
||||||
|
mymap[VALT('d')]=VALU(80);
|
||||||
|
mymap[VALT('e')]=VALU(100);
|
||||||
|
|
||||||
|
itlow=mymap.lower_bound (VALT('b')); // itlow points to b
|
||||||
|
itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
|
||||||
|
|
||||||
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_equal_range)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
|
||||||
|
ft::pair<typename ft::map<T,U>::iterator,typename ft::map<T,U>::iterator> ret;
|
||||||
|
ret = mymap.equal_range(VALT('b'));
|
||||||
|
|
||||||
|
std::cout << "lower bound points to: ";
|
||||||
|
std::cout << ret.first->first << " => " << ret.first->second << '\n';
|
||||||
|
|
||||||
|
std::cout << "upper bound points to: ";
|
||||||
|
std::cout << ret.second->first << " => " << ret.second->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_get_allocator)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
int psize;
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
ft::pair<const T,U>* p;
|
||||||
|
|
||||||
|
// allocate an array of 5 elements using mymap's allocator:
|
||||||
|
p=mymap.get_allocator().allocate(5);
|
||||||
|
|
||||||
|
// assign some values to array
|
||||||
|
psize = sizeof(typename ft::map<T,U>::value_type)*5;
|
||||||
|
|
||||||
|
std::cout << "The allocated array has a size of " << psize << " bytes.\n";
|
||||||
|
|
||||||
|
mymap.get_allocator().deallocate(p,5);
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_relational_operators)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> alice;
|
||||||
|
ft::map<T,U> bob;
|
||||||
|
ft::map<T,U> eve;
|
||||||
|
(void)alice;
|
||||||
|
(void)bob;
|
||||||
|
(void)eve;
|
||||||
|
|
||||||
|
alice[VALT(1)]=VALU('a');
|
||||||
|
alice[VALT(2)]=VALU('b');
|
||||||
|
alice[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
bob[VALT(7)]=VALU('Z');
|
||||||
|
bob[VALT(8)]=VALU('Y');
|
||||||
|
bob[VALT(9)]=VALU('X');
|
||||||
|
bob[VALT(10)]=VALU('W');
|
||||||
|
|
||||||
|
eve[VALT(1)]=VALU('a');
|
||||||
|
eve[VALT(2)]=VALU('b');
|
||||||
|
eve[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
std::cout << std::boolalpha;
|
||||||
|
|
||||||
|
// Compare non equal containers
|
||||||
|
std::cout << "alice == bob returns " << (alice == bob) << '\n';
|
||||||
|
std::cout << "alice != bob returns " << (alice != bob) << '\n';
|
||||||
|
std::cout << "alice < bob returns " << (alice < bob) << '\n';
|
||||||
|
std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
|
||||||
|
std::cout << "alice > bob returns " << (alice > bob) << '\n';
|
||||||
|
std::cout << "alice >= bob returns " << (alice >= bob) << '\n';
|
||||||
|
|
||||||
|
std::cout << '\n';
|
||||||
|
|
||||||
|
// Compare equal containers
|
||||||
|
std::cout << "alice == eve returns " << (alice == eve) << '\n';
|
||||||
|
std::cout << "alice != eve returns " << (alice != eve) << '\n';
|
||||||
|
std::cout << "alice < eve returns " << (alice < eve) << '\n';
|
||||||
|
std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
|
||||||
|
std::cout << "alice > eve returns " << (alice > eve) << '\n';
|
||||||
|
std::cout << "alice >= eve returns " << (alice >= eve) << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_M(tests_map_swap_non_member)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T, U> alice;
|
||||||
|
ft::map<T, U> bob;
|
||||||
|
|
||||||
|
alice[VALT(1)]=VALU('a');
|
||||||
|
alice[VALT(2)]=VALU('b');
|
||||||
|
alice[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
bob[VALT(7)]=VALU('Z');
|
||||||
|
bob[VALT(8)]=VALU('Y');
|
||||||
|
bob[VALT(9)]=VALU('X');
|
||||||
|
bob[VALT(10)]=VALU('W');
|
||||||
|
|
||||||
|
// Print state before swap
|
||||||
|
PRINT(alice)
|
||||||
|
PRINT(bob)
|
||||||
|
|
||||||
|
std::cout << "-- SWAP\n";
|
||||||
|
std::swap(alice, bob);
|
||||||
|
|
||||||
|
// Print state after swap
|
||||||
|
PRINT(alice)
|
||||||
|
PRINT(bob)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class U>
|
||||||
|
void call_tests() {
|
||||||
|
|
||||||
|
tests_map_operator_assignation<T,U>();
|
||||||
|
tests_map_begin<T,U>();
|
||||||
|
tests_map_end<T,U>();
|
||||||
|
tests_map_rbegin<T,U>();
|
||||||
|
tests_map_rend<T,U>();
|
||||||
|
tests_map_empty<T,U>();
|
||||||
|
tests_map_size<T,U>();
|
||||||
|
tests_map_max_size<T,U>();
|
||||||
|
tests_map_operator_access<T,U>();
|
||||||
|
tests_map_insert<T,U>();
|
||||||
|
tests_map_erase<T,U>();
|
||||||
|
tests_map_swap<T,U>();
|
||||||
|
tests_map_clear<T,U>();
|
||||||
|
tests_map_key_comp<T,U>();
|
||||||
|
tests_map_value_comp<T,U>();
|
||||||
|
tests_map_find<T,U>();
|
||||||
|
tests_map_count<T,U>();
|
||||||
|
tests_map_lower_bound<T,U>();
|
||||||
|
tests_map_upper_bound<T,U>();
|
||||||
|
tests_map_equal_range<T,U>();
|
||||||
|
tests_map_get_allocator<T,U>();
|
||||||
|
tests_map_relational_operators<T,U>();
|
||||||
|
tests_map_swap_non_member<T,U>();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
call_tests<char,int>();
|
||||||
|
call_tests<char,char>();
|
||||||
|
call_tests<char,std::string>();
|
||||||
|
call_tests<char,mystruct*>();
|
||||||
|
call_tests<int,int>();
|
||||||
|
call_tests<int,char>();
|
||||||
|
call_tests<int,std::string>();
|
||||||
|
call_tests<int,mystruct*>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
862
tests/main_map_2.cpp
Normal file
862
tests/main_map_2.cpp
Normal file
@@ -0,0 +1,862 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <iomanip> // std::setw()
|
||||||
|
#include <iterator> // std::reverse_iterator
|
||||||
|
#include <utility> // std::make_pair
|
||||||
|
#include <sstream> // std::stringstream
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
// toogle between test ft and stl
|
||||||
|
// *************************
|
||||||
|
#include <map>
|
||||||
|
#ifdef STL
|
||||||
|
namespace ft = std;
|
||||||
|
#else
|
||||||
|
#include "vector.hpp"
|
||||||
|
#include "map.hpp"
|
||||||
|
#include "reverse_iterator.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// colors
|
||||||
|
// *************************************************
|
||||||
|
# define GRAY "\e[0;30m"
|
||||||
|
# define RED "\e[0;31m"
|
||||||
|
# define GREEN "\e[0;32m"
|
||||||
|
# define YELLOW "\e[0;33m"
|
||||||
|
# define BLUE "\e[0;34m"
|
||||||
|
# define PURPLE "\e[0;35m"
|
||||||
|
# define CYAN "\e[0;36m"
|
||||||
|
# define WHITE "\e[0;37m"
|
||||||
|
|
||||||
|
# define B_GRAY "\e[1;30m"
|
||||||
|
# define B_RED "\e[1;31m"
|
||||||
|
# define B_GREEN "\e[1;32m"
|
||||||
|
# define B_YELLOW "\e[1;33m"
|
||||||
|
# define B_BLUE "\e[1;34m"
|
||||||
|
# define B_PURPLE "\e[1;35m"
|
||||||
|
# define B_CYAN "\e[1;36m"
|
||||||
|
# define B_WHITE "\e[1;37m"
|
||||||
|
|
||||||
|
# define RESET "\e[0m"
|
||||||
|
|
||||||
|
|
||||||
|
// defines
|
||||||
|
// ****************************************
|
||||||
|
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
|
||||||
|
# define VAL(n) val<T>(n)
|
||||||
|
# define VALT(n) val<T>(n)
|
||||||
|
# define VALU(n) val<U>(n)
|
||||||
|
# define TOI(n) toi<T>(n)
|
||||||
|
# define PRINT(n) print<>(n, #n);
|
||||||
|
# define DELETE delete_structs();
|
||||||
|
|
||||||
|
|
||||||
|
// adding each test to the list
|
||||||
|
// ***************************
|
||||||
|
#define TEST_V(f_name) \
|
||||||
|
template <class T> struct s_ ## f_name : public A_test\
|
||||||
|
{ void func(); };\
|
||||||
|
void f_name () {\
|
||||||
|
add_to_list("", "", NULL);\
|
||||||
|
add_to_list(#f_name, "int", new(s_ ## f_name <int>));\
|
||||||
|
add_to_list(#f_name, "char", new(s_ ## f_name <char>));\
|
||||||
|
add_to_list(#f_name, "std::string", new(s_ ## f_name <std::string>));\
|
||||||
|
add_to_list(#f_name, "mystruct*", new(s_ ## f_name <mystruct*>));\
|
||||||
|
}\
|
||||||
|
template <class T>\
|
||||||
|
void s_ ## f_name <T>::func()
|
||||||
|
|
||||||
|
#define TEST_M(f_name) \
|
||||||
|
template <class T, class U> struct s_ ## f_name : public A_test\
|
||||||
|
{ void func(); };\
|
||||||
|
void f_name () {\
|
||||||
|
add_to_list("", "", NULL);\
|
||||||
|
add_to_list(#f_name, "char, int", new(s_ ## f_name <char, int>));\
|
||||||
|
add_to_list(#f_name, "char, char", new(s_ ## f_name <char, char>));\
|
||||||
|
add_to_list(#f_name, "char, std::string", new(s_ ## f_name <char, std::string>));\
|
||||||
|
add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name <char, mystruct*>));\
|
||||||
|
add_to_list(#f_name, "int, int", new(s_ ## f_name <int, int>));\
|
||||||
|
add_to_list(#f_name, "int, char", new(s_ ## f_name <int, char>));\
|
||||||
|
add_to_list(#f_name, "int, std::string", new(s_ ## f_name <int, std::string>));\
|
||||||
|
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
|
||||||
|
}\
|
||||||
|
template <class T, class U>\
|
||||||
|
void s_ ## f_name <T, U>::func()
|
||||||
|
|
||||||
|
|
||||||
|
// structures
|
||||||
|
// *********************************************
|
||||||
|
struct A_test
|
||||||
|
{
|
||||||
|
virtual ~A_test(){};
|
||||||
|
std::string title;
|
||||||
|
std::string type;
|
||||||
|
virtual void func() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mystruct {
|
||||||
|
public:
|
||||||
|
mystruct(int data = 0)
|
||||||
|
{_val = new int[2]; _val[0] = data; _val[1] = data;}
|
||||||
|
~mystruct()
|
||||||
|
{delete[] _val;}
|
||||||
|
int * get_data() const
|
||||||
|
{return _val;}
|
||||||
|
private:
|
||||||
|
int * _val;
|
||||||
|
};
|
||||||
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs) {
|
||||||
|
if (rhs != NULL)
|
||||||
|
o << (*rhs).get_data()[0] << "," << (*rhs).get_data()[1];
|
||||||
|
else
|
||||||
|
o << "NULL";
|
||||||
|
return (o);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// global variables
|
||||||
|
// ***************************************
|
||||||
|
std::vector< std::vector<A_test*> > test_list;
|
||||||
|
std::vector< mystruct* > mem_list;
|
||||||
|
|
||||||
|
|
||||||
|
// functions utiles
|
||||||
|
// ***************************************
|
||||||
|
void add_to_list(std::string title, std::string type, A_test* test) {
|
||||||
|
|
||||||
|
std::vector<A_test*> test_sub_list;
|
||||||
|
std::vector< std::vector<A_test*> >::iterator it;
|
||||||
|
|
||||||
|
// title != NULL for the first element
|
||||||
|
if (test == NULL)
|
||||||
|
{
|
||||||
|
test_list.push_back(test_sub_list);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
test->title = title;
|
||||||
|
test->type = type;
|
||||||
|
it = test_list.end() - 1;
|
||||||
|
(*it).push_back(test);
|
||||||
|
}
|
||||||
|
void delete_structs() {
|
||||||
|
|
||||||
|
std::vector<mystruct*>::iterator it;
|
||||||
|
std::vector<mystruct*>::iterator it_end = mem_list.end();
|
||||||
|
|
||||||
|
for (it = mem_list.begin(); it != it_end; ++it)
|
||||||
|
delete *it;
|
||||||
|
mem_list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// templates print
|
||||||
|
// *****************************************
|
||||||
|
template <class T>
|
||||||
|
void print(ft::vector<T> vec, std::string name) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
typename ft::vector<T>::iterator it;
|
||||||
|
typename ft::vector<T>::iterator it_end = vec.end();
|
||||||
|
|
||||||
|
std::cout << "\n" << name << ":(vector)\n";
|
||||||
|
for (it = vec.begin(); it != it_end; ++it, i++)
|
||||||
|
std::cout << "[" << i << "]" << *it << " ";
|
||||||
|
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
|
||||||
|
}
|
||||||
|
template <class T, class U>
|
||||||
|
void print(ft::map<T, U> mp, std::string name) {
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
typename ft::map<T, U>::iterator it;
|
||||||
|
typename ft::map<T, U>::iterator it_end = mp.end();
|
||||||
|
|
||||||
|
std::cout << "\n" << name << ":(map)\n";
|
||||||
|
for (it = mp.begin(); it != it_end; ++it, i++)
|
||||||
|
std::cout << "[" << i << "]" << it->first << ":" << it->second << " ";
|
||||||
|
std::cout << "\nsize:" << mp.size() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// templates get value
|
||||||
|
// *************************************
|
||||||
|
// specialization in header, make it inline :
|
||||||
|
// https://stackoverflow.com/questions/63529059/c-specialized-method-templates-produce-multiple-definition-errors
|
||||||
|
template <class T>
|
||||||
|
T val(int n) { (void)n; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(int n) { return (n); }
|
||||||
|
template <>
|
||||||
|
inline char val(int n) {
|
||||||
|
|
||||||
|
if (n <= 126 && n >= 33)
|
||||||
|
return n;
|
||||||
|
return (n % 94 + 33);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline std::string val(int n) {
|
||||||
|
|
||||||
|
std::string str;
|
||||||
|
std::stringstream stream;
|
||||||
|
|
||||||
|
stream << n;
|
||||||
|
stream >> str;
|
||||||
|
stream.clear();
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(int n) {
|
||||||
|
|
||||||
|
mystruct *s = new mystruct(n);
|
||||||
|
mem_list.push_back(s);
|
||||||
|
return ( s );
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
T val(std::string str) { (void)str; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(std::string str) { int i = str[0]; return (val<int>(i)); }
|
||||||
|
template <>
|
||||||
|
inline char val(std::string str) { int i = str[0]; return (val<char>(i)); }
|
||||||
|
template <>
|
||||||
|
inline std::string val(std::string str) { return (str); }
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(std::string str) { int i = str[0]; return (val<mystruct*>(i)); }
|
||||||
|
|
||||||
|
|
||||||
|
// templates to value
|
||||||
|
// **************************************
|
||||||
|
template <class T>
|
||||||
|
int toi(T t) {(void)t; return (0);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline int toi(int i) {return (i);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline int toi(char c) {return (c);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline int toi(std::string str) {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
std::stringstream stream;
|
||||||
|
|
||||||
|
stream << str;
|
||||||
|
stream >> i;
|
||||||
|
stream.clear();
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline int toi(mystruct* s) {
|
||||||
|
|
||||||
|
return ( s->get_data()[0] );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// tests functions
|
||||||
|
// *****************************************
|
||||||
|
TEST_M(tests_map_operator_assignation)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> first;
|
||||||
|
ft::map<T,U> second;
|
||||||
|
|
||||||
|
first[VALT('x')]=VALU(8);
|
||||||
|
first[VALT('y')]=VALU(16);
|
||||||
|
first[VALT('z')]=VALU(32);
|
||||||
|
|
||||||
|
PRINT(first)
|
||||||
|
PRINT(second)
|
||||||
|
|
||||||
|
second=first; // second now contains 3 ints
|
||||||
|
first=ft::map<T,U>(); // and first is now empty
|
||||||
|
|
||||||
|
std::cout << "Size of first: " << first.size() << '\n';
|
||||||
|
std::cout << "Size of second: " << second.size() << '\n';
|
||||||
|
|
||||||
|
PRINT(first)
|
||||||
|
PRINT(second)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_begin)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('b')] = VALU(100);
|
||||||
|
mymap[VALT('a')] = VALU(200);
|
||||||
|
mymap[VALT('c')] = VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_end)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('b')] = VALU(100);
|
||||||
|
mymap[VALT('a')] = VALU(200);
|
||||||
|
mymap[VALT('c')] = VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_rbegin)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')] = VALU(100);
|
||||||
|
mymap[VALT('y')] = VALU(200);
|
||||||
|
mymap[VALT('z')] = VALU(300);
|
||||||
|
|
||||||
|
// show content:
|
||||||
|
typename ft::map<T,U>::reverse_iterator rit;
|
||||||
|
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
|
||||||
|
std::cout << rit->first << " => " << rit->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_rend)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')] = VALU(100);
|
||||||
|
mymap[VALT('y')] = VALU(200);
|
||||||
|
mymap[VALT('z')] = VALU(300);
|
||||||
|
|
||||||
|
// show content:
|
||||||
|
typename ft::map<T,U>::reverse_iterator rit;
|
||||||
|
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
|
||||||
|
std::cout << rit->first << " => " << rit->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_empty)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
|
||||||
|
while (!mymap.empty())
|
||||||
|
{
|
||||||
|
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
|
||||||
|
mymap.erase(mymap.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_size)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
mymap[VALT('a')]=VALU(101);
|
||||||
|
mymap[VALT('b')]=VALU(202);
|
||||||
|
mymap[VALT('c')]=VALU(302);
|
||||||
|
|
||||||
|
std::cout << "mymap.size() is " << mymap.size() << '\n';
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_max_size)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
int i;
|
||||||
|
std::map<T,U> mymap;
|
||||||
|
|
||||||
|
if (mymap.max_size()>1000)
|
||||||
|
{
|
||||||
|
for (i=0; i<1000; i++) mymap[i]=VALU(0);
|
||||||
|
std::cout << "The map contains 1000 elements.\n";
|
||||||
|
}
|
||||||
|
else std::cout << "The map could not hold 1000 elements.\n";
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_operator_access)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU("An element");
|
||||||
|
mymap[VALT('b')]=VALU("another element");
|
||||||
|
mymap[VALT('c')]=mymap[VAL('b')];
|
||||||
|
|
||||||
|
std::cout << "mymap['a'] is " << mymap[VALT('a')] << '\n';
|
||||||
|
std::cout << "mymap['b'] is " << mymap[VALT('b')] << '\n';
|
||||||
|
std::cout << "mymap['c'] is " << mymap[VALT('c')] << '\n';
|
||||||
|
std::cout << "mymap['d'] is " << mymap[VALT('d')] << '\n';
|
||||||
|
|
||||||
|
std::cout << "mymap now contains " << mymap.size() << " elements.\n";
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_insert)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
// first insert function version (single parameter):
|
||||||
|
mymap.insert ( ft::pair<T,U>(VALT('a'),VALU(100)) );
|
||||||
|
mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(200)) );
|
||||||
|
|
||||||
|
ft::pair<typename ft::map<T,U>::iterator, bool> ret;
|
||||||
|
ret = mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(500)) );
|
||||||
|
if (ret.second==false) {
|
||||||
|
std::cout << "element 'z' already existed";
|
||||||
|
std::cout << " with a value of " << ret.first->second << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// second insert function version (with hint position):
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
mymap.insert (it, ft::pair<T,U>(VALT('b'),VALU(300))); // max efficiency inserting
|
||||||
|
mymap.insert (it, ft::pair<T,U>(VALT('c'),VALU(400))); // no max efficiency inserting
|
||||||
|
|
||||||
|
// third insert function version (range insertion):
|
||||||
|
ft::map<T,U> anothermap;
|
||||||
|
anothermap.insert(mymap.begin(),mymap.find('c'));
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
PRINT(anothermap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_erase)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator it;
|
||||||
|
|
||||||
|
// insert some values:
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
mymap[VALT('d')]=VALU(40);
|
||||||
|
mymap[VALT('e')]=VALU(50);
|
||||||
|
mymap[VALT('f')]=VALU(60);
|
||||||
|
|
||||||
|
it=mymap.find(VALT('b'));
|
||||||
|
mymap.erase (it); // erasing by iterator
|
||||||
|
|
||||||
|
mymap.erase (VALT('c')); // erasing by key
|
||||||
|
|
||||||
|
it=mymap.find (VALT('e'));
|
||||||
|
mymap.erase ( it, mymap.end() ); // erasing by range
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_swap)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> foo,bar;
|
||||||
|
|
||||||
|
foo[VALT('x')]=VALU(100);
|
||||||
|
foo[VALT('y')]=VALU(200);
|
||||||
|
|
||||||
|
bar[VALT('a')]=VALU(11);
|
||||||
|
bar[VALT('b')]=VALU(22);
|
||||||
|
bar[VALT('c')]=VALU(33);
|
||||||
|
|
||||||
|
foo.swap(bar);
|
||||||
|
|
||||||
|
PRINT(foo)
|
||||||
|
PRINT(bar)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_clear)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')]=VALU(100);
|
||||||
|
mymap[VALT('y')]=VALU(200);
|
||||||
|
mymap[VALT('z')]=VALU(300);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
mymap.clear();
|
||||||
|
mymap[VALT('a')]=VALU(1101);
|
||||||
|
mymap[VALT('b')]=VALU(2202);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_key_comp)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
typename ft::map<T,U>::key_compare mycomp = mymap.key_comp();
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(100);
|
||||||
|
mymap[VALT('b')]=VALU(200);
|
||||||
|
mymap[VALT('c')]=VALU(300);
|
||||||
|
|
||||||
|
std::cout << "mymap contains:\n";
|
||||||
|
|
||||||
|
T highest = mymap.rbegin()->first; // key value of last element
|
||||||
|
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
do {
|
||||||
|
std::cout << it->first << " => " << it->second << '\n';
|
||||||
|
} while ( mycomp((*it++).first, highest) );
|
||||||
|
|
||||||
|
std::cout << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_value_comp)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('x')]=VALU(1001);
|
||||||
|
mymap[VALT('y')]=VALU(2002);
|
||||||
|
mymap[VALT('z')]=VALU(3003);
|
||||||
|
|
||||||
|
std::cout << "mymap contains:\n";
|
||||||
|
|
||||||
|
ft::pair<T,U> highest = *mymap.rbegin(); // last element
|
||||||
|
|
||||||
|
typename ft::map<T,U>::iterator it = mymap.begin();
|
||||||
|
do {
|
||||||
|
std::cout << it->first << " => " << it->second << '\n';
|
||||||
|
} while ( mymap.value_comp()(*it++, highest) );
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_find)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator it;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(50);
|
||||||
|
mymap[VALT('b')]=VALU(100);
|
||||||
|
mymap[VALT('c')]=VALU(150);
|
||||||
|
mymap[VALT('d')]=VALU(200);
|
||||||
|
|
||||||
|
it = mymap.find(VALT('b'));
|
||||||
|
if (it != mymap.end())
|
||||||
|
mymap.erase (it);
|
||||||
|
|
||||||
|
// print content:
|
||||||
|
std::cout << "elements in mymap:" << '\n';
|
||||||
|
std::cout << "a => " << mymap.find(VALT('a'))->second << '\n';
|
||||||
|
std::cout << "c => " << mymap.find(VALT('c'))->second << '\n';
|
||||||
|
std::cout << "d => " << mymap.find(VALT('d'))->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_count)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
T c;
|
||||||
|
|
||||||
|
mymap [VALT('a')]=VALU(101);
|
||||||
|
mymap [VALT('c')]=VALU(202);
|
||||||
|
mymap [VALT('f')]=VALU(303);
|
||||||
|
|
||||||
|
// to do this test with T as a 'string' or 'mystruct*' we should add overload
|
||||||
|
for (c=VALT('a'); c<VALT('h'); c++)
|
||||||
|
{
|
||||||
|
std::cout << c;
|
||||||
|
if (mymap.count(c)>0)
|
||||||
|
std::cout << " is an element of mymap.\n";
|
||||||
|
else
|
||||||
|
std::cout << " is not an element of mymap.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_lower_bound)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator itlow,itup;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(20);
|
||||||
|
mymap[VALT('b')]=VALU(40);
|
||||||
|
mymap[VALT('c')]=VALU(60);
|
||||||
|
mymap[VALT('d')]=VALU(80);
|
||||||
|
mymap[VALT('e')]=VALU(100);
|
||||||
|
|
||||||
|
itlow=mymap.lower_bound (VALT('b')); // itlow points to b
|
||||||
|
itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
|
||||||
|
|
||||||
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_upper_bound)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
typename ft::map<T,U>::iterator itlow,itup;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(20);
|
||||||
|
mymap[VALT('b')]=VALU(40);
|
||||||
|
mymap[VALT('c')]=VALU(60);
|
||||||
|
mymap[VALT('d')]=VALU(80);
|
||||||
|
mymap[VALT('e')]=VALU(100);
|
||||||
|
|
||||||
|
itlow=mymap.lower_bound (VALT('b')); // itlow points to b
|
||||||
|
itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
|
||||||
|
|
||||||
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_equal_range)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
|
||||||
|
ft::pair<typename ft::map<T,U>::iterator,typename ft::map<T,U>::iterator> ret;
|
||||||
|
ret = mymap.equal_range(VALT('b'));
|
||||||
|
|
||||||
|
std::cout << "lower bound points to: ";
|
||||||
|
std::cout << ret.first->first << " => " << ret.first->second << '\n';
|
||||||
|
|
||||||
|
std::cout << "upper bound points to: ";
|
||||||
|
std::cout << ret.second->first << " => " << ret.second->second << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_get_allocator)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
int psize;
|
||||||
|
ft::map<T,U> mymap;
|
||||||
|
ft::pair<const T,U>* p;
|
||||||
|
|
||||||
|
// allocate an array of 5 elements using mymap's allocator:
|
||||||
|
p=mymap.get_allocator().allocate(5);
|
||||||
|
|
||||||
|
// assign some values to array
|
||||||
|
psize = sizeof(typename ft::map<T,U>::value_type)*5;
|
||||||
|
|
||||||
|
std::cout << "The allocated array has a size of " << psize << " bytes.\n";
|
||||||
|
|
||||||
|
mymap.get_allocator().deallocate(p,5);
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_M(tests_map_relational_operators)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T,U> alice;
|
||||||
|
ft::map<T,U> bob;
|
||||||
|
ft::map<T,U> eve;
|
||||||
|
(void)alice;
|
||||||
|
(void)bob;
|
||||||
|
(void)eve;
|
||||||
|
|
||||||
|
alice[VALT(1)]=VALU('a');
|
||||||
|
alice[VALT(2)]=VALU('b');
|
||||||
|
alice[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
bob[VALT(7)]=VALU('Z');
|
||||||
|
bob[VALT(8)]=VALU('Y');
|
||||||
|
bob[VALT(9)]=VALU('X');
|
||||||
|
bob[VALT(10)]=VALU('W');
|
||||||
|
|
||||||
|
eve[VALT(1)]=VALU('a');
|
||||||
|
eve[VALT(2)]=VALU('b');
|
||||||
|
eve[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
std::cout << std::boolalpha;
|
||||||
|
|
||||||
|
// Compare non equal containers
|
||||||
|
std::cout << "alice == bob returns " << (alice == bob) << '\n';
|
||||||
|
std::cout << "alice != bob returns " << (alice != bob) << '\n';
|
||||||
|
std::cout << "alice < bob returns " << (alice < bob) << '\n';
|
||||||
|
std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
|
||||||
|
std::cout << "alice > bob returns " << (alice > bob) << '\n';
|
||||||
|
std::cout << "alice >= bob returns " << (alice >= bob) << '\n';
|
||||||
|
|
||||||
|
std::cout << '\n';
|
||||||
|
|
||||||
|
// Compare equal containers
|
||||||
|
std::cout << "alice == eve returns " << (alice == eve) << '\n';
|
||||||
|
std::cout << "alice != eve returns " << (alice != eve) << '\n';
|
||||||
|
std::cout << "alice < eve returns " << (alice < eve) << '\n';
|
||||||
|
std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
|
||||||
|
std::cout << "alice > eve returns " << (alice > eve) << '\n';
|
||||||
|
std::cout << "alice >= eve returns " << (alice >= eve) << '\n';
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_M(tests_map_swap_non_member)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(cplusplus.com reference)
|
||||||
|
|
||||||
|
ft::map<T, U> alice;
|
||||||
|
ft::map<T, U> bob;
|
||||||
|
|
||||||
|
alice[VALT(1)]=VALU('a');
|
||||||
|
alice[VALT(2)]=VALU('b');
|
||||||
|
alice[VALT(3)]=VALU('c');
|
||||||
|
|
||||||
|
bob[VALT(7)]=VALU('Z');
|
||||||
|
bob[VALT(8)]=VALU('Y');
|
||||||
|
bob[VALT(9)]=VALU('X');
|
||||||
|
bob[VALT(10)]=VALU('W');
|
||||||
|
|
||||||
|
// Print state before swap
|
||||||
|
PRINT(alice)
|
||||||
|
PRINT(bob)
|
||||||
|
|
||||||
|
std::cout << "-- SWAP\n";
|
||||||
|
std::swap(alice, bob);
|
||||||
|
|
||||||
|
// Print state after swap
|
||||||
|
PRINT(alice)
|
||||||
|
PRINT(bob)
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
tests_map_operator_assignation();
|
||||||
|
tests_map_begin();
|
||||||
|
tests_map_end();
|
||||||
|
tests_map_rbegin();
|
||||||
|
tests_map_rend();
|
||||||
|
tests_map_empty();
|
||||||
|
tests_map_size();
|
||||||
|
tests_map_max_size();
|
||||||
|
tests_map_operator_access();
|
||||||
|
tests_map_insert();
|
||||||
|
tests_map_erase();
|
||||||
|
tests_map_swap();
|
||||||
|
tests_map_clear();
|
||||||
|
tests_map_key_comp();
|
||||||
|
tests_map_value_comp();
|
||||||
|
tests_map_find();
|
||||||
|
tests_map_count();
|
||||||
|
tests_map_lower_bound();
|
||||||
|
tests_map_upper_bound();
|
||||||
|
tests_map_equal_range();
|
||||||
|
tests_map_get_allocator();
|
||||||
|
tests_map_relational_operators();
|
||||||
|
tests_map_swap_non_member();
|
||||||
|
|
||||||
|
|
||||||
|
// execute tests and print them :
|
||||||
|
int size = test_list.size();
|
||||||
|
int sub_size;
|
||||||
|
for(int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
std::cout << "\n" B_YELLOW "[" << i + 1 << "/" << size << "] "
|
||||||
|
<< test_list[i][0]->title << RESET << "\n";
|
||||||
|
sub_size = test_list[i].size();
|
||||||
|
for (int j = 0; j < sub_size; j++)
|
||||||
|
{
|
||||||
|
std::cout << "\n" << B_CYAN << "-- " << test_list[i][j]->type
|
||||||
|
<< " --" << RESET "\n";
|
||||||
|
test_list[i][j]->func();
|
||||||
|
delete test_list[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
187
tests/main_stack_1.cpp
Normal file
187
tests/main_stack_1.cpp
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <iomanip> // std::setw()
|
||||||
|
#include <iterator> // std::reverse_iterator
|
||||||
|
#include <utility> // std::make_pair
|
||||||
|
#include <sstream> // std::stringstream
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
// toogle ft in stl
|
||||||
|
#ifdef STL
|
||||||
|
namespace ft = std;
|
||||||
|
#else
|
||||||
|
#include "vector.hpp"
|
||||||
|
#include "map.hpp"
|
||||||
|
#include "stack.hpp"
|
||||||
|
#include "reverse_iterator.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// defines
|
||||||
|
# define TEST(s) template <class T> void s ()
|
||||||
|
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
|
||||||
|
# define VAL(n) val<T>(n)
|
||||||
|
# define TOI(n) toi<T>(n)
|
||||||
|
# define PRINT(n) print<>(n, #n);
|
||||||
|
# define DELETE delete_structs();
|
||||||
|
|
||||||
|
// colors
|
||||||
|
# define GRAY "\e[0;30m"
|
||||||
|
# define RED "\e[0;31m"
|
||||||
|
# define GREEN "\e[0;32m"
|
||||||
|
# define YELLOW "\e[0;33m"
|
||||||
|
# define BLUE "\e[0;34m"
|
||||||
|
# define PURPLE "\e[0;35m"
|
||||||
|
# define CYAN "\e[0;36m"
|
||||||
|
# define WHITE "\e[0;37m"
|
||||||
|
|
||||||
|
# define B_GRAY "\e[1;30m"
|
||||||
|
# define B_RED "\e[1;31m"
|
||||||
|
# define B_GREEN "\e[1;32m"
|
||||||
|
# define B_YELLOW "\e[1;33m"
|
||||||
|
# define B_BLUE "\e[1;34m"
|
||||||
|
# define B_PURPLE "\e[1;35m"
|
||||||
|
# define B_CYAN "\e[1;36m"
|
||||||
|
# define B_WHITE "\e[1;37m"
|
||||||
|
|
||||||
|
# define RESET "\e[0m"
|
||||||
|
|
||||||
|
// mystruct declaration
|
||||||
|
struct mystruct {
|
||||||
|
public:
|
||||||
|
mystruct(int data = 0);
|
||||||
|
~mystruct();
|
||||||
|
int * get_data() const;
|
||||||
|
private:
|
||||||
|
int * _val;
|
||||||
|
};
|
||||||
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs);
|
||||||
|
|
||||||
|
// mystruct definition
|
||||||
|
mystruct::mystruct(int data)
|
||||||
|
{_val = new int[2]; _val[0] = data; _val[1] = data;}
|
||||||
|
mystruct::~mystruct()
|
||||||
|
{delete[] _val;}
|
||||||
|
int * mystruct::get_data() const
|
||||||
|
{return _val;}
|
||||||
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs) {
|
||||||
|
if (rhs != NULL)
|
||||||
|
o << (*rhs).get_data()[0] << "," << (*rhs).get_data()[1];
|
||||||
|
else
|
||||||
|
o << "NULL";
|
||||||
|
return (o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// global variable for mem gestion of mystruct
|
||||||
|
std::vector< mystruct* > mem_list;
|
||||||
|
|
||||||
|
// template print function
|
||||||
|
template <class T, class cont>
|
||||||
|
void print(ft::stack<T,cont>& st, std::string name) {
|
||||||
|
|
||||||
|
std::cout << "\n" << name << ":(map)\n";
|
||||||
|
for (int i = st.size(); i > 0 ; i--, st.pop())
|
||||||
|
std::cout << "[" << i << "]" << st.top() << " ";
|
||||||
|
std::cout << "\nsize:" << st.size() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// template val() for instanciation of values for types :
|
||||||
|
// int, char, std::string, and mystruct*
|
||||||
|
template <class T>
|
||||||
|
T val(int n) { (void)n; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(int n) { return (n); }
|
||||||
|
template <>
|
||||||
|
inline char val(int n) {
|
||||||
|
|
||||||
|
if (n <= 126 && n >= 33)
|
||||||
|
return n;
|
||||||
|
return (n % 94 + 33);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline std::string val(int n) {
|
||||||
|
|
||||||
|
std::string str;
|
||||||
|
std::stringstream stream;
|
||||||
|
|
||||||
|
stream << n;
|
||||||
|
stream >> str;
|
||||||
|
stream.clear();
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(int n) {
|
||||||
|
|
||||||
|
mystruct *s = new mystruct(n);
|
||||||
|
mem_list.push_back(s);
|
||||||
|
return ( s );
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
T val(std::string str) { (void)str; return (T()); }
|
||||||
|
template <>
|
||||||
|
inline int val(std::string str) { int i = str[0]; return (val<int>(i)); }
|
||||||
|
template <>
|
||||||
|
inline char val(std::string str) { int i = str[0]; return (val<char>(i)); }
|
||||||
|
template <>
|
||||||
|
inline std::string val(std::string str) { return (str); }
|
||||||
|
template <>
|
||||||
|
inline mystruct* val(std::string str) { int i = str[0]; return (val<mystruct*>(i)); }
|
||||||
|
|
||||||
|
// delete function for mem gestion of mystruct*
|
||||||
|
void delete_structs() {
|
||||||
|
|
||||||
|
std::vector<mystruct*>::iterator it;
|
||||||
|
std::vector<mystruct*>::iterator it_end = mem_list.end();
|
||||||
|
|
||||||
|
for (it = mem_list.begin(); it != it_end; ++it)
|
||||||
|
delete *it;
|
||||||
|
mem_list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// all tests
|
||||||
|
TEST(tests_stack_constructor)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(simple test)
|
||||||
|
|
||||||
|
std::deque<T> mydeque (3,VAL(100)); // deque with 3 elements
|
||||||
|
std::vector<T> myvector (2,VAL(200)); // vector with 2 elements
|
||||||
|
|
||||||
|
ft::stack<T> first; // empty stack
|
||||||
|
ft::stack<T> second (mydeque); // stack initialized to copy of deque
|
||||||
|
|
||||||
|
// ft::stack< T,std::vector<T> > third; // empty stack using vector
|
||||||
|
// ft::stack< T,std::vector<T> > fourth (myvector);
|
||||||
|
//
|
||||||
|
// std::cout << "size of first: " << first.size() << '\n';
|
||||||
|
// std::cout << "size of second: " << second.size() << '\n';
|
||||||
|
// std::cout << "size of third: " << third.size() << '\n';
|
||||||
|
// std::cout << "size of fourth: " << fourth.size() << '\n';
|
||||||
|
//
|
||||||
|
// PRINT(first)
|
||||||
|
// PRINT(second)
|
||||||
|
// PRINT(third)
|
||||||
|
// PRINT(fourth)
|
||||||
|
//
|
||||||
|
// DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void call_tests() {
|
||||||
|
|
||||||
|
tests_stack_constructor<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
call_tests<int>();
|
||||||
|
// call_tests<char>();
|
||||||
|
// call_tests<std::string>();
|
||||||
|
// call_tests<mystruct*>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -5,13 +5,13 @@ TEST_DIR=$(dirname $0)
|
|||||||
OUTPUT_STL="output_stl.log"
|
OUTPUT_STL="output_stl.log"
|
||||||
OUTPUT_FT="output_ft.log"
|
OUTPUT_FT="output_ft.log"
|
||||||
|
|
||||||
make stl > /dev/null
|
make > /dev/null
|
||||||
echo -e "\nstl :"
|
|
||||||
time ./containers > tests/$OUTPUT_STL
|
echo -e "\nstl :"
|
||||||
|
time ./containers_ft > tests/$OUTPUT_STL
|
||||||
|
|
||||||
make ft > /dev/null
|
|
||||||
echo -e "\nft :"
|
echo -e "\nft :"
|
||||||
time ./containers > tests/$OUTPUT_FT
|
time ./containers_stl > tests/$OUTPUT_FT
|
||||||
|
|
||||||
diff --context=0 --color=always tests/$OUTPUT_STL tests/$OUTPUT_FT
|
diff --context=0 --color=always tests/$OUTPUT_STL tests/$OUTPUT_FT
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
|
|
||||||
#ifndef TESTS_MAP_CPP
|
|
||||||
#define TESTS_MAP_CPP
|
|
||||||
|
|
||||||
#include "tests_utils.hpp"
|
#include "tests_utils.hpp"
|
||||||
|
|
||||||
/**/ // UTILS for some tests
|
/**/ // UTILS for some tests
|
||||||
@@ -336,7 +333,6 @@ TEST_M(tests_map_clear)
|
|||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
TEST_M(tests_map_key_comp)
|
TEST_M(tests_map_key_comp)
|
||||||
{
|
{
|
||||||
// title
|
// title
|
||||||
@@ -386,7 +382,6 @@ TEST_M(tests_map_value_comp)
|
|||||||
|
|
||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
TEST_M(tests_map_find)
|
TEST_M(tests_map_find)
|
||||||
{
|
{
|
||||||
@@ -439,7 +434,6 @@ TEST_M(tests_map_count)
|
|||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
TEST_M(tests_map_lower_bound)
|
TEST_M(tests_map_lower_bound)
|
||||||
{
|
{
|
||||||
// title
|
// title
|
||||||
@@ -532,7 +526,6 @@ TEST_M(tests_map_get_allocator)
|
|||||||
|
|
||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
TEST_M(tests_map_relational_operators)
|
TEST_M(tests_map_relational_operators)
|
||||||
{
|
{
|
||||||
@@ -614,5 +607,3 @@ TEST_M(tests_map_swap_non_member)
|
|||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|||||||
29
tests/tests_stack.cpp
Normal file
29
tests/tests_stack.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
#include "tests_utils.hpp"
|
||||||
|
|
||||||
|
TEST(tests_stack_constructor)
|
||||||
|
{
|
||||||
|
// title
|
||||||
|
TITLE(simple test)
|
||||||
|
|
||||||
|
std::deque<T> mydeque (3,VAL(100)); // deque with 3 elements
|
||||||
|
std::vector<T> myvector (2,VAL(200)); // vector with 2 elements
|
||||||
|
|
||||||
|
ft::stack<T> first; // empty stack
|
||||||
|
ft::stack<T> second (mydeque); // stack initialized to copy of deque
|
||||||
|
|
||||||
|
// ft::stack< T,std::vector<T> > third; // empty stack using vector
|
||||||
|
// ft::stack< T,std::vector<T> > fourth (myvector);
|
||||||
|
//
|
||||||
|
// std::cout << "size of first: " << first.size() << '\n';
|
||||||
|
// std::cout << "size of second: " << second.size() << '\n';
|
||||||
|
// std::cout << "size of third: " << third.size() << '\n';
|
||||||
|
// std::cout << "size of fourth: " << fourth.size() << '\n';
|
||||||
|
//
|
||||||
|
// PRINT(first)
|
||||||
|
// PRINT(second)
|
||||||
|
// PRINT(third)
|
||||||
|
// PRINT(fourth)
|
||||||
|
//
|
||||||
|
// DELETE
|
||||||
|
}
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
|
|
||||||
#ifndef TESTS_VECTOR_CPP
|
|
||||||
#define TESTS_VECTOR_CPP
|
|
||||||
|
|
||||||
#include "tests_utils.hpp"
|
#include "tests_utils.hpp"
|
||||||
|
|
||||||
TEST_V(tests_vector_constructor)
|
TEST_V(tests_vector_constructor)
|
||||||
@@ -870,4 +867,3 @@ TEST_V(tests_vector_reverse_iterators)
|
|||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user