summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-16 06:59:49 +0100
committerkdx <kdx.42@42l.fr>2023-01-16 06:59:49 +0100
commitd62bb1700590b1c8179ba39e8962836e97ee3cbf (patch)
treeb73aeba9505f0c585f132c13389c32e64b556d02
parent0ebc9b2bce403762759b83803b4253e59e69ac79 (diff)
download42-containers-d62bb1700590b1c8179ba39e8962836e97ee3cbf.tar.gz
move class implementation out of namespace
-rw-r--r--vector.hpp616
1 files changed, 320 insertions, 296 deletions
diff --git a/vector.hpp b/vector.hpp
index b0c0d52..b63d6b8 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -8,347 +8,371 @@ namespace ft {
template <
class T,
class Allocator = std::allocator<T>
- > class vector {
- public:
- typedef T value_type;
- typedef Allocator allocator_type;
- typedef std::size_t size_type;
- typedef std::ptrdiff_t difference_type;
- typedef value_type& reference;
- typedef const value_type& const_reference;
- typedef typename Allocator::pointer pointer;
- /* TODO */
- typedef vector* iterator;
- typedef vector* const_iterator;
- typedef vector* reverse_iterator;
- typedef vector* const_reverse_iterator;
- private:
- size_type _size;
- size_type _capacity;
- T *_data;
- allocator_type _alloc;
- public:
- /* constructor */
- vector()
- {
- vector(size_type(0), T(), allocator_type());
- }
-
- explicit vector(const Allocator& alloc) {
- vector(size_type(0), T(), alloc);
- }
-
- explicit vector(size_type count,
- const T& value = T(),
- const Allocator& alloc = Allocator())
- {
- _alloc = alloc;
- _size = count;
- _capacity = count;
- if (_capacity > 0)
- _data = _alloc.allocate(_capacity);
- for (size_type i = 0; i < count; i++)
- _alloc.construct(_data + i, value);
- }
-
- template<class InputIt>
- vector(InputIt first, InputIt last,
- const Allocator& alloc = Allocator())
- {
- /* TODO */
- }
-
- vector(const vector& other) : _data(NULL)
- {
- *this = other;
- }
-
- /* destructor */
- ~vector()
- {
- if (_data != NULL) {
- for (size_type i = 0; i < _size; i++)
- _alloc.destroy(_data + i);
- _alloc.deallocate(_data, _capacity);
- _size = 0;
- _capacity = 0;
- _data = NULL;
- }
- }
+ > class vector;
+ template<class T, class Alloc>
+ void swap(vector<T,Alloc>& lhs,
+ vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator==(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator!=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator<(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator<=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator>(const ft::vector<T,Alloc>& lhs,
+ const ft::vector<T,Alloc>& rhs);
+ template<class T, class Alloc>
+ bool operator>=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs);
+}
- /* operator= */
- vector& operator=(const vector& other)
- {
- if (_data != NULL) {
- for (size_type i = 0; i < _size; i++)
- _alloc.destroy(_data + i);
- _alloc.deallocate(_data, _capacity);
- _size = 0;
- _capacity = 0;
- _data = NULL;
- }
- _alloc = allocator_type();
- _size = other.size();
- _capacity = other.size();
- if (_capacity > 0)
- _data = _alloc.allocate(_capacity);
- for (size_type i = 0; i < other.size(); i++)
- _alloc.construct(_data + i, other._data[i]);
- return *this;
- }
+template <class T, class Allocator> class ft::vector {
+public:
+ typedef T value_type;
+ typedef Allocator allocator_type;
+ typedef std::size_t size_type;
+ typedef std::ptrdiff_t difference_type;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+ typedef typename Allocator::pointer pointer;
+ /* TODO */
+ typedef vector* iterator;
+ typedef vector* const_iterator;
+ typedef vector* reverse_iterator;
+ typedef vector* const_reverse_iterator;
+private:
+ size_type _size;
+ size_type _capacity;
+ T *_data;
+ allocator_type _alloc;
+public:
+ /* constructor */
+ vector()
+ {
+ vector(size_type(0), T(), allocator_type());
+ }
- /* assign */
- void assign(size_type count, const T& value)
- {
- *this = vector(count, value);
- }
+ explicit vector(const Allocator& alloc) {
+ vector(size_type(0), T(), alloc);
+ }
- template<class InputIt>
- void assign(InputIt first, InputIt last)
- {
- /* TODO */
- }
+ explicit vector(size_type count,
+ const T& value = T(),
+ const Allocator& alloc = Allocator())
+ {
+ _alloc = alloc;
+ _size = count;
+ _capacity = count;
+ if (_capacity > 0)
+ _data = _alloc.allocate(_capacity);
+ for (size_type i = 0; i < count; i++)
+ _alloc.construct(_data + i, value);
+ }
- /* get_allocator */
- allocator_type get_allocator() const
- {
- return _alloc;
- }
+ template<class InputIt>
+ vector(InputIt first, InputIt last,
+ const Allocator& alloc = Allocator())
+ {
+ /* TODO */
+ }
- /* ELEMENT ACCESS */
- /* at */
- reference at(size_type pos)
- {
- if (pos >= _size)
- throw std::out_of_range("pos out of bounds");
- return _data[pos];
- }
+ vector(const vector& other) : _data(NULL)
+ {
+ *this = other;
+ }
- const_reference at(size_type pos) const
- {
- if (pos >= _size)
- throw std::out_of_range("pos out of bounds");
- return _data[pos];
+ /* destructor */
+ ~vector()
+ {
+ if (_data != NULL) {
+ for (size_type i = 0; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _alloc.deallocate(_data, _capacity);
+ _size = 0;
+ _capacity = 0;
+ _data = NULL;
}
+ }
- /* operator[] */
- reference operator[](size_type pos)
- {
- return _data[pos];
- }
+ /* operator= */
+ vector& operator=(const vector& other)
+ {
+ if (_data != NULL) {
+ for (size_type i = 0; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _alloc.deallocate(_data, _capacity);
+ _size = 0;
+ _capacity = 0;
+ _data = NULL;
+ }
+ _alloc = allocator_type();
+ _size = other.size();
+ _capacity = other.size();
+ if (_capacity > 0)
+ _data = _alloc.allocate(_capacity);
+ for (size_type i = 0; i < other.size(); i++)
+ _alloc.construct(_data + i, other._data[i]);
+ return *this;
+ }
- const_reference operator[](size_type pos) const
- {
- return _data[pos];
- }
+ /* assign */
+ void assign(size_type count, const T& value)
+ {
+ *this = vector(count, value);
+ }
- /* front */
- reference front()
- {
- return _data[0];
- }
+ template<class InputIt>
+ void assign(InputIt first, InputIt last)
+ {
+ /* TODO */
+ }
- const_reference front() const
- {
- return _data[0];
- }
+ /* get_allocator */
+ allocator_type get_allocator() const
+ {
+ return _alloc;
+ }
- /* back */
- reference back()
- {
- return _data[_size - 1];
- }
+ /* ELEMENT ACCESS */
+ /* at */
+ reference at(size_type pos)
+ {
+ if (pos >= _size)
+ throw std::out_of_range("pos out of bounds");
+ return _data[pos];
+ }
- const_reference back() const
- {
- return _data[_size - 1];
- }
+ const_reference at(size_type pos) const
+ {
+ if (pos >= _size)
+ throw std::out_of_range("pos out of bounds");
+ return _data[pos];
+ }
- /* data */
- T* data()
- {
- return _data;
- }
+ /* operator[] */
+ reference operator[](size_type pos)
+ {
+ return _data[pos];
+ }
- const T* data() const
- {
- return _data;
- }
+ const_reference operator[](size_type pos) const
+ {
+ return _data[pos];
+ }
- /* ITERATORS TODO */
- /* CAPACITY */
- bool empty() const
- {
- return _size == 0;
- }
+ /* front */
+ reference front()
+ {
+ return _data[0];
+ }
- size_type size() const
- {
- return _size;
- }
+ const_reference front() const
+ {
+ return _data[0];
+ }
- size_type max_size() const
- {
- return std::numeric_limits<difference_type>::max();
- }
+ /* back */
+ reference back()
+ {
+ return _data[_size - 1];
+ }
- void reserve(size_type new_cap)
- {
- if (new_cap <= _capacity)
- return;
- if (new_cap > max_size())
- throw std::length_error("new_cap > max_size()");
- T *const new_data (_alloc.allocate(new_cap));
- for (size_type i = 0; i < _size; i++) {
- _alloc.construct(new_data + i, _data[i]);
- _alloc.destroy(_data + i);
- }
- _alloc.deallocate(_data, _capacity);
- _capacity = new_cap;
- _data = new_data;
- }
+ const_reference back() const
+ {
+ return _data[_size - 1];
+ }
- size_type capacity() const
- {
- return _capacity;
- }
+ /* data */
+ T* data()
+ {
+ return _data;
+ }
- /* MODIFIERS */
- /* clear */
- void clear()
- {
- for (size_type i = 0; i < _size; i++)
- _alloc.destroy(_data + i);
- _size = 0;
- }
+ const T* data() const
+ {
+ return _data;
+ }
- /* insert */
- iterator insert(const_iterator pos, const T& value);
- iterator insert(const_iterator pos, size_type count,
- const T& value);
- template<class InputIt>
- iterator insert(const_iterator pos, InputIt first,
- InputIt last);
- /* erase */
- iterator erase(iterator pos);
- iterator erase(iterator first, iterator last);
-
- /* push_back */
- void push_back(const T& value)
- {
- if (_size == _capacity)
- reserve(_size * 2);
- _alloc.construct(_data + _size, value);
- _size += 1;
- }
+ /* ITERATORS TODO */
+ /* CAPACITY */
+ bool empty() const
+ {
+ return _size == 0;
+ }
- /* pop_back */
- void pop_back()
- {
- _alloc.destroy(_data + _size - 1);
- _size -= 1;
- }
+ size_type size() const
+ {
+ return _size;
+ }
- /* resize */
- void resize(size_type count)
- {
- return resize(count, T());
- }
+ size_type max_size() const
+ {
+ return std::numeric_limits<difference_type>::max();
+ }
- void resize(size_type count, const value_type& value)
- {
- if (count < _size) {
- for (size_type i = count; i < _size; i++)
- _alloc.destroy(_data + i);
- _size = count;
- return;
- }
- reserve(count);
- for (size_type i = _size; i < count; i++)
- _alloc.construct(_data + i, value);
- _size = count;
- }
+ void reserve(size_type new_cap)
+ {
+ if (new_cap <= _capacity)
+ return;
+ if (new_cap > max_size())
+ throw std::length_error("new_cap > max_size()");
+ T *const new_data (_alloc.allocate(new_cap));
+ for (size_type i = 0; i < _size; i++) {
+ _alloc.construct(new_data + i, _data[i]);
+ _alloc.destroy(_data + i);
+ }
+ _alloc.deallocate(_data, _capacity);
+ _capacity = new_cap;
+ _data = new_data;
+ }
- /* swap */
- void swap(vector& other)
- {
- const size_type tmp_size (_size);
- const size_type tmp_capacity (_capacity);
- const T *tmp_data (_data);
- const allocator_type tmp_alloc (_alloc);
- _size = other._size;
- _capacity = other._capacity;
- _data = other._data;
- _alloc = other._alloc;
- other._size = tmp_size;
- other._capacity = tmp_capacity;
- other._data = tmp_data;
- other._alloc = tmp_alloc;
- }
- };
- template<class T, class Alloc>
- void swap(vector<T,Alloc>& lhs,
- vector<T,Alloc>& rhs)
+ size_type capacity() const
{
- lhs.swap(rhs);
+ return _capacity;
}
- template<class T, class Alloc>
- bool operator==(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ /* MODIFIERS */
+ /* clear */
+ void clear()
{
- if (lhs.size() != rhs.size())
- return false;
- for (typename vector<T,Alloc>::size_type i = 0; i < lhs.size(); i++)
- if (lhs[i] != rhs[i])
- return false;
- return true;
+ for (size_type i = 0; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _size = 0;
}
- template<class T, class Alloc>
- bool operator!=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ /* insert */
+ iterator insert(const_iterator pos, const T& value);
+ iterator insert(const_iterator pos, size_type count,
+ const T& value);
+ template<class InputIt>
+ iterator insert(const_iterator pos, InputIt first,
+ InputIt last);
+ /* erase */
+ iterator erase(iterator pos);
+ iterator erase(iterator first, iterator last);
+
+ /* push_back */
+ void push_back(const T& value)
{
- return !(lhs == rhs);
+ if (_size == _capacity)
+ reserve(_size * 2);
+ _alloc.construct(_data + _size, value);
+ _size += 1;
}
- template<class T, class Alloc>
- bool operator<(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ /* pop_back */
+ void pop_back()
{
- const typename vector<T,Alloc>::size_type end
- (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
- for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
- if (lhs[i] < rhs[i])
- return true;
- }
- return lhs.size() < rhs.size();
+ _alloc.destroy(_data + _size - 1);
+ _size -= 1;
}
- template<class T, class Alloc>
- bool operator<=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ /* resize */
+ void resize(size_type count)
{
- return !(lhs > rhs);
+ return resize(count, T());
}
- template<class T, class Alloc>
- bool operator>(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ void resize(size_type count, const value_type& value)
{
- const typename vector<T,Alloc>::size_type end
- (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
- for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
- if (lhs[i] > rhs[i])
- return true;
+ if (count < _size) {
+ for (size_type i = count; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _size = count;
+ return;
}
- return lhs.size() > rhs.size();
+ reserve(count);
+ for (size_type i = _size; i < count; i++)
+ _alloc.construct(_data + i, value);
+ _size = count;
}
- template<class T, class Alloc>
- bool operator>=(const vector<T,Alloc>& lhs,
- const vector<T,Alloc>& rhs)
+ /* swap */
+ void swap(vector& other)
{
- return !(lhs < rhs);
+ const size_type tmp_size (_size);
+ const size_type tmp_capacity (_capacity);
+ const T *tmp_data (_data);
+ const allocator_type tmp_alloc (_alloc);
+ _size = other._size;
+ _capacity = other._capacity;
+ _data = other._data;
+ _alloc = other._alloc;
+ other._size = tmp_size;
+ other._capacity = tmp_capacity;
+ other._data = tmp_data;
+ other._alloc = tmp_alloc;
+ }
+};
+
+template<class T, class Alloc>
+void ft::swap(vector<T,Alloc>& lhs,
+ vector<T,Alloc>& rhs)
+{
+ lhs.swap(rhs);
+}
+
+template<class T, class Alloc>
+bool ft::operator==(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ if (lhs.size() != rhs.size())
+ return false;
+ for (typename vector<T,Alloc>::size_type i = 0; i < lhs.size(); i++)
+ if (lhs[i] != rhs[i])
+ return false;
+ return true;
+}
+
+template<class T, class Alloc>
+bool ft::operator!=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ return !(lhs == rhs);
+}
+
+template<class T, class Alloc>
+bool ft::operator<(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ const typename vector<T,Alloc>::size_type end
+ (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
+ for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
+ if (lhs[i] < rhs[i])
+ return true;
}
+ return lhs.size() < rhs.size();
+}
+
+template<class T, class Alloc>
+bool ft::operator<=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ return !(lhs > rhs);
+}
+
+template<class T, class Alloc>
+bool ft::operator>(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ const typename vector<T,Alloc>::size_type end
+ (lhs.size() < rhs.size() ? lhs.size() : rhs.size());
+ for (typename vector<T,Alloc>::size_type i = 0; i < end; i++) {
+ if (lhs[i] > rhs[i])
+ return true;
+ }
+ return lhs.size() > rhs.size();
+}
+
+template<class T, class Alloc>
+bool ft::operator>=(const vector<T,Alloc>& lhs,
+ const vector<T,Alloc>& rhs)
+{
+ return !(lhs < rhs);
}