aboutsummaryrefslogtreecommitdiff
path: root/List.hpp
blob: ebe0dc23848ba4af2c5e8437fa435a77cb63534b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   List.hpp                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/01 09:31:07 by cacharle          #+#    #+#             */
/*   Updated: 2020/04/24 19:16:46 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef LIST_HPP
# define LIST_HPP

# include <cstddef>
# include <memory>

namespace ft
{
	template < typename T, typename Alloc = std::allocator<T> >
	class List
	{
    public:
		typedef T                                        value_type;
		typedef Alloc                                    allocator_type;
		typedef typename allocator_type::reference       reference;
		typedef typename allocator_type::const_reference const_reference;
		typedef typename allocator_type::pointer         pointer;
		typedef typename allocator_type::const_pointer   const_pointer;
		typedef ptrdiff_t                                difference_type;
		typedef size_t                                   size_type;

        explicit List(const allocator_type& alloc = allocator_type())
            : m_front(NULL), m_back(NULL), m_size(0) {}

        List(const List& other) { *this = other; }

        List& operator=(const List& other)
        {
            if (*this == other)
                return *this;
            delete m_front;
            m_front = NULL;
            m_back = NULL;
            // use iterator
            return *this;
        }

        ~List() {
            clear();
        }

        explicit List(size_type n,
                      const value_type& val = value_type(),
                      const allocator_type& alloc = allocator_type()) {}

        template<typename InputIterator>
        List(InputIterator first,
             InputIterator last,
             const allocator_type& alloc = allocator_type()) {}

    private:
        class ListNode
        {
        public:
            ListNode() : m_next(NULL), m_prev(NULL), m_data(value_type()) {}

            ListNode(const ListNode& other)
            {
                if (*this != other)
                    *this = other;
            }

            ListNode& operator=(const ListNode& other)
            {
                m_next = other.m_next;
                m_prev = other.m_prev;
                m_data = other.m_data;
                return *this;
            }

            ~ListNode()
            {
                if (m_next != NULL) {
                    m_next->m_prev = NULL;
                    delete m_next;
                }
                if (m_prev != NULL) {
                    m_prev->m_next = NULL;
                    delete m_prev;
                }
            }

            ListNode(value_type& data) : m_next(NULL), m_prev(NULL), m_data(data) {}

            ListNode*   getNext() const { return m_next; }
            ListNode*   getPrev() const { return m_prev; }
            value_type& getData() const { return m_data; }

        private:
            ListNode*  m_next;
            ListNode*  m_prev;
            value_type m_data;
        };

    public:
        class iterator
        {
        public:
            iterator() : m_current(NULL) {}

            iterator(const iterator& other) {
                *this = other;
            }

            iterator& operator=(const iterator& other) {
                if (*this == other)
                    return *this;
                m_current = other.m_current;
                return *this;
            }

            ~iterator() {}

            value_type& operator*()  { return m_current->getData(); }
            value_type& operator->() { return m_current->getData(); }

            bool operator==(const iterator& other) { return m_current == other.m_current; }
            bool operator!=(const iterator& other) { return !(operator==(other)); }

            iterator& operator++(int)
            {
                m_current = m_current->getNext();
                return *this;
            }

            iterator& operator--(int)
            {
                m_current = m_current->getPrev();
                return *this;
            }

            iterator& operator++() { return operator++; }
            iterator& operator--() { return operator--; }

        private:
            iterator(ListNode* current) : m_current(current) {}

            List::ListNode* m_current;
        };

        iterator begin()           { return iterator(m_front); }
        iterator end()             { return iterator(NULL);    }

        bool      empty()    const { return m_front == NULL; }
        size_type size()     const { return m_size;          }
        size_type max_size() const { return m_size;          }

        reference front()          { return m_front->getData(); }
        reference back()           { return m_back->getData();  }

        void push_front(const value_type& val)
        {
            ListNode* new_front = new ListNode(val);
            new_front->setNext(m_front);
            m_front->setPrev(new_front);
            m_front = new_front;
        }

        void push_back(const value_type& val)
        {
            ListNode* new_back = new ListNode(val);
            new_back->setPrev(m_back);
            m_back->setNext(new_back);
            m_back = new_back;
        }

        void pop_front()
        {
            ListNode* tmp = m_front->getNext();
            tmp->setPrev(NULL);
            delete m_front;
            m_front = tmp;
        }

        void pop_back()
        {
            ListNode* tmp = m_back->getPrev();
            tmp->setNext(NULL);
            delete m_back;
            m_back = tmp;
        }

        void clear()
        {
            delete m_front;
            m_front = NULL;
            m_back = NULL;
            m_size = 0;
        }


    private:
        ListNode* m_front;
        ListNode* m_back;
        size_type m_size;
    };
}

#endif