StuBS
|
Templated Queue for arbitrary objects. More...
#include <queue.h>
Public Member Functions | |
Queue (unsigned link_index, NextFunc get_link=default_get_link) | |
Constructor. More... | |
bool | enqueue (T &item) |
Enqueues the provided item at the end of the queue. If the element is already contained in the queue, false will be returned. More... | |
bool | insertFirst (T &item) |
insert a new element at the start of the queue More... | |
bool | insertAfter (T &after, T &item) |
Insert a new element item into the list after an element after. Returns false if item is already in the/a list or after is not in this list. More... | |
T * | next (T &item) |
return the next element of a given one or nullptr if the end is reached More... | |
bool | is_empty () |
Return whether or not the queue is empty. More... | |
T * | dequeue () |
Removes the first element in the queue and returns it. More... | |
T * | remove (T *that) |
Removes a given element from the queue and returns that element, or nullptr if it was not present. More... | |
T * | first () |
get the first element of the queue | |
T * | last () |
get the last element of the queue | |
Private Types | |
typedef T **(* | NextFunc) (T &, unsigned) |
Type definition for the get_link function. | |
Static Private Member Functions | |
static T ** | default_get_link (T &obj, unsigned link_index) |
Default get_link implementation returns a pointer to the link_index'th element of the next-pointer array. The function assumes a member named "queue_link" that stores the next-pointer. More... | |
Private Attributes | |
unsigned | link_index |
Queue-local index into the next-pointer array. | |
const NextFunc | get_link |
T * | head |
T * | tail |
Templated Queue for arbitrary objects.
Queue is implemented by a head-object (Queue<T>) and next-pointers embedded in the queued objects. This Queue supports arrays of next-pointers by passing an index into the constructor identifying the index into the next-pointer array. By passing a different get_link function into the constructor, the member name of the next-pointer array can be changed and objects can be contained in different independent queues.
|
inlineexplicit |
Constructor.
[in] | link_index | denotes the index into the next-pointer array to be used by this queue-object |
[in] | get_link | A function pointer to the get_link, i.e. a function which returns a pointer to the next-pointer of an element in the Queue. |
|
inlinestaticprivate |
Default get_link implementation returns a pointer to the link_index'th element of the next-pointer array. The function assumes a member named "queue_link" that stores the next-pointer.
If your object contains a queue_link member you can just ignore this function and the get_link keyword argument of the constructor.
[in] | obj | the object which link should be accessed. |
[in] | link_index | the index within the array. |
|
inline |
Removes the first element in the queue and returns it.
nullptr
if the queue was empty.
|
inline |
Enqueues the provided item at the end of the queue. If the element is already contained in the queue, false will be returned.
[in] | item | element to be appended (enqueued). |
|
inline |
Insert a new element item into the list after an element after. Returns false if item is already in the/a list or after is not in this list.
[in] | after | the element after which the new one should be inserted |
[in] | item | the new element to add |
|
inline |
insert a new element at the start of the queue
[in] | item | the new item to add |
|
inline |
Return whether or not the queue is empty.
|
inline |
return the next element of a given one or nullptr if the end is reached
[in] | item | the current item |
|
inline |
Removes a given element from the queue and returns that element, or nullptr if it was not present.
Function pointer to the get_link function, called whenever the next pointer array is referenced
|
private |
head points to the first element (the one returned on first dequeue). Can be nullptr if the queue is empty.
|
private |
tail points to the last element (the one last added). Is only valid if head != nullptr