mdds
Loading...
Searching...
No Matches
util.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2021 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include "./block_funcs.hpp"
10
11#include <sstream>
12
13namespace mdds { namespace mtv {
14
20{
30 {
31 (void)block;
32 }
33
43 {
44 (void)block;
45 }
46};
47
49{
50};
51
56{
62
67 static constexpr lu_factor_t loop_unrolling = lu_factor_t::lu16;
68
75 static constexpr bool enable_cow = false;
76
81
91};
92
93namespace detail {
94
99
105{
106};
107
108#ifdef MDDS_MULTI_TYPE_VECTOR_TRACE
109
110template<typename T>
111concept has_trace = requires { T::trace; };
112
113template<typename Traits>
114struct call_trace
115{
116 int& call_depth;
117
118 call_trace(int& _call_depth) : call_depth(_call_depth)
119 {
120 ++call_depth;
121 }
122 ~call_trace() noexcept
123 {
124 --call_depth;
125 }
126
127 void operator()(const ::mdds::mtv::trace_method_properties_t& props) const
128 {
129 if constexpr (has_trace<Traits>)
130 {
131 // In case of recursive calls, only trace the first encountered method.
132 if (call_depth <= 1)
133 Traits::trace(props);
134 }
135 else
136 {
137 // sink
138 }
139 }
140};
141
142#endif
143
144inline void throw_block_position_not_found(
145 const char* method_sig, int line, size_t pos, size_t block_size, size_t container_size)
146{
147 std::ostringstream os;
148 os << method_sig << "#" << line << ": block position not found! (logical pos=" << pos
149 << ", block size=" << block_size << ", logical size=" << container_size << ")";
150 throw std::out_of_range(os.str());
151}
152
171template<typename _T, typename _SizeT>
172std::pair<_SizeT, bool> calc_input_end_position(const _T& it_begin, const _T& it_end, _SizeT pos, _SizeT total_size)
173{
174 using ret_type = std::pair<_SizeT, bool>;
175
176 _SizeT length = std::distance(it_begin, it_end);
177 if (!length)
178 // empty data array. nothing to do.
179 return ret_type(0, false);
180
181 _SizeT end_pos = pos + length - 1;
182 if (end_pos >= total_size)
183 throw std::out_of_range("Input data sequence is too long.");
184
185 return ret_type(end_pos, true);
186}
187
188template<typename T>
189T advance_position(const T& pos, int steps)
190{
191 T ret = pos;
192
193 if (steps > 0)
194 {
195 while (steps > 0)
196 {
197 if (ret.second + steps < ret.first->size)
198 {
199 // element is still in the same block.
200 ret.second += steps;
201 break;
202 }
203 else
204 {
205 steps -= static_cast<int>(ret.first->size - ret.second);
206 ++ret.first;
207 ret.second = 0;
208 }
209 }
210 }
211 else
212 {
213 while (steps < 0)
214 {
215 if (static_cast<int>(ret.second) >= -steps)
216 {
217 ret.second += steps;
218 break;
219 }
220 else
221 {
222 steps += static_cast<int>(ret.second + 1);
223 --ret.first;
224 ret.second = ret.first->size - 1;
225 }
226 }
227 }
228
229 return ret;
230}
231
232} // namespace detail
233
234}} // namespace mdds::mtv
235
236#ifdef MDDS_MULTI_TYPE_VECTOR_TRACE
237
238#define MDDS_MTV_TRACE(method_type) \
239 ::mdds::mtv::detail::call_trace<Traits> mdds_mtv_ct(m_trace_call_depth); \
240 mdds_mtv_ct({trace_method_t::method_type, this, __func__, "", __FILE__, __LINE__})
241
242#define MDDS_MTV_TRACE_ARGS(method_type, stream) \
243 ::mdds::mtv::detail::call_trace<Traits> mdds_mtv_ct(m_trace_call_depth); \
244 do \
245 { \
246 std::ostringstream _os_; \
247 _os_ << stream; \
248 mdds_mtv_ct({trace_method_t::method_type, this, __func__, _os_.str(), __FILE__, __LINE__}); \
249 } while (false)
250
251#else
252
253#define MDDS_MTV_TRACE(...)
254
255#define MDDS_MTV_TRACE_ARGS(...)
256
257#endif
258
259/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition types.hpp:231
Definition util.hpp:49
Definition util.hpp:56
default_exec_policy exec_policy
Definition util.hpp:80
static constexpr lu_factor_t loop_unrolling
Definition util.hpp:67
empty_event_func event_func
Definition util.hpp:61
static constexpr bool enable_cow
Definition util.hpp:75
element_block_funcs<> block_funcs
Definition util.hpp:90
Definition util.hpp:105
Definition util.hpp:20
void element_block_acquired(const base_element_block *block)
Definition util.hpp:29
void element_block_released(const base_element_block *block)
Definition util.hpp:42