diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-01 14:17:04 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-15 12:59:58 +0200 |
| commit | be2676e56ae0c72321c60c4848ce720ccf095b26 (patch) | |
| tree | de96e98403ea8740cea76d9081c932cbbf8edb44 | |
| parent | 7af930f2242f933f79dfbb4ddc84bfd532069556 (diff) | |
| download | ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.tar.gz ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.tar.bz2 ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.zip | |
Remaking List, Removed include/, test/ and Makefile
| -rw-r--r-- | List.hpp | 204 | ||||
| -rw-r--r-- | Makefile | 45 | ||||
| -rw-r--r-- | Stack.hpp (renamed from include/Stack.hpp) | 0 | ||||
| -rw-r--r-- | Vector.hpp (renamed from include/Vector.hpp) | 0 | ||||
| -rw-r--r-- | include/List.hpp | 186 | ||||
| -rw-r--r-- | include/ft_containers.hpp | 22 | ||||
| -rw-r--r-- | main.cpp (renamed from test/main.cpp) | 9 | ||||
| -rw-r--r-- | test/test_list.cpp | 47 |
8 files changed, 207 insertions, 306 deletions
diff --git a/List.hpp b/List.hpp new file mode 100644 index 0000000..64fd14a --- /dev/null +++ b/List.hpp @@ -0,0 +1,204 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* List.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/01 09:31:07 by cacharle #+# #+# */ +/* Updated: 2020/04/15 12:57:58 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_HPP +# define LIST_HPP + +# include <cstddef> +# include <memory> + +namespace ft +{ + template < typename T, typename Alloc = std::allocator<T> > + class List + { + public: + typedef T value_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 ptrdiff_t difference_type; + typedef size_t size_type; + + explicit List(const allocator_type& alloc = allocator_type()) + : m_front(NULL), m_back(NULL), m_size(0) {} + + List(const List& other) { + *this = other; + } + + List& operator=(const List& other) { + if (*this == other) + return *this; + delete m_front; + m_front = NULL; + m_back = NULL; + // use iterator + return *this; + } + + ~List() { + clear(); + } + + explicit List(size_type n, + const value_type& val = value_type(), + const allocator_type& alloc = allocator_type()) {} + + template<typename InputIterator> + List(InputIterator first, + InputIterator last, + const allocator_type& alloc = allocator_type()) {} + + private: + class ListNode + { + public: + ListNode() : m_next(NULL), m_prev(NULL), m_data(value_type()) {} + + ListNode(const ListNode& other) { + if (*this != other) + *this = other; + } + + ListNode& operator=(const ListNode& other) { + m_next = other.m_next; + m_prev = other.m_prev; + m_data = other.m_data; + return *this; + } + + ~ListNode() { + if (m_next != NULL) { + m_next->m_prev = NULL; + delete m_next; + } + if (m_prev != NULL) { + m_prev->m_next = NULL; + delete m_prev; + } + } + + ListNode(T& data) : m_next(NULL), m_prev(NULL), m_data(data) {} + + ListNode* getNext() const { return m_next; } + ListNode* getPrev() const { return m_prev; } + T& getData() const { return m_data; } + + private: + ListNode* m_next; + ListNode* m_prev; + T m_data; + }; + + public: + class iterator + { + public: + iterator() : m_current(NULL) {} + + iterator(const iterator& other) { + *this = other; + } + + iterator& operator=(const iterator& other) { + if (*this == other) + return *this; + m_current = other.m_current; + return *this; + } + + ~iterator() {} + + iterator(ListNode* current) : m_current(current) {} + + T& operator*() { + return m_current->getData(); + } + + bool operator==(const iterator& other) { + return m_current == other.m_current; + } + bool operator!=(const iterator& other) { return !(operator==(other)); } + + iterator& operator++(int) { // post + m_current = m_current->getNext(); + return *this; + } + iterator& operator++() { return operator++; } + + iterator& operator--(int) { + m_current = m_current->getPrev(); + return *this; + } + iterator& operator--() { return operator++; } + + private: + List::ListNode* m_current; + }; + + iterator begin() { return iterator(m_front); } + iterator end() { return iterator(NULL); } + + bool empty() const { return m_front == NULL; } + size_type size() const { return m_size; } + size_type max_size() const { return m_size; } + + reference front() { return m_front->getData(); } + reference back() { return m_back->getData(); } + + void push_front(const value_type& val) { + ListNode* new_front = new ListNode(val); + new_front->setNext(m_front); + m_front->setPrev(new_front); + m_front = new_front; + } + + void push_back(const value_type& val) { + ListNode* new_back = new ListNode(val); + new_back->setPrev(m_back); + m_back->setNext(new_back); + m_back = new_back; + } + + void pop_front() { + ListNode* tmp = m_front->getNext(); + tmp->setPrev(NULL); + delete m_front; + m_front = tmp; + } + + void pop_back() { + ListNode* tmp = m_back->getPrev(); + tmp->setNext(NULL); + delete m_back; + m_back = tmp; + } + + void clear() { + delete m_front; + m_front = NULL; + m_back = NULL; + m_size = 0; + } + + + private: + ListNode* m_front; + ListNode* m_back; + size_type m_size; + }; +} + +#endif diff --git a/Makefile b/Makefile deleted file mode 100644 index 26db30e..0000000 --- a/Makefile +++ /dev/null @@ -1,45 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: cacharle <marvin@42.fr> +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2020/02/01 09:48:56 by cacharle #+# #+# # -# Updated: 2020/02/01 10:11:21 by cacharle ### ########.fr # -# # -# **************************************************************************** # - -RM = rm -f - -INCLUDE_DIR = include -TEST_DIR = test -OBJ_DIR = obj - -CC = clang++ -CCFLAGS = -I$(INCLUDE_DIR) -Wall -Wextra #-Werror - -INCLUDE = $(shell find $(INCLUDE_DIR) -type f -name "*.hpp") -TEST_SRC = $(shell find $(TEST_DIR) -type f -name "*.cpp") -OBJ = $(TEST_SRC:$(TEST_DIR)/%.cpp=$(OBJ_DIR)/%.o) - -NAME = ft_containers_test - -all: prebuild $(NAME) - -prebuild: - if [ ! -d $(OBJ_DIR) ]; then mkdir $(OBJ_DIR); fi - -$(NAME): $(OBJ) - $(CC) -o $@ $^ - -$(OBJ_DIR)/%.o: $(TEST_DIR)/%.cpp $(INCLUDE) - $(CC) $(CCFLAGS) -c -o $@ $< - -clean: - $(RM) $(OBJ) - -fclean: clean - $(RM) $(NAME) - -re: fclean all diff --git a/include/Stack.hpp b/Stack.hpp index 64bfc66..64bfc66 100644 --- a/include/Stack.hpp +++ b/Stack.hpp diff --git a/include/Vector.hpp b/Vector.hpp index 0f21995..0f21995 100644 --- a/include/Vector.hpp +++ b/Vector.hpp diff --git a/include/List.hpp b/include/List.hpp deleted file mode 100644 index 096660c..0000000 --- a/include/List.hpp +++ /dev/null @@ -1,186 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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 diff --git a/include/ft_containers.hpp b/include/ft_containers.hpp deleted file mode 100644 index 76637ac..0000000 --- a/include/ft_containers.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_containers.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/01 10:09:01 by cacharle #+# #+# */ -/* Updated: 2020/02/01 10:09:50 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_CONTAINERS_HPP -# define FT_CONTAINERS_HPP - -# include <iostream> - -# include "List.hpp" - -void test_list_base(); - -#endif @@ -6,17 +6,14 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 10:06:52 by cacharle #+# #+# */ -/* Updated: 2020/02/01 10:12:49 by cacharle ### ########.fr */ +/* Updated: 2020/04/15 10:56:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "ft_containers.hpp" +#include "List.hpp" int main() { - std::cout << "=== ft_containers ===" << std::endl << std::endl; - std::cout << "TEST: List.hpp" << std::endl; - test_list_base(); - + ft::List<int> l; return 0; } diff --git a/test/test_list.cpp b/test/test_list.cpp deleted file mode 100644 index ad50d66..0000000 --- a/test/test_list.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* test_list.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/01 09:49:44 by cacharle #+# #+# */ -/* Updated: 2020/02/01 10:33:00 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_containers.hpp" - -// template < typename T > -// static void print_list(ft::List<T> l) -// { -// std::cout << "List:" << std::endl << " size: " << l.size() << " content: "; -// for (ft::List<T>::iterator i = l.begin(); i != l.end(); i++) -// std::cout << "[" << *i << "] "; -// } - -void test_list_base() -{ - ft::List<int> l; - - std::cout << l.size() << std::endl; - l.push_front(1); - std::cout << l.size() << std::endl; - l.push_front(1); - l.push_front(2); - l.push_front(5); - l.push_front(8); - l.push_front(1); - l.push_front(10); - std::cout << l.size() << std::endl; - l.pop_front(); - std::cout << l.size() << std::endl; - - l.push_back(5); - l.push_back(5); - std::cout << l.size() << std::endl; - l.pop_back(); - l.pop_back(); - std::cout << l.size() << std::endl; -} - |
