add capacity() and basic assign()
This commit is contained in:
@@ -30,15 +30,13 @@ public:
|
||||
* copliens :
|
||||
************/
|
||||
// constructor -------------------------------
|
||||
// explicit vector (const allocator_type& alloc = allocator_type());
|
||||
// explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
|
||||
explicit vector (const allocator_type & alloc = allocator_type());
|
||||
explicit vector (size_type n, const value_type& val = value_type(),
|
||||
const allocator_type& alloc = allocator_type());
|
||||
// template <class InputIterator>
|
||||
// vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
|
||||
// vector (const vector& x);
|
||||
// TMP
|
||||
vector();
|
||||
// vector (InputIterator first, InputIterator last,
|
||||
// const allocator_type& alloc = allocator_type());
|
||||
vector( vector const & src );
|
||||
// TMP END
|
||||
// destructor --------------------------------
|
||||
~vector();
|
||||
// operator= ---------------------------------
|
||||
@@ -71,7 +69,7 @@ public:
|
||||
// resize ------------------------------------
|
||||
//void resize(size_type n, value_type val = value_type());
|
||||
// capacity ----------------------------------
|
||||
//size_type capacity() const;
|
||||
size_type capacity() const;
|
||||
// empty -------------------------------------
|
||||
//bool empty() const;
|
||||
// reserve -----------------------------------
|
||||
@@ -99,7 +97,7 @@ public:
|
||||
// assign ------------------------------------
|
||||
//template <class InputIterator>
|
||||
// void assign(InputIterator first, InputIterator last);
|
||||
//void assign(size_type n, const value_type& val);
|
||||
void assign(size_type n, const value_type& val);
|
||||
// push_back ---------------------------------
|
||||
void push_back(const value_type & val);
|
||||
// pop_back ----------------------------------
|
||||
|
||||
@@ -9,19 +9,38 @@ namespace ft {
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
// template <class InputIterator>
|
||||
// vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
|
||||
|
||||
template <class T, class Allocator>
|
||||
vector<T, Allocator>::vector()
|
||||
vector<T, Allocator>::
|
||||
vector( const Allocator & alloc )
|
||||
: _size(0)
|
||||
, _capacity(0)
|
||||
, _mem_ptr(NULL)
|
||||
, _allocator(alloc)
|
||||
{
|
||||
// std::cout << COPLIEN_COLOR "vector constructor" RESET "\n";
|
||||
_allocator = allocator_type();
|
||||
return;
|
||||
}
|
||||
|
||||
template <class T, class Allocator>
|
||||
vector<T, Allocator>::vector( vector const & src ) {
|
||||
vector<T, Allocator>::
|
||||
vector( size_type n, const T & val, const Allocator & alloc )
|
||||
: _size(0)
|
||||
, _capacity(0)
|
||||
, _mem_ptr(NULL)
|
||||
, _allocator(alloc)
|
||||
{
|
||||
// std::cout << COPLIEN_COLOR "vector constructor" RESET "\n";
|
||||
assign(n, val);
|
||||
return;
|
||||
}
|
||||
|
||||
template <class T, class Allocator>
|
||||
vector<T, Allocator>::
|
||||
vector( vector const & src )
|
||||
{
|
||||
// std::cout << COPLIEN_COLOR "vector copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
@@ -32,7 +51,9 @@ vector<T, Allocator>::vector( vector const & src ) {
|
||||
*********************************************/
|
||||
|
||||
template <class T, class Allocator>
|
||||
vector<T, Allocator>::~vector() {
|
||||
vector<T, Allocator>::
|
||||
~vector()
|
||||
{
|
||||
// std::cout << COPLIEN_COLOR "vector destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
@@ -42,7 +63,9 @@ vector<T, Allocator>::~vector() {
|
||||
*********************************************/
|
||||
|
||||
template <class T, class Allocator>
|
||||
vector<T, Allocator> & vector<T, Allocator>::operator=( vector const & rhs ) {
|
||||
vector<T, Allocator> & vector<T, Allocator>::
|
||||
operator=( vector const & rhs )
|
||||
{
|
||||
// Base::operator=(rhs);
|
||||
if ( this != &rhs )
|
||||
{
|
||||
@@ -67,10 +90,18 @@ vector<T, Allocator> & vector<T, Allocator>::operator=( vector const & rhs ) {
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
template <class T, class Allocator>
|
||||
typename vector<T, Allocator>::iterator vector<T, Allocator>::begin() {return _mem_ptr;}
|
||||
typename vector<T, Allocator>::iterator vector<T, Allocator>::
|
||||
begin()
|
||||
{
|
||||
return _mem_ptr;
|
||||
}
|
||||
// end ---------------------------------------
|
||||
template <class T, class Allocator>
|
||||
typename vector<T, Allocator>::iterator vector<T, Allocator>::end() {return &_mem_ptr[_size];}
|
||||
typename vector<T, Allocator>::iterator vector<T, Allocator>::
|
||||
end()
|
||||
{
|
||||
return &_mem_ptr[_size];
|
||||
}
|
||||
// rbegin ------------------------------------
|
||||
// rend --------------------------------------
|
||||
|
||||
@@ -79,22 +110,32 @@ typename vector<T, Allocator>::iterator vector<T, Allocator>::end() {return &_me
|
||||
************/
|
||||
// size --------------------------------------
|
||||
template <class T, class Allocator>
|
||||
typename vector<T, Allocator>::size_type vector<T, Allocator>::size() const {return _size;}
|
||||
typename vector<T, Allocator>::size_type vector<T, Allocator>::
|
||||
size( ) const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
// max_size ----------------------------------
|
||||
// resize ------------------------------------
|
||||
// capacity ----------------------------------
|
||||
template <class T, class Allocator>
|
||||
typename vector<T, Allocator>::size_type vector<T, Allocator>::
|
||||
capacity() const
|
||||
{
|
||||
return _capacity;
|
||||
}
|
||||
// empty -------------------------------------
|
||||
// reserve -----------------------------------
|
||||
template <class T, class Allocator>
|
||||
void vector<T, Allocator>::reserve(size_type new_cap) {
|
||||
void vector<T, Allocator>::
|
||||
reserve( size_type new_cap )
|
||||
{
|
||||
value_type * tmp_ptr;
|
||||
iterator first = begin();
|
||||
iterator last = end();
|
||||
|
||||
if (new_cap > _allocator.max_size())
|
||||
throw std::length_error("new_cap > max_size");
|
||||
throw std::length_error("reserve: new_cap > max_size");
|
||||
if (_capacity == _allocator.max_size())
|
||||
throw std::length_error("capacity == max_size");
|
||||
throw std::length_error("reserve: capacity == max_size");
|
||||
if (new_cap <= _capacity)
|
||||
return ;
|
||||
|
||||
@@ -103,11 +144,11 @@ void vector<T, Allocator>::reserve(size_type new_cap) {
|
||||
|
||||
if (_mem_ptr)
|
||||
{
|
||||
// TMP replacing assign()
|
||||
// TMP replacing assign(first, last)
|
||||
for (size_type i = 0; i < _size; i++)
|
||||
tmp_ptr[i] = _mem_ptr[i];
|
||||
// TMP END
|
||||
_destroy(first, last);
|
||||
_destroy(begin(), end());
|
||||
_allocator.deallocate(_mem_ptr, _capacity);
|
||||
}
|
||||
_mem_ptr = tmp_ptr;
|
||||
@@ -118,7 +159,11 @@ void vector<T, Allocator>::reserve(size_type new_cap) {
|
||||
******************/
|
||||
// operator[] --------------------------------
|
||||
template <class T, class Allocator>
|
||||
typename vector<T, Allocator>::reference vector<T, Allocator>::operator[](size_type n) {return _mem_ptr[n];}
|
||||
typename vector<T, Allocator>::reference vector<T, Allocator>::
|
||||
operator[](size_type n)
|
||||
{
|
||||
return _mem_ptr[n];
|
||||
}
|
||||
// at ----------------------------------------
|
||||
// front -------------------------------------
|
||||
// back --------------------------------------
|
||||
@@ -127,9 +172,33 @@ typename vector<T, Allocator>::reference vector<T, Allocator>::operator[](size_t
|
||||
* modifiers :
|
||||
*************/
|
||||
// assign ------------------------------------
|
||||
template <class T, class Allocator>
|
||||
void vector<T, Allocator>::
|
||||
assign( size_type n, const T & val )
|
||||
{
|
||||
if (n > _allocator.max_size())
|
||||
throw std::length_error("assign: n > max_size");
|
||||
|
||||
value_type * tmp_ptr;
|
||||
|
||||
_destroy(begin(), end());
|
||||
if (n > _capacity)
|
||||
{
|
||||
_capacity = n;
|
||||
tmp_ptr = _allocator.allocate(n);
|
||||
if (_mem_ptr)
|
||||
_allocator.deallocate(_mem_ptr, _capacity);
|
||||
_mem_ptr = tmp_ptr;
|
||||
}
|
||||
_size = n;
|
||||
while (n)
|
||||
_allocator.construct(&_mem_ptr[--n], val);
|
||||
}
|
||||
// push_back ---------------------------------
|
||||
template <class T, class Allocator>
|
||||
void vector<T, Allocator>::push_back(const value_type & element) {
|
||||
void vector<T, Allocator>::
|
||||
push_back( const value_type & element )
|
||||
{
|
||||
if (_size >= _capacity)
|
||||
reserve(std::min(_size + 1, _allocator.max_size() / 2) * 2);
|
||||
_allocator.construct(&_mem_ptr[_size], element);
|
||||
@@ -149,7 +218,9 @@ void vector<T, Allocator>::push_back(const value_type & element) {
|
||||
*********************************************/
|
||||
|
||||
template <class T, class Allocator>
|
||||
void vector<T, Allocator>::_destroy(iterator first, iterator last) {
|
||||
void vector<T, Allocator>::
|
||||
_destroy(iterator first, iterator last)
|
||||
{
|
||||
while (first != last)
|
||||
{
|
||||
_allocator.destroy(first);
|
||||
|
||||
@@ -13,19 +13,33 @@
|
||||
|
||||
int main() {
|
||||
|
||||
TEST(vector::vector (constructor))
|
||||
{
|
||||
TEST(test assign capacity)
|
||||
{
|
||||
ft::vector<int> myvector;
|
||||
int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1};
|
||||
int size = sizeof(myint) / sizeof(myint[0]);
|
||||
int size;
|
||||
|
||||
std::cout << "capacity before assignation : " << myvector.capacity() << "\n";
|
||||
|
||||
std::cout << "\nassign 1\n";
|
||||
myvector.assign(1, 12);
|
||||
size = myvector.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
myvector.push_back(myint[i]);
|
||||
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << " - ";
|
||||
std::cout << "\nsize :" << size << " , capacity :" << myvector.capacity() << "\n";
|
||||
|
||||
std::cout << "\nassign 3\n";
|
||||
myvector.assign(3, 12);
|
||||
size = myvector.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << "\n";
|
||||
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << " - ";
|
||||
std::cout << "\nsize :" << size << " , capacity :" << myvector.capacity() << "\n";
|
||||
|
||||
std::cout << " -> myvector stores " << int(myvector.size()) << " numbers.\n";
|
||||
std::cout << "\nassign 7268\n";
|
||||
myvector.assign(7268, 12);
|
||||
size = myvector.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << " - ";
|
||||
std::cout << "\nsize :" << size << " , capacity :" << myvector.capacity() << "\n";
|
||||
}
|
||||
TESTEND
|
||||
|
||||
@@ -317,30 +331,27 @@ int main() {
|
||||
std::cout << "Size of third: " << int (third.size()) << '\n';
|
||||
}
|
||||
TESTEND
|
||||
*/
|
||||
|
||||
TEST(vector::push_back)
|
||||
{
|
||||
std::vector<int> myvector;
|
||||
ft::vector<int> myvector;
|
||||
|
||||
// original :
|
||||
//
|
||||
// int myint;
|
||||
// std::cout << "Please enter some integers (enter 0 to end):\n";
|
||||
// do {
|
||||
// std::cin >> myint;
|
||||
// myvector.push_back (myint);
|
||||
// } while (myint);
|
||||
//
|
||||
// replaced by :
|
||||
int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1};
|
||||
int size = sizeof(myint) / sizeof(myint[0]);
|
||||
for (int i = 0; i < size; i++)
|
||||
myvector.push_back (myint[i]);
|
||||
myvector.push_back(myint[i]);
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << "\n";
|
||||
|
||||
std::cout << " -> myvector stores " << int(myvector.size()) << " numbers.\n";
|
||||
|
||||
|
||||
std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
|
||||
}
|
||||
TESTEND
|
||||
|
||||
/*
|
||||
TEST(vector::pop_back)
|
||||
{
|
||||
std::vector<int> myvector;
|
||||
|
||||
Reference in New Issue
Block a user