libhatchet
Loading...
Searching...
No Matches
hxdeque.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2025 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxallocator.hpp"
17#include "hxutility.h"
18
19HX_NS_BEGIN_
20
26template<typename T_, hxsize_t capacity_=hxallocator_dynamic_capacity>
27class hxdeque : private hxallocator<T_, capacity_> {
28public:
32 explicit hxdeque(hxsize_t dynamic_capacity_=0);
33
35 ~hxdeque(void);
36
40
43 hxattr_nodiscard const T_& operator[](hxsize_t index_) const;
44
48
51 hxattr_nodiscard const T_& at(hxsize_t index_) const;
52
55
57 hxattr_nodiscard const T_& back(void) const;
58
61
63 void clear(void) noexcept;
64
68 template<typename... args_t_>
69 void emplace_back(args_t_&&... args_) noexcept;
70
74 template<typename... args_t_>
75 void emplace_front(args_t_&&... args_) noexcept;
76
78 hxattr_nodiscard bool empty(void) const;
79
82
84 hxattr_nodiscard const T_& front(void) const;
85
87 hxattr_nodiscard bool full(void) const;
88
90 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
91
93 void pop_back(void) noexcept;
94
96 void pop_front(void) noexcept;
97
101 template<typename... args_t_>
102 void push_back(args_t_&&... args_) noexcept;
103
107 template<typename... args_t_>
108 void push_front(args_t_&&... args_) noexcept;
109
113 void reserve(hxsize_t dynamic_capacity_);
114
117
118private:
119 // This is raw underlying data and would not be what was expected.
120 using hxallocator<T_, capacity_>::data;
121
122 hxdeque(const hxdeque&) = delete;
123 void operator=(const hxdeque&) = delete;
124
125 size_t m_head_;
126 size_t m_tail_;
127};
128
129#include "detail/hxdeque.inl"
130HX_NS_END_
const T * data(void) const
Returns a pointer to a const and potentially uninitialized array of T.
Definition hxallocator.hpp:96
hxallocator(void)
Does not allocate until reserve_storage is called.
Definition hxallocator.hpp:78
T & at(hxsize_t index_)
Returns a reference to the element at logical index index.
T & front(void)
Returns a reference to the front element.
void pop_back(void) noexcept
Removes and destroys the back element.
void emplace_front(args_t_ &&... args_) noexcept
Constructs an element in place at the front using forwarded arguments.
hxsize_t size(void) const
Returns the number of elements currently in the deque.
void emplace_back(args_t_ &&... args_) noexcept
Constructs an element in place at the back using forwarded arguments.
hxsize_t max_size(void) const
Returns the capacity of the deque.
Definition hxdeque.hpp:90
void clear(void) noexcept
Destroys all elements and resets the deque to empty without deallocating.
bool empty(void) const
Returns true if the deque contains no elements.
bool full(void) const
Returns true if the deque is at capacity.
const T & operator[](hxsize_t index_) const
Returns a const reference to the element at logical index index.
const T & back(void) const
Returns a const reference to the back element.
hxdeque(hxsize_t dynamic_capacity_=0)
Constructs an empty hxdeque.
void push_front(args_t_ &&... args_) noexcept
Prepends an element at the front using forwarded arguments.
T & operator[](hxsize_t index_)
Returns a reference to the element at logical index index.
T & back(void)
Returns a reference to the back element.
~hxdeque(void)
Destroys all elements in the deque.
void pop_front(void) noexcept
Removes and destroys the front element.
const T & front(void) const
Returns a const reference to the front element.
const T & at(hxsize_t index_) const
Returns a const reference to the element at logical index index.
void push_back(args_t_ &&... args_) noexcept
Appends an element at the back using forwarded arguments.
hxsize_t capacity(void) const
Returns the capacity of the deque or 0 if unallocated.
void reserve(hxsize_t dynamic_capacity_)
Allocates storage for a dynamic deque.
Similar to std::allocator.
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
A few utility functions and most standard C++ meta programming functions.
Provides core macros, memory management and feature detection.
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209