summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-01-13 06:37:08 +0100
committerkdx <kikoodx@paranoici.org>2023-01-13 06:37:08 +0100
commit1d56888259cb7047898d678204580cc840c12cd0 (patch)
treef66f018cdd0ce357181f1ffafe42e6fd421e1095
parent87e96b663e3f006dc9222ad058024a37ec20acc7 (diff)
download42-containers-1d56888259cb7047898d678204580cc840c12cd0.tar.gz
half of vector i would say
-rw-r--r--Makefile2
-rw-r--r--main.cpp13
-rw-r--r--vector.hpp186
3 files changed, 170 insertions, 31 deletions
diff --git a/Makefile b/Makefile
index 811a240..89ea6f0 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC := c++
-CFLAGS := -O3 -Wall -Wextra -Werror -std=c++98
+CFLAGS := -O0 -g -Wall -Wextra -Werror -std=c++98
SRC := main.cpp
OBJ := $(patsubst %.cpp,%.o,$(SRC))
NAME := a.out
diff --git a/main.cpp b/main.cpp
index d567012..7909b6b 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,9 +1,22 @@
#include "vector.hpp"
+#include <iostream>
int main()
{
ft::vector<int> vec(10);
ft::vector<int> other(vec);
vec = other;
+ other.assign(ft::vector<int>::size_type(5), 3);
+ for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
+ std::cout << other.at(i) << std::endl;
+ std::cout << std::endl;
+ other.push_back(6);
+ other.push_back(8);
+ for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
+ std::cout << other.at(i) << std::endl;
+ std::cout << std::endl;
+ other.pop_back();
+ for (ft::vector<int>::size_type i = 0; i < other.size(); i++)
+ std::cout << other.at(i) << std::endl;
return 0;
}
diff --git a/vector.hpp b/vector.hpp
index c8f7dfb..b43d994 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -2,13 +2,14 @@
#include <memory>
#include <limits>
#include <cstddef>
+#include <stdexcept>
namespace ft {
template <
class T,
class Allocator = std::allocator<T>
> class vector {
- private:
+ public:
typedef T value_type;
typedef Allocator allocator_type;
typedef std::size_t size_type;
@@ -17,10 +18,11 @@ namespace ft {
typedef const value_type& const_reference;
typedef typename Allocator::pointer pointer;
/* TODO */
- typedef int iterator;
- typedef int const_iterator;
- typedef int reverse_iterator;
- typedef int const_reverse_iterator;
+ 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;
@@ -43,7 +45,6 @@ namespace ft {
_alloc = alloc;
_size = count;
_capacity = count;
- /* TODO check error */
if (_capacity > 0)
_data = _alloc.allocate(_capacity);
for (size_type i = 0; i < count; i++)
@@ -57,7 +58,7 @@ namespace ft {
/* TODO */
}
- vector(const vector& other)
+ vector(const vector& other) : _data(NULL)
{
*this = other;
}
@@ -89,7 +90,6 @@ namespace ft {
_alloc = allocator_type();
_size = other.size();
_capacity = other.size();
- /* TODO check error */
if (_capacity > 0)
_data = _alloc.allocate(_capacity);
for (size_type i = 0; i < other.size(); i++)
@@ -98,49 +98,131 @@ namespace ft {
}
/* assign */
- void assign(size_type count, const T& value);
+ void assign(size_type count, const T& value)
+ {
+ *this = vector(count, value);
+ }
+
template<class InputIt>
- void assign(InputIt first, InputIt last);
+ void assign(InputIt first, InputIt last)
+ {
+ /* TODO */
+ }
/* get_allocator */
- allocator_type get_allocator() const;
+ allocator_type get_allocator() const
+ {
+ return _alloc;
+ }
+
/* ELEMENT ACCESS */
/* at */
- reference at(size_type pos);
- const_reference at(size_type pos) const;
+ reference at(size_type pos)
+ {
+ if (pos >= _size)
+ throw std::out_of_range("pos out of bounds");
+ return _data[pos];
+ }
+
+ const_reference at(size_type pos) const
+ {
+ if (pos >= _size)
+ throw std::out_of_range("pos out of bounds");
+ return _data[pos];
+ }
+
/* operator[] */
- reference operator[](size_type pos);
- const_reference operator[](size_type pos) const;
+ reference operator[](size_type pos)
+ {
+ return _data[pos];
+ }
+
+ const_reference operator[](size_type pos) const
+ {
+ return _data[pos];
+ }
+
/* front */
- reference front();
- const_reference front() const;
+ reference front()
+ {
+ return _data[0];
+ }
+
+ const_reference front() const
+ {
+ return _data[0];
+ }
+
/* back */
- reference back();
- const_reference back() const;
+ reference back()
+ {
+ return _data[_size - 1];
+ }
+
+ const_reference back() const
+ {
+ return _data[_size - 1];
+ }
+
/* data */
- T* data();
- const T* data() const;
+ T* data()
+ {
+ return _data;
+ }
+
+ const T* data() const
+ {
+ return _data;
+ }
+
/* ITERATORS TODO */
/* CAPACITY */
bool empty() const
{
return _size == 0;
}
+
size_type size() const
{
return _size;
}
+
size_type max_size() const
{
return std::numeric_limits<difference_type>::max();
}
- void reserve(size_type new_cap);
- size_type capacity() const;
+
+ 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;
+ }
+
+ size_type capacity() const
+ {
+ return _capacity;
+ }
+
/* MODIFIERS */
/* clear */
- void clear();
+ void clear()
+ {
+ for (size_type i = 0; i < _size; i++)
+ _alloc.destroy(_data + i);
+ _size = 0;
+ }
+
/* insert */
- void insert();
iterator insert(const_iterator pos, const T& value);
iterator insert(const_iterator pos, size_type count,
const T& value);
@@ -150,15 +232,59 @@ namespace ft {
/* erase */
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
+
/* push_back */
- void push_back(const T& value);
+ void push_back(const T& value)
+ {
+ if (_size == _capacity)
+ reserve(_size * 2);
+ _alloc.construct(_data + _size, value);
+ _size += 1;
+ }
+
/* pop_back */
- void pop_back();
+ void pop_back()
+ {
+ _alloc.destroy(_data + _size - 1);
+ _size -= 1;
+ }
+
/* resize */
- void resize(size_type count);
- void resize(size_type count, const value_type& value);
+ void resize(size_type count)
+ {
+ return resize(count, T());
+ }
+
+ 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;
+ }
+
/* swap */
- void swap(vector& other);
+ 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,