diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-11-19 01:21:14 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-11-19 01:21:14 +0100 |
| commit | febf0637f9869419bc5864543c89e3fd0b1ac316 (patch) | |
| tree | c88262c3f9461beb7acd60e53cc3c19d480c1348 | |
| download | ft_containers-febf0637f9869419bc5864543c89e3fd0b1ac316.tar.gz ft_containers-febf0637f9869419bc5864543c89e3fd0b1ac316.tar.bz2 ft_containers-febf0637f9869419bc5864543c89e3fd0b1ac316.zip | |
vector basic
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | list.cpp | 0 | ||||
| -rw-r--r-- | list.hpp | 12 | ||||
| -rw-r--r-- | vector.hpp | 133 |
5 files changed, 151 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd1c1ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.ghc +a.out diff --git a/README.md b/README.md new file mode 100644 index 0000000..915b177 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ft_containers + +ft_containers project of school 42. diff --git a/list.cpp b/list.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/list.cpp diff --git a/list.hpp b/list.hpp new file mode 100644 index 0000000..aaff3e8 --- /dev/null +++ b/list.hpp @@ -0,0 +1,12 @@ +#ifndef LIST_HPP +# define LIST_HPP + +template < class T, class Alloc = allocator<T> > +class list +{ + + + t_list *head; +}; + +#endif diff --git a/vector.hpp b/vector.hpp new file mode 100644 index 0000000..98038c2 --- /dev/null +++ b/vector.hpp @@ -0,0 +1,133 @@ +#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 |
