diff options
Diffstat (limited to 'include/List.hpp')
| -rw-r--r-- | include/List.hpp | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/include/List.hpp b/include/List.hpp new file mode 100644 index 0000000..096660c --- /dev/null +++ b/include/List.hpp @@ -0,0 +1,186 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* List.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/01 09:31:07 by cacharle #+# #+# */ +/* Updated: 2020/02/01 10:36:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_HPP +# define LIST_HPP + +# include <memory> + +namespace ft +{ + template < typename T, typename Alloc = std::allocator<T> > + class List + { + + typedef T value_type; + typedef Alloc allocator_type; + typedef typename allocator_type::reference reference; + // typename allocator_type::const_reference const_reference; + // typename allocator_type::pointer pointer; + // typename allocator_type::const_pointer const_pointer; + + typedef size_t size_type; + + public: + List(const allocator_type& alloc = allocator_type()) + { + ptr_front = nullptr; + ptr_back = nullptr; + p_size = 0; + } + // List(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); + // template <class InputIterator> + // List(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); + // List(const list& x); + ~List() + { + while (size() > 0) + pop_front(); + } + // List& operator = (const List& x); + + // iterators + // iterator begin(); + // iterator end(); + // reverse_iterator rbegin(); + // reverse_iterator rend(); + // const_iterator begin() const; + // const_iterator end() const; + // const_reverse_iterator rbegin() const; + // const_reverse_iterator rend() const; + + // capacity + bool empty() const + { + return ptr_front == nullptr; + } + size_type size() const + { + return p_size; + } + + // size_type max_size() const; + + // element access + reference front() + { + return *ptr_front; + } + + reference back() + { + return *ptr_back; + } + // const_reference front() const; + // const_reference back(); + + // modifiers + // template <class InputIterator> + // void assign (InputIterator first, InputIterator last); + // void assign (size_type n, const value_type& val); + void push_front (const value_type &val) + { + PrivateList *nfront = new PrivateList(val); + nfront->next = ptr_front; + ptr_front = nfront; + if (ptr_back == nullptr) + ptr_back = ptr_front; + p_size++; + } + void pop_front() + { + PrivateList *nfront = ptr_front->next; + if (size() == 1) + ptr_back = nullptr; + delete ptr_front; + ptr_front = nfront; + p_size--; + } + void push_back (const value_type& val) + { + PrivateList *nback = new PrivateList(val); + if (empty()) + { + ptr_back = nback; + ptr_front = ptr_back; + return; + } + ptr_back->next = nback; + ptr_back = nback; + p_size++; + } + void pop_back() + { + PrivateList *tmp = ptr_front; + + while (tmp->next != ptr_back) + tmp = tmp->next; + delete ptr_back; + ptr_back = tmp; + p_size--; + } + // iterator insert (iterator position, const value_type& val); + // void insert (iterator position, size_type n, const value_type& val); + // template <class InputIterator> + // void insert (iterator position, InputIterator first, InputIterator last); + // iterator erase (iterator position); + // iterator erase (iterator first, iterator last); + // void swap (list& x); + // void resize (size_type n, value_type val = value_type()); + // void clear(); + + // operations + // void splice (iterator position, list& x); + // void splice (iterator position, list& x, iterator i); + // void splice (iterator position, list& x, iterator first, iterator last); + // void remove (const value_type& val); + // template <class Predicate> + // void remove_if (Predicate pred); + // void unique(); + // template <class BinaryPredicate> + // void unique (BinaryPredicate binary_pred); + // + // void merge (list& x); + // template <class Compare> + // void merge (list& x, Compare comp); + // void sort(); + // template <class Compare> + // void sort (Compare comp); + // void reverse(); + + // observers + // allocator_type get_allocator() const; + + private: + class PrivateList + { + public: + PrivateList(const value_type &val) : val(val) + { + next = nullptr; + } + + // ~PrivateList() + // { + // delete val; + // } + + PrivateList *next; + const value_type &val; + }; + PrivateList *ptr_front; + PrivateList *ptr_back; + size_type p_size; + }; +} + +#endif |
