OS  v1.7.5
Documentation
Loading...
Searching...
No Matches
list.hpp
1#ifndef QOS_CPP_LIST
2#define QOS_CPP_LIST
3
4#include "include/types.hpp"
5
6namespace qOS {
7
53 enum listPosition : int32_t {
55 AT_BACK = -2
56 };
57
58 class list;
62 class node {
63 private:
64 node *next{ nullptr };
65 node *prev{ nullptr };
66 list *container{ nullptr };
67 void init( void ) noexcept;
68 node( node const& ) = delete;
69 void operator=( node const& ) = delete;
70 public:
75 inline list* getContainer( void ) noexcept
76 {
77 return container;
78 }
79 node() : next(nullptr), prev(nullptr), container(nullptr) {}
81 virtual ~node() {}
84 friend class list;
85 friend class listIterator;
86 };
87
104 using listCompareFcn_t = bool (*)( const void *, const void * );
105
109 enum class listDirection {
110 FORWARD,
112 };
113
114 class listIterator;
115
119 class list : protected node {
120 private:
121 node *head{ nullptr };
122 node *tail{ nullptr };
123 size_t size{ 0U };
124 bool isMember( const void * const xNode ) const noexcept;
125 void insertAtFront( node * const xNode ) noexcept;
126 void insertAtBack( node * const xNode ) noexcept;
127 node* removeFront( void ) noexcept;
128 node* removeBack( void ) noexcept;
129 node* getNodeAtIndex( const listPosition p ) const noexcept;
130 void givenNodeSwapBoundaries( node *n1, node *n2 ) noexcept;
131 static void givenNodeSwapAdjacent( node *n1, node *n2 ) noexcept;
132 static void givenNodesUpdateOuterLinks( node *n1, node *n2 ) noexcept;
133 list( list const& ) = delete;
134 void operator=( list const& ) = delete;
135 public:
136 list() noexcept;
138 virtual ~list() {}
149 bool insert( void * const xNode, const listPosition p = listPosition::AT_BACK ) noexcept;
155 bool remove( void * const xNode ) noexcept;
166 void* remove( const listPosition p, void * const xNode = nullptr ) noexcept;
171 void* getFront( void ) const noexcept;
176 void* getBack( void ) const noexcept;
181 bool isEmpty( void ) const noexcept;
186 size_t length( void ) const noexcept;
206 bool sort( listCompareFcn_t f ) noexcept;
217 bool swap( void* node1, void* node2 ) noexcept;
229 bool move( list& src, const listPosition p = listPosition::AT_BACK ) noexcept;
233 void clean( void ) noexcept;
239 listIterator begin( void ) noexcept;
245 listIterator end( void ) noexcept;
251 listIterator from( void *offset ) noexcept;
252 friend class listIterator;
253 };
254
259 private:
260 list *l{ nullptr };
261 node *iter{ nullptr };
262 void *current{ nullptr };
263 public:
264 listIterator() = delete;
266 virtual ~listIterator() {}
275 explicit listIterator( list& xList, listDirection dir = listDirection::FORWARD, void *nodeOffset = nullptr ) noexcept;
280 bool untilEnd( void ) const noexcept;
286 bool untilEnd( void* node ) const noexcept;
290 listIterator& operator++( int ) noexcept;
294 listIterator& operator--( int ) noexcept;
299 template <typename T>
300 inline T get( void ) noexcept
301 {
302 /*cstat -CERT-EXP36-C_b*/
303 return static_cast<T>( current );
304 /*cstat +CERT-EXP36-C_b*/
305 }
306 };
308}
309
310#endif /*QOS_CPP_LIST*/
A list object (Generic double-linked)
Definition list.hpp:119
bool insert(void *const xNode, const listPosition p=listPosition::AT_BACK) noexcept
Insert an item into the list.
listIterator begin(void) noexcept
Returns an iterator pointing to the first element in the list container.
bool isEmpty(void) const noexcept
Check if the list is empty.
list() noexcept
void * remove(const listPosition p, void *const xNode=nullptr) noexcept
Remove an item from the list.
size_t length(void) const noexcept
Get the number of items inside the list.
listIterator from(void *offset) noexcept
Returns an iterator pointing to the element given by offset in the list container.
bool swap(void *node1, void *node2) noexcept
Swap two nodes that belongs to the list by changing its own links.
bool remove(void *const xNode) noexcept
If the node is member of a list, the node will be removed from it.
void * getFront(void) const noexcept
Get a pointer to the front item of the list.
bool sort(listCompareFcn_t f) noexcept
Sort the double linked list using the f function to determine the order. The sorting algorithm used b...
listIterator end(void) noexcept
Returns an iterator pointing to the last element in the list container.
void * getBack(void) const noexcept
Get a pointer to the back item of the list.
void clean(void) noexcept
Clean up the entire list leaving it empty.
bool move(list &src, const listPosition p=listPosition::AT_BACK) noexcept
Moves(or merge) the entire list src to the given list. After the move operation, this function leaves...
A list iterator.
Definition list.hpp:258
listIterator()=delete
listIterator(list &xList, listDirection dir=listDirection::FORWARD, void *nodeOffset=nullptr) noexcept
Instantiate a list iterator for the given list.
A list-node object (Used internally)
Definition list.hpp:62
list * getContainer(void) noexcept
Get a pointer to the list in which this node is contained.
Definition list.hpp:75
node()
Definition list.hpp:79
listDirection
An enum with the possible options to transverse a list.
Definition list.hpp:109
listPosition
An enum with the possible options to specify a target position for a list.
Definition list.hpp:53
bool(*)(const void *, const void *) listCompareFcn_t
Pointer to a function used by the list::sort() method to compare nodes of a list.
Definition list.hpp:104
@ AT_FRONT
Definition list.hpp:54
@ AT_BACK
Definition list.hpp:55
OS/Kernel interfaces.
Definition bytebuffer.hpp:7