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