You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.8 KiB
149 lines
4.8 KiB
#pragma once
|
|
#include "iterator.hpp"
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
|
|
namespace ft {
|
|
|
|
template < class T, class Allocator = std::allocator< T > > class vector {
|
|
public:
|
|
typedef T value_type;
|
|
typedef Allocator allocator_type;
|
|
typedef class Allocator::pointer pointer;
|
|
typedef class Allocator::const_pointer cst_pointer;
|
|
typedef value_type &reference;
|
|
typedef const value_type &cst_reference;
|
|
typedef ft::ra_iterator< pointer > iterator;
|
|
typedef ft::ra_iterator< cst_pointer > cst_iterator;
|
|
typedef ft::rev_iterator< iterator > reverse_iterator;
|
|
typedef ft::rev_iterator< cst_iterator > cst_reverse_iterator;
|
|
typedef std::ptrdiff_t difference_type;
|
|
typedef std::size_t size_type;
|
|
|
|
private:
|
|
Allocator _alloc;
|
|
pointer _begin;
|
|
size_type _size;
|
|
size_type _capacity;
|
|
|
|
public:
|
|
vector(void) : _alloc(Allocator()), _begin(NULL), _size(0), _capacity(0) {}
|
|
|
|
explicit vector(const Allocator &alloc)
|
|
: _alloc(alloc), _begin(NULL), _size(0), _capacity(0){};
|
|
|
|
explicit vector(size_type count, const T &value = T(),
|
|
const Allocator &alloc = Allocator())
|
|
: _alloc(alloc), _size(count), _capacity(count) {
|
|
_begin = _alloc.allocate(count);
|
|
for (size_type i = 0; i < count; i++)
|
|
alloc.construct(_begin + i, value);
|
|
}
|
|
|
|
template < class It >
|
|
vector(It first, It last, const Allocator &alloc = Allocator())
|
|
: _alloc(alloc) {
|
|
_size = distance(first, last);
|
|
_capacity = _size;
|
|
_begin = _alloc.allocate(_size);
|
|
for (size_type i = 0; i < _size; i++)
|
|
_alloc.construct(_begin + i, *(first + i));
|
|
}
|
|
|
|
vector(const vector &other)
|
|
: _alloc(other._alloc), _size(other._size), _capacity(other._capacity) {
|
|
_begin = _alloc.allocate(_capacity);
|
|
for (size_type i = 0; i < _size; i++)
|
|
_alloc.construct(_begin + i, *(other._begin + i));
|
|
}
|
|
|
|
~vector(void) {
|
|
clear();
|
|
if (_capacity)
|
|
_alloc.deallocate(_begin, _capacity);
|
|
}
|
|
|
|
vector &operator=(const vector &other) {
|
|
clear();
|
|
_alloc.deallocate(_begin, _capacity);
|
|
_alloc = other._alloc;
|
|
_capacity = other._capacity;
|
|
_size = other._size;
|
|
_begin = _alloc.allocate(_capacity);
|
|
for (size_type i = 0; i < _size; i++)
|
|
_alloc.construct(_begin + i, *(other._begin + i));
|
|
return (*this);
|
|
}
|
|
|
|
void assign(size_type count, const T &value);
|
|
template < class It > void assign(It first, It last);
|
|
allocator_type get_allocator(void) const { return _alloc; }
|
|
|
|
// ACCESS:
|
|
reference at(size_type pos);
|
|
cst_reference at(size_type pos) const;
|
|
|
|
reference operator[](size_type pos);
|
|
cst_reference operator[](size_type pos) const;
|
|
|
|
reference front(void);
|
|
cst_reference front(void) const;
|
|
|
|
reference back(void);
|
|
cst_reference back(void) const;
|
|
|
|
T *data(void);
|
|
const T *data(void) const;
|
|
|
|
// ITERATORS:
|
|
ft::ra_iterator< reference > begin(void);
|
|
ft::ra_iterator< cst_reference > begin(void) const;
|
|
|
|
ft::ra_iterator< reference > end(void);
|
|
ft::ra_iterator< cst_reference > end(void) const;
|
|
|
|
ft::rev_iterator< iterator > rbegin(void);
|
|
ft::rev_iterator< cst_iterator > rbegin(void) const;
|
|
|
|
ft::rev_iterator< iterator > rend(void);
|
|
ft::rev_iterator< cst_iterator > rend(void) const;
|
|
|
|
// CAPACITY:
|
|
bool empty(void) const { return _size == 0; }
|
|
size_type size(void) const { return _size; }
|
|
size_type max_size(void) const { return _alloc.max_size(); }
|
|
void reserve(size_type new_cap) {
|
|
if (new_cap > max_size())
|
|
throw(std::length_error("vector::reserve: new_cap > max_size"));
|
|
Allocator new_alloc;
|
|
pointer new_begin;
|
|
new_begin = new_alloc.allocate(new_cap);
|
|
for (size_type i = 0; i < _size; i++)
|
|
new_alloc.construct(new_begin + i, *(_begin + i));
|
|
clear();
|
|
_alloc.deallocate(_begin, _capacity);
|
|
_alloc = new_alloc;
|
|
_begin = new_begin;
|
|
_capacity = new_cap;
|
|
}
|
|
size_type capacity(void) const { return _capacity; }
|
|
|
|
// MODIFIERS:
|
|
void clear(void) {
|
|
for (size_type i = 0; i < _size; i++)
|
|
_alloc.destroy(_begin + i);
|
|
_size = 0;
|
|
}
|
|
iterator insert(cst_iterator pos, const T &value);
|
|
iterator insert(cst_iterator pos, size_type count, const T &value);
|
|
template < class It > iterator insert(cst_iterator pos, It first, It last);
|
|
iterator erase(iterator pos);
|
|
iterator erase(iterator first, iterator last);
|
|
void push_back(const T &value);
|
|
void pop_back(void);
|
|
void resize(size_type count);
|
|
void resize(size_type count, T value = T());
|
|
void swap(vector &other);
|
|
};
|
|
|
|
} // namespace ft
|
|
|