diff --git a/containers/iterators/random_access_iterator.hpp b/containers/iterators/random_access_iterator.hpp index e906236..352486c 100644 --- a/containers/iterators/random_access_iterator.hpp +++ b/containers/iterators/random_access_iterator.hpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/30 16:14:35 by apommier #+# #+# */ -/* Updated: 2022/10/30 16:36:23 by apommier ### ########.fr */ +/* Updated: 2022/11/14 06:27:37 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,60 +18,125 @@ namespace ft template class random_access_iterator { + public: + + typedef T value_type; + typedef std::ptrdiff_t difference_type; + typedef T* pointer; + typedef T& reference; + + private: + + pointer _ptr; + public: //--------------------------------------- //--------CONSTRUCTOR DESTRUCTOR--------- //--------------------------------------- - random_access_iterator() - { - - } - random_access_iterator() + random_access_iterator(){ _ptr = NULL;} + random_access_iterator(pointer ptr){ _ptr = ptr;} + random_access_iterator(random_access_iterator const &cpy) {*this = cpy;} + + ~random_access_iterator(){} + + random_access_iterator &operator=(random_access_iterator const &cpy) { + _ptr = cpy._ptr; + return (*this); + } - } - - random_access_iterator(random_access_iterator &cpy) - { - - } - - ~random_access_iterator() - { - - } - - vectorIterator &operator=() - { - - } + // random_access_iterator &operator=(pointer &ptr) + // { + // _ptr = ptr; + // return (*this); + // } //--------------------------------------- //-------------COMPARAISON--------------- //--------------------------------------- - bool operator ==() {} - bool operator !=() {} - bool operator <() {} - bool operator >() {} - bool operator <=() {} - bool operator >=() {} + + friend bool operator ==(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr == b._ptr); } + friend bool operator !=(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr != b._ptr); } + friend bool operator <(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr < b._ptr); } + friend bool operator >(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr > b._ptr); } + friend bool operator <=(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr <= b._ptr); } + friend bool operator >=(random_access_iterator const& a, random_access_iterator const& b) { return (a._ptr >= b._ptr); } //--------------------------------------- //-------------INCREMENTERS-------------- //--------------------------------------- - random_access_iterator operator ++() {} - random_access_iterator operator ++(int) {} - random_access_iterator operator --() {} - random_access_iterator operator --(int) {} + random_access_iterator &operator ++() + { + _ptr++; + return (*this); + } + + random_access_iterator operator ++(int) + { + random_access_iterator tmp(*this); + ++(*this); + return (tmp); + } + + random_access_iterator &operator --() + { + _ptr--; + return (*this); + } + + random_access_iterator operator --(int) + { + random_access_iterator tmp(*this); + --(*this); + return (tmp); + } //--------------------------------------- - //-------------INCREMENTERS-------------- + //----------------ADRESS----------------- //--------------------------------------- - private: + value_type operator *() { return (*_ptr); } + pointer operator ->() { return (_ptr); } + value_type operator [](difference_type nbr) { return (*(_ptr + nbr)); } + + //--------------------------------------- + //--------------OPERATION---------------- + //--------------------------------------- + + // addition | soustraction + random_access_iterator operator +(difference_type const nbr) const { return (random_access_iterator(_ptr + nbr)); } // a + n + friend random_access_iterator operator +(int const lhs, random_access_iterator const& rhs) { return (rhs + lhs); } // n + a + random_access_iterator &operator -(difference_type nbr) { return random_access_iterator(_ptr - nbr); } // a - n + difference_type operator -(random_access_iterator const &rhs) const { return (_ptr - rhs._ptr) ; } // a - b + //friend difference_type &operator -(random_access_iterator const& lhs, random_access_iterator const& rhs) { return ((difference_type)(lhs._ptr - rhs._ptr)); } // a - b + //random_access_iterator operator -(const random_access_iterator &b) { return random_access_iterator(_ptr + b); } // a - b 2? + + random_access_iterator &operator +=(difference_type nbr) + { + _ptr += nbr; + return (*this); + } + + random_access_iterator &operator -=(difference_type nbr) + { + _ptr -= nbr; + return (*this); + } + + //--------------------------------- + //--------------GET---------------- + //--------------------------------- + + pointer getPointer() + { + return (_ptr); + } }; + + } + #endif \ No newline at end of file diff --git a/containers/vector.hpp b/containers/vector.hpp index 9a71496..8e6476c 100644 --- a/containers/vector.hpp +++ b/containers/vector.hpp @@ -6,13 +6,15 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 19:46:32 by apommier #+# #+# */ -/* Updated: 2022/10/28 17:58:43 by apommier ### ########.fr */ +/* Updated: 2022/11/14 06:50:18 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef VECTOR_HPP # define VECTOR_HPP +# include "./iterators/random_access_iterator.hpp" + namespace ft { @@ -27,15 +29,33 @@ class vector typedef Allocator allocator_type; typedef value_type& reference; typedef const value_type& const_reference; - typedef typename Allocator::pointer pointer; - typedef typename Allocator::const_pointer const_pointer; - typedef value_type* iterator; - typedef value_type* const_iterator; + typedef T* pointer; + typedef const T* const_pointer; + typedef ft::random_access_iterator iterator; + typedef const ft::random_access_iterator const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; typedef std::ptrdiff_t difference_type; typedef std::size_t size_type; + + //----------------------------- + //-----PRIVATE MEMBER TYPE----- + //----------------------------- + private: + + value_type *_tab; + size_type _size; + size_type _capacity; + + allocator_type _alloc; + ft::random_access_iterator _end; + ft::random_access_iterator _start; + + //pointer _end_capacity; + + public: + //--------------------------------------- //---------COPLIEN FORM FUNCTION--------- //--------------------------------------- @@ -44,10 +64,13 @@ class vector _tab = 0; _size = 0; _capacity = 0; - _start = 0; - _end = 0; + //_start = 0; + //_end = 0; + + _alloc = alloc; - //_end_capacity = 0; + _start = _alloc.allocate(0); + _end = _start; } explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()): _alloc(alloc) //fill constructor @@ -55,24 +78,25 @@ class vector _tab = 0; _size = 0; _capacity = 0; - _start = 0; - _end = 0; - //_end_capacity = 0; _alloc = alloc; - _start = _alloc.allocate(n); - _end = _start + n; + _tab = _alloc.allocate(n); + _start = _tab; _size = n; _capacity = n; + _end = _start + n; while (n--) - alloc.construct(val, _end - n); + _alloc.construct(_tab + n, val); + //_end = _start; + //_start + 5; + ///5 + _start; } - template - vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) //range constructor - { + //template + //vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) //range constructor + //{ - } + //} vector (const vector& x)//copy constructor { @@ -246,15 +270,15 @@ class vector void push_back (const value_type& val) { - if (!this->max_size() - _size) - ; + //if (!this->max_size() - _size) + // ; //throw or alloc - else - { - _alloc.construct(val, end); + //else + //{ + _alloc.construct(_end.getPointer() - 2, val); _size++; _end++; - } + //} } void pop_back() @@ -355,16 +379,7 @@ class vector // } - private: - - value_type *_tab; - size_type _size; - size_type _capacity; - - allocator_type _alloc; - pointer _end; - pointer _start; - //pointer _end_capacity; + }; } diff --git a/tests/main.cpp b/tests/main.cpp index dadb6f0..468ae97 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/19 14:04:37 by apommier #+# #+# */ -/* Updated: 2022/10/28 17:12:41 by apommier ### ########.fr */ +/* Updated: 2022/11/14 06:48:31 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,8 @@ void stack_tester() void vector_tester() { - ft::vector first; + //ft::vector first; + ft::vector first(5, 3); std::cout << "------empty------\n"; std::cout << first.empty() << std::endl; std::cout << "------size------\n"; @@ -31,6 +32,33 @@ void vector_tester() std::cout << "------pushback then size------\n"; first.push_back(5); std::cout << first.size() << std::endl; + std::cout << "------------\n"; + + // std::cout << first[1] << std::endl; + // std::cout << "------1------\n"; + // std::cout << first.at(1) << std::endl; + // std::cout << "-------2-----\n"; + // std::cout << first.at(2) << std::endl; + // std::cout << "-------3-----\n"; + // std::cout << first.at(3) << std::endl; + // std::cout << "-------4-----\n"; + // std::cout << first.at(4) << std::endl; + // std::cout << "-------5-----\n"; + + // std::cout << first.at(0) << std::endl; + // std::cout << "--------6----\n"; + // std::cout << first.at(6) << std::endl; + // std::cout << "------------\n"; + + std::cout << "------for------\n"; + for (size_t i = 0; i < first.size(); i++) + { + std::cout << first[1] << std::endl; + // std::cout << "------------\n"; + } + std::cout << "-------0-----\n"; + std::cout << first.at(5) << std::endl; + } int main() diff --git a/tests/stack_test.o b/tests/stack_test.o deleted file mode 100644 index 1aacd53..0000000 Binary files a/tests/stack_test.o and /dev/null differ