- More Documentation
This commit is contained in:
@@ -59,34 +59,30 @@ namespace fennec
|
||||
/// | find | \f$O(N)\f$ |
|
||||
/// | insertion | \f$O(1)\f$ |
|
||||
/// | deletion | \f$O(1)\f$ |
|
||||
/// | space | \f$O(N)\f$ |
|
||||
///
|
||||
/// \tparam TypeT value type
|
||||
template<typename TypeT, typename AllocT = allocator<TypeT>>
|
||||
struct deque {
|
||||
|
||||
// Definitions =========================================================================================================
|
||||
public:
|
||||
using value_t = TypeT; ///< Alias for the value type
|
||||
class iterator;
|
||||
|
||||
private:
|
||||
struct node {
|
||||
value_t value;
|
||||
node *prev, *next;
|
||||
|
||||
template<typename...ArgsT>
|
||||
node(node* prev, node* next, ArgsT&&...args)
|
||||
: value(fennec::forward<ArgsT>(args)...)
|
||||
, prev(prev), next(next) {
|
||||
}
|
||||
|
||||
~node() = default;
|
||||
};
|
||||
struct node;
|
||||
|
||||
public:
|
||||
|
||||
/// \name Definitions
|
||||
/// @{
|
||||
|
||||
using value_t = TypeT; //!< Alias for the value type
|
||||
using alloc_t = allocator_traits<AllocT>::template rebind<node>; //!< The underlying allocator type
|
||||
using elem_t = node*; //!< The underlying element type
|
||||
|
||||
/// @}
|
||||
|
||||
|
||||
class iterator;
|
||||
|
||||
|
||||
// Constructors ========================================================================================================
|
||||
|
||||
@@ -95,6 +91,10 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Default Constructor, initializes an empty deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
deque()
|
||||
: _alloc()
|
||||
, _first(nullptr)
|
||||
@@ -105,6 +105,10 @@ public:
|
||||
///
|
||||
/// \brief Alloc Constructor, initializes an empty deque with the specified allocator
|
||||
/// \param alloc the allocator to copy
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
deque(const alloc_t& alloc)
|
||||
: _alloc(alloc)
|
||||
, _first(nullptr)
|
||||
@@ -115,6 +119,10 @@ public:
|
||||
///
|
||||
/// \brief Copy Constructor
|
||||
/// \param deque the deque to copy
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(N)\f$
|
||||
///
|
||||
deque(const deque& deque)
|
||||
: _alloc(deque._alloc)
|
||||
, _first(nullptr)
|
||||
@@ -130,6 +138,10 @@ public:
|
||||
///
|
||||
/// \brief Deque Move Constructor
|
||||
/// \param deque the deque to move
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
deque(deque&& deque) noexcept
|
||||
: _alloc(deque._alloc)
|
||||
, _first(deque._first)
|
||||
@@ -141,6 +153,10 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Destructor, calls deque::clear
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(N)\f$
|
||||
///
|
||||
~deque() {
|
||||
clear();
|
||||
}
|
||||
@@ -154,12 +170,20 @@ public:
|
||||
|
||||
///
|
||||
/// \returns \f$true\f$ when the deque is empty, \f$false\f$ otherwise
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
constexpr bool is_empty() const {
|
||||
return _size == 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// \returns the number of elements in size()
|
||||
/// \returns the number of elements in the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
constexpr size_t size() const {
|
||||
return _size;
|
||||
}
|
||||
@@ -174,6 +198,10 @@ public:
|
||||
|
||||
///
|
||||
/// \returns a reference to the first element in the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
value_t& front() {
|
||||
assert(not is_empty(), "Attempted to access an empty deque.");
|
||||
return _first->value;
|
||||
@@ -181,6 +209,10 @@ public:
|
||||
|
||||
///
|
||||
/// \returns a const-qualified reference to the first element in the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
const value_t& front() const {
|
||||
assert(not is_empty(), "Attempted to access an empty deque.");
|
||||
return _first->value;
|
||||
@@ -188,6 +220,10 @@ public:
|
||||
|
||||
///
|
||||
/// \returns a reference to the last element in the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
value_t& back() {
|
||||
assert(not is_empty(), "Attempted to access an empty deque.");
|
||||
return _last->value;
|
||||
@@ -195,6 +231,10 @@ public:
|
||||
|
||||
///
|
||||
/// \returns a const-qualified reference to the last element in the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
const value_t& back() const {
|
||||
assert(not is_empty(), "Attempted to access an empty deque.");
|
||||
return _last->value;
|
||||
@@ -211,6 +251,10 @@ public:
|
||||
///
|
||||
/// \brief Push Front Move, moves a value to the front of the deque
|
||||
/// \param elem the value to move
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void push_front(value_t&& elem) {
|
||||
this->_push_front(elem);
|
||||
}
|
||||
@@ -218,6 +262,10 @@ public:
|
||||
///
|
||||
/// \brief Push Front Copy, copies a value to the front of the deque
|
||||
/// \param elem the value to copy
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void push_front(const value_t& elem) {
|
||||
this->_push_front(elem);
|
||||
}
|
||||
@@ -226,6 +274,10 @@ public:
|
||||
/// \brief Emplace Front, constructs a new value at the front of the deque
|
||||
/// \tparam ArgsT Argument types
|
||||
/// \param args Arguments used to construct the value
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
template<typename...ArgsT>
|
||||
void emplace_front(ArgsT&&...args) {
|
||||
this->_push_front(fennec::forward<ArgsT>(args)...);
|
||||
@@ -235,6 +287,10 @@ public:
|
||||
///
|
||||
/// \brief Push Back Move, moves a value to the back of the deque
|
||||
/// \param elem the value to move
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void push_back(value_t&& elem) {
|
||||
this->_push_back(elem);
|
||||
}
|
||||
@@ -242,6 +298,10 @@ public:
|
||||
///
|
||||
/// \brief Push Back Copy, copies a value to the back of the deque
|
||||
/// \param elem the value to copy
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void push_back(const value_t& elem) {
|
||||
this->_push_back(elem);
|
||||
}
|
||||
@@ -250,6 +310,10 @@ public:
|
||||
/// \brief Emplace Back, constructs a new value at the back of the deque
|
||||
/// \tparam ArgsT Argument types
|
||||
/// \param args Arguments used to construct the value
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
template<typename...ArgsT>
|
||||
void emplace_back(ArgsT&&...args) {
|
||||
this->_push_back(fennec::forward<ArgsT>(args)...);
|
||||
@@ -257,6 +321,10 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Clears the contents of the deque
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(N)\f$
|
||||
///
|
||||
void clear() {
|
||||
elem_t it = _first;
|
||||
while (it) {
|
||||
@@ -272,6 +340,10 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Erase the First Element
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void pop_front() {
|
||||
if (_first == nullptr) {
|
||||
return;
|
||||
@@ -286,6 +358,10 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Erase the Last Element
|
||||
///
|
||||
/// \par Complexity
|
||||
/// \f$O(1)\f$
|
||||
///
|
||||
void pop_back() {
|
||||
if (_last == nullptr) {
|
||||
return;
|
||||
@@ -307,11 +383,16 @@ public:
|
||||
* TODO: Decide whether you should be able to iterate over a deque
|
||||
*/
|
||||
|
||||
|
||||
// Private Member Variables ============================================================================================
|
||||
private:
|
||||
alloc_t _alloc;
|
||||
node *_first, *_last;
|
||||
size_t _size;
|
||||
|
||||
|
||||
// Private Helpers =====================================================================================================
|
||||
private:
|
||||
template<typename...ArgsT>
|
||||
void _push_front(ArgsT&&...args) {
|
||||
elem_t next = _first;
|
||||
@@ -337,6 +418,22 @@ private:
|
||||
}
|
||||
++_size;
|
||||
}
|
||||
|
||||
|
||||
// Private Definitions =================================================================================================
|
||||
private:
|
||||
struct node {
|
||||
value_t value;
|
||||
node *prev, *next;
|
||||
|
||||
template<typename...ArgsT>
|
||||
node(node* prev, node* next, ArgsT&&...args)
|
||||
: value(fennec::forward<ArgsT>(args)...)
|
||||
, prev(prev), next(next) {
|
||||
}
|
||||
|
||||
~node() = default;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user