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