aboutsummaryrefslogtreecommitdiff
path: root/src/Vector.hpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-01 09:48:31 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-01 09:48:31 +0100
commit6e191a07bbc57d73152ba886b6f76f694a97e525 (patch)
treeb37718035e5a9b31bc3a5875b4e12a2305021a39 /src/Vector.hpp
parent46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f (diff)
downloadft_containers-6e191a07bbc57d73152ba886b6f76f694a97e525.tar.gz
ft_containers-6e191a07bbc57d73152ba886b6f76f694a97e525.tar.bz2
ft_containers-6e191a07bbc57d73152ba886b6f76f694a97e525.zip
src test dir, makefile, Added subject
Diffstat (limited to 'src/Vector.hpp')
-rw-r--r--src/Vector.hpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/Vector.hpp b/src/Vector.hpp
new file mode 100644
index 0000000..0f21995
--- /dev/null
+++ b/src/Vector.hpp
@@ -0,0 +1,128 @@
+#ifndef VECTOR_HPP
+# define VECTOR_HPP
+
+# include <stddef.h>
+
+# define GROWTH_FACTOR 1.5
+
+typedef size_t size_type;
+
+
+namespace ft
+{
+ template < class T, class 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;
+ }
+ };
+}
+
+#endif