aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-12-17 11:23:15 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-12-17 11:23:15 +0100
commit377863dd1b8116f81e0d9bef6e2cd10557dd890e (patch)
tree9b2265c5d3bc6bf70b54da95e51d8079d8954f11
parent4115f22c50ee0571e88a8ec44c0241693af7ed38 (diff)
downloadpiscine_cpp-377863dd1b8116f81e0d9bef6e2cd10557dd890e.tar.gz
piscine_cpp-377863dd1b8116f81e0d9bef6e2cd10557dd890e.tar.bz2
piscine_cpp-377863dd1b8116f81e0d9bef6e2cd10557dd890e.zip
Updated cpp08/02 with reverse_iterator, fixed access
-rw-r--r--cpp08/ex02/main.cpp195
-rw-r--r--cpp08/ex02/mutantstack.hpp35
2 files changed, 199 insertions, 31 deletions
diff --git a/cpp08/ex02/main.cpp b/cpp08/ex02/main.cpp
index cb138b3..a9f0995 100644
--- a/cpp08/ex02/main.cpp
+++ b/cpp08/ex02/main.cpp
@@ -6,37 +6,196 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 07:30:43 by charles #+# #+# */
-/* Updated: 2020/12/15 14:24:54 by cacharle ### ########.fr */
+/* Updated: 2020/12/17 11:22:07 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
+#include <list>
#include "mutantstack.hpp"
int main()
{
{
std::cout << "=================== SUBJECT MAIN ======================" << std::endl;
- MutantStack<int> mstack;
- mstack.push(5);
- mstack.push(17);
- std::cout << mstack.top() << std::endl;
- mstack.pop();
- std::cout << mstack.size() << std::endl;
- mstack.push(3);
- mstack.push(5);
- mstack.push(737);
- mstack.push(0);
- MutantStack<int>::iterator it = mstack.begin();
- MutantStack<int>::iterator ite = mstack.end();
- ++it;
- --it;
- while (it != ite)
{
- std::cout << *it << std::endl;
+ MutantStack<int> mstack;
+ mstack.push(5);
+ mstack.push(17);
+ std::cout << mstack.top() << std::endl;
+ mstack.pop();
+ std::cout << mstack.size() << std::endl;
+ mstack.push(3);
+ mstack.push(5);
+ mstack.push(737);
+ mstack.push(0);
+ MutantStack<int>::iterator it = mstack.begin();
+ MutantStack<int>::iterator ite = mstack.end();
++it;
+ --it;
+ while (it != ite)
+ {
+ std::cout << *it << std::endl;
+ ++it;
+ }
+ std::stack<int> s(mstack);
+ }
+ std::cout << "-----------------------------------------------------" << std::endl;
+ {
+ std::list<int> mstack;
+ mstack.push_back(5);
+ mstack.push_back(17);
+ std::cout << mstack.back() << std::endl;
+ mstack.pop_back();
+ std::cout << mstack.size() << std::endl;
+ mstack.push_back(3);
+ mstack.push_back(5);
+ mstack.push_back(737);
+ mstack.push_back(0);
+ std::list<int>::iterator it = mstack.begin();
+ std::list<int>::iterator ite = mstack.end();
+ ++it;
+ --it;
+ while (it != ite)
+ {
+ std::cout << *it << std::endl;
+ ++it;
+ }
+ std::list<int> s(mstack);
}
- std::stack<int> s(mstack);
}
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "=================== METHODS ======================" << std::endl;
+
+ std::cout << "------------------- DEFAULT CONSTRUCTOR" << std::endl;
+ MutantStack<int> a;
+ std::cout << a.empty() << std::endl;
+ std::cout << a.size() << std::endl;
+ a.push(1);
+ a.push(2);
+ a.push(3);
+ std::cout << a.empty() << std::endl;
+ std::cout << a.size() << std::endl;
+ std::cout << a.top() << std::endl;
+
+ std::cout << "------------------- COPY CONSTRUCTOR" << std::endl;
+ MutantStack<int> b(a);
+ std::cout << b.empty() << std::endl;
+ std::cout << b.size() << std::endl;
+ std::cout << b.top() << std::endl;
+ b.push(4);
+ std::cout << b.top() << std::endl;
+ std::cout << a.top() << std::endl;
+
+ std::cout << "------------------- ASSIGN" << std::endl;
+ MutantStack<int> c;
+ c.push(-1);
+ c = a;
+ std::cout << c.empty() << std::endl;
+ std::cout << c.size() << std::endl;
+ std::cout << c.top() << std::endl;
+ c.push(4);
+ std::cout << c.top() << std::endl;
+ std::cout << a.top() << std::endl;
+
+ std::cout << "------------------- POP" << std::endl;
+ a.pop();
+ a.pop();
+ std::cout << a.empty() << std::endl;
+ std::cout << a.size() << std::endl;
+ std::cout << a.top() << std::endl;
+ std::cout << "------------------- COMPARISON" << std::endl;
+ std::cout << (a == b) << std::endl;
+ std::cout << (a != c) << std::endl;
+ std::cout << (a < c ) << std::endl;
+ std::cout << (a > c ) << std::endl;
+ std::cout << (a <= c) << std::endl;
+ std::cout << (a >= c) << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "=================== ITERATOR ======================" << std::endl;
+ MutantStack<float> a;
+ a.push(1.1f);
+ a.push(2.2f);
+ a.push(3.3f);
+
+ std::cout << "------------------- PRE INCREMENT" << std::endl;
+ for (MutantStack<float>::iterator it = a.begin(); it != a.end(); ++it)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- POST INCREMENT" << std::endl;
+ for (MutantStack<float>::iterator it = a.begin(); it != a.end(); it++)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- PRE DECREMENT" << std::endl;
+ for (MutantStack<float>::iterator it = --a.end(); it != a.begin(); --it)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- POST DECREMENT" << std::endl;
+ for (MutantStack<float>::iterator it = --a.end(); it != a.begin(); it--)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- ARROW OPERATOR" << std::endl;
+ MutantStack<float>::iterator it_arrow = a.begin();
+ std::cout << it_arrow.operator->() << std::endl;
+ std::cout << *(it_arrow.operator->()) << std::endl;
+
+ std::cout << "------------------- PRE INCREMENT / DEREF" << std::endl;
+ for (MutantStack<float>::iterator it = a.begin(); it != --a.end();)
+ std::cout << (*++it) << std::endl;
+
+ std::cout << "------------------- POST INCREMENT / DEREF" << std::endl;
+ for (MutantStack<float>::iterator it = a.begin(); it != --a.end();)
+ std::cout << *it++ << std::endl;
+
+ std::cout << "------------------- COMPARISON" << std::endl;
+ std::cout << (a.begin() == a.end()) << std::endl;
+ std::cout << (a.begin() != a.end()) << std::endl;
+
+ std::cout << "------------------- COPY CONSTRUCTOR" << std::endl;
+ MutantStack<float>::iterator it_copied = ++a.begin();
+ MutantStack<float>::iterator it_copy(it_copied);
+ std::cout << *it_copied << std::endl;
+ std::cout << *it_copy << std::endl;
+
+ std::cout << "------------------- ASSIGNMENT" << std::endl;
+ MutantStack<float>::iterator it_assign(a.begin());
+ it_assign = it_copied;
+ std::cout << *it_copied << std::endl;
+ std::cout << *it_assign << std::endl;
+
+ std::cout << "------------------- ITERATOR ASSIGN" << std::endl;
+ for (MutantStack<float>::iterator it = a.begin(); it != a.end(); ++it)
+ *it = 4.4f;
+ for (MutantStack<float>::iterator it = a.begin(); it != a.end(); ++it)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- MORE" << std::endl;
+ MutantStack<float> d;
+ d.push(0.5f);
+ d.push(1.5f);
+ d.push(2.5f);
+ d.push(3.5f);
+ d.push(4.5f);
+ d.push(5.5f);
+ d.push(6.5f);
+ d.push(7.5f);
+ d.push(8.5f);
+ d.push(9.5f);
+ d.push(1.9f);
+ for (MutantStack<float>::iterator it = d.begin(); it != d.end(); ++it)
+ std::cout << *it << std::endl;
+
+ std::cout << "------------------- REVERSE" << std::endl;
+ for (MutantStack<float>::reverse_iterator it = d.rbegin(); it != d.rend(); ++it)
+ std::cout << *it << std::endl;
+ }
+
return 0;
}
diff --git a/cpp08/ex02/mutantstack.hpp b/cpp08/ex02/mutantstack.hpp
index 56a5bbf..f9faab1 100644
--- a/cpp08/ex02/mutantstack.hpp
+++ b/cpp08/ex02/mutantstack.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 07:29:42 by charles #+# #+# */
-/* Updated: 2020/12/15 14:21:26 by cacharle ### ########.fr */
+/* Updated: 2020/12/17 11:20:10 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,24 +35,28 @@ public:
{
public:
iterator(iterator const& other)
- : m_parentStack(other.m_parentStack), m_pos(other.m_pos) {}
+ : m_parentStack(other.m_parentStack),
+ m_pos(other.m_pos),
+ m_dir(other.m_dir) {}
- void operator=(iterator const& other)
+ iterator& operator=(iterator const& other)
{
m_parentStack = other.m_parentStack;
m_pos = other.m_pos;
+ m_dir = other.m_dir;
+ return *this;
}
~iterator() {}
- iterator(MutantStack<T>& parentStack, unsigned int n)
- : m_parentStack(parentStack), m_pos(n) {}
+ iterator(MutantStack<T>& parentStack, size_t n, size_t dir)
+ : m_parentStack(parentStack), m_pos(n), m_dir(dir) {}
T& operator*()
{
std::stack<T> tmp;
- for (unsigned int i = m_pos; i != 0; i--)
+ for (size_t i = 0; m_parentStack.size() != m_pos + 1; i++)
{
tmp.push(m_parentStack.top());
m_parentStack.pop();
@@ -68,10 +72,10 @@ public:
T* operator->() { return &(*(*this)); }
- iterator& operator++() { m_pos++; return *this; }
- iterator& operator++(int) { m_pos++; return *this; }
- iterator& operator--() { m_pos--; return *this; }
- iterator& operator--(int) { m_pos--; return *this; }
+ iterator& operator++() { m_pos += m_dir; return *this; }
+ iterator operator++(int) { iterator ret(*this); m_pos += m_dir; return ret; }
+ iterator& operator--() { m_pos -= m_dir; return *this; }
+ iterator operator--(int) { iterator ret(*this); m_pos -= m_dir; return ret; }
bool operator==(iterator const& right) { return m_pos == right.m_pos; }
bool operator!=(iterator const& right) { return !(*this == right); }
@@ -80,11 +84,16 @@ public:
iterator() : m_pos(0) {}
MutantStack<T>& m_parentStack;
- int m_pos;
+ size_t m_pos;
+ size_t m_dir;
};
- iterator begin() { return iterator(*this, 0); }
- iterator end() { return iterator(*this, this->size()); }
+ typedef iterator reverse_iterator;
+
+ iterator begin() { return iterator(*this, 0, 1); }
+ iterator end() { return iterator(*this, this->size(), 1); }
+ iterator rbegin() { return reverse_iterator(*this, this->size() - 1, -1); }
+ iterator rend() { return reverse_iterator(*this, -1, -1); }
};
#endif