From 12eb0f6c5cfdf2ea794e674aa959c74aeaca0384 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 24 Apr 2020 19:17:49 +0200 Subject: WIP: Added Vector iterator, insert, erase --- List.hpp | 71 ++++++++++++++++++++----------------- Vector.hpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 148 insertions(+), 40 deletions(-) diff --git a/List.hpp b/List.hpp index 64fd14a..ebe0dc2 100644 --- a/List.hpp +++ b/List.hpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 09:31:07 by cacharle #+# #+# */ -/* Updated: 2020/04/15 12:57:58 by charles ### ########.fr */ +/* Updated: 2020/04/24 19:16:46 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,11 +34,10 @@ namespace ft explicit List(const allocator_type& alloc = allocator_type()) : m_front(NULL), m_back(NULL), m_size(0) {} - List(const List& other) { - *this = other; - } + List(const List& other) { *this = other; } - List& operator=(const List& other) { + List& operator=(const List& other) + { if (*this == other) return *this; delete m_front; @@ -67,19 +66,22 @@ namespace ft public: ListNode() : m_next(NULL), m_prev(NULL), m_data(value_type()) {} - ListNode(const ListNode& other) { + ListNode(const ListNode& other) + { if (*this != other) *this = other; } - ListNode& operator=(const ListNode& other) { + ListNode& operator=(const ListNode& other) + { m_next = other.m_next; m_prev = other.m_prev; m_data = other.m_data; return *this; } - ~ListNode() { + ~ListNode() + { if (m_next != NULL) { m_next->m_prev = NULL; delete m_next; @@ -90,16 +92,16 @@ namespace ft } } - ListNode(T& data) : m_next(NULL), m_prev(NULL), m_data(data) {} + ListNode(value_type& 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; } + ListNode* getNext() const { return m_next; } + ListNode* getPrev() const { return m_prev; } + value_type& getData() const { return m_data; } private: - ListNode* m_next; - ListNode* m_prev; - T m_data; + ListNode* m_next; + ListNode* m_prev; + value_type m_data; }; public: @@ -121,30 +123,30 @@ namespace ft ~iterator() {} - iterator(ListNode* current) : m_current(current) {} - - T& operator*() { - return m_current->getData(); - } + value_type& operator*() { return m_current->getData(); } + value_type& operator->() { return m_current->getData(); } - bool operator==(const iterator& other) { - return m_current == other.m_current; - } + bool operator==(const iterator& other) { return m_current == other.m_current; } bool operator!=(const iterator& other) { return !(operator==(other)); } - iterator& operator++(int) { // post + iterator& operator++(int) + { m_current = m_current->getNext(); return *this; } - iterator& operator++() { return operator++; } - iterator& operator--(int) { + iterator& operator--(int) + { m_current = m_current->getPrev(); return *this; } - iterator& operator--() { return operator++; } + + iterator& operator++() { return operator++; } + iterator& operator--() { return operator--; } private: + iterator(ListNode* current) : m_current(current) {} + List::ListNode* m_current; }; @@ -158,35 +160,40 @@ namespace ft reference front() { return m_front->getData(); } reference back() { return m_back->getData(); } - void push_front(const value_type& val) { + 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) { + 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() { + void pop_front() + { ListNode* tmp = m_front->getNext(); tmp->setPrev(NULL); delete m_front; m_front = tmp; } - void pop_back() { + void pop_back() + { ListNode* tmp = m_back->getPrev(); tmp->setNext(NULL); delete m_back; m_back = tmp; } - void clear() { + void clear() + { delete m_front; m_front = NULL; m_back = NULL; diff --git a/Vector.hpp b/Vector.hpp index 7d6b40b..1366ec6 100644 --- a/Vector.hpp +++ b/Vector.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/24 16:19:04 by charles #+# #+# */ -/* Updated: 2020/04/24 16:19:05 by charles ### ########.fr */ +/* Updated: 2020/04/24 19:16:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,6 +78,16 @@ namespace ft delete [] m_under; } + iterator begin() { return iterator(*this, 0, 1); } + const_iterator begin() const { return iterator(*this, 0, 1); } + iterator end() { return const_iterator(*this, m_size, 1); } + const_iterator end() const { return const_iterator(*this, m_size, 1); } + iterator rbegin() { return iterator(*this, m_size - 1, -1); } + const_iterator rbegin() const { return iterator(*this, m_size - 1, -1); } + iterator rend() { return const_iterator(*this, -1, -1); } + const_iterator rend() const { return const_iterator(*this, -1, -1); } + + size_type size() const { return m_size; } size_type max_size() const { return UINT_MAX; } size_type capacity() const { return m_capacity; } @@ -144,10 +154,8 @@ namespace ft void assign(InputIterator first, InputIterator last) { size_type count; - InputIterator copy; - copy = first; - count = 0; - for (; copy != last; copy++) + InputIterator copy(first); + for (count = 0; copy != last; copy++) count++; reserve(count); for (size_type i = 0; first != last; first++, i++) @@ -172,12 +180,45 @@ namespace ft { m_size--; ~value_type(m_under[m_size]); - m_under[m_size] = value_type(); } - // insert + iterator insert(iterator position, const value_type& val) + { + m_size++; + reserve(m_size); + for (iterator it = rbegin(); it != position; ++it) + *it = *(it - 1); + *position = val; + } + + void insert(iterator position, size_type n, const value_type& val) + { + reserve(m_size + n); + while (n-- > 0) + insert(position, val); + } + + template + void insert(iterator position, InputIterator first, InputIterator last) + { + for (; first != last; ++first) + insert(position, *first); + } + + iterator erase(iterator position) + { + for (; position != end(); ++position) + *position = *(position + 1); + m_size--; + } - // erase + iterator erase(iterator first, iterator last) + { + size_type i; + for (i = 0; first != last && last + i != end(); ++first, i++) + *first = *(last + i); + m_size -= i; + } void swap(Vector& x) { @@ -202,6 +243,66 @@ namespace ft m_capacity = 0; } + class iterator + { + public: + iterator() : m_pos(-1) {} + + iterator(const iterator& other) { *this = other; } + + iterator& operator=(const iterator& other) + { + if (this == &other) + return *this; + m_ref = other.m_ref; + m_pos = other.m_pos; + m_inc = other.m_inc; + return *this; + } + + ~iterator() {} + + value_type& operator*() { return m_ref[m_pos]; } + value_type& operator->() { return m_ref[m_pos]; } + + bool operator==(const iterator& other) { return m_pos == other.m_pos; } + bool operator!=(const iterator& other) { return !(operator==(other)); } + + bool operator<(const iterator& other) { return m_pos < other.m_pos; } + bool operator>(const iterator& other) { return !(this <= other); } + bool operator<=(const iterator& other) { return !(this > other); } + bool operator>=(const iterator& other) { return !(this < other); } + + iterator& operator++() { m_pos += m_inc; return *this; } + iterator& operator--() { m_pos -= m_inc; return *this; } + iterator& operator++(int) { iterator copy(this); m_pos += m_inc; return copy; } + iterator& operator--(int) { iterator copy(this); m_pos -= m_inc; return copy; } + + iterator operator+(int n) { iterator copy(this); return copy += n; } + iterator operator-(int n) { iterator copy(this); return copy -= n; } + iterator operator+(iterator& it) { iterator copy(this); return copy += it.m_pos; } + iterator operator-(iterator& it) { iterator copy(this); return copy -= it.m_pos; } + + iterator& operator+=(int n) { m_pos += n; return *this; } + iterator& operator-=(int n) { m_pos -= n; return *this; } + + value_type& operator[](size_type n) { return m_ref[m_pos + n]; } + + private: + iterator(Vector* ref, size_type pos, int inc) + : m_pos(pos), m_ref(ref), m_inc(int) {} + + Vector& m_ref; + size_type m_pos; + int inc; + } + + class const_iterator : public iterator + { + const value_type& operator*() { return m_ref[m_pos]; } + const value_type& operator->() { return m_ref[m_pos]; } + } + private: value_type* m_under; size_type m_size; -- cgit