28 #ifndef INCLUDED_MDDS_MULTI_TYPE_VECTOR_TYPES_HPP
29 #define INCLUDED_MDDS_MULTI_TYPE_VECTOR_TYPES_HPP
38 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
44 #if defined(MDDS_UNIT_TEST) || defined (MDDS_MULTI_TYPE_VECTOR_DEBUG)
52 namespace mdds {
namespace mtv {
54 using element_t = int;
56 constexpr element_t element_type_empty = -1;
58 constexpr element_t element_type_boolean = 0;
59 constexpr element_t element_type_int8 = 1;
60 constexpr element_t element_type_uint8 = 2;
61 constexpr element_t element_type_int16 = 3;
62 constexpr element_t element_type_uint16 = 4;
63 constexpr element_t element_type_int32 = 5;
64 constexpr element_t element_type_uint32 = 6;
65 constexpr element_t element_type_int64 = 7;
66 constexpr element_t element_type_uint64 = 8;
67 constexpr element_t element_type_float = 9;
68 constexpr element_t element_type_double = 10;
69 constexpr element_t element_type_string = 11;
71 constexpr element_t element_type_user_start = 50;
97 template<
typename _Self, element_t _TypeId,
typename _Data>
100 #ifdef MDDS_UNIT_TEST
101 struct print_block_array
103 void operator() (
const _Data& val)
const
105 std::cout << val <<
" ";
111 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
112 typedef std::deque<_Data> store_type;
114 typedef std::vector<_Data> store_type;
122 template<
typename _Iter>
126 static const element_t block_type = _TypeId;
128 typedef typename store_type::iterator iterator;
129 typedef typename store_type::reverse_iterator reverse_iterator;
130 typedef typename store_type::const_iterator const_iterator;
131 typedef typename store_type::const_reverse_iterator const_reverse_iterator;
132 typedef _Data value_type;
134 bool operator== (
const _Self& r)
const
136 return m_array == r.m_array;
139 bool operator!= (
const _Self& r)
const
141 return !operator==(r);
144 static const value_type& at(
const base_element_block& block,
typename store_type::size_type pos)
146 return get(block).m_array.at(pos);
151 return get(block).m_array.at(pos);
156 return get(block).m_array.data();
161 return get(block).m_array.size();
166 return get(block).m_array.begin();
171 return get(block).m_array.end();
176 return get(block).m_array.begin();
181 return get(block).m_array.end();
186 return get(block).m_array.begin();
191 return get(block).m_array.end();
196 return get(block).m_array.rbegin();
201 return get(block).m_array.rend();
206 return get(block).m_array.rbegin();
211 return get(block).m_array.rend();
216 return get(block).m_array.rbegin();
221 return get(block).m_array.rend();
226 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
229 std::ostringstream os;
230 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" <<
get_block_type(block);
234 return static_cast<_Self&
>(block);
239 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
242 std::ostringstream os;
243 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" <<
get_block_type(block);
247 return static_cast<const _Self&
>(block);
252 get(blk).m_array[pos] = val;
257 val = get(blk).m_array[pos];
262 return get(blk).m_array[pos];
267 get(blk).m_array.push_back(val);
272 store_type& blk2 = get(blk).m_array;
273 blk2.insert(blk2.begin(), val);
276 static _Self* create_block(
size_t init_size)
278 return new _Self(init_size);
283 delete static_cast<const _Self*
>(p);
288 store_type& st = get(blk).m_array;
293 if (new_size < (st.capacity() / 2))
297 #ifdef MDDS_UNIT_TEST
300 const store_type& blk2 = get(blk).m_array;
301 std::for_each(blk2.begin(), blk2.end(), print_block_array());
302 std::cout << std::endl;
310 store_type& blk2 = get(blk).m_array;
311 blk2.erase(blk2.begin()+pos);
316 store_type& blk2 = get(blk).m_array;
317 blk2.erase(blk2.begin()+pos, blk2.begin()+pos+size);
322 store_type& d = get(dest).m_array;
323 const store_type& s = get(src).m_array;
324 d.insert(d.end(), s.begin(), s.end());
327 static void append_values_from_block(
330 store_type& d = get(dest).m_array;
331 const store_type& s = get(src).m_array;
332 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
333 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
334 d.reserve(d.size() + len);
336 d.insert(d.end(), its.first, its.second);
339 static void assign_values_from_block(
342 store_type& d = get(dest).m_array;
343 const store_type& s = get(src).m_array;
344 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
345 d.assign(its.first, its.second);
348 static void prepend_values_from_block(
351 store_type& d = get(dest).m_array;
352 const store_type& s = get(src).m_array;
353 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
354 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
355 d.reserve(d.size() + len);
357 d.insert(d.begin(), its.first, its.second);
360 static void swap_values(
363 store_type& st1 = get(blk1).m_array;
364 store_type& st2 = get(blk2).m_array;
365 assert(pos1 + len <= st1.size());
366 assert(pos2 + len <= st2.size());
368 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
369 std::advance(it1, pos1);
370 std::advance(it2, pos2);
371 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
373 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
374 std::swap(*it1, *it2);
376 value_type v1 = *it1, v2 = *it2;
383 template<
typename _Iter>
384 static void set_values(
387 store_type& d = get(block).m_array;
388 typename store_type::iterator it_dest = d.begin();
389 std::advance(it_dest, pos);
390 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
394 template<
typename _Iter>
395 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
397 store_type& d = get(block).m_array;
398 typename store_type::iterator it = d.end();
399 d.insert(it, it_begin, it_end);
402 template<
typename _Iter>
403 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
405 store_type& d = get(block).m_array;
406 d.insert(d.begin(), it_begin, it_end);
409 template<
typename _Iter>
410 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
412 store_type& d = get(dest).m_array;
413 d.assign(it_begin, it_end);
416 template<
typename _Iter>
417 static void insert_values(
420 store_type& blk = get(block).m_array;
421 blk.insert(blk.begin()+pos, it_begin, it_end);
426 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
429 const store_type& blk = get(block).m_array;
430 return blk.capacity();
436 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
437 get(block).m_array.shrink_to_fit();
442 static std::pair<const_iterator,const_iterator>
443 get_iterator_pair(
const store_type& array,
size_t begin_pos,
size_t len)
445 assert(begin_pos + len <= array.size());
446 const_iterator it = array.begin();
447 std::advance(it, begin_pos);
448 const_iterator it_end = it;
449 std::advance(it_end, len);
450 return std::pair<const_iterator,const_iterator>(it, it_end);
454 template<
typename _Self, element_t _TypeId,
typename _Data>
463 template<
typename _Iter>
467 using base_type::get;
472 return new _Self(get(blk));
476 template<
typename _Self, element_t _TypeId,
typename _Data>
485 template<
typename _Iter>
514 template<element_t _TypeId,
typename _Data>
524 template<
typename _Iter>
527 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
532 template<
typename _Iter>
533 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
548 template<element_t _TypeId,
typename _Data>
554 using base_type::get;
555 using base_type::set_value;
556 using base_type::m_array;
562 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE
563 m_array.reserve(r.m_array.size());
565 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
566 for (; it != it_end; ++it)
567 m_array.push_back(
new _Data(**it));
570 template<
typename _Iter>
575 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
578 static self_type* create_block_with_value(
size_t init_size, _Data* val)
582 throw general_error(
"You can't create a managed block with initial value.");
584 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
586 set_value(*blk, 0, val);
588 return blk.release();
591 template<
typename _Iter>
592 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
600 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
601 typename managed_element_block::store_type::iterator it_end = it + len;
602 std::for_each(it, it_end, std::default_delete<_Data>());
606 template<element_t _TypeId,
typename _Data>
612 using base_type::get;
613 using base_type::m_array;
614 using base_type::set_value;
619 template<
typename _Iter>
624 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
627 static self_type* create_block_with_value(
size_t init_size, _Data* val)
631 throw general_error(
"You can't create a managed block with initial value.");
633 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
635 set_value(*blk, 0, val);
637 return blk.release();
640 template<
typename _Iter>
641 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
649 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
650 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
651 std::for_each(it, it_end, std::default_delete<_Data>());