aboutsummaryrefslogtreecommitdiff
path: root/cpp08/ex02/mutantstack.hpp
blob: 56a5bbfebfa8f0c986b90b366c87ff411173dc70 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   mutantstack.hpp                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   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       */
/*                                                                            */
/* ************************************************************************** */

#ifndef MUTANTSTACK_HPP
# define MUTANTSTACK_HPP

# include <stack>

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;
    }

    ~MutantStack() {}

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

        void operator=(iterator const& other)
        {
            m_parentStack = other.m_parentStack;
            m_pos         = other.m_pos;
        }

        ~iterator() {}

        iterator(MutantStack<T>& parentStack, unsigned int n)
            : m_parentStack(parentStack), m_pos(n) {}

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

            for (unsigned int i = m_pos; i != 0; 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++; return *this; }
        iterator& operator++(int) { m_pos++; return *this; }
        iterator& operator--()    { m_pos--; return *this; }
        iterator& operator--(int) { m_pos--; return *this; }

        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;
        int             m_pos;
    };

    iterator begin() { return iterator(*this, 0);            }
    iterator end()   { return iterator(*this, this->size()); }
};

#endif