merged from home
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
#ifndef COLORS_H
|
||||
# define COLORS_H
|
||||
|
||||
|
||||
16
headers/enable_if.hpp
Normal file
16
headers/enable_if.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef ENABLE_IF_HPP
|
||||
# define ENABLE_IF_HPP
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <bool B, class T = void>
|
||||
struct enable_if {};
|
||||
|
||||
|
||||
template <class T>
|
||||
struct enable_if<true, T> { typedef T type; };
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
36
headers/equal.hpp
Normal file
36
headers/equal.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
#ifndef EQUAL_HPP
|
||||
# define EQUAL_HPP
|
||||
|
||||
namespace ft {
|
||||
|
||||
template < typename InputIt1, typename InputIt2 >
|
||||
bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2) {
|
||||
|
||||
while (first1 != last1)
|
||||
{
|
||||
if (!(*first1 == *first2))
|
||||
return false;
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template < class InputIt1, class InputIt2, class BinaryPred >
|
||||
bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred pred) {
|
||||
|
||||
while (first1 != last1)
|
||||
{
|
||||
if (!pred(*first1, *first2))
|
||||
return false;
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
46
headers/is_integral.hpp
Normal file
46
headers/is_integral.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef IS_INTEGRAL_HPP
|
||||
# define IS_INTEGRAL_HPP
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <class T> struct is_integral
|
||||
{
|
||||
typedef char yes[1];
|
||||
typedef yes no[2];
|
||||
|
||||
static T type;
|
||||
|
||||
// integral types : https://www.cplusplus.com/reference/type_traits/is_integral/
|
||||
static yes& test(bool);
|
||||
static yes& test(char);
|
||||
// static yes& test(char16_t);
|
||||
// static yes& test(char32_t);
|
||||
static yes& test(wchar_t);
|
||||
static yes& test(signed char);
|
||||
static yes& test(short int);
|
||||
static yes& test(int);
|
||||
static yes& test(long int);
|
||||
static yes& test(long long int);
|
||||
static yes& test(unsigned char);
|
||||
static yes& test(unsigned short int);
|
||||
static yes& test(unsigned int);
|
||||
static yes& test(unsigned long int);
|
||||
static yes& test(unsigned long long int);
|
||||
|
||||
// non-template function with direct matching are always considered first
|
||||
// then the function template with direct matching are considered
|
||||
// https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on
|
||||
template <class C> static no& test(C);
|
||||
|
||||
static const bool value = sizeof(test(type)) == sizeof(yes);
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
// "template <>" introduce a total specialization of a template :
|
||||
// https://stackoverflow.com/questions/6288812/what-is-the-meaning-of-template-with-empty-angle-brackets-in-c
|
||||
|
||||
// SFINAE : https://jguegant.github.io/blogs/tech/sfinae-introduction.html
|
||||
|
||||
#endif
|
||||
|
||||
44
headers/iterator_traits.hpp
Normal file
44
headers/iterator_traits.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
#ifndef ITERATOR_TRAITS_HPP
|
||||
# define ITERATOR_TRAITS_HPP
|
||||
|
||||
# include <iterator> // iterator_tag
|
||||
# include <cstddef> // ptrdiff_t
|
||||
|
||||
namespace ft {
|
||||
|
||||
template < typename Iterator >
|
||||
struct iterator_traits
|
||||
{
|
||||
typedef typename Iterator::iterator_category iterator_category;
|
||||
typedef typename Iterator::value_type value_type;
|
||||
typedef typename Iterator::difference_type difference_type;
|
||||
typedef typename Iterator::pointer pointer;
|
||||
typedef typename Iterator::reference reference;
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
struct iterator_traits<T*>
|
||||
{
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef T value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef T* pointer;
|
||||
typedef T& reference;
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
struct iterator_traits<const T*>
|
||||
{
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
typedef T value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef const T* pointer;
|
||||
typedef const T& reference;
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
// https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/
|
||||
23
headers/lexicographical_compare.hpp
Normal file
23
headers/lexicographical_compare.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#ifndef LEXICOGRAPHICAL_COMPARE_HPP
|
||||
# define LEXICOGRAPHICAL_COMPARE_HPP
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <class InputIt1, class InputIt2>
|
||||
bool lexicographical_compare
|
||||
( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ) {
|
||||
|
||||
for (; first1 != last1; first1++, first2++) {
|
||||
if (*first1 < *first2)
|
||||
return true;
|
||||
if (*first2 < *first1)
|
||||
return false;
|
||||
}
|
||||
return (first1 == last1 && first2 != last2);
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
23
headers/make_pair.hpp
Normal file
23
headers/make_pair.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef MAKE_PAIR_HPP
|
||||
# define MAKE_PAIR_HPP
|
||||
|
||||
# define PR_TPL template < class T1, class T2 >
|
||||
# define PR pair<T1, T2>
|
||||
|
||||
#include "pair.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
PR_TPL PR
|
||||
make_pair(T1 x, T2 y) {
|
||||
|
||||
return PR(x, y) ;
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# undef PR
|
||||
# undef PR_TPL
|
||||
|
||||
#endif
|
||||
|
||||
222
headers/map.hpp
Normal file
222
headers/map.hpp
Normal file
@@ -0,0 +1,222 @@
|
||||
|
||||
#ifndef MAP_HPP
|
||||
# define MAP_HPP
|
||||
|
||||
# include <memory> // std::allocator
|
||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||
# include <algorithm> // std::max()
|
||||
# include <functional> // std::less, std::binary_function
|
||||
|
||||
# include "reverse_iterator.hpp"
|
||||
# include "equal.hpp"
|
||||
# include "lexicographical_compare.hpp"
|
||||
|
||||
# include "pair.hpp"
|
||||
# include "make_pair.hpp"
|
||||
# include "map_node.hpp"
|
||||
# include "map_iterator.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <
|
||||
class Key, // map::key_type
|
||||
class T, // map::mapped_type
|
||||
class Compare = std::less<Key>, // map::key_compare
|
||||
class Alloc = std::allocator< ft::pair<const Key, T> > // map::allocator_type
|
||||
> class map {
|
||||
|
||||
public:
|
||||
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Alloc allocator_type;
|
||||
|
||||
typedef map_iterator<Key, T, Compare, Alloc> iterator;
|
||||
typedef map_const_iterator<Key, T, Compare, Alloc> const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> 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> {
|
||||
|
||||
friend class map;
|
||||
protected:
|
||||
Compare comp;
|
||||
value_compare(Compare c) : comp(c) {}
|
||||
public:
|
||||
bool operator() (const value_type& x, const value_type& y) const
|
||||
{ return comp(x.first, y.first); }
|
||||
};
|
||||
|
||||
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
// 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();
|
||||
// operator= ---------------------------------
|
||||
map& operator= (const map& x);
|
||||
|
||||
|
||||
/*************
|
||||
* iterators :
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
// end ---------------------------------------
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
// rbegin ------------------------------------
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
// rend --------------------------------------
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
|
||||
|
||||
/************
|
||||
* capacity :
|
||||
************/
|
||||
// empty -------------------------------------
|
||||
bool empty() const;
|
||||
// size --------------------------------------
|
||||
size_type size() const;
|
||||
// max_size ----------------------------------
|
||||
size_type max_size() const;
|
||||
|
||||
|
||||
/******************
|
||||
* element access :
|
||||
******************/
|
||||
// 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:
|
||||
size_type _size;
|
||||
node<value_type>* _root;
|
||||
sentinel<value_type>* _sentinel;
|
||||
Compare _comp;
|
||||
Alloc _allocator;
|
||||
|
||||
// https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
|
||||
typename Alloc::template rebind< node<value_type> >::other _allocator_node;
|
||||
typename Alloc::template rebind< sentinel<value_type> >::other _allocator_sentinel;
|
||||
|
||||
// BBST
|
||||
enum {INSERT, ERASE};
|
||||
void _balance(node<value_type>* n, bool action);
|
||||
short _compute_height(node<value_type>* n);
|
||||
short _balance_factor(node<value_type>* n);
|
||||
node<value_type>* _rotate_left(node<value_type>* n);
|
||||
node<value_type>* _rotate_right(node<value_type>* n);
|
||||
};
|
||||
|
||||
|
||||
/************************
|
||||
* 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
|
||||
|
||||
# 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
|
||||
|
||||
193
headers/map_iterator.hpp
Normal file
193
headers/map_iterator.hpp
Normal file
@@ -0,0 +1,193 @@
|
||||
#ifndef MAP_ITERATOR_HPP
|
||||
# define MAP_ITERATOR_HPP
|
||||
|
||||
# include <cstddef> // NULL, ptrdiff_t
|
||||
# include <iterator> // iterator_tag
|
||||
|
||||
# include "pair.hpp"
|
||||
# include "map_node.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <
|
||||
typename Key,
|
||||
typename T,
|
||||
typename Compare,
|
||||
typename Allocator
|
||||
> class map_iterator {
|
||||
|
||||
private:
|
||||
typedef map_iterator self;
|
||||
|
||||
public:
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef value_type* pointer;
|
||||
typedef value_type& reference;
|
||||
|
||||
map_iterator() : _node(), _sentinel() {}
|
||||
map_iterator(node<value_type>* n, sentinel<value_type>* sentinel)
|
||||
: _node(n), _sentinel(sentinel) {}
|
||||
|
||||
reference operator*() const {
|
||||
return _node->value; }
|
||||
pointer operator->() const {
|
||||
return &_node->value; }
|
||||
|
||||
self& operator++() {
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->min();
|
||||
else if (_node->right)
|
||||
_node = _node->right->min();
|
||||
else
|
||||
{
|
||||
node<value_type>* up = _node->up;
|
||||
while (up != NULL && _node == up->right)
|
||||
{
|
||||
_node = up;
|
||||
up = up->up;
|
||||
}
|
||||
_node = up;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self& operator--() {
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->max();
|
||||
else if (_node->left)
|
||||
_node = _node->left->max();
|
||||
else
|
||||
{
|
||||
node<value_type>* up = _node->up;
|
||||
while (up != NULL && _node == up->left)
|
||||
{
|
||||
_node = up;
|
||||
up = up->up;
|
||||
}
|
||||
_node = up;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self operator++(int) {
|
||||
self old = *this;
|
||||
++(*this);
|
||||
return old;
|
||||
}
|
||||
|
||||
self operator--(int) {
|
||||
self old = *this;
|
||||
--(*this);
|
||||
return old;
|
||||
}
|
||||
|
||||
node<value_type>* get_node() { return _node; }
|
||||
const node<value_type>* get_node() const { return _node; }
|
||||
const sentinel<value_type>* getSentinel() const { return _sentinel; }
|
||||
|
||||
friend bool operator==(const self &lhs, const self &rhs) {
|
||||
return lhs._node == rhs._node; }
|
||||
friend bool operator!=(const self &lhs, const self &rhs) {
|
||||
return !(lhs._node == rhs._node); }
|
||||
|
||||
private:
|
||||
node<value_type>* _node;
|
||||
sentinel<value_type>* _sentinel;
|
||||
};
|
||||
|
||||
template <
|
||||
typename Key,
|
||||
typename T,
|
||||
typename Compare,
|
||||
typename Allocator
|
||||
> class map_const_iterator {
|
||||
|
||||
private:
|
||||
typedef map_const_iterator self;
|
||||
|
||||
public:
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef const value_type* pointer;
|
||||
typedef const value_type& reference;
|
||||
|
||||
map_const_iterator() : _node(), _sentinel() {}
|
||||
map_const_iterator (
|
||||
const node<value_type>* node,
|
||||
const sentinel<value_type>* sentinel)
|
||||
: _node(node), _sentinel(sentinel) {}
|
||||
map_const_iterator (const map_iterator< Key, T, Compare, Allocator >& src)
|
||||
: _node(src.get_node()), _sentinel(src.getSentinel()) {}
|
||||
|
||||
reference operator*() const {
|
||||
return _node->value; }
|
||||
pointer operator->() const {
|
||||
return &_node->value; }
|
||||
|
||||
self& operator++() {
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->min();
|
||||
else if (_node->right)
|
||||
_node = _node->right->min();
|
||||
else
|
||||
{
|
||||
node<value_type>* up = _node->up;
|
||||
while (up != NULL && _node == up->right)
|
||||
{
|
||||
_node = up;
|
||||
up = up->up;
|
||||
}
|
||||
_node = up;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self& operator--() {
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->max();
|
||||
else if (_node->left)
|
||||
_node = _node->left->max();
|
||||
else
|
||||
{
|
||||
node<value_type>* up = _node->up;
|
||||
while (up != NULL && _node == up->left)
|
||||
{
|
||||
_node = up;
|
||||
up = up->up;
|
||||
}
|
||||
_node = up;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
self operator++(int) {
|
||||
self old = *this;
|
||||
++(*this);
|
||||
return old;
|
||||
}
|
||||
|
||||
self operator--(int) {
|
||||
self old = *this;
|
||||
--(*this);
|
||||
return old;
|
||||
}
|
||||
|
||||
node<value_type>* get_node() const { return _node; }
|
||||
|
||||
friend bool operator==(const self &lhs, const self &rhs) {
|
||||
return lhs._node == rhs._node; }
|
||||
friend bool operator!=(const self &lhs, const self &rhs) {
|
||||
return !(lhs._node == rhs._node); }
|
||||
|
||||
private:
|
||||
const node<value_type>* _node;
|
||||
const sentinel<value_type>* _sentinel;
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
56
headers/map_node.hpp
Normal file
56
headers/map_node.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
#ifndef MAP_NODE_HPP
|
||||
# define MAP_NODE_HPP
|
||||
|
||||
# include <cstddef> // NULL
|
||||
|
||||
namespace ft {
|
||||
|
||||
template < typename ValueType >
|
||||
struct node {
|
||||
|
||||
ValueType value;
|
||||
node *up;
|
||||
node *left;
|
||||
node *right;
|
||||
short height;
|
||||
|
||||
node(const ValueType& val)
|
||||
: value(val)
|
||||
, up(NULL)
|
||||
, left(NULL)
|
||||
, right(NULL)
|
||||
, height(1)
|
||||
{}
|
||||
|
||||
node* min() {
|
||||
|
||||
node* n = this;
|
||||
|
||||
while (n->left)
|
||||
n = n->left;
|
||||
return n;
|
||||
}
|
||||
|
||||
node* max() {
|
||||
|
||||
node* n = this;
|
||||
|
||||
while (n->right)
|
||||
n = n->right;
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
template < typename ValueType >
|
||||
struct sentinel {
|
||||
|
||||
node<ValueType> *child;
|
||||
|
||||
sentinel() : child(NULL) {}
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
86
headers/pair.hpp
Normal file
86
headers/pair.hpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#ifndef PAIR_HPP
|
||||
# define PAIR_HPP
|
||||
|
||||
# define PR_TPL template < class T1, class T2 >
|
||||
# define PR pair<T1, T2>
|
||||
|
||||
namespace ft {
|
||||
|
||||
PR_TPL
|
||||
struct pair {
|
||||
|
||||
typedef T1 first_type;
|
||||
typedef T2 second_type;
|
||||
|
||||
pair();
|
||||
template< typename U, typename V >
|
||||
pair(const pair<U,V>& pr);
|
||||
pair(const first_type& a, const second_type& b);
|
||||
|
||||
T1 first;
|
||||
T2 second;
|
||||
};
|
||||
|
||||
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
PR_TPL PR::
|
||||
pair() {}
|
||||
PR_TPL template< typename U, typename V > PR::
|
||||
pair(const pair<U,V>& pr)
|
||||
: first(pr.first)
|
||||
, second(pr.second) {
|
||||
}
|
||||
PR_TPL PR::
|
||||
pair(const first_type& a, const second_type& b)
|
||||
: first(a)
|
||||
, second(b) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/************************
|
||||
* non-member functions :
|
||||
************************/
|
||||
PR_TPL
|
||||
bool operator==(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return (lhs.first == rhs.first) && (lhs.second == rhs.second);
|
||||
}
|
||||
PR_TPL
|
||||
bool operator<(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return (lhs.first < rhs.first
|
||||
|| ( !(rhs.first < lhs.first) && (lhs.second < rhs.second) )
|
||||
);
|
||||
}
|
||||
PR_TPL
|
||||
bool operator!=(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
PR_TPL
|
||||
bool operator>(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return (rhs < lhs);
|
||||
}
|
||||
PR_TPL
|
||||
bool operator<=(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
PR_TPL
|
||||
bool operator>=(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# undef PR
|
||||
# undef PR_TPL
|
||||
|
||||
#endif
|
||||
|
||||
140
headers/reverse_iterator.hpp
Normal file
140
headers/reverse_iterator.hpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#ifndef REVERSE_ITERATOR_HPP
|
||||
# define REVERSE_ITERATOR_HPP
|
||||
|
||||
# include <iterator> // iterator_tag
|
||||
# include "iterator_traits.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
template < class Iter >
|
||||
class reverse_iterator {
|
||||
|
||||
private:
|
||||
|
||||
Iter _it;
|
||||
typedef ft::iterator_traits<Iter> _traits;
|
||||
|
||||
public:
|
||||
typedef Iter iterator_type;
|
||||
typedef typename _traits::iterator_category iterator_category;
|
||||
typedef typename _traits::value_type value_type;
|
||||
typedef typename _traits::difference_type difference_type;
|
||||
typedef typename _traits::pointer pointer;
|
||||
typedef typename _traits::reference reference;
|
||||
|
||||
|
||||
/****************
|
||||
* constructors :
|
||||
****************/
|
||||
reverse_iterator()
|
||||
: _it()
|
||||
{}
|
||||
explicit reverse_iterator(iterator_type it)
|
||||
: _it(it)
|
||||
{}
|
||||
// reverse_iterator(const reverse_iterator& src)
|
||||
// : _it(src._it)
|
||||
// {}
|
||||
template < class Iterator >
|
||||
reverse_iterator(const reverse_iterator<Iterator>& rev_it)
|
||||
: _it(rev_it.base())
|
||||
{}
|
||||
|
||||
/*********************
|
||||
* members functions :
|
||||
*********************/
|
||||
// base --------------------------------------
|
||||
iterator_type base() const
|
||||
{ return _it; }
|
||||
// operator * --------------------------------
|
||||
reference operator*() const
|
||||
{ Iter tmp = _it; return *--tmp; }
|
||||
// operator + --------------------------------
|
||||
reverse_iterator operator+(difference_type n) const
|
||||
{ return reverse_iterator(_it - n); }
|
||||
// operator ++ -------------------------------
|
||||
reverse_iterator operator++(int)
|
||||
{ reverse_iterator old(*this); ++(*this); return old; }
|
||||
reverse_iterator & operator++()
|
||||
{ --_it; return *this; }
|
||||
// operator += -------------------------------
|
||||
reverse_iterator & operator+=(difference_type n)
|
||||
{ _it -= n; return *this; }
|
||||
// operator - --------------------------------
|
||||
reverse_iterator operator-(difference_type n) const
|
||||
{ return reverse_iterator(_it + n); }
|
||||
// operator -- -------------------------------
|
||||
reverse_iterator operator--(int)
|
||||
{ reverse_iterator old(*this); --(*this); return old; }
|
||||
reverse_iterator & operator--()
|
||||
{ ++_it; return *this; }
|
||||
// operator -= -------------------------------
|
||||
reverse_iterator & operator-=(difference_type n)
|
||||
{ _it += n; return *this; }
|
||||
// operator -> -------------------------------
|
||||
pointer operator->() const
|
||||
{ return &(operator*()); }
|
||||
// operator [] -------------------------------
|
||||
reference operator[] (difference_type n) const
|
||||
{ return _it[-n-1]; }
|
||||
};
|
||||
|
||||
/*************************
|
||||
* non-members functions :
|
||||
*************************/
|
||||
// operator == -------------------------------
|
||||
template <class Iterator>
|
||||
bool operator== (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() == rhs.base(); }
|
||||
// operator != -------------------------------
|
||||
template <class Iterator>
|
||||
bool operator!= (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() != rhs.base(); }
|
||||
// operator < --------------------------------
|
||||
template <class Iterator>
|
||||
bool operator< (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() > rhs.base(); }
|
||||
// operator <= -------------------------------
|
||||
template <class Iterator>
|
||||
bool operator<= (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() >= rhs.base(); }
|
||||
// operator > --------------------------------
|
||||
template <class Iterator>
|
||||
bool operator> (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() < rhs.base(); }
|
||||
// operator >= -------------------------------
|
||||
template <class Iterator>
|
||||
bool operator>= (const reverse_iterator<Iterator>& lhs,
|
||||
const reverse_iterator<Iterator>& rhs)
|
||||
{ return lhs.base() <= rhs.base(); }
|
||||
|
||||
|
||||
/*******************
|
||||
* other operators :
|
||||
*******************/
|
||||
// operator + non-member overload ------------
|
||||
template <class Iterator>
|
||||
reverse_iterator<Iterator> operator+
|
||||
(typename reverse_iterator<Iterator>::difference_type n
|
||||
,const reverse_iterator<Iterator>& rhs)
|
||||
{ return reverse_iterator<Iterator>(rhs.base() - n); }
|
||||
|
||||
// operator - non-member overload ------------
|
||||
template <class Iterator>
|
||||
typename reverse_iterator<Iterator>::difference_type operator-
|
||||
(const reverse_iterator<Iterator>& lhs
|
||||
,const reverse_iterator<Iterator>& rhs)
|
||||
{ return rhs.base() - lhs.base(); }
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
68
headers/stack.hpp
Normal file
68
headers/stack.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
#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());
|
||||
|
||||
|
||||
/**********************
|
||||
* overload functions :
|
||||
**********************/
|
||||
// empty -------------------------------------
|
||||
bool empty() const ;
|
||||
// size --------------------------------------
|
||||
size_type size() const ;
|
||||
// top ---------------------------------------
|
||||
value_type& top();
|
||||
const value_type& top() const ;
|
||||
// push --------------------------------------
|
||||
void push(const value_type& value);
|
||||
// pop ---------------------------------------
|
||||
void pop();
|
||||
|
||||
/*********************************
|
||||
* 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_type c;
|
||||
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# include "stack.tpp"
|
||||
|
||||
#endif
|
||||
|
||||
188
headers/vector.hpp
Normal file
188
headers/vector.hpp
Normal file
@@ -0,0 +1,188 @@
|
||||
#ifndef VECTOR_HPP
|
||||
# define VECTOR_HPP
|
||||
|
||||
# include "colors.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <memory> // std::allocator
|
||||
# include <algorithm> // std::min, std::max
|
||||
# include <stdexcept> // out_of_range, length_error, logic_error
|
||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||
|
||||
# include "enable_if.hpp"
|
||||
# include "is_integral.hpp"
|
||||
# include "reverse_iterator.hpp"
|
||||
# include "equal.hpp"
|
||||
# include "lexicographical_compare.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <
|
||||
class T,
|
||||
class Allocator = std::allocator<T> >
|
||||
class vector {
|
||||
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef Allocator allocator_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
typedef T * iterator;
|
||||
typedef T const * const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
// dependent qualified name :
|
||||
// https://en.cppreference.com/w/cpp/keyword/typename
|
||||
typedef typename Allocator::reference reference;
|
||||
typedef typename Allocator::const_reference const_reference;
|
||||
|
||||
|
||||
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
// constructors ------------------------------
|
||||
explicit vector (const allocator_type & alloc = allocator_type());
|
||||
explicit vector (size_type n, const value_type& val = value_type(),
|
||||
const allocator_type& alloc = allocator_type());
|
||||
template <class InputIterator>
|
||||
vector (InputIterator first, InputIterator last,
|
||||
const allocator_type& alloc = allocator_type());
|
||||
vector( vector const & src );
|
||||
// destructor --------------------------------
|
||||
~vector();
|
||||
// operator= ---------------------------------
|
||||
vector & operator=( vector const & rhs );
|
||||
|
||||
|
||||
/*************
|
||||
* iterators :
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
// end ---------------------------------------
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
// rbegin ------------------------------------
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
// rend --------------------------------------
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
|
||||
|
||||
/************
|
||||
* capacity :
|
||||
************/
|
||||
// size --------------------------------------
|
||||
size_type size() const;
|
||||
// max_size ----------------------------------
|
||||
size_type max_size() const;
|
||||
// resize ------------------------------------
|
||||
void resize(size_type n, value_type val = value_type());
|
||||
// capacity ----------------------------------
|
||||
size_type capacity() const;
|
||||
// empty -------------------------------------
|
||||
bool empty() const;
|
||||
// reserve -----------------------------------
|
||||
void reserve(size_type n);
|
||||
|
||||
|
||||
/******************
|
||||
* element access :
|
||||
******************/
|
||||
// operator[] --------------------------------
|
||||
reference operator[](size_type n);
|
||||
const_reference operator[](size_type n) const;
|
||||
// at ----------------------------------------
|
||||
reference at(size_type n);
|
||||
const_reference at(size_type n) const;
|
||||
// front -------------------------------------
|
||||
reference front();
|
||||
const_reference front() const;
|
||||
// back --------------------------------------
|
||||
reference back();
|
||||
const_reference back() const;
|
||||
|
||||
|
||||
/*************
|
||||
* modifiers :
|
||||
*************/
|
||||
// assign ------------------------------------
|
||||
template <class InputIterator>
|
||||
typename enable_if< !is_integral<InputIterator>::value,void >::type
|
||||
assign(InputIterator first, InputIterator last);
|
||||
void assign(size_type n, const value_type& val);
|
||||
// push_back ---------------------------------
|
||||
void push_back(const value_type & val);
|
||||
// pop_back ----------------------------------
|
||||
void pop_back();
|
||||
// insert ------------------------------------
|
||||
iterator insert(iterator position, const value_type& val);
|
||||
void insert(iterator position, size_type n, const value_type& val);
|
||||
template <class InputIterator>
|
||||
typename enable_if< !is_integral<InputIterator>::value,void >::type
|
||||
insert(iterator position, InputIterator first, InputIterator last);
|
||||
// erase -------------------------------------
|
||||
iterator erase(iterator position);
|
||||
iterator erase(iterator first, iterator last);
|
||||
// swap --------------------------------------
|
||||
void swap(vector& x);
|
||||
// clear -------------------------------------
|
||||
void clear();
|
||||
|
||||
|
||||
/*************
|
||||
* allocator :
|
||||
*************/
|
||||
// get_allocator -----------------------------
|
||||
allocator_type get_allocator() const;
|
||||
|
||||
private:
|
||||
|
||||
size_type _size;
|
||||
size_type _capacity;
|
||||
value_type * _mem_ptr;
|
||||
allocator_type _allocator;
|
||||
|
||||
void _destroy(iterator first, iterator last);
|
||||
void _increment_capacity(size_type n);
|
||||
|
||||
};
|
||||
|
||||
|
||||
/************************
|
||||
* non-member functions :
|
||||
************************/
|
||||
// operator == -------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator== (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// operator != -------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// operator < --------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator< (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// operator <= -------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator<= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// operator > --------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator> (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// operator >= -------------------------------
|
||||
template <class T, class Alloc>
|
||||
bool operator>= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
|
||||
// swap (vector) -----------------------------
|
||||
template <class T, class Alloc>
|
||||
void swap (vector<T,Alloc>& x, vector<T,Alloc>& y);
|
||||
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# include "vector.tpp"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user