aboutsummaryrefslogtreecommitdiff
path: root/cpp08/ex02/mutantstack.hpp
blob: 2cbeb9e0e9c29e0bc28abfcd80da413b0e328cb0 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   mutantstack.hpp                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/15 07:29:42 by charles           #+#    #+#             */
/*   Updated: 2020/04/15 09:25:55 by charles          ###   ########.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)
    {}

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

    ~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->();

        // pre increment;
        iterator& operator++()
        {
            m_pos++;
            return *this;
        }

        // post increment;
        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