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