ALib C++ Library
Library Version: 2510 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 {
46 ALIB_ASSERT_ERROR( fillSize < TSize, "MONOMEM/UT",
47 "FixedCapacityVector overflow: ", fillSize )
48 (*this)[fillSize++]= value;
49 }
50
51 /// Decreases the size of this vector by destructing and removing the given \p value at the
52 /// current end.<br>
53 /// In debug compilations, an \alib assertion is raised, if no elements are left.
54 void pop_back()
55 {
56 ALIB_ASSERT_ERROR( fillSize > 0 , "MONOMEM/UT",
57 "FixedCapacityVector underflow: ", fillSize )
58 (*this)[--fillSize].~T();
59 }
60
61#if !DOXYGEN
62 [[nodiscard]]
63 typename std::array<T, TSize>::size_type
64 size() const
65 { return fillSize; }
66
67 [[nodiscard]]
68 constexpr typename std::array<T, TSize>::iterator end() noexcept
69 { return std::array<T, TSize>::begin() + fillSize; }
70
71 [[nodiscard]]
72 constexpr typename std::array<T, TSize>::const_iterator end() const noexcept
73 { return std::array<T, TSize>::begin() + fillSize; }
74
75 [[nodiscard]]
76 constexpr
77 typename std::array<T, TSize>::const_iterator cend() noexcept
78 { return std::array<T, TSize>::cbegin() + fillSize; }
79
80 [[nodiscard]]
81 constexpr
82 typename std::array<T, TSize>::const_iterator cend() const noexcept
83 { return std::array<T, TSize>::cbegin() + fillSize; }
84#endif // !DOXYGEN
85
86}; // FixedCapacityVector
87
88/// Type alias which denotes a <c>std::priority_queue</c> using
89/// a \alib{containers;FixedCapacityVector} as its underlying container type.
90template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
91using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
92
93} // namespace alib[::containers]
94
95
96/// Type alias in namespace \b alib.
97template<typename T, std::size_t TSize>
99
100
101/// Type alias in namespace \b alib, which denotes a <c>std::priority_queue</c> using
102/// a \alib{containers;FixedCapacityVector} as its underlying container type.
103template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
104using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
105
106} // namespace [alib]
107
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
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.