Horizon Official Technical Documentation
Head.hpp
Go to the documentation of this file.
1
2#ifndef HORIZON_CORE_STRUCTURES_LINKEDLIST_HEAD_HPP
3#define HORIZON_CORE_STRUCTURES_LINKEDLIST_HEAD_HPP
4
5#include <cstdlib>
6#include <iterator>
7
8#include "Element.hpp"
9#include <cstdint>
10
11namespace Horizon
12{
13namespace Structures
14{
15namespace LinkedList
16{
25class Head
26{
27private:
30 uint32_t _size;
31
32public:
33 Head(): _size(0)
34 {
35 // create empty list
36
39 }
40
43 bool is_empty() const { return(_first._next == &_last); }
44
46 Element *first() { return (is_empty() ? nullptr : _first._next); }
47 Element const *first() const { return (is_empty() ? nullptr : _first._next); }
48
50 Element *last() { return(is_empty() ? nullptr : _last._prev); }
51 Element const *last() const { return(is_empty() ? nullptr : _last._prev); }
52
56 void push_front(Element *pElem)
57 {
58 _first.push_after(pElem);
59 }
60
63 void push_back(Element *pElem)
64 {
65 _last.push_before(pElem);
66 }
67
69 uint32_t get_size() const
70 {
71 if (!_size)
72 {
73 uint32_t result = 0;
74 Element const* e = first();
75
76 while (e != nullptr)
77 {
78 e = e->next();
79 if (e)
80 ++result;
81 }
82 return result;
83 }
84 else
85 return _size;
86 }
87
88 void inc_size() { ++_size; }
89 void dec_size() { --_size; }
90
91 // iterator support. These are used to iterate through the list. The iterator is a pointer to an Element.
92 // The iterator is not a pointer to a Head. The iterator is not a pointer to a list of Elements.
93 // The iterator is not a pointer to a list of Heads or Elements. The iterator is a pointer to an Element.
94 template<class _Ty>
96 {
97 public:
98 typedef std::bidirectional_iterator_tag iterator_category;
99 typedef _Ty value_type;
100 typedef _Ty* pointer;
101 typedef _Ty const* const_pointer;
102 typedef _Ty& reference;
103 typedef _Ty const & const_reference;
104
105 Iterator() : _Ptr(nullptr)
106 { // construct with null node pointer
107 }
108
109 Iterator(pointer _Pnode) : _Ptr(_Pnode)
110 { // construct with node pointer _Pnode
111 }
112
114 {
115 _Ptr = _Right._Ptr;
116 return *this;
117 }
118
120 {
121 _Ptr = pointer(_Right);
122 return *this;
123 }
124
126 { // return designated value
127 return *_Ptr;
128 }
129
131 { // return pointer to class object
132 return _Ptr;
133 }
134
136 { // preincrement
137 _Ptr = static_cast<pointer>(_Ptr->next());
138 return (*this);
139 }
140
142 { // postincrement
143 Iterator<_Ty> _Tmp = *this;
144 ++*this;
145 return (_Tmp);
146 }
147
149 { // predecrement
150 _Ptr = static_cast<pointer>(_Ptr->prev());
151 return (*this);
152 }
153
155 { // postdecrement
156 Iterator<_Ty> _Tmp = *this;
157 --*this;
158 return (_Tmp);
159 }
160
161 bool operator==(Iterator const &_Right) const
162 { // test for iterator equality
163 return (_Ptr == _Right._Ptr);
164 }
165
166 bool operator!=(Iterator const &_Right) const
167 { // test for iterator inequality
168 return (!(*this == _Right));
169 }
170
171 bool operator==(pointer const &_Right) const
172 { // test for pointer equality
173 return (_Ptr != _Right);
174 }
175
176 bool operator!=(pointer const &_Right) const
177 { // test for pointer equality
178 return (!(*this == _Right));
179 }
180
181 bool operator==(const_reference _Right) const
182 { // test for reference equality
183 return (_Ptr == &_Right);
184 }
185
186 bool operator!=(const_reference _Right) const
187 { // test for reference equality
188 return (_Ptr != &_Right);
189 }
190
192 { // return node pointer
193 return (_Ptr);
194 }
195
196 protected:
197 pointer _Ptr; // pointer to node
198 };
199
201
202private:
203 Head(Head const&) = delete;
204 Head& operator=(Head const&) = delete;
205
206protected:
207 ~Head() { }
208};
209}
210}
211}
212
213#endif
This class is used to manage a linked list of Elements. It is not intended to be used directly....
Definition: Element.hpp:18
void push_after(Element *pElem)
Pushes this Element after the given Element.
Definition: Element.hpp:75
Element * _next
Definition: Element.hpp:22
Element * next()
Returns the next Element in the list.
Definition: Element.hpp:36
void push_before(Element *pElem)
Pushes this Element before the given Element.
Definition: Element.hpp:64
Element * _prev
Definition: Element.hpp:23
bool operator==(pointer const &_Right) const
Definition: Head.hpp:171
Iterator operator++(int)
Definition: Head.hpp:141
bool operator==(Iterator const &_Right) const
Definition: Head.hpp:161
pointer _Ptr
Definition: Head.hpp:197
Iterator(pointer _Pnode)
Definition: Head.hpp:109
Iterator & operator=(const_pointer const &_Right)
Definition: Head.hpp:119
Iterator & operator--()
Definition: Head.hpp:148
_Ty & reference
Definition: Head.hpp:102
bool operator!=(const_reference _Right) const
Definition: Head.hpp:186
Iterator & operator++()
Definition: Head.hpp:135
Iterator & operator=(Iterator const &_Right)
Definition: Head.hpp:113
reference operator*()
Definition: Head.hpp:125
pointer _Mynode()
Definition: Head.hpp:191
std::bidirectional_iterator_tag iterator_category
Definition: Head.hpp:98
Iterator operator--(int)
Definition: Head.hpp:154
_Ty const & const_reference
Definition: Head.hpp:103
bool operator!=(Iterator const &_Right) const
Definition: Head.hpp:166
bool operator==(const_reference _Right) const
Definition: Head.hpp:181
bool operator!=(pointer const &_Right) const
Definition: Head.hpp:176
_Ty const * const_pointer
Definition: Head.hpp:101
pointer operator->()
Definition: Head.hpp:130
LinkedList Head class. This class is used to manage a linked list of Elements. It is used as a base c...
Definition: Head.hpp:26
Element * last()
Returns the last Element in the list.
Definition: Head.hpp:50
uint32_t get_size() const
returns the number of Elements in the list (not including the head and tail Elements or the first and...
Definition: Head.hpp:69
Iterator< Element > iterator
Definition: Head.hpp:200
Head()
Definition: Head.hpp:33
Element const * last() const
Definition: Head.hpp:51
uint32_t _size
Definition: Head.hpp:30
Element _first
Definition: Head.hpp:28
Element _last
Definition: Head.hpp:29
bool is_empty() const
Returns true if the list is empty.
Definition: Head.hpp:43
void push_front(Element *pElem)
push_front() and push_back() are used to add an Element to the list.
Definition: Head.hpp:56
Head & operator=(Head const &)=delete
void dec_size()
Definition: Head.hpp:89
~Head()
Definition: Head.hpp:207
void push_back(Element *pElem)
Definition: Head.hpp:63
void inc_size()
Definition: Head.hpp:88
Element const * first() const
Definition: Head.hpp:47
Element * first()
Returns the first Element in the list.
Definition: Head.hpp:46
Definition: Element.hpp:7