aboutsummaryrefslogtreecommitdiff
path: root/cpp08/ex02/mutantstack.hpp
blob: 4d7176bdc56bd56e858a6730b1eee79cf62fcc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   mutantstack.hpp                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/15 07:29:42 by charles           #+#    #+#             */
/*   Updated: 2020/12/17 14:06:59 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef MUTANTSTACK_HPP
# define MUTANTSTACK_HPP

# include <stack>
# include <cstddef>

template <typename T>
class MutantStack : public std::stack<T>
{
public:
    MutantStack() : std::stack<T>() {}

    MutantStack(MutantStack const& other) : std::stack<T>(other) {}

    MutantStack& operator=(MutantStack const& other)
    {
        std::stack<T>::operator=(other);
        return *this;
    }

    virtual ~MutantStack() {}

    class iterator
    {
    public:
        iterator(iterator const& other)
            : m_parentStack(other.m_parentStack),
              m_pos(other.m_pos),
              m_dir(other.m_dir) {}

        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, size_t n, size_t dir)
            : m_parentStack(parentStack), m_pos(n), m_dir(dir) {}

        T& operator*()
        {
            std::stack<T> tmp;

            for (size_t i = 0; m_parentStack.size() != m_pos + 1; i++)
            {
                tmp.push(m_parentStack.top());
                m_parentStack.pop();
            }
            T& res = m_parentStack.top();
            while (tmp.size() != 0)
            {
                m_parentStack.push(tmp.top());
                tmp.pop();
            }
            return res;
        }

        T* operator->() { 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);    }

    private:
        iterator() : m_pos(0) {}

        MutantStack<T>& m_parentStack;
        size_t          m_pos;
        size_t          m_dir;
    };

    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