ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
fixedcapacityvector.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_containers of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_MONOMEM_CONTAINERS_FIXED_CAPACITY_VECTOR
9#define HPP_ALIB_MONOMEM_CONTAINERS_FIXED_CAPACITY_VECTOR
10#pragma once
11#if !defined(DOXYGEN)
12# include "alib/alib.hpp"
13#endif
14ALIB_ASSERT_MODULE(CONTAINERS)
15#include <array>
16#include <queue>
17
18namespace alib { namespace containers {
19
20//==================================================================================================
21/// This class fills the gap between standard types <c>std::array</c> and <c>std::vector</c> by
22/// implementing a fixed size array, with a current filling size index.
23/// With that methods #push_back and #pop_back could be implemented, which satisfies the constraints
24/// given for example to underlying containers of type <c>std::priority_queue</c>.
25///
26/// The rationale here is to use fixed size memory allocation or even stack allocation, in situations
27/// where the maximum capacity needed at runtime is known upfront.
28///
29/// Besides aforementioned methods \c push_back and \c pop_back, a few necessary other methods
30/// of parent class <c>std::array</c> have been replaced, but are not explicitly mentioned with
31/// this reference documentation. Among them are \c size(), \c end(), \c rend() or \c cend().
32///
33/// \attention
34/// While this type inherits <c>std::array</c> publicly, not all standard methods and type
35/// traits have been overwritten according to the ever more complicated C++ specification.
36/// Therefore, there is not only no guarantee given that this type behaves as it is supposed to in
37/// each and every complex situation (e.g template meta programing).
38/// However, the use in accordance with type \c std::priority_queue is certified.
39///
40/// @tparam T The type to store.
41/// @tparam TSize The fixed sitze of the vector.
42//==================================================================================================
43template<typename T, std::size_t TSize>
44class FixedCapacityVector : public std::array<T, TSize>
45{
46 protected:
47 std::size_t fillSize= 0; ///< The current fill
48
49 public:
50 /// Increases the size of this vector by inserting given \p value at the end.<br>
51 /// In debug compilations, an \alib assertion is raised, if this fixed sized vector's
52 /// capacity is exceeded.
53 /// @param value The value to insert.
54 void push_back( const T& value )
55 {
56 ALIB_ASSERT_ERROR( fillSize < TSize, "MONOMEM/UT", "FixedCapacityVector overflow: ",
57 int(fillSize) )
58 (*this)[fillSize++]= value;
59 }
60
61 /// Decreases the size of this vector by destructing and removing the given \p value at the
62 /// current end.<br>
63 /// In debug compilations, an \alib assertion is raised, if no elements are left.
64 void pop_back()
65 {
66 ALIB_ASSERT_ERROR( fillSize > 0 , "MONOMEM/UT", "FixedCapacityVector underflow: ",
67 int(fillSize) )
68 (*this)[--fillSize].~T();
69 }
70
71#if !DOXYGEN
73 [[nodiscard]]
74 typename std::array<T, TSize>::size_type
75 size() const
76 { return fillSize; }
77
78 [[nodiscard]]
79 constexpr typename std::array<T, TSize>::iterator end() noexcept
80 { return std::array<T, TSize>::begin() + fillSize; }
81
82 [[nodiscard]]
83 constexpr typename std::array<T, TSize>::const_iterator end() const noexcept
84 { return std::array<T, TSize>::begin() + fillSize; }
85
86 [[nodiscard]]
87 constexpr
88 typename std::array<T, TSize>::const_iterator cend() noexcept
89 { return std::array<T, TSize>::cbegin() + fillSize; }
90
91 [[nodiscard]]
92 constexpr
93 typename std::array<T, TSize>::const_iterator cend() const noexcept
94 { return std::array<T, TSize>::cbegin() + fillSize; }
96#endif // !DOXYGEN
97
98}; // FixedCapacityVector
99
100/// Type alias which denotes a <c>std::priority_queue</c> using
101/// a \alib{containers;FixedCapacityVector} as its underlying container type.
102template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
103using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
104
105} // namespace alib[::containers]
106
107
108/// Type alias in namespace \b alib.
109template<typename T, std::size_t TSize>
111
112
113/// Type alias in namespace \b alib, which denotes a <c>std::priority_queue</c> using
114/// a \alib{containers;FixedCapacityVector} as its underlying container type.
115template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
116using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
117
118} // namespace [alib]
119
120
121
122#endif // HPP_ALIB_MONOMEM_CONTAINERS_FIXED_CAPACITY_VECTOR
123
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:849
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
std::priority_queue< T, FixedCapacityVector< T, TSize >, TCompare > FixedSizePriorityQueue
Definition alib.cpp:69
std::priority_queue< T, FixedCapacityVector< T, TSize >, TCompare > FixedSizePriorityQueue