Deque¶
| Spice | |
|---|---|
Deque<T> struct¶
A deque in Spice is a commonly used data structure that allows insertion and removal of elements from both ends: front and back.
Time complexity:
Insert at front: O(1)
Insert at back: O(1)
Delete from front: O(1)
Delete from back: O(1)
Search: O(n)
Deques pre-allocate space using an initial size and a resize factor to not have to re-allocate with every item pushed. This implementation uses a ring buffer, that wraps around when one of the indices reach the start or end of the allocated heap space.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a deque pre-filled with a number of copies of a default value
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned long |
Number of items to insert |
defaultValue |
const T& |
Value to fill the deque with |
ctor¶
| Spice | |
|---|---|
Construct a deque, pre-allocating space for the given number of items
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned int |
Number of items to pre-allocate space for |
ctor¶
| Spice | |
|---|---|
Construct a deque, pre-allocating space for the given number of items
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned long |
Number of items to pre-allocate space for (default: INITIAL_ALLOC_COUNT) |
ctor¶
| Spice | |
|---|---|
Construct a deque as a deep copy of another deque
Parameters
| Name | Type | Description |
|---|---|---|
original |
const Deque<T>& |
Deque to copy |
Methods¶
pushBack¶
| Spice | |
|---|---|
Add an item at the back of the deque
Parameters
| Name | Type | Description |
|---|---|---|
item |
const T& |
pushFront¶
| Spice | |
|---|---|
Add an item at the front of the deque
Parameters
| Name | Type | Description |
|---|---|---|
item |
const T& |
popFront¶
| Spice | |
|---|---|
Retrieve the first item and remove it
Returns: T& — First item
popBack¶
| Spice | |
|---|---|
Retrieve the last item and remove it
Returns: T& — Last item
front¶
| Spice | |
|---|---|
Retrieve the first item without removing it from the deque
Returns: T& — First item
back¶
| Spice | |
|---|---|
Retrieve the last item without removing it from the deque
Returns: T& — Last item
getSize¶
| Spice | |
|---|---|
Retrieve the current size of the deque
Returns: long — Current size of the deque
getCapacity¶
| Spice | |
|---|---|
Retrieve the current capacity of the deque
Returns: long — Current capacity of the deque
isEmpty¶
| Spice | |
|---|---|
Checks if the deque contains any items at the moment
Returns: bool — Empty or not empty
isFull¶
| Spice | |
|---|---|
Checks if the deque exhausts its capacity and needs to resize at the next call of push
Returns: bool — Full or not full
reserve¶
| Spice | |
|---|---|
Reserves itemCount items
Parameters
| Name | Type | Description |
|---|---|---|
itemCount |
unsigned long |
pack¶
| Spice | |
|---|---|
Frees allocated memory that is not used by the deque
Operators¶
operator=¶
| Spice | |
|---|---|
Copy-assign the contents of another deque into this one
Parameters
| Name | Type | Description |
|---|---|---|
newValue |
const Deque<T>& |
Deque to copy from |
operator==¶
| Spice | |
|---|---|
Check if two deques are equal, i.e. they have the same size and equal contents in order
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const Deque<T>& |
|
rhs |
const Deque<T>& |
Returns: bool — Equal or not equal
operator!=¶
| Spice | |
|---|---|
Check if two deques are not equal
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const Deque<T>& |
|
rhs |
const Deque<T>& |
Returns: bool — Not equal or equal