ALib C++ Framework
by
Library Version: 2605 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/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_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 that uses C++20 concepts.
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 protected:
36 std::size_t fillSize= 0; ///< The current fill
37
38 public:
39 /// Increases the size of this vector by inserting given \p{value} at the end.<br>
40 /// In debug compilations an \alib_assertion is raised if this fixed-sized vector's
41 /// capacity is exceeded.
42 /// @param value The value to insert.
43 void push_back( const T& value ) {
44 ALIB_ASSERT_ERROR( fillSize < TSize, "MONOMEM/UT",
45 "FixedCapacityVector overflow: ", fillSize )
46 (*this)[fillSize++]= value;
47 }
48
49 /// Decreases the size of this vector by destructing and removing the given \p{value} at the
50 /// current end.<br>
51 /// In debug compilations an \alib_assertion is raised if no elements are left.
52 void pop_back() {
53 ALIB_ASSERT_ERROR( fillSize > 0 , "MONOMEM/UT",
54 "FixedCapacityVector underflow: ", fillSize )
55 (*this)[--fillSize].~T();
56 }
57
58 #if !DOXYGEN
59 [[nodiscard]]
60 typename std::array<T, TSize>::size_type
61 size() const { return fillSize; }
62
63 [[nodiscard]]
64 constexpr typename std::array<T, TSize>::iterator end() noexcept
65 { return std::array<T, TSize>::begin() + fillSize; }
66
67 [[nodiscard]]
68 constexpr typename std::array<T, TSize>::const_iterator end() const noexcept
69 { return std::array<T, TSize>::begin() + fillSize; }
70
71 [[nodiscard]]
72 constexpr
73 typename std::array<T, TSize>::const_iterator cend() noexcept
74 { return std::array<T, TSize>::cbegin() + fillSize; }
75
76 [[nodiscard]]
77 constexpr
78 typename std::array<T, TSize>::const_iterator cend() const noexcept
79 { return std::array<T, TSize>::cbegin() + fillSize; }
80 #endif // !DOXYGEN
81
82}; // FixedCapacityVector
83
84/// Type alias which denotes a <c>std::priority_queue</c> using
85/// a #"FixedCapacityVector" as its underlying container type.
86template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
87using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
88
89} // namespace alib[::containers]
90
91
92/// Type alias in namespace #"%alib".
93template<typename T, std::size_t TSize>
95
96
97/// Type alias in namespace #"%alib", which denotes a <c>std::priority_queue</c> using
98/// a #"FixedCapacityVector" as its underlying container type.
99template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
100using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
101
102} // namespace [alib]
#define ALIB_EXPORT
#define ALIB_ASSERT_ERROR(cond, domain,...)
std::priority_queue< T, FixedCapacityVector< T, TSize >, TCompare > FixedSizePriorityQueue
Definition alox.cpp:14
std::priority_queue< T, FixedCapacityVector< T, TSize >, TCompare > FixedSizePriorityQueue
containers::FixedCapacityVector< T, TSize > FixedCapacityVector
Type alias in namespace #"%alib".