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 
65 };
66 
71 {
77 
82  static constexpr lu_factor_t loop_unrolling = lu_factor_t::lu16;
83 };
84 
85 } // namespace mtv
86 
87 namespace detail { namespace mtv {
88 
89 inline void throw_block_position_not_found(
90  const char* method_sig, int line, size_t pos, size_t block_size, size_t container_size)
91 {
92  std::ostringstream os;
93  os << method_sig << "#" << line << ": block position not found! (logical pos="
94  << pos << ", block size=" << block_size << ", logical size=" << container_size << ")";
95  throw std::out_of_range(os.str());
96 }
97 
116 template<typename _T, typename _SizeT>
117 std::pair<_SizeT, bool> calc_input_end_position(
118  const _T& it_begin, const _T& it_end, _SizeT pos, _SizeT total_size)
119 {
120  using ret_type = std::pair<_SizeT, bool>;
121 
122  _SizeT length = std::distance(it_begin, it_end);
123  if (!length)
124  // empty data array. nothing to do.
125  return ret_type(0, false);
126 
127  _SizeT end_pos = pos + length - 1;
128  if (end_pos >= total_size)
129  throw std::out_of_range("Input data sequence is too long.");
130 
131  return ret_type(end_pos, true);
132 }
133 
134 template<typename T>
135 T advance_position(const T& pos, int steps)
136 {
137  T ret = pos;
138 
139  if (steps > 0)
140  {
141  while (steps > 0)
142  {
143  if (ret.second + steps < ret.first->size)
144  {
145  // element is still in the same block.
146  ret.second += steps;
147  break;
148  }
149  else
150  {
151  steps -= static_cast<int>(ret.first->size - ret.second);
152  ++ret.first;
153  ret.second = 0;
154  }
155  }
156  }
157  else
158  {
159  while (steps < 0)
160  {
161  if (static_cast<int>(ret.second) >= -steps)
162  {
163  ret.second += steps;
164  break;
165  }
166  else
167  {
168  steps += static_cast<int>(ret.second + 1);
169  --ret.first;
170  ret.second = ret.first->size - 1;
171  }
172  }
173  }
174 
175  return ret;
176 }
177 
178 template<typename _Blk>
179 inline typename _Blk::value_type get_block_element_at(const mdds::mtv::base_element_block& data, size_t offset)
180 {
181  return _Blk::at(data, offset);
182 }
183 
184 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
185 
186 template<>
187 inline bool get_block_element_at<mdds::mtv::boolean_element_block>(const mdds::mtv::base_element_block& data, size_t offset)
188 {
189  auto it = mdds::mtv::boolean_element_block::cbegin(data);
190  std::advance(it, offset);
191  return *it;
192 }
193 
194 #endif
195 
196 }} // namespace detail::mtv
197 
198 }
199 
200 #endif
201 
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
203 
Definition: types.hpp:113
Definition: util.hpp:71
static constexpr lu_factor_t loop_unrolling
Definition: util.hpp:82
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:64