diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-31 12:46:19 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-31 12:46:19 +0100 |
| commit | 46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f (patch) | |
| tree | c33522b5337b0214154232870eaa830b3f0e4108 /vector.hpp | |
| parent | f89b202792c118a0baa5fb7f4978dbc2077b64d4 (diff) | |
| download | ft_containers-46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f.tar.gz ft_containers-46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f.tar.bz2 ft_containers-46b9e6f61275c4ed4cd2e8f319cad4f6bc73100f.zip | |
List split cpp hpp (may be ridiculous and may have forgot how templates work)
Diffstat (limited to 'vector.hpp')
| -rw-r--r-- | vector.hpp | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/vector.hpp b/vector.hpp deleted file mode 100644 index 0f21995..0000000 --- a/vector.hpp +++ /dev/null @@ -1,128 +0,0 @@ -#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 |
