vector without reverse iterator not tested yet

This commit is contained in:
kinou-p 2022-11-17 06:17:54 +01:00
parent 2124474c9a
commit 706985d893
5 changed files with 196 additions and 129 deletions

View File

@ -6,13 +6,12 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/10/12 19:45:18 by apommier #+# #+# #
# Updated: 2022/10/19 14:25:28 by apommier ### ########.fr #
# Updated: 2022/11/16 21:54:17 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = tests/stack_test.cpp\
tests/main.cpp
SRCS = tests/main.cpp
OBJS = ${SRCS:.cpp=.o}

View File

@ -6,13 +6,15 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/30 16:14:35 by apommier #+# #+# */
/* Updated: 2022/11/14 06:27:37 by apommier ### ########.fr */
/* Updated: 2022/11/16 19:21:41 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RANDOM_ACCESS_ITERATOR_HPP
# define RANDOM_ACCESS_ITERATOR_HPP
# include <cstddef>
namespace ft
{
template<typename T>

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:32 by apommier #+# #+# */
/* Updated: 2022/11/14 06:50:18 by apommier ### ########.fr */
/* Updated: 2022/11/16 22:42:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,11 @@
# include "./iterators/random_access_iterator.hpp"
# include <cstddef>
# include <memory>
# include <stdexcept>
namespace ft
{
@ -25,18 +30,18 @@ class vector
//-----------------------------
//---------MEMBER TYPE---------
//-----------------------------
typedef T value_type;
typedef Allocator allocator_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T value_type;
typedef Allocator allocator_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef ft::random_access_iterator<value_type> iterator;
typedef const ft::random_access_iterator<value_type> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef ptrdiff_t difference_type;
typedef std::size_t size_type;
//-----------------------------
@ -48,9 +53,9 @@ class vector
size_type _size;
size_type _capacity;
allocator_type _alloc;
ft::random_access_iterator<value_type> _end;
ft::random_access_iterator<value_type> _start;
allocator_type _alloc;
//ft::random_access_iterator<value_type> _end;
//ft::random_access_iterator<value_type> _start;
//pointer _end_capacity;
@ -64,13 +69,7 @@ class vector
_tab = 0;
_size = 0;
_capacity = 0;
//_start = 0;
//_end = 0;
_alloc = alloc;
_start = _alloc.allocate(0);
_end = _start;
}
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()): _alloc(alloc) //fill constructor
@ -81,36 +80,30 @@ class vector
_alloc = alloc;
_tab = _alloc.allocate(n);
_start = _tab;
_size = n;
_capacity = n;
_end = _start + n;
while (n--)
_alloc.construct(_tab + n, val);
//_end = _start;
//_start + 5;
///5 + _start;
}
//template <class InputIterator>
//vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) //range constructor
//{
//}
vector (const vector& x)//copy constructor
{
*this = x;
}
~vector()
{
if (_tab && _capacity)
_alloc.deallocate(_tab, _capacity);
}
vector& operator= (const vector& x)//assignation operator
{
_tab = x._tab;
_size = x._size;
_capacity = x._capacity;
_alloc = x._alloc;
return (*this);
}
//----------------------------------
@ -122,22 +115,22 @@ class vector
//-------------------------
iterator begin()
{
return (_start);
return (_tab);
}
iterator end()
{
return (_end);
return (_tab + _size);
}
reverse_iterator rbegin()
{
return (_end);
return (_tab + _size);
}
reverse_iterator rend()
{
return (_start);
return (_tab);
}
//------------------------
@ -145,7 +138,7 @@ class vector
//------------------------
size_type size() const
{
return (_end - _start);
return (_size);
}
size_type max_size() const
@ -155,18 +148,26 @@ class vector
void resize (size_type n, value_type val = value_type()) //Resizes the container so that it contains n elements.
{
if (n > this->max_size())
throw (std::length_error("vector::resize"));
else if (n < this->size())
if (n < _size)
{
while (n > this->size())
while (n < _size)
{
_end--;
_alloc.destroy(_end);
//_end--;
_size--;
_alloc.destroy(_tab + _size);
}
}
//else // n > size
//insert()
else if (n > _size)
{
this->reserve(n);
while (n > _size)
{
_alloc.construct(_tab + _size, val);
//_end++;
}
_size = n;
}
}
size_type capacity() const
@ -176,14 +177,25 @@ class vector
bool empty() const
{
if (_end == _start)
if (!_size)
return (1);
return(0);
}
void reserve (size_type n)
{
if (n > this->max_size())
throw (std::length_error("ft::vector::reserve"));
else if (n > _capacity)
{
value_type *tmp;
tmp = _alloc.allocate(n, 0);
for (int i = 0; _tab + i != _tab + _size; i++)
*(tmp + i) = *(_tab + i);
_alloc.deallocate(_tab, _capacity);
_tab = tmp;
_capacity = n;
}
}
//------------------------------
@ -213,32 +225,32 @@ class vector
reference front()
{
return (*_start);
return (*_tab);
}
const_reference front() const
{
return (*_start);
return (*_tab);
}
reference back()
{
return (*_end);
return (*(_tab + _size));
}
const_reference back() const
{
return (*_end);
return (*(_tab + _size));
}
value_type* data()
{
return (_start);
return (_tab);
}
const value_type* data() const
{
return (_start);
return (_tab);
}
//-------------------------
@ -247,86 +259,147 @@ class vector
template <class InputIterator>
void assign (InputIterator first, InputIterator last) //range
{
this->clear();
while (first < last)
{
push_back(*first);
first++;
}
}
void assign (size_type n, const value_type& val) //fill
{
this->clear();
if (n > this->max_size())
throw (std::length_error("vector::resize"));
else if (n < this->size())
{
while (n > this->size())
{
_end--;
_alloc.destroy(_end);
}
}
//else // n > size
//insert()
this->clear();
this->resize(n);
while (n--)
_alloc.construct(_tab + n, val);
}
void push_back (const value_type& val)
{
//if (!this->max_size() - _size)
// ;
//throw or alloc
//else
//{
_alloc.construct(_end.getPointer() - 2, val);
_size++;
_end++;
//}
if (!_capacity)
this->reserve(1);
else if (_size == _capacity)
this->reserve(_size);
_alloc.construct(_tab + _size, val);
_size++;
}
void pop_back()
{
//if (!_size)
// ;
//throw std::;
_alloc.destroy(_end - 1);
_end--;
_size--;
if (_size)
{
_alloc.destroy(_tab + _size - 1);
_size--;
}
}
iterator insert (iterator position, const value_type& val) //single element
{
this->insert(position, 1, val);
}
void insert (iterator position, size_type n, const value_type& val) //fill
{
int j = 0;
int i;
value_type *tmp;
if (_size + n > this->max_size())
throw (std::length_error("vector::resize"));
tmp = _alloc.allocate(_size + n);
for (i = 0; i < position; i++)
tmp[i] = _tab[i];
while (n - j)
{
tmp[i + j] = val;
j++;
}
while (_size - i)
tmp[i + j] = _tab[i];
this->clear();
_tab = tmp;
_size += n;
_capacity = _size;
}
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last) //range
{
int j = 0;
int i;
value_type *tmp;
if (_size + (last - first) > this->max_size())
throw (std::length_error("vector::resize"));
tmp = _alloc.allocate(_size + (last - first));
for (i = 0; i < position; i++)
tmp[i] = _tab[i];
while (first + j != last)
{
tmp[i + j] = *(first + j);
j++;
}
while (_size - i)
tmp[i + j] = _tab[i];
this->clear();
_tab = tmp;
_size += (last - first);
_capacity = _size;
}
iterator erase (iterator position)
{
iterator ret = position;
_alloc.destroy(position);
while (_tab + _size - 1 > position + _tab)
{
*position = *(position + 1);
position++;
_alloc.destroy(position);
}
_size--;
return (ret + 1);
}
iterator erase (iterator first, iterator last)
{
difference_type lenght = last - first;
for (int i = 0; lenght - i; i++)
_alloc.destroy(first + i);
while (_tab + _size - 1 != _tab + lenght)
{
*first = *(first + lenght);
first++;
_alloc.destroy(first + lenght);
}
return (last + 1);
}
void swap (vector& x)
{
value_type *tab_tmp = _tab;
size_type size_tmp = _size;
size_type capacity_tmp = _capacity;
allocator_type alloc_tmp = _alloc;
_tab = x._tab;
_size = x._size;
_capacity = x._capacity;
_alloc = x._alloc;
x._tab = tab_tmp;
x._size = size_tmp;
x._capacity = capacity_tmp;
x._alloc = alloc_tmp;
}
void clear()
{
int i = 0;
while (_start != _end)
while (_size)
this->pop_back();
}
@ -377,9 +450,6 @@ class vector
// {
// }
};
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:33 by apommier #+# #+# */
/* Updated: 2022/10/28 17:39:13 by apommier ### ########.fr */
/* Updated: 2022/11/16 19:07:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,11 +6,20 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/19 14:04:37 by apommier #+# #+# */
/* Updated: 2022/11/14 06:48:31 by apommier ### ########.fr */
/* Updated: 2022/11/16 22:18:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_containers.hpp"
#include <sys/time.h>
long get_time(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
void stack_tester()
{
@ -21,41 +30,28 @@ void stack_tester()
std::cout << first.size() << std::endl;
}
void print_vector(ft::vector<int> x)
{
std::cout << "-----print vector----\n";
for (size_t i = 0; i < x.size(); i++)
std::cout << x[i] << std::endl;
}
void vector_tester()
{
//ft::vector<int> first;
ft::vector<int> first(5, 3);
ft::vector<int> second;
ft::vector<int>::iterator it = first.begin();
std::cout << "------empty------\n";
std::cout << first.empty() << std::endl;
std::cout << second.empty() << std::endl;
std::cout << "------size------\n";
std::cout << first.size() << std::endl;
std::cout << "------pushback then size------\n";
first.push_back(5);
first.push_back(5);
std::cout << first.size() << std::endl;
std::cout << "------------\n";
// std::cout << first[1] << std::endl;
// std::cout << "------1------\n";
// std::cout << first.at(1) << std::endl;
// std::cout << "-------2-----\n";
// std::cout << first.at(2) << std::endl;
// std::cout << "-------3-----\n";
// std::cout << first.at(3) << std::endl;
// std::cout << "-------4-----\n";
// std::cout << first.at(4) << std::endl;
// std::cout << "-------5-----\n";
// std::cout << first.at(0) << std::endl;
// std::cout << "--------6----\n";
// std::cout << first.at(6) << std::endl;
// std::cout << "------------\n";
std::cout << "------for------\n";
for (size_t i = 0; i < first.size(); i++)
{
std::cout << first[1] << std::endl;
// std::cout << "------------\n";
}
print_vector(first);
std::cout << "-------0-----\n";
std::cout << first.at(5) << std::endl;