diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-01 10:37:15 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-01 10:37:15 +0100 |
| commit | 7af930f2242f933f79dfbb4ddc84bfd532069556 (patch) | |
| tree | bc24aae2b8f8f446bdfbe83f56336a7c6d7a98db | |
| parent | 6e191a07bbc57d73152ba886b6f76f694a97e525 (diff) | |
| download | ft_containers-7af930f2242f933f79dfbb4ddc84bfd532069556.tar.gz ft_containers-7af930f2242f933f79dfbb4ddc84bfd532069556.tar.bz2 ft_containers-7af930f2242f933f79dfbb4ddc84bfd532069556.zip | |
Basic list methods
| -rw-r--r-- | Makefile | 24 | ||||
| -rw-r--r-- | include/List.hpp (renamed from src/List.hpp) | 112 | ||||
| -rw-r--r-- | include/Stack.hpp (renamed from src/Stack.hpp) | 0 | ||||
| -rw-r--r-- | include/Vector.hpp (renamed from src/Vector.hpp) | 0 | ||||
| -rw-r--r-- | include/ft_containers.hpp | 22 | ||||
| -rw-r--r-- | test/main.cpp | 19 | ||||
| -rw-r--r-- | test/test_list.cpp | 47 |
7 files changed, 161 insertions, 63 deletions
@@ -1,15 +1,27 @@ -RM = rm -f +# **************************************************************************** # +# # +# ::: :::::::: # +# 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 # +# # +# **************************************************************************** # -CC = clang++ -CCFLAGS = -Wall -Wextra #-Werror +RM = rm -f -SRC_DIR = src +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) -$(info $(OBJ)) NAME = ft_containers_test @@ -21,7 +33,7 @@ prebuild: $(NAME): $(OBJ) $(CC) -o $@ $^ -$(OBJ_DIR)/%.o: $(TEST_DIR)/%.cpp +$(OBJ_DIR)/%.o: $(TEST_DIR)/%.cpp $(INCLUDE) $(CC) $(CCFLAGS) -c -o $@ $< clean: diff --git a/src/List.hpp b/include/List.hpp index aca8f06..096660c 100644 --- a/src/List.hpp +++ b/include/List.hpp @@ -6,34 +6,36 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 09:31:07 by cacharle #+# #+# */ -/* Updated: 2020/02/01 09:38:20 by cacharle ### ########.fr */ +/* 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 = allocator<T> > + template < typename T, typename Alloc = std::allocator<T> > class List { typedef T value_type; typedef Alloc allocator_type; - typedef allocator_type::reference reference; - typedef allocator_type::const_reference const_reference; - typedef allocator_type::pointer pointer; - typedef allocator_type::const_pointer const_pointer; + 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()); + List(const allocator_type& alloc = allocator_type()) { - front = nullptr; - back = nullptr; - size = 0; + 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> @@ -41,89 +43,90 @@ namespace ft // List(const list& x); ~List() { - while (size > 0) + while (size() > 0) pop_front(); } // List& operator = (const List& x); // iterators // iterator begin(); - // const_iterator begin() const; // iterator end(); - // const_iterator end() const; // reverse_iterator rbegin(); - // const_reverse_iterator rbegin() const; // 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 front == nullptr; + return ptr_front == nullptr; } size_type size() const { - return size; + return p_size; } - size_type max_size() const; + // size_type max_size() const; // element access reference front() { - return *front; + return *ptr_front; } reference back() { - return *back; + return *ptr_back; } - const_reference front() const; - const_reference 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) + void push_front (const value_type &val) { PrivateList *nfront = new PrivateList(val); - nfront->next = front; - front = nfront; - if (back == nullptr) - back = front; - size++; + nfront->next = ptr_front; + ptr_front = nfront; + if (ptr_back == nullptr) + ptr_back = ptr_front; + p_size++; } void pop_front() { - t_llist *nfront = front->next; - if (size == 1) - back = nullptr; - delete front; - front = nfront; - size--; + 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()) { - back = nback; - front = back; + ptr_back = nback; + ptr_front = ptr_back; return; } - back->next = nback; - back = nback; - size++; + ptr_back->next = nback; + ptr_back = nback; + p_size++; } void pop_back() { - t_llist *tmp = front; + PrivateList *tmp = ptr_front; - while (tmp->next != back) + while (tmp->next != ptr_back) tmp = tmp->next; - delete back; - back = tmp; + 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); @@ -161,23 +164,22 @@ namespace ft class PrivateList { public: - PrivateList(const value_type& val) + PrivateList(const value_type &val) : val(val) { next = nullptr; - val = val; - } - - ~PrivateList() - { - delete val; } + // ~PrivateList() + // { + // delete val; + // } + PrivateList *next; - T val; - } - PrivateList *front; - PrivateList *back; - size_type size; + const value_type &val; + }; + PrivateList *ptr_front; + PrivateList *ptr_back; + size_type p_size; }; } diff --git a/src/Stack.hpp b/include/Stack.hpp index 64bfc66..64bfc66 100644 --- a/src/Stack.hpp +++ b/include/Stack.hpp diff --git a/src/Vector.hpp b/include/Vector.hpp index 0f21995..0f21995 100644 --- a/src/Vector.hpp +++ b/include/Vector.hpp diff --git a/include/ft_containers.hpp b/include/ft_containers.hpp new file mode 100644 index 0000000..76637ac --- /dev/null +++ b/include/ft_containers.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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 diff --git a/test/main.cpp b/test/main.cpp index 156eae7..761095f 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,7 +1,22 @@ -#include <iostream> +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/01 10:06:52 by cacharle #+# #+# */ +/* Updated: 2020/02/01 10:12:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_containers.hpp" int main() { - std::cout << "bonjour" << std::endl; + std::cout << "=== ft_containers ===" << std::endl << std::endl; + std::cout << "TEST: List.hpp" << std::endl; + test_list_base(); + return 0; } diff --git a/test/test_list.cpp b/test/test_list.cpp new file mode 100644 index 0000000..ad50d66 --- /dev/null +++ b/test/test_list.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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; +} + |
