diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-01 14:17:04 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-15 12:59:58 +0200 |
| commit | be2676e56ae0c72321c60c4848ce720ccf095b26 (patch) | |
| tree | de96e98403ea8740cea76d9081c932cbbf8edb44 /Vector.hpp | |
| parent | 7af930f2242f933f79dfbb4ddc84bfd532069556 (diff) | |
| download | ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.tar.gz ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.tar.bz2 ft_containers-be2676e56ae0c72321c60c4848ce720ccf095b26.zip | |
Remaking List, Removed include/, test/ and Makefile
Diffstat (limited to 'Vector.hpp')
| -rw-r--r-- | Vector.hpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/Vector.hpp b/Vector.hpp new file mode 100644 index 0000000..0f21995 --- /dev/null +++ b/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 |
