aboutsummaryrefslogtreecommitdiff
path: root/Vector.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'Vector.hpp')
-rw-r--r--Vector.hpp173
1 files changed, 90 insertions, 83 deletions
diff --git a/Vector.hpp b/Vector.hpp
index 0fcfe8a..4a3c537 100644
--- a/Vector.hpp
+++ b/Vector.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/24 16:19:04 by charles #+# #+# */
-/* Updated: 2020/04/26 08:40:14 by charles ### ########.fr */
+/* Updated: 2020/04/27 12:54:08 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,7 @@
# include <climits>
# include <cstddef>
# include <stdexcept>
+# include "ReverseIterator.hpp"
# define FT_VECTOR_GROWTH_FACTOR 1.5
@@ -25,6 +26,7 @@ namespace ft
class Vector
{
typedef Vector<T, Alloc> m_type;
+
public:
typedef T value_type;
typedef Alloc allocator_type;
@@ -35,6 +37,69 @@ namespace ft
typedef ptrdiff_t difference_type;
typedef size_t size_type;
+ class iterator
+ {
+ friend class Vector;
+ public:
+ typedef Vector::value_type value_type;
+ typedef Vector::difference_type difference_type;
+
+ iterator() : m_ptr(NULL), m_pos(-1) {}
+ iterator(const iterator& other) : m_ptr(other.m_ptr), m_pos(other.m_pos) {}
+ ~iterator() {}
+
+ iterator& operator=(const iterator& other)
+ {
+ if (this == &other)
+ return *this;
+ m_ptr = other.m_ptr;
+ m_pos = other.m_pos;
+ return *this;
+ }
+
+ value_type& operator*() { return (*m_ptr)[m_pos]; }
+ value_type* operator->() { return &(*m_ptr)[m_pos]; }
+
+ bool operator==(const iterator& other) const { return m_pos == other.m_pos; }
+ bool operator!=(const iterator& other) const { return !(operator==(other)); }
+
+ bool operator<(const iterator& other) const { return m_pos < other.m_pos; }
+ bool operator>(const iterator& other) const { return other < *this; }
+ bool operator<=(const iterator& other) const { return !(other < *this); }
+ bool operator>=(const iterator& other) const { return !(*this < other); }
+
+ iterator& operator++() { m_pos++; return *this; }
+ iterator& operator--() { m_pos--; return *this; }
+ iterator operator++(int) { iterator copy(*this); m_pos++; return copy; }
+ iterator operator--(int) { iterator copy(*this); m_pos--; 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_ptr)[m_pos + n]; }
+
+ // tmp
+ iterator(Vector* ptr, size_type pos) : m_ptr(ptr), m_pos(pos) {}
+ protected:
+
+ Vector* m_ptr;
+ size_type m_pos;
+ };
+
+ class const_iterator : public iterator
+ {
+ // const value_type& operator*() { return m_ptr[m_pos]; }
+ // const value_type& operator->() { return m_ptr[m_pos]; }
+ };
+
+ typedef ReverseIterator<iterator> reverse_iterator;
+ typedef ReverseIterator<const_iterator> const_reverse_iterator;
+
explicit Vector(const allocator_type& alloc = allocator_type())
: m_under(new value_type[0]), m_size(0), m_capacity(0), m_alloc(alloc)
{}
@@ -83,83 +148,19 @@ namespace ft
m_under[i] = x.m_under[i];
}
- class iterator
- {
- friend class Vector;
- public:
- iterator() : m_ptr(NULL), m_pos(-1), m_inc(0) {}
-
- iterator(const iterator& other)
- : m_ptr(other.m_ptr), m_pos(other.m_pos), m_inc(other.m_inc)
- {}
-
- iterator& operator=(const iterator& other)
- {
- if (this == &other)
- return *this;
- m_ptr = other.m_ptr;
- m_pos = other.m_pos;
- m_inc = other.m_inc;
- return *this;
- }
-
- ~iterator() {}
-
- value_type& operator*() { return (*m_ptr)[m_pos]; }
- value_type* operator->() { return &(*m_ptr)[m_pos]; }
-
- bool operator==(const iterator& other) const { return m_pos == other.m_pos; }
- bool operator!=(const iterator& other) const { return !(operator==(other)); }
-
- bool operator<(const iterator& other) const { return m_pos < other.m_pos; }
- bool operator>(const iterator& other) const { return other < *this; }
- bool operator<=(const iterator& other) const { return !(other < *this); }
- bool operator>=(const iterator& other) const { 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 += m_inc * n; }
- iterator operator-(int n) { iterator copy(*this); return copy -= m_inc * n; }
- iterator operator+(iterator& it) { iterator copy(*this); return copy += m_inc * it.m_pos; }
- iterator operator-(iterator& it) { iterator copy(*this); return copy -= m_inc * it.m_pos; }
+ iterator begin() { return iterator(this, 0); }
+ iterator end() { return iterator(this, m_size); }
+ const_iterator begin() const { return const_iterator(this, 0); }
+ const_iterator end() const { return const_iterator(this, m_size); }
+ reverse_iterator rbegin() { return reverse_iterator(iterator(this, m_size - 1)); }
+ reverse_iterator rend() { return reverse_iterator(iterator(this, -1)); }
+ const_reverse_iterator rbegin() const { return reverse_iterator(const_iterator(this, m_size - 1)); }
+ const_reverse_iterator rend() const { return reverse_iterator(const_iterator(this, -1)); }
- 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_ptr)[m_pos + m_inc * n]; }
-
- // tmp
- iterator(Vector* ptr, size_type pos, int inc)
- : m_ptr(ptr), m_pos(pos), m_inc(inc) {}
- protected:
-
- Vector* m_ptr;
- size_type m_pos;
- int m_inc;
- };
-
- class const_iterator : public iterator
- {
- // const value_type& operator*() { return m_ptr[m_pos]; }
- // const value_type& operator->() { return m_ptr[m_pos]; }
- };
-
- iterator begin() { return m_type::iterator(this, 0, 1); }
- iterator end() { return m_type::iterator(this, m_size, 1); }
- iterator rbegin() { return m_type::iterator(this, m_size - 1, -1); }
- iterator rend() { return m_type::iterator(this, -1, -1); }
- const_iterator begin() const { return m_type::const_iterator(this, 0, 1); }
- const_iterator end() const { return m_type::const_iterator(this, m_size, 1); }
- const_iterator rbegin() const { return m_type::const_iterator(this, m_size - 1, -1); }
- const_iterator rend() const { return m_type::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; }
- bool empty() const { return m_size == 0; }
+ size_type size() const { return m_size; }
+ size_type max_size() const { return UINT_MAX; }
+ size_type capacity() const { return m_capacity; }
+ bool empty() const { return m_size == 0; }
void resize(size_type n, value_type val = value_type())
{
@@ -253,12 +254,18 @@ namespace ft
iterator insert(iterator position, const value_type& val)
{
- m_size++;
- reserve(m_size);
- if (position != end())
- for (iterator it = rbegin(); it != position; ++it)
- *it = *(it + 1);
- *position = val;
+ reverse_iterator rev_position(position);
+
+ if (position == end())
+ push_back(val);
+ else
+ {
+ m_size++;
+ reserve(m_size);
+ for (reverse_iterator it = rbegin(); it != rev_position; ++it)
+ it[0] = it[1];
+ *position = val;
+ }
return position;
}