summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx.42@42l.fr>2023-01-17 18:52:21 +0100
committerkdx <kdx.42@42l.fr>2023-01-17 18:52:21 +0100
commit30beabd25ccac9f6cfca59cdeb5d1c9849d57059 (patch)
tree01f7739e9bc5b6076bf9b286ed3af07e3f2419af
parentfc6f771143a0d60055f9a7ce0e6559089ab59349 (diff)
download42-containers-main.tar.gz
i hate itHEADmain
-rw-r--r--main.cpp5
-rw-r--r--vector.hpp302
2 files changed, 268 insertions, 39 deletions
diff --git a/main.cpp b/main.cpp
index 0f91611..e0e97c3 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,11 +20,12 @@ int main()
other.push_back(8);
std::for_each(other.begin(), other.end(), print);
std::cout << std::endl;
- const vector<int> constvec(other.rbegin(), other.rend());
+ const vector<int> constvec(other);
std::for_each(constvec.rbegin(), constvec.rend(), print);
+ std::for_each(constvec.begin(), constvec.end(), print);
std::cout << std::endl;
other.pop_back();
- std::for_each(other.begin(), other.end(), print);
+ std::for_each(other.rbegin(), other.rend(), print);
std::cout << std::endl;
std::cout << "comparaisons" << std::endl;
std::cout << (vec == other) << std::endl;
diff --git a/vector.hpp b/vector.hpp
index e983b27..ad8b716 100644
--- a/vector.hpp
+++ b/vector.hpp
@@ -42,77 +42,304 @@ public:
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename Allocator::pointer pointer;
- typedef T* iterator;
- typedef T* const_iterator;
+ class iterator {
+ public:
+ typedef iterator self_type;
+ typedef reference reference_type;
+ typedef T* pointer_type;
+ private:
+ pointer_type _ptr;
+ public:
+ iterator(pointer_type ptr) : _ptr(ptr)
+ {
+ }
+ iterator(const self_type& src) : _ptr(src._ptr)
+ {
+ }
+ reference_type operator*()
+ {
+ return *_ptr;
+ }
+ pointer_type operator->()
+ {
+ return _ptr;
+ }
+ self_type& operator++()
+ {
+ _ptr++;
+ return *this;
+ }
+ self_type& operator++(int)
+ {
+ self_type tmp (*this);
+ _ptr++;
+ return tmp;
+ }
+ self_type operator--()
+ {
+ _ptr--;
+ return *this;
+ }
+ self_type operator--(int)
+ {
+ self_type tmp (*this);
+ _ptr--;
+ return tmp;
+ }
+ self_type& operator+=(difference_type n)
+ {
+ *this = *this + n;
+ return *this;
+ }
+ self_type operator+(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp += n;
+ return tmp;
+ }
+ self_type operator-(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp -= n;
+ return tmp;
+ }
+ self_type& operator-=(difference_type n)
+ {
+ *this = *this - n;
+ return *this;
+ }
+ bool operator==(const self_type& other) const
+ {
+ return _ptr == other._ptr;
+ }
+ bool operator!=(const self_type& other) const
+ {
+ return !(*this == other);
+ }
+ };
+ class const_iterator {
+ private:
+ typedef iterator self_type;
+ typedef const_reference reference_type;
+ typedef const T* pointer_type;
+ pointer_type _ptr;
+ public:
+ const_iterator(pointer_type ptr) : _ptr(ptr)
+ {
+ }
+ const_iterator(const self_type& src) : _ptr(src._ptr)
+ {
+ }
+ reference_type operator*()
+ {
+ return *_ptr;
+ }
+ pointer_type operator->()
+ {
+ return _ptr;
+ }
+ self_type& operator++()
+ {
+ _ptr++;
+ return *this;
+ }
+ self_type& operator++(int)
+ {
+ self_type tmp (*this);
+ _ptr++;
+ return tmp;
+ }
+ self_type operator--()
+ {
+ _ptr--;
+ return *this;
+ }
+ self_type operator--(int)
+ {
+ self_type tmp (*this);
+ _ptr--;
+ return tmp;
+ }
+ self_type& operator+=(difference_type n)
+ {
+ *this = *this + n;
+ return *this;
+ }
+ self_type operator+(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp += n;
+ return tmp;
+ }
+ self_type operator-(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp -= n;
+ return tmp;
+ }
+ self_type& operator-=(difference_type n)
+ {
+ *this = *this - n;
+ return *this;
+ }
+ bool operator==(const self_type& other) const
+ {
+ return _ptr == other._ptr;
+ }
+ bool operator!=(const self_type& other) const
+ {
+ return !(*this == other);
+ }
+ };
class reverse_iterator {
private:
- T* _ptr;
+ typedef reverse_iterator self_type;
+ typedef reference reference_type;
+ typedef T* pointer_type;
+ pointer_type _ptr;
public:
- reverse_iterator(T* ptr) : _ptr(ptr)
+ reverse_iterator(pointer_type ptr) : _ptr(ptr)
+ {
+ }
+ reverse_iterator(const self_type& src) : _ptr(src._ptr)
{
}
- reference operator*()
+ reference_type operator*()
{
return *_ptr;
}
- T* operator->()
+ pointer_type operator->()
{
return _ptr;
}
- reverse_iterator& operator++()
+ self_type& operator++()
{
_ptr--;
return *this;
}
- reverse_iterator& operator++(int)
+ self_type& operator++(int)
{
- reverse_iterator tmp (*this);
+ self_type tmp (*this);
_ptr--;
return tmp;
}
- reverse_iterator operator--()
+ self_type operator--()
{
_ptr++;
return *this;
}
- reverse_iterator operator--(int)
+ self_type operator--(int)
{
- reverse_iterator tmp (*this);
+ self_type tmp (*this);
_ptr++;
return tmp;
}
- reverse_iterator& operator+=(difference_type n)
+ self_type& operator+=(difference_type n)
{
*this = *this - n;
return *this;
}
- reverse_iterator operator+(difference_type n)
+ self_type operator+(difference_type n)
{
- reverse_iterator tmp (*this);
+ self_type tmp (*this);
tmp += n;
return tmp;
}
- reverse_iterator operator-(difference_type n)
+ self_type operator-(difference_type n)
{
- reverse_iterator tmp (*this);
+ self_type tmp (*this);
tmp -= n;
return tmp;
}
- reverse_iterator& operator-=(difference_type n)
+ self_type& operator-=(difference_type n)
{
*this = *this + n;
return *this;
}
- bool operator==(const reverse_iterator& other) const
+ bool operator==(const self_type& other) const
{
return _ptr == other._ptr;
}
- bool operator!=(const reverse_iterator& other) const
+ bool operator!=(const self_type& other) const
{
return !(*this == other);
}
};
- typedef const reverse_iterator const_reverse_iterator;
+ class const_reverse_iterator {
+ private:
+ typedef const_reverse_iterator self_type;
+ typedef const_reference reference_type;
+ typedef const T* pointer_type;
+ pointer_type _ptr;
+ public:
+ const_reverse_iterator(pointer_type ptr) : _ptr(ptr)
+ {
+ }
+ const_reverse_iterator(const self_type& src) : _ptr(src._ptr)
+ {
+ }
+ reference_type operator*()
+ {
+ return *_ptr;
+ }
+ pointer_type operator->()
+ {
+ return _ptr;
+ }
+ self_type& operator++()
+ {
+ _ptr--;
+ return *this;
+ }
+ self_type& operator++(int)
+ {
+ self_type tmp (*this);
+ _ptr--;
+ return tmp;
+ }
+ self_type operator--()
+ {
+ _ptr++;
+ return *this;
+ }
+ self_type operator--(int)
+ {
+ self_type tmp (*this);
+ _ptr++;
+ return tmp;
+ }
+ self_type& operator+=(difference_type n)
+ {
+ *this = *this - n;
+ return *this;
+ }
+ self_type operator+(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp += n;
+ return tmp;
+ }
+ self_type operator-(difference_type n)
+ {
+ self_type tmp (*this);
+ tmp -= n;
+ return tmp;
+ }
+ self_type& operator-=(difference_type n)
+ {
+ *this = *this + n;
+ return *this;
+ }
+ bool operator==(const self_type& other) const
+ {
+ return _ptr == other._ptr;
+ }
+ bool operator!=(const self_type& other) const
+ {
+ return !(*this == other);
+ }
+ };
+
private:
size_type _size;
size_type _capacity;
@@ -276,44 +503,44 @@ public:
}
/* ITERATORS */
- iterator begin()
+ typename iterator::self_type begin()
{
- return _data;
+ return iterator(_data);
}
- const_iterator begin() const
+ typename const_iterator::self_type begin() const
{
- return _data;
+ return const_iterator(_data);
}
- iterator end()
+ typename iterator::self_type end()
{
- return _data + _size;
+ return iterator(_data + _size);
}
- const_iterator end() const
+ typename const_iterator::self_type end() const
{
- return _data + _size;
+ return const_iterator(_data + _size);
}
- reverse_iterator rbegin()
+ typename reverse_iterator::self_type rbegin()
{
- return _data + _size - 1;
+ return reverse_iterator(_data + _size - 1);
}
- const_reverse_iterator rbegin() const
+ typename const_reverse_iterator::self_type rbegin() const
{
- return _data + _size - 1;
+ return const_reverse_iterator(_data + _size - 1);
}
- reverse_iterator rend()
+ typename reverse_iterator::self_type rend()
{
- return _data - 1;
+ return reverse_iterator(_data - 1);
}
- const_reverse_iterator rend() const
+ typename const_reverse_iterator::self_type rend() const
{
- return _data - 1;
+ return const_reverse_iterator(_data - 1);
}
/* CAPACITY */
@@ -369,6 +596,7 @@ public:
template<class InputIt>
iterator insert(const_iterator pos, InputIt first,
InputIt last);
+ /* TODO */
/* erase */
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);