sincerely Singaporean

If you have not done so, read this full tutorial on how to use SGEXTN to build an application.

SGLDeque

see header file

(no source file, everything inside header)

template ‹typename T› class SGLDeque;

part of SGEXTN module SG_Containers

double ended queue for any type of data

detailed description

list of all including inherited members

implementation details

preprocessor file inclusion directive: #include ‹SGLDeque.h›

CMake target for BuildLah: SGEXTN::SG_Containers

see this link for more information about BuildLah

parent class: (none)

children classes: (none)

instance member functions

SGLDeque();

SGLDeque(int count);

SGLDeque(int count, const T& defaultValue);

void assign(int count, const T& defaultValue);

[[nodiscard]] const T& at(int i) const;

[[nodiscard]] T& at(int i);

[[nodiscard]] const T& back() const;

void fill(const T& defaultValue);

[[nodiscard]] const T& front() const;

[[nodiscard]] int length() const;

[[nodiscard]] const T* pointerToData(int n) const;

[[nodiscard]] T* pointerToData(int n);

void popBack();

void popFront();

void pushBack(const T& x);

void pushFront(const T& x);

void reserve(int newMemoryLength);

Detailed Description

SGLDeque provides a template based double queue, an array-like data structure that you can freely insert elements at and remove elements from both ends, in addition to randomly reading from and writing to elements by their index. If only queue or stack behaviour is needed, pls use SGLQueue or SGLStack instead for better readability and performance. This is a template based class with no separate source file. This class is a SGEXTN container. Copy constructor, copy assignment, move constructor, move assignment, and destructor work as expected. A deep copy is performed whenever this class is copied, and the new instance will not be linked to the old instance in any way. It is assumed that the contents placed into this SGEXTN container can be copied and moved. This means that their copy constructor copy assignment, move constructor, move assignment, and destructor work as expected. If this is not the case or if you want the container to store references or constant references, store pointers instead.

Implementation Details

SGLDeque internally stores a buffer in the form of a C array. Elements are placed into the buffer starting at approximately a third of the buffer's length and the start point and end point of the data is kept track of. Insertion at both ends expand the section of the buffer being used. If there is no more space for the buffer to expand, the buffer triples in size and all elements are shifted to align with the 1/3 mark.

SGLDeque();

Creates a SGLDeque containing no elements.

SGLDeque(int count);

Creates a SGLDeque containing count elements which are default initialised.

The type stored in the SGLDeque must have a default constructor (the one taking no arguments) for this to compile.

You must ensure that count is nonnegative. Negative count will crash.

SGLDeque(int count, const T& defaultValue);

Creates a SGLDeque containing count copies of defaultValue.

You must ensure that count is nonnegative. Negative count will crash.

void assign(int count, const T& defaultValue);

Resets the SGLDeque to have count copies of defaultValue.

dq.assign(count, defaultValue); is functionally identical to dq = SGLDeque‹T›(count, defaultValue);

You must ensure that count is nonnegative. Negative count will crash.

[[nodiscard]] const T& at(int i) const;

Returns a constant reference to element i of the SGLDeque.

This will crash for out of bounds i.

[[nodiscard]] T& at(int i);

Returns a reference to element i of the SGLDeque.

Since this returns a reference and not a copy, assigning to it directly (using it as a lvalue) would modify the SGLDeque.

This will crash for out of bounds i.

[[nodiscard]] const T& back() const;

Returns a constant reference to the last element of SGLDeque.

dq.back() has the same effect as dq.at(dq.length() - 1)

Use SGLDeque::atSGLDeque::at if the element needs to be modified.

If the SGLDeque has a size of 0, this will crash.

void fill(const T& defaultValue);

Sets every element in the SGLDeque to defaultValue. This does not resize the SGLDeque.

If the SGLDeque needs to be resized, use SGLDeque::assign.

[[nodiscard]] const T& front() const;

Returns a constant reference to the first element of SGLDeque.

dq.front() has the same effect as dq.at(0)

Use SGLDeque::atSGLDeque::at if the element needs to be modified.

If the SGLDeque has a size of 0, this will crash.

[[nodiscard]] int length() const;

Returns the length of the SGLDeque, which is the number of elements currently stored inside it.

[[nodiscard]] const T* pointerToData(int n) const;

Returns a read only pointer to element n of the SGLDeque. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLDeque to run SGLSort on.

The returned value may function as an iterator.

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

[[nodiscard]] T* pointerToData(int n);

Returns a pointer to element n of the SGLDeque. This does not dereference the returned pointer.

Using an out of bounds value for n is ok for this function as long as the returned pointer is not dereferenced. Out of bounds values for n may be used intentionally to specify parts of the SGLDeque to run SGLSort on.

The returned value may function as an iterator.

Assigning to the dereferenced value from the returned pointer would modify the SGLDeque.

Dereferencing the returned pointer when n is out of bounds results in undefined behaviour.

void popBack();

Removes the last element of the SGLDeque.

This runs in truly constant time.

This does not return the last element. If you need the last element, access it with SGLDeque::back before running this.

Running SGLDeque::popBack on an empty SGLDeque will crash.

void popFront();

Removes the first element of the SGLDeque.

This runs in truly constant time.

This does not return the first element. If you need the first element, access it with SGLDeque::front before running this.

Running SGLDeque::popFront on an empty SGLDeque will crash.

void pushBack(const T& x);

Appends x at the end of the SGLDeque.

This runs in amortised O(1) per use. To make it run in truly constant time, use SGLDeque::reserve to pre allocate the internal memory buffer with 3 times the required memory.

void pushFront(const T& x);

Prepends x at the start of the SGLDeque.

This runs in amortised O(1) per use. To make it run in truly constant time, use SGLDeque::reserve to pre allocate the internal memory buffer with 3 times the required memory.

void reserve(int newMemoryLength);

Pre allocates the internal memory buffer to a size sufficient to store newMemoryLength elements in the SGLDeque.

Pre allocate 3 times the required memory to avoid resizing. This is only necessary for SGLDeque and not SGLVector as only SGLDeque permits insertion on both ends.

If the internal memory buffer is larger than the requested length, nothing is done.

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects