From 46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 31 Jan 2020 12:46:19 +0100 Subject: List split cpp hpp (may be ridiculous and may have forgot how templates work) --- List.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ List.hpp | 95 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- Stack.htpp | 29 ++++++++++++++ Vector.hpp | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ list.hpp | 78 ------------------------------------- stack.hpp | 29 -------------- vector.hpp | 128 ------------------------------------------------------------- 8 files changed, 342 insertions(+), 236 deletions(-) create mode 100644 List.cpp create mode 100644 List.hpp create mode 100644 Stack.htpp create mode 100644 Vector.hpp delete mode 100644 list.hpp delete mode 100644 stack.hpp delete mode 100644 vector.hpp diff --git a/List.cpp b/List.cpp new file mode 100644 index 0000000..f88ca36 --- /dev/null +++ b/List.cpp @@ -0,0 +1,89 @@ +#include "List.hpp" + +ft::List::List(const allocator_type& alloc = allocator_type()) +{ + front = nullptr; + back = nullptr; + size = 0; + +} + +ft::List::~List() +{ + while (size > 0) + pop_front(); +} + +ft::List::bool empty() const +{ + return front == nullptr; +} +ft::List::size_type size() const +{ + return size; +} + +ft::List::reference front() +{ + return *front; +} + +ft::List::reference back() +{ + return *back; +} + +ft::List::void push_front (const value_type& val) +{ + PrivateList *nfront = new PrivateList(val); + nfront->next = front; + front = nfront; + if (back == nullptr) + back = front; + size++; +} + +ft::List::void pop_front() +{ + t_llist *nfront = front->next; + if (size == 1) + back = nullptr; + delete front; + front = nfront; + size--; +} + +ft::List::void push_back (const value_type& val) +{ + PrivateList *nback = new PrivateList(val); + if (empty()) + { + back = nback; + front = back; + return; + } + back->next = nback; + back = nback; + size++; +} + +ft::List::void pop_back() +{ + t_llist *tmp = front; + + while (tmp->next != back) + tmp = tmp->next; + delete back; + back = tmp; +} + +ft::List::PrivateList::PrivateList(const value_type& val); +{ + next = nullptr; + val = val; +} + +ft::List::PrivateList::~PrivateList() +{ + delete val; +} diff --git a/List.hpp b/List.hpp new file mode 100644 index 0000000..57a9a8f --- /dev/null +++ b/List.hpp @@ -0,0 +1,95 @@ +#ifndef LIST_HPP +# define LIST_HPP + +namespace ft +{ + template < class T, class Alloc = allocator > + class List + { + public: + List(const allocator_type& alloc = allocator_type()); + // List(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); + // template + // List(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); + // List(const list& x); + ~List(); + // 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_reverse_iterator rend() const; + + // capacity + bool empty() const; + size_type size() const; + size_type max_size() const; + + // element access + reference front(); + const_reference front() const; + reference back(); + const_reference back(); + + // modifiers + // template + // void assign (InputIterator first, InputIterator last); + // void assign (size_type n, const value_type& val); + void push_front (const value_type& val); + void pop_front(); + void push_back (const value_type& val); + void pop_back(); + // iterator insert (iterator position, const value_type& val); + // void insert (iterator position, size_type n, const value_type& val); + // template + // 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 + // void remove_if (Predicate pred); + // void unique(); + // template + // void unique (BinaryPredicate binary_pred); + // + // void merge (list& x); + // template + // void merge (list& x, Compare comp); + // void sort(); + // template + // void sort (Compare comp); + // void reverse(); + + // observers + // allocator_type get_allocator() const; + + private: + class PrivateList + { + public: + PrivateList(const value_type& val); + ~PrivateList(); + + PrivateList *next; + T val; + } + PrivateList *front; + PrivateList *back; + size_type size; + }; +} + +#endif diff --git a/README.md b/README.md index 915b177..ef3104c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # ft_containers -ft_containers project of school 42. +ft\_containers project of school 42. diff --git a/Stack.htpp b/Stack.htpp new file mode 100644 index 0000000..64bfc66 --- /dev/null +++ b/Stack.htpp @@ -0,0 +1,29 @@ +#ifndef STACK_HPP +# define STACK_HPP + +# include "vector.hpp" + +namespace ft +{ + template > + class stack : public vector + { + public: + explicit stack (const container_type& ctnr = container_type()); + value_type& top() + { + return back(); + } + void push (const value_type& val) + { + push_back(val); + } + void pop() + { + pop_back(); + } + }; +} + +#endif + diff --git a/Vector.hpp b/Vector.hpp new file mode 100644 index 0000000..0f21995 --- /dev/null +++ b/Vector.hpp @@ -0,0 +1,128 @@ +#ifndef VECTOR_HPP +# define VECTOR_HPP + +# include + +# define GROWTH_FACTOR 1.5 + +typedef size_t size_type; + + +namespace ft +{ + template < class T, class Alloc = allocator > + class vector + { + typedef T value_type; + typedef T& reference; + + public: + // capacity + size_type size() const; + { + return size; + } + void resize (size_type n, value_type val = value_type()) + { + if (n < size) + { + for (int i = n; i < size; i++) + ~T(under[i]); + size = n; + return; + } + if (n > capacity) + grow(); + size_type prev_n; + for (prev_n = n; prev_n < size; prev_n++) + under[n] = val; + size = n; + } + size_type capacity() const + { + return capacity; + } + bool empty() const + { + return size == 0; + } + void reserve (size_type n) + { + if (n > capacity) + grow_to(n); + } + + // element access + reference operator[] (size_type n) + { + return under[n]; + } + reference at (size_type n) + { + if (n >= size) + raise std::out_of_range; + return under[n]; + } + reference front() + { + return under[0]; + } + reference back() + { + return under[size - 1]; + } + + // modifiers + template + void assign (InputIterator first, InputIterator last); + void assign (size_type n, const value_type& val); + void push_back (const value_type& val) + { + if (size >= capacity) + grow(); + under[size] = val; + size++; + } + void pop_back() + { + ~T(back()); + size--; + } + iterator insert (iterator position, const value_type& val) + { + + } + void insert (iterator position, size_type n, const value_type& val); + template + void insert (iterator position, InputIterator first, InputIterator last); + + + + + private: + T *under; + size_type size; + size_type capacity; + + void grow() + { + capacity *= GROWTH_FACTOR; + grow_to(capacity); + } + + void grow_to(size_type new_capacity) + { + T *under_copy = new T[capacity]; + for (int i = 0; i < capacity; i++) + under_copy[i] = under[i]; + delete [] under; + capacity = new_capacity; + under = new T[capacity]; + for (int i = 0; i < capacity; i++) + under[i] = under_copy[i]; + delete [] under_copy; + } + }; +} + +#endif diff --git a/list.hpp b/list.hpp deleted file mode 100644 index c8645b4..0000000 --- a/list.hpp +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef LIST_HPP -# define LIST_HPP - -namespace ft -{ - template < class T, class Alloc = allocator > - class list - { - public: - bool empty() const - { - return front == nullptr; - } - size_type size() const - { - return size; - } - - reference front() - { - return *front; - } - reference back() - { - return *back; - } - void push_front (const value_type& val) - { - t_inner_list *nfront = new t_inner_list; - nfront->content = val; - nfront->next = front; - front = nfront; - if (back == nullptr) - back = front; - size++; - } - void pop_front() - { - t_inner_list *nfront = front->next; - if (nfront == nullptr) - back = nullptr; - ~T(front->content); - delete front; - front = nfront; - size--; - } - void push_back (const value_type& val) - { - t_inner_list *nback = new t_inner_list; - nback->content = val; - if (back == nullptr) - { - back = nback; - front = back; - return; - } - back->next = nback; - back = nback; - size++; - } - void pop_back() - { - - } - - private: - typedef struct - { - t_inner_list *next; - T content; - } t_inner_list; - t_inner_list *front; - t_inner_list *back; - size_type size; - }; -} - -#endif diff --git a/stack.hpp b/stack.hpp deleted file mode 100644 index 64bfc66..0000000 --- a/stack.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef STACK_HPP -# define STACK_HPP - -# include "vector.hpp" - -namespace ft -{ - template > - class stack : public vector - { - public: - explicit stack (const container_type& ctnr = container_type()); - value_type& top() - { - return back(); - } - void push (const value_type& val) - { - push_back(val); - } - void pop() - { - pop_back(); - } - }; -} - -#endif - diff --git a/vector.hpp b/vector.hpp deleted file mode 100644 index 0f21995..0000000 --- a/vector.hpp +++ /dev/null @@ -1,128 +0,0 @@ -#ifndef VECTOR_HPP -# define VECTOR_HPP - -# include - -# define GROWTH_FACTOR 1.5 - -typedef size_t size_type; - - -namespace ft -{ - template < class T, class Alloc = allocator > - class vector - { - typedef T value_type; - typedef T& reference; - - public: - // capacity - size_type size() const; - { - return size; - } - void resize (size_type n, value_type val = value_type()) - { - if (n < size) - { - for (int i = n; i < size; i++) - ~T(under[i]); - size = n; - return; - } - if (n > capacity) - grow(); - size_type prev_n; - for (prev_n = n; prev_n < size; prev_n++) - under[n] = val; - size = n; - } - size_type capacity() const - { - return capacity; - } - bool empty() const - { - return size == 0; - } - void reserve (size_type n) - { - if (n > capacity) - grow_to(n); - } - - // element access - reference operator[] (size_type n) - { - return under[n]; - } - reference at (size_type n) - { - if (n >= size) - raise std::out_of_range; - return under[n]; - } - reference front() - { - return under[0]; - } - reference back() - { - return under[size - 1]; - } - - // modifiers - template - void assign (InputIterator first, InputIterator last); - void assign (size_type n, const value_type& val); - void push_back (const value_type& val) - { - if (size >= capacity) - grow(); - under[size] = val; - size++; - } - void pop_back() - { - ~T(back()); - size--; - } - iterator insert (iterator position, const value_type& val) - { - - } - void insert (iterator position, size_type n, const value_type& val); - template - void insert (iterator position, InputIterator first, InputIterator last); - - - - - private: - T *under; - size_type size; - size_type capacity; - - void grow() - { - capacity *= GROWTH_FACTOR; - grow_to(capacity); - } - - void grow_to(size_type new_capacity) - { - T *under_copy = new T[capacity]; - for (int i = 0; i < capacity; i++) - under_copy[i] = under[i]; - delete [] under; - capacity = new_capacity; - under = new T[capacity]; - for (int i = 0; i < capacity; i++) - under[i] = under_copy[i]; - delete [] under_copy; - } - }; -} - -#endif -- cgit