From 89bfc42fdd563fc570752381abc140259896a2d7 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Mon, 5 Dec 2022 17:49:03 +0100 Subject: [PATCH] save 5-12-22 --- includes/ft_container.hpp | 16 ++++ includes/iterator.hpp | 29 +++--- includes/vector.hpp | 179 ++++++++++++++++++++++++++++---------- tester.cpp | 11 +++ 4 files changed, 177 insertions(+), 58 deletions(-) diff --git a/includes/ft_container.hpp b/includes/ft_container.hpp index 3262933..b8752dc 100644 --- a/includes/ft_container.hpp +++ b/includes/ft_container.hpp @@ -2,4 +2,20 @@ #include "vector.hpp" #include "iterator.hpp" +#include "iostream" + +template is_integer(T input) {} + +template struct enable_if {}; + +template struct enable_if { typedef T type; }; + +template +void debug_vector(ft::vector vec) { + ft::ra_iterator i = vec.begin(); + while (i < vec.end()) { + std::cout << *i << " "; + } + std::cout << "\n"; +} diff --git a/includes/iterator.hpp b/includes/iterator.hpp index 20d339b..4496a50 100644 --- a/includes/iterator.hpp +++ b/includes/iterator.hpp @@ -10,11 +10,11 @@ struct bidirectional_iterator_tag : public forward_iterator_tag {}; struct random_access_iterator_tag : public bidirectional_iterator_tag {}; template < class Iter > struct iterator_traits { - typedef class Iter::iterator_category iterator_category; - typedef class Iter::value_type value_type; - typedef class Iter::difference_type difference_type; - typedef class Iter::pointer pointer; - typedef class Iter::reference reference; + typedef typename Iter::iterator_category iterator_category; + typedef typename Iter::value_type value_type; + typedef typename Iter::difference_type difference_type; + typedef typename Iter::pointer pointer; + typedef typename Iter::reference reference; }; template < class T > struct iterator_traits< T * > { @@ -42,13 +42,13 @@ template < class T > class ra_iterator { typedef std::ptrdiff_t difference_type; typedef T *pointer; typedef T &reference; - T current; + pointer current; public: - ra_iterator(T value = NULL) : current(value) {} + ra_iterator(pointer value = NULL) : current(value) {} ra_iterator(const ra_iterator &iter) : current(iter.base()) {} - ra_iterator &operator=(const ra_iterator< T > &iter) { + ra_iterator &operator=(const ra_iterator< T > &iter) { current = iter.base(); return (*this); } @@ -59,7 +59,7 @@ public: reference operator[](const difference_type n) const { return *(current + n); } ra_iterator operator+(const difference_type n) const { - return (iterator(current + n)); + return (ra_iterator(current + n)); } ra_iterator &operator++() { ++current; @@ -120,12 +120,13 @@ template < class T > class rev_iterator { typedef T *pointer; typedef T &reference; -public: - rev_iterator(T value = NULL) : current(value) {} - rev_iterator(const rev_iterator &iter) : current(iter.base()) {} +public: + template + rev_iterator(It value = NULL) : current(value) {} + rev_iterator(const rev_iterator &iter) : current(iter.base()) {} rev_iterator &operator=(const rev_iterator< T > &iter) { - current = iter.base(); - return (*this); + current = iter.base(); + return (*this); } T base(void) const { return current; } diff --git a/includes/vector.hpp b/includes/vector.hpp index 471e3bf..972c086 100644 --- a/includes/vector.hpp +++ b/includes/vector.hpp @@ -1,4 +1,5 @@ #pragma once +#include "ft_conatainer.hpp" #include "iterator.hpp" #include #include @@ -9,12 +10,12 @@ template < class T, class Allocator = std::allocator< T > > class vector { public: typedef T value_type; typedef Allocator allocator_type; - typedef class Allocator::pointer pointer; - typedef class Allocator::const_pointer cst_pointer; + typedef typename Allocator::pointer pointer; + typedef typename Allocator::const_pointer cst_pointer; typedef value_type &reference; typedef const value_type &cst_reference; - typedef ft::ra_iterator< pointer > iterator; - typedef ft::ra_iterator< cst_pointer > cst_iterator; + typedef ft::ra_iterator< reference > iterator; + typedef ft::ra_iterator< cst_reference > cst_iterator; typedef ft::rev_iterator< iterator > reverse_iterator; typedef ft::rev_iterator< cst_iterator > cst_reverse_iterator; typedef std::ptrdiff_t difference_type; @@ -27,34 +28,31 @@ private: size_type _capacity; public: - vector(void) : _alloc(Allocator()), _begin(NULL), _size(0), _capacity(0) {} - - explicit vector(const Allocator &alloc) + explicit vector(const Allocator &alloc = allocator_type()) : _alloc(alloc), _begin(NULL), _size(0), _capacity(0){}; - explicit vector(size_type count, const T &value = T(), - const Allocator &alloc = Allocator()) + explicit vector(size_type count, cst_reference value = T(), const Allocator &alloc = allocator_type()) : _alloc(alloc), _size(count), _capacity(count) { _begin = _alloc.allocate(count); for (size_type i = 0; i < count; i++) - alloc.construct(_begin + i, value); + _alloc.construct(_begin + i, value); } template < class It > - vector(It first, It last, const Allocator &alloc = Allocator()) + vector(It first, It last, const Allocator &alloc = allocator_type(), typename enable_if, 1>::type is_it = 0 ) : _alloc(alloc) { _size = distance(first, last); _capacity = _size; _begin = _alloc.allocate(_size); for (size_type i = 0; i < _size; i++) - _alloc.construct(_begin + i, *(first + i)); + _alloc.construct(_begin + i, first[i]); } vector(const vector &other) : _alloc(other._alloc), _size(other._size), _capacity(other._capacity) { _begin = _alloc.allocate(_capacity); for (size_type i = 0; i < _size; i++) - _alloc.construct(_begin + i, *(other._begin + i)); + _alloc.construct(_begin + i, other._begin[i]); } ~vector(void) { @@ -71,42 +69,64 @@ public: _size = other._size; _begin = _alloc.allocate(_capacity); for (size_type i = 0; i < _size; i++) - _alloc.construct(_begin + i, *(other._begin + i)); + _alloc.construct(_begin + i, other._begin[i]); return (*this); } - void assign(size_type count, const T &value); - template < class It > void assign(It first, It last); + void assign(size_type count, cst_reference value) { + resize(count); + while (count > 0){ + _alloc.destroy(this[count]); + _alloc.contruct(this[count], value); + count--; + } + } + template < class It > void assign(It first, It last) { + size_type i = ft::distance(first, last); + resize(i); + while (i > 0) { + _alloc.destroy(this[i]); + _alloc.contruct(this[i], first[i - 1]); + i--; + } + } + allocator_type get_allocator(void) const { return _alloc; } // ACCESS: - reference at(size_type pos); - cst_reference at(size_type pos) const; + reference at(size_type pos) { + if (pos > _size) throw (std::out_of_range("vector: Out of range access")); + return *(_begin + pos); + } + cst_reference at(size_type pos) const { + if (pos > _size) throw (std::out_of_range("vector: Out of range access")); + return *(_begin + pos); + } - reference operator[](size_type pos); - cst_reference operator[](size_type pos) const; + reference operator[](size_type pos) {return *(_begin + pos);} + cst_reference operator[](size_type pos) const {return *(_begin + pos);} - reference front(void); - cst_reference front(void) const; + reference front(void) {return this[0];} + cst_reference front(void) const {return this[0];} - reference back(void); - cst_reference back(void) const; + reference back(void) {return this[_size - 1];} + cst_reference back(void) const {return this[_size - 1];} - T *data(void); - const T *data(void) const; + pointer data(void) {return _begin;} + cst_pointer data(void) const {return _begin;} // ITERATORS: - ft::ra_iterator< reference > begin(void); - ft::ra_iterator< cst_reference > begin(void) const; + ra_iterator< T > begin(void) {return ra_iterator(front());} + ra_iterator< const T > begin(void) const {return ra_iterator(front());} - ft::ra_iterator< reference > end(void); - ft::ra_iterator< cst_reference > end(void) const; + ra_iterator< T > end(void) {return ra_iterator(this[_size]);} + ra_iterator< const T > end(void) const {return ra_iterator(this[_size]);} - ft::rev_iterator< iterator > rbegin(void); - ft::rev_iterator< cst_iterator > rbegin(void) const; + rev_iterator< iterator > rbegin(void) {return rev_iterator(end());} + rev_iterator< cst_iterator > rbegin(void) const {return rev_iterator(end());} - ft::rev_iterator< iterator > rend(void); - ft::rev_iterator< cst_iterator > rend(void) const; + rev_iterator< iterator > rend(void) {return rev_iterator(begin());} + rev_iterator< cst_iterator > rend(void) const {return rev_iterator(begin());} // CAPACITY: bool empty(void) const { return _size == 0; } @@ -134,16 +154,87 @@ public: _alloc.destroy(_begin + i); _size = 0; } - iterator insert(cst_iterator pos, const T &value); - iterator insert(cst_iterator pos, size_type count, const T &value); - template < class It > iterator insert(cst_iterator pos, It first, It last); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); - void push_back(const T &value); - void pop_back(void); - void resize(size_type count); - void resize(size_type count, T value = T()); - void swap(vector &other); + iterator insert(iterator pos, cst_reference value) { + if (_size == _capacity) + resize(_size + 1); + iterator it = end() + 1;; + while (--it >= pos) + *it = *(it - 1); + *it = value; + return _begin; + } + iterator insert(iterator pos, size_type count, cst_reference value) { + if ((_size + count) > _capacity) + resize(_size + count); + iterator it = end() + count; + while (--it >= (pos + count)) + *it = *(it - count); + while (it >= pos) { + *it = value; + it--; + } + return _begin; + } + template < class It > iterator insert(iterator pos, It first, It last) { + size_type count = ft::distance(first, last); + if ((_size + count) > _capacity) + resize(_size + count); + iterator it = end() + count; + while (--it >= (pos + count)) + *it = *(it - count); + while (it >= pos) { + *it = *last; + it--; + last--; + } + return _begin; + } + iterator erase(iterator pos) { + while (pos < end()) { + _alloc.destroy(pos); + *pos = *(pos + 1); + pos++; + } + _size--; + return _begin; + } + iterator erase(iterator first, iterator last) { + while (last++ < end()){ + _alloc.destroy(first); + *(first++) = *last; + } + while (first < end() && _size--) { + _alloc.destroy(first); + *(first++) = value_type(); + } + return _begin; + } + void push_back(cst_reference value) { + if (_size == _capacity) + resize(_size + 1); + _alloc.construct(back(), value); + } + void pop_back(void) { + _alloc.destroy(back()); + _size--; + } + void resize(size_type count, T value = T()) { + size_type i = count; + if (count < _size) { + while (i < _size && --_size) + _alloc.destroy(this[i]); + } else { + if (count > _capacity) + reserve(count); + while (i >= _size) + _alloc.construct(_begin[--i], value); + } + } + void swap(vector &other) { + vector tmp = other; + other = this; + this = tmp; + } }; } // namespace ft diff --git a/tester.cpp b/tester.cpp index e6f2e3a..9d3db52 100644 --- a/tester.cpp +++ b/tester.cpp @@ -1,5 +1,16 @@ #include "ft_container.hpp" int main(void) { + ft::vector test1 = ft::vector(); + ft::vector test2 = ft::vector(10); + ft::vector test3 = ft::vector(10, 4); + ft::vector test4 = ft::vector(test3.begin(), test3.begin() + 4); + ft::vector test5 = ft::vector(test3); + + debug_vector(test1); + debug_vector(test2); + debug_vector(test3); + debug_vector(test4); + debug_vector(test5); }