Debian Codegraph @

src/xrt/auxiliary/util/u_template_historybuf_impl_helpers.hpp

raw symbols SCIP
1 // Copyright 2021-2022, Collabora, Ltd.cxx . . $ `<file>/src/xrt/auxiliary/util/u_template_historybuf_impl_helpers.hpp`/
2 //
3 // SPDX-License-Identifier: BSL-1.0
4 /*!
5 * @file
6 * @brief All the "element-type-independent" code (helper objects, base classes) for a ringbuffer implementation on top
7 * of a fixed size array
8 * @author Rylie Pavlik <[email protected]>
9 * @author Moshi Turner <[email protected]>
10 * @ingroup aux_util
11 */
12
13 #pragma once
14
15 // IWYU pragma: private, include "util/u_template_historybuf.hpp"
16
17 #include <algorithm>
18 #include <assert.h>
19 #include <stdlib.h>
20
21
22 //| -4 | -3 | -2 | -1 | Top | Garbage |
23 // OR
24 //| -4 | -3 | -2 | -1 | Top | -7 | -6 | -5 |
25
26 namespace xrtcxx . . $ xrt/::auxiliarycxx . . $ xrt/auxiliary/::utilcxx . . $ xrt/auxiliary/util/::detailcxx . . $ xrt/auxiliary/util/detail/ {
27
28 /**
29 * @brief All the bookkeeping for adapting a fixed-size array to a ring buffer.
30 *
31 * This is all the guts of the ring buffer except for the actual buffer.
32 * We split it out to
33 * - reduce code size (this can be shared among multiple types)
34 * - separate concerns (keeping track of the indices separate from owning the buffer)
35 * - allow easier implementation of both const iterators and non-const iterators
36 *
37 * There are a few types of "index":
38 *
39 * - just "index": an index where the least-recently-added element still remaining is numbered 0, the next
40 * oldest is 1, etc. (Chronological)
41 * - "age": Reverse chronological order: 0 means most-recently-added, 1 means the one before it, etc.
42 * - "inner" index: the index in the underlying array/buffer. It's called "inner" because the consumer of the
43 * ring buffer should not ever deal with this index, it's an implementation detail.
44 */
45 class RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#
46 {
47 public:
48 //! Construct for a given size
49 explicit constexpr RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#RingBufferHelper(2c44cd906ae4e829).(size_t capacity) : capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.(capacity) {}
50 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#RingBufferHelper(af6ca9a163663005).(RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# const &) = default;
51 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#RingBufferHelper(9efc606122bb3a56).(RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# &&) = default;
52 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# &
53 operatorcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#`operator=`(f8892ad86c10840a).=(RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# const &) = default;
54 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# &
55 operatorcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#`operator=`(da6385d921ded7dc).=(RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper# &&) = default;
56
57 //! Get the inner index for a given age (if possible)
58 bool
59 age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#age_to_inner_index(597887df2858fe53).(size_t age, size_t &out_inner_idx) const noexcept;
60
61 //! Get the inner index for a given age, clamping it if out of bounds
62 bool
63 clamped_age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clamped_age_to_inner_index(597887df2858fe53).(size_t age, size_t &out_inner_idx) const noexcept;
64
65 //! Get the inner index for a given index (if possible)
66 bool
67 index_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#index_to_inner_index(597887df2858fe53).(size_t index, size_t &out_inner_idx) const noexcept;
68
69 //! Is the buffer empty?
70 bool
71 emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).() const noexcept
72 {
73 return length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. == 0;
74 }
75
76 //! How many elements are in the buffer?
77 size_t
78 sizecxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#size(c9b1e749b64d5edc).() const noexcept
79 {
80 return length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_.;
81 }
82
83 /*!
84 * @brief Update internal state for pushing an element to the back, and return the inner index to store
85 * the element at.
86 *
87 * This is the implementation of "push_back" excluding all the messy "actually dealing with the data"
88 * part ;-)
89 */
90 size_t
91 push_back_locationcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#push_back_location(cee159cc271e827).() noexcept;
92
93 /*!
94 * @brief Record the logical removal of the front element, if any.
95 *
96 * Does nothing if the buffer is empty. Does not actually modify the value stored in the backing array.
97 */
98 void
99 pop_frontcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_front(ced63f7c635d850d).() noexcept;
100
101 /*!
102 * @brief Record the logical removal of the back element, if any.
103 *
104 * Returns false if the buffer is empty. Does not actually modify the
105 * value stored in the backing array.
106 */
107 bool
108 pop_backcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_back(77ce49a7840d23c7).() noexcept;
109
110 //! Get the inner index of the front (oldest) value, or capacity_ if empty.
111 size_t
112 front_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_inner_index(c9b1e749b64d5edc).() const noexcept;
113
114 //! Get the inner index of the back (newest) value, or capacity_ if empty.
115 size_t
116 back_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#back_inner_index(c9b1e749b64d5edc).() const noexcept;
117
118 void
119 clearcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clear(49f6e7a06ebc5aa8).();
120
121 private:
122 // Would be const, but that would mess up our ability to copy/move containers using this.
123 size_t capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
124
125 //! The inner index containing the most recently added element, if any
126 size_t latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. = 0;
127
128 //! The number of elements populated.
129 size_t length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. = 0;
130
131 /**
132 * @brief Get the inner index of the front (oldest) value: assumes not empty!
133 *
134 * For internal use in this class only.
135 *
136 * @see front_inner_index() for the "safe" equivalent (that wraps this with error handling)
137 */
138 size_t
139 front_impl_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_impl_(c9b1e749b64d5edc).() const noexcept;
140 };
141
142
143 inline void
144 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::clearcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clear(49f6e7a06ebc5aa8).()
145 {
146 this->latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. = 0;
147 this->length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. = 0;
148 }
149
150 inline size_t
151 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::front_impl_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_impl_(c9b1e749b64d5edc).() const noexcept
152 {
153 assertcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).(!empty());
154 // length will not exceed capacity_, so this will not underflow
155 return (latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. + capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_. - length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. + 1) % capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
156 }
157
158 inline bool
159 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#age_to_inner_index(597887df2858fe53).(size_t age, size_t &out_inner_idx) const noexcept
160 {
161
162 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
163 return false;
164 }
165 if (age >= length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_.) {
166 return false;
167 }
168 // latest_inner_idx_ is the same as (latest_inner_idx_ + capacity_) % capacity_ so we add capacity_ to
169 // prevent underflow with unsigned values
170 out_inner_idx = (latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. + capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_. - age) % capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
171 return true;
172 }
173
174 inline bool
175 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::clamped_age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#clamped_age_to_inner_index(597887df2858fe53).(size_t age, size_t &out_inner_idx) const noexcept
176 {
177 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
178 return false;
179 }
180 return age_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#age_to_inner_index(597887df2858fe53).((stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).)(age, length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. - 1), out_inner_idx);
181 }
182
183 inline bool
184 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::index_to_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#index_to_inner_index(597887df2858fe53).(size_t index, size_t &out_inner_idx) const noexcept
185 {
186
187 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
188 return false;
189 }
190 if (index >= length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_.) {
191 return false;
192 }
193 // add to the front (oldest) index and take modulo capacity_
194 out_inner_idx = (front_impl_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_impl_(c9b1e749b64d5edc).() + index) % capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
195 return true;
196 }
197
198 inline size_t
199 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::push_back_locationcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#push_back_location(cee159cc271e827).() noexcept
200 {
201 // We always increment the latest inner index modulo capacity_
202 latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. = (latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. + 1) % capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
203 // Length cannot exceed capacity_. If it already was capacity_, that means we're overwriting something at
204 // latest_inner_idx_
205 length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_. + 1, capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.);
206 return latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_.;
207 }
208
209 inline void
210 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::pop_frontcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_front(ced63f7c635d850d).() noexcept
211 {
212 if (!emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
213 length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_.--;
214 }
215 }
216 inline bool
217 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::pop_backcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#pop_back(77ce49a7840d23c7).() noexcept
218 {
219 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
220 return false;
221 }
222 // adding capacity before -1 to avoid overflow
223 latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. = (latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_. + capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_. - 1) % capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
224 length_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#length_.--;
225 return true;
226 }
227 inline size_t
228 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::front_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_inner_index(c9b1e749b64d5edc).() const noexcept
229 {
230 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
231 return capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
232 }
233 return front_impl_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#front_impl_(c9b1e749b64d5edc).();
234 }
235
236 inline size_t
237 RingBufferHelpercxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#::back_inner_indexcxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#back_inner_index(c9b1e749b64d5edc).() const noexcept
238 {
239 if (emptycxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#empty(3482b152b9333168).()) {
240 return capacity_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#capacity_.;
241 }
242 return latest_inner_idx_cxx . . $ xrt/auxiliary/util/detail/RingBufferHelper#latest_inner_idx_.;
243 }
244
245 } // namespace xrt::auxiliary::util::detail