aboutsummaryrefslogtreecommitdiff
path: root/List.hpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-24 19:17:49 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-24 19:17:49 +0200
commit12eb0f6c5cfdf2ea794e674aa959c74aeaca0384 (patch)
treee44d526bb45ffb2e601c1263604daad3c95103b1 /List.hpp
parent84b8cd74d206cf7e0aac85d4833c754f627a4127 (diff)
downloadft_containers-12eb0f6c5cfdf2ea794e674aa959c74aeaca0384.tar.gz
ft_containers-12eb0f6c5cfdf2ea794e674aa959c74aeaca0384.tar.bz2
ft_containers-12eb0f6c5cfdf2ea794e674aa959c74aeaca0384.zip
WIP: Added Vector iterator, insert, erase
Diffstat (limited to 'List.hpp')
-rw-r--r--List.hpp71
1 files changed, 39 insertions, 32 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;