OS  v1.8.0
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
49
53 enum listPosition : int32_t {
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 ) const noexcept
76 {
77 /*cstat -CONST-member-ret -MISRAC++2008-9-3-1*/
78 return container;
79 /*cstat +CONST-member-ret +MISRAC++2008-9-3-1*/
80 }
81 node() noexcept : next(nullptr), prev(nullptr), container(nullptr) {}
83 virtual ~node() {}
85
86 friend class list;
87 friend class listIterator;
88 };
89
106 using listCompareFcn_t = bool (*)( const void *, const void * );
107
115
116 class listIterator;
117
121 class list : protected node {
122 private:
123 node *head{ nullptr };
124 node *tail{ nullptr };
125 size_t size{ 0U };
126 bool isMember( const void * const xNode ) const noexcept;
127 void insertAtFront( node * const xNode ) noexcept;
128 void insertAtBack( node * const xNode ) noexcept;
129 node* removeFront( void ) noexcept;
130 node* removeBack( void ) noexcept;
131 node* getNodeAtIndex( const listPosition p ) const noexcept;
132 void givenNodeSwapBoundaries( node *n1, node *n2 ) noexcept;
133 static void givenNodeSwapAdjacent( node *n1, node *n2 ) noexcept;
134 static void givenNodesUpdateOuterLinks( node *n1, node *n2 ) noexcept;
135 list( list const& ) = delete;
136 void operator=( list const& ) = delete;
137 public:
138 list() noexcept;
140 virtual ~list() {}
151 bool insert( void * const xNode, const listPosition p = listPosition::AT_BACK ) noexcept;
157 bool remove( void * const xNode ) noexcept;
168 void* remove( const listPosition p, void * const xNode = nullptr ) noexcept;
173 void* getFront( void ) const noexcept;
178 void* getBack( void ) const noexcept;
183 bool isEmpty( void ) const noexcept;
188 size_t length( void ) const noexcept;
208 bool sort( listCompareFcn_t f ) noexcept;
219 bool swap( void* node1, void* node2 ) noexcept;
231 bool move( list& src, const listPosition p = listPosition::AT_BACK ) noexcept;
235 void clean( void ) noexcept;
241 listIterator begin( void ) noexcept;
247 listIterator end( void ) noexcept;
253 listIterator from( void *offset ) noexcept;
258 explicit operator bool() const noexcept {
259 return ( nullptr != head );
260 }
261 friend class listIterator;
262 };
263
268 private:
269 list *l{ nullptr };
270 node *iter{ nullptr };
271 void *current{ nullptr };
272 public:
273 listIterator() = delete;
275 virtual ~listIterator() {}
284 explicit listIterator( list& xList, listDirection dir = listDirection::FORWARD, void *nodeOffset = nullptr ) noexcept;
289 bool untilEnd( void ) const noexcept;
295 bool untilEnd( void* node ) const noexcept;
299 listIterator& operator++( int ) noexcept;
303 listIterator& operator--( int ) noexcept;
308 template <typename T>
309 inline T get( void ) noexcept
310 {
311 /*cstat -CERT-EXP36-C_b*/
312 return static_cast<T>( current );
313 /*cstat +CERT-EXP36-C_b*/
314 }
315 };
316
317}
318
319#endif /*QOS_CPP_LIST*/
A list object (Generic double-linked)
Definition list.hpp:121
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...
friend class listIterator
Definition list.hpp:261
listIterator()=delete
bool untilEnd(void) const noexcept
Check until current iterator reach one of its ends.
T get(void) noexcept
Gets the node that the iterator is currently pointing to.
Definition list.hpp:309
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
node() noexcept
Definition list.hpp:81
list * getContainer(void) const noexcept
Get a pointer to the list in which this node is contained.
Definition list.hpp:75
friend class list
Definition list.hpp:86
friend class listIterator
Definition list.hpp:87
listDirection
An enum with the possible options to transverse a list.
Definition list.hpp:111
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:106
@ BACKWARD
Definition list.hpp:113
@ FORWARD
Definition list.hpp:112
@ AT_FRONT
Definition list.hpp:54
@ AT_BACK
Definition list.hpp:55
OS/Kernel interfaces.
Definition bytebuffer.hpp:7