ALib C++ Library
Library Version: 2402 R1
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_monomem 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_UTIL_FIXED_CAPACITY_VECTOR
9#define HPP_ALIB_MONOMEM_UTIL_FIXED_CAPACITY_VECTOR
10
11#if !defined (HPP_ALIB_MONOMEM_MONOMEM)
13#endif
14
15#if !defined (_GLIBCXX_ARRAY) && !defined(_ARRAY_)
16# include <array>
17#endif
18
19#if !defined (_GLIBCXX_QUEUE) && !defined(_QUEUE_)
20# include <queue>
21#endif
22
23namespace alib { namespace monomem { namespace util {
24
25/** ************************************************************************************************
26 * This class fills the gap between standard types <c>std::array</c> and <c>std::vector</c> by
27 * implementing a fixed size array, with a current filling size index.
28 * With that methods #push_back and #pop_back could be implemented, which satisfies the constraints
29 * given for example to underlying containers of type <c>std::priority_queue</c>.
30 *
31 * The rationale here is to use fixed size memory allocation or even stack allocation, in situations
32 * where the maximum capacity needed at runtime is known upfront.
33 *
34 * Besides aforementioned methods \c push_back and \c pop_back, a few necessary other methods
35 * of parent class <c>std::array</c> have been replaced, but are not explicitly mentioned with
36 * this reference documentation. Among them are \c size(), \c end(), \c rend() or \c cend().
37 *
38 * \attention
39 * While this type inherits <c>std::array</c> publically, not all standard methods and type
40 * traits have been overwritten according to the ever more complicated C++ specification.
41 * Therefore, there is not only no guarantee given that this type behaves as it is supposed to in
42 * each and every complex situation (e.g template meta programing).
43 * However, the use in accordance with type \c std::priority_queue is certified.
44 **************************************************************************************************/
45template<typename T, std::size_t TSize>
46class FixedCapacityVector : public std::array<T, TSize>
47{
48 protected:
49 std::size_t fillSize= 0; ///< The current fill
50
51 public:
52 /**
53 * Increases the size of this vector by inserting given \p value at the end.<br>
54 * In debug compilations, an \alib assertion is raised, if this fixed sized vector's
55 * capacity is exceeded.
56 * @param value The value to insert.
57 */
58 void push_back( const T& value )
59 {
60 ALIB_ASSERT_ERROR( fillSize < TSize, "MONOMEM/UT", "FixedCapacityVector overflow: ",
61 int(fillSize) )
62 (*this)[fillSize++]= value;
63 }
64
65 /**
66 * Decreases the size of this vector by destructing and removing the given \p value at the
67 * current end.<br>
68 * In debug compilations, an \alib assertion is raised, if no elements are left.
69 */
70 void pop_back()
71 {
72 ALIB_ASSERT_ERROR( fillSize > 0 , "MONOMEM/UT", "FixedCapacityVector underflow: ",
73 int(fillSize) )
74 (*this)[--fillSize].~T();
75 }
76
77#if !defined(ALIB_DOX)
79 [[nodiscard]]
80 typename std::array<T, TSize>::size_type
81 size() const
82 { return fillSize; }
83
84 [[nodiscard]]
85 constexpr typename std::array<T, TSize>::iterator end() noexcept
86 { return std::array<T, TSize>::begin() + fillSize; }
87
88 [[nodiscard]]
89 constexpr typename std::array<T, TSize>::const_iterator end() const noexcept
90 { return std::array<T, TSize>::begin() + fillSize; }
91
92 [[nodiscard]]
93 constexpr
94 typename std::array<T, TSize>::const_iterator cend() noexcept
95 { return std::array<T, TSize>::cbegin() + fillSize; }
96
97 [[nodiscard]]
98 constexpr
99 typename std::array<T, TSize>::const_iterator cend() const noexcept
100 { return std::array<T, TSize>::cbegin() + fillSize; }
102#endif // !defined(ALIB_DOX)
103
104}; // FixedCapacityVector
105
106}} // namespace alib[::monomem::util]
107
108
109/// Type alias in namespace \b alib.
110template<typename T, std::size_t TSize>
112
113
114/// Type alias in namespace \b alib, which denotes a <c>std::priority_queue</c> using
115/// a \alib{monomem::util;FixedCapacityVector} as its underlying container type.
116template<typename T, std::size_t TSize, typename TCompare= std::less<T>>
117using FixedSizePriorityQueue = std::priority_queue< T, FixedCapacityVector<T, TSize>, TCompare>;
118
119} // namespace [alib]
120
121
122
123#endif // HPP_ALIB_MONOMEM_UTIL_RTTRALLOCATOR
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
Definition alib.cpp:57
std::priority_queue< T, FixedCapacityVector< T, TSize >, TCompare > FixedSizePriorityQueue