mdds
util.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2021 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #ifndef INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_UTIL_HPP
30 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_DIR_UTIL_HPP
31 
32 #include "./types.hpp"
33 
34 #include <sstream>
35 
36 namespace mdds {
37 
38 namespace mtv {
39 
45 {
55  {
56  (void)block;
57  }
58 
68  {
69  (void)block;
70  }
71 };
72 
77 {
83 
88  static constexpr lu_factor_t loop_unrolling = lu_factor_t::lu16;
89 };
90 
91 } // namespace mtv
92 
93 namespace detail { namespace mtv {
94 
95 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
96 
97 template<typename T, typename = void>
98 struct has_trace : std::false_type {};
99 
100 template<typename T>
101 struct has_trace<T, decltype((void)T::trace)> : std::true_type {};
102 
103 template<typename Trait>
104 struct call_trace
105 {
106  int& call_depth;
107 
108  call_trace(int& _call_depth) : call_depth(_call_depth) { ++call_depth; }
109  ~call_trace() noexcept { --call_depth; }
110 
111  void call(std::false_type, const ::mdds::mtv::trace_method_properties_t&) const
112  {
113  // sink
114  }
115 
116  void call(std::true_type, const ::mdds::mtv::trace_method_properties_t& props) const
117  {
118  // In case of recursive calls, only trace the first encountered method.
119  if (call_depth <= 1)
120  Trait::trace(props);
121  }
122 
123  void operator()(const ::mdds::mtv::trace_method_properties_t& props) const
124  {
125  call(has_trace<Trait>{}, props);
126  }
127 };
128 
129 #endif
130 
131 inline void throw_block_position_not_found(
132  const char* method_sig, int line, size_t pos, size_t block_size, size_t container_size)
133 {
134  std::ostringstream os;
135  os << method_sig << "#" << line << ": block position not found! (logical pos="
136  << pos << ", block size=" << block_size << ", logical size=" << container_size << ")";
137  throw std::out_of_range(os.str());
138 }
139 
158 template<typename _T, typename _SizeT>
159 std::pair<_SizeT, bool> calc_input_end_position(
160  const _T& it_begin, const _T& it_end, _SizeT pos, _SizeT total_size)
161 {
162  using ret_type = std::pair<_SizeT, bool>;
163 
164  _SizeT length = std::distance(it_begin, it_end);
165  if (!length)
166  // empty data array. nothing to do.
167  return ret_type(0, false);
168 
169  _SizeT end_pos = pos + length - 1;
170  if (end_pos >= total_size)
171  throw std::out_of_range("Input data sequence is too long.");
172 
173  return ret_type(end_pos, true);
174 }
175 
176 template<typename T>
177 T advance_position(const T& pos, int steps)
178 {
179  T ret = pos;
180 
181  if (steps > 0)
182  {
183  while (steps > 0)
184  {
185  if (ret.second + steps < ret.first->size)
186  {
187  // element is still in the same block.
188  ret.second += steps;
189  break;
190  }
191  else
192  {
193  steps -= static_cast<int>(ret.first->size - ret.second);
194  ++ret.first;
195  ret.second = 0;
196  }
197  }
198  }
199  else
200  {
201  while (steps < 0)
202  {
203  if (static_cast<int>(ret.second) >= -steps)
204  {
205  ret.second += steps;
206  break;
207  }
208  else
209  {
210  steps += static_cast<int>(ret.second + 1);
211  --ret.first;
212  ret.second = ret.first->size - 1;
213  }
214  }
215  }
216 
217  return ret;
218 }
219 
220 template<typename _Blk>
221 inline typename _Blk::value_type get_block_element_at(const mdds::mtv::base_element_block& data, size_t offset)
222 {
223  return _Blk::at(data, offset);
224 }
225 
226 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
227 
228 template<>
229 inline bool get_block_element_at<mdds::mtv::boolean_element_block>(const mdds::mtv::base_element_block& data, size_t offset)
230 {
231  auto it = mdds::mtv::boolean_element_block::cbegin(data);
232  std::advance(it, offset);
233  return *it;
234 }
235 
236 #endif
237 
238 }} // namespace detail::mtv
239 
240 } // namespace mdds
241 
242 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
243 
244 #define MDDS_MTV_TRACE(method_type) \
245  ::mdds::detail::mtv::call_trace<Trait> mdds_mtv_ct(m_trace_call_depth); \
246  mdds_mtv_ct( \
247  { trace_method_t::method_type, this, __func__, "", __FILE__, __LINE__ } \
248  )
249 
250 #define MDDS_MTV_TRACE_ARGS(method_type, stream) \
251  ::mdds::detail::mtv::call_trace<Trait> mdds_mtv_ct(m_trace_call_depth); \
252  do \
253  { \
254  std::ostringstream _os_; \
255  _os_ << stream; \
256  mdds_mtv_ct( \
257  { trace_method_t::method_type, this, __func__, _os_.str(), __FILE__, __LINE__ } \
258  ); \
259  } while (false)
260 
261 #else
262 
263 #define MDDS_MTV_TRACE(...)
264 
265 #define MDDS_MTV_TRACE_ARGS(...)
266 
267 #endif
268 
269 #endif
270 
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
272 
Definition: types.hpp:173
Definition: util.hpp:77
static constexpr lu_factor_t loop_unrolling
Definition: util.hpp:88
Definition: util.hpp:45
void element_block_acquired(const base_element_block *block)
Definition: util.hpp:54
void element_block_released(const base_element_block *block)
Definition: util.hpp:67