aboutsummaryrefslogtreecommitdiff
path: root/Vector.hpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-24 16:19:51 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-24 16:19:51 +0200
commit84b8cd74d206cf7e0aac85d4833c754f627a4127 (patch)
tree6d73fdbda55a6a6ecfbe8bb923cc5922c359c82a /Vector.hpp
parentbe2676e56ae0c72321c60c4848ce720ccf095b26 (diff)
downloadft_containers-84b8cd74d206cf7e0aac85d4833c754f627a4127.tar.gz
ft_containers-84b8cd74d206cf7e0aac85d4833c754f627a4127.tar.bz2
ft_containers-84b8cd74d206cf7e0aac85d4833c754f627a4127.zip
Vector draft all but iterator
Diffstat (limited to 'Vector.hpp')
-rw-r--r--Vector.hpp364
1 files changed, 249 insertions, 115 deletions
diff --git a/Vector.hpp b/Vector.hpp
index 0f21995..7d6b40b 100644
--- a/Vector.hpp
+++ b/Vector.hpp
@@ -1,128 +1,262 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* Vector.hpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/24 16:19:04 by charles #+# #+# */
+/* Updated: 2020/04/24 16:19:05 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#ifndef VECTOR_HPP
# define VECTOR_HPP
-# include <stddef.h>
-
-# define GROWTH_FACTOR 1.5
-
-typedef size_t size_type;
+# include <stdexcept>
+# define FT_VECTOR_GROWTH_FACTOR 1.5
namespace ft
{
- template < class T, class Alloc = allocator<T> >
+ template < typename T, typename Alloc = allocator<T> >
class vector
{
- typedef T value_type;
- typedef T& reference;
-
- public:
- // capacity
- size_type size() const;
- {
- return size;
- }
- void resize (size_type n, value_type val = value_type())
- {
- if (n < size)
- {
- for (int i = n; i < size; i++)
- ~T(under[i]);
- size = n;
- return;
- }
- if (n > capacity)
- grow();
- size_type prev_n;
- for (prev_n = n; prev_n < size; prev_n++)
- under[n] = val;
- size = n;
- }
- size_type capacity() const
- {
- return capacity;
- }
- bool empty() const
- {
- return size == 0;
- }
- void reserve (size_type n)
- {
- if (n > capacity)
- grow_to(n);
- }
-
- // element access
- reference operator[] (size_type n)
- {
- return under[n];
- }
- reference at (size_type n)
- {
- if (n >= size)
- raise std::out_of_range;
- return under[n];
- }
- reference front()
- {
- return under[0];
- }
- reference back()
- {
- return under[size - 1];
- }
-
- // modifiers
- template <class InputIterator>
- void assign (InputIterator first, InputIterator last);
- void assign (size_type n, const value_type& val);
- void push_back (const value_type& val)
- {
- if (size >= capacity)
- grow();
- under[size] = val;
- size++;
- }
- void pop_back()
- {
- ~T(back());
- size--;
- }
- iterator insert (iterator position, const value_type& val)
- {
-
- }
- void insert (iterator position, size_type n, const value_type& val);
- template <class InputIterator>
- void insert (iterator position, InputIterator first, InputIterator last);
-
-
-
-
- private:
- T *under;
- size_type size;
- size_type capacity;
-
- void grow()
- {
- capacity *= GROWTH_FACTOR;
- grow_to(capacity);
- }
-
- void grow_to(size_type new_capacity)
- {
- T *under_copy = new T[capacity];
- for (int i = 0; i < capacity; i++)
- under_copy[i] = under[i];
- delete [] under;
- capacity = new_capacity;
- under = new T[capacity];
- for (int i = 0; i < capacity; i++)
- under[i] = under_copy[i];
- delete [] under_copy;
- }
+ public:
+ typedef T value_type;
+ typedef Alloc allocator_type;
+ typedef typename allocator_type::reference reference;
+ typedef typename allocator_type::const_reference const_reference;
+ typedef typename allocator_type::pointer pointer;
+ typedef typename allocator_type::const_pointer const_pointer;
+ typedef ptrdiff_t difference_type;
+ typedef size_t size_type;
+
+ explicit Vector(const allocator_type& alloc = allocator_type())
+ : m_under(new value_type[0]), m_size(0), m_capacity(0)
+ {}
+
+ explicit Vector(size_type n,
+ const value_type& val = value_type(),
+ const allocator_type& alloc = allocator_type())
+ : m_under(new value_type[n]), m_size(n), m_capacity(n)
+ {
+ for (size_type i = 0; i < n; i++)
+ m_under[i] = val;
+ }
+
+ template <typename InputIterator>
+ Vector (InputIterator first,
+ InputIterator last,
+ const allocator_type& alloc = allocator_type())
+ : m_under(new value_type[0]), m_size(0), m_capacity(0)
+ {
+ for (; first != last; first++)
+ push_back(*first);
+ }
+
+ Vector (const Vector& x)
+ {
+ *this = x;
+ }
+
+ Vector& operator=(const Vector& x)
+ {
+ if (this == &x)
+ return *this;
+ delete [] m_under;
+ m_capacity = x.m_capacity;
+ m_size = x.m_size;
+ m_under = new value_type[m_capacity];
+ for (size_type i = 0; i < m_size; i++)
+ m_under[i] = x.m_under[i];
+ return *this;
+ }
+
+ ~Vector()
+ {
+ delete [] m_under;
+ }
+
+ 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())
+ {
+ if (n == m_size)
+ return ;
+ if (n > m_size)
+ {
+ reserve(n);
+ for (; m_size < n; m_size++)
+ m_under[m_size] = val;
+ }
+ else
+ {
+ value_type* newUnder = new value_type[n];
+ m_capacity = n;
+ m_size = n;
+ for (size_type i = 0; i < m_size; i++)
+ newUnder[i] = m_under[i];
+ delete [] m_under;
+ m_under = newUnder;
+ }
+ }
+
+ void reserve(size_type n)
+ {
+ if (m_capacity < 2)
+ m_capacity = 2;
+ while (m_capacity < n)
+ m_capacity *= FT_VECTOR_GROWTH_FACTOR;
+ value_type* newUnder = new value_type[m_capacity];
+ for (size_type i = 0; i < m_size; i++)
+ newUnder[i] = m_under[i];
+ delete [] m_under;
+ m_under = newUnder;
+ }
+
+ reference operator[](size_type n) { return m_under[n]; }
+ const_reference operator[](size_type n) const { return m_under[n]; }
+
+ reference at(size_type n)
+ {
+ if (n >= m_size)
+ throw std::out_of_range("vector::_M_range_check");
+ return m_under[n];
+ }
+
+ const_reference at(size_type n) const
+ {
+ if (n >= m_size)
+ throw std::out_of_range("vector::_M_range_check");
+ return m_under[n];
+ }
+
+ reference front() { return m_under[0]; }
+ const_reference front() const { return m_under[0]; }
+ reference back() { return m_under[m_size - 1]; }
+ const_reference back() const { return m_under[m_size - 1]; }
+
+ template <typename InputIterator>
+ void assign(InputIterator first, InputIterator last)
+ {
+ size_type count;
+ InputIterator copy;
+ copy = first;
+ count = 0;
+ for (; copy != last; copy++)
+ count++;
+ reserve(count);
+ for (size_type i = 0; first != last; first++, i++)
+ m_under[i] = *first;
+ }
+
+ void assign(size_type n, const value_type& val)
+ {
+ reserve(n);
+ for (size_type i = 0; i < n; ++)
+ m_under[i] = val;
+ }
+
+ void push_back(const value_type& val)
+ {
+ m_size++;
+ reserve(m_size);
+ m_under[m_size - 1] = val;
+ }
+
+ void pop_back()
+ {
+ m_size--;
+ ~value_type(m_under[m_size]);
+ m_under[m_size] = value_type();
+ }
+
+ // insert
+
+ // erase
+
+ void swap(Vector& x)
+ {
+ value_type* tmpUnder = m_under;
+ size_type* tmpSize = m_size;
+ size_type* tmpCapacity = m_capacity;
+
+ m_under = x.m_under;
+ m_size = x.m_size;
+ m_capacity = x.m_capacity;
+
+ x.m_under = tmpUnder;
+ x.m_size = tmpSize;
+ x.m_capacity = tmpCapacity;
+ }
+
+ void clear()
+ {
+ delete [] m_under;
+ m_under = new value_type[0];
+ m_size = 0;
+ m_capacity = 0;
+ }
+
+ private:
+ value_type* m_under;
+ size_type m_size;
+ size_type m_capacity;
};
}
+template <typename T, typename Alloc>
+bool operator==(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ if (lhs.size() != rhs.size())
+ return false;
+ for (Vector::size_type i = 0; i < lhs.size(); i++)
+ if (!(lhs[i] == rhs[i]))
+ return false;
+ return true;
+}
+
+template <typename T, typename Alloc>
+bool operator!=(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ return !(lhs == rhs);
+}
+
+template <typename T, typename Alloc>
+bool operator<const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ for (Vector::size_type i = 0; i < lhs.size() && i < rhs.size(); i++)
+ if (lhs[i] < rhs[i] && !(rhs[i] < lhs[i]))
+ return true;
+ return false;
+}
+
+template <typename T, typename Alloc>
+bool operator<=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ return !(rhs < lhs);
+}
+
+template <typename T, typename Alloc>
+bool operator>const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ return rhs < lhs;
+}
+
+template <typename T, typename Alloc>
+bool operator>=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
+{
+ return !(lhs < rhs);
+}
+
+template <typename T, typename Alloc>
+void swap(Vector<T, Alloc>& x, Vector<T, Alloc>& y)
+{
+ x.swap(y);
+}
+
#endif