28 #ifndef MDDS_MULTI_TYPE_VECTOR_TYPES_HPP 29 #define MDDS_MULTI_TYPE_VECTOR_TYPES_HPP 37 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 43 #if defined(MDDS_UNIT_TEST) || defined (MDDS_MULTI_TYPE_VECTOR_DEBUG) 51 namespace mdds {
namespace mtv {
53 typedef int element_t;
55 const element_t element_type_empty = -1;
57 const element_t element_type_numeric = 0;
58 const element_t element_type_string = 1;
59 const element_t element_type_short = 2;
60 const element_t element_type_ushort = 3;
61 const element_t element_type_int = 4;
62 const element_t element_type_uint = 5;
63 const element_t element_type_long = 6;
64 const element_t element_type_ulong = 7;
65 const element_t element_type_boolean = 8;
66 const element_t element_type_char = 9;
67 const element_t element_type_uchar = 10;
69 const element_t element_type_user_start = 50;
95 template<
typename _Self, element_t _TypeId,
typename _Data>
99 struct print_block_array
101 void operator() (
const _Data& val)
const 103 std::cout << val <<
" ";
109 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 110 typedef std::deque<_Data> store_type;
112 typedef std::vector<_Data> store_type;
120 template<
typename _Iter>
124 static const element_t block_type = _TypeId;
126 typedef typename store_type::iterator iterator;
127 typedef typename store_type::reverse_iterator reverse_iterator;
128 typedef typename store_type::const_iterator const_iterator;
129 typedef typename store_type::const_reverse_iterator const_reverse_iterator;
130 typedef _Data value_type;
132 bool operator== (
const _Self& r)
const 134 return m_array == r.m_array;
137 bool operator!= (
const _Self& r)
const 139 return !operator==(r);
142 static const value_type& at(
const base_element_block& block,
typename store_type::size_type pos)
144 return get(block).m_array.at(pos);
149 return get(block).m_array.at(pos);
154 return get(block).m_array.data();
159 return get(block).m_array.size();
164 return get(block).m_array.begin();
169 return get(block).m_array.end();
174 return get(block).m_array.begin();
179 return get(block).m_array.end();
184 return get(block).m_array.begin();
189 return get(block).m_array.end();
194 return get(block).m_array.rbegin();
199 return get(block).m_array.rend();
204 return get(block).m_array.rbegin();
209 return get(block).m_array.rend();
214 return get(block).m_array.rbegin();
219 return get(block).m_array.rend();
224 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 225 if (get_block_type(block) != _TypeId)
227 std::ostringstream os;
228 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
232 return static_cast<_Self&
>(block);
237 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 238 if (get_block_type(block) != _TypeId)
240 std::ostringstream os;
241 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
245 return static_cast<const _Self&
>(block);
250 get(blk).m_array[pos] = val;
255 val =
get(blk).m_array[pos];
260 return get(blk).m_array[pos];
265 get(blk).m_array.push_back(val);
270 store_type& blk2 =
get(blk).m_array;
271 blk2.insert(blk2.begin(), val);
274 static _Self* create_block(
size_t init_size)
276 return new _Self(init_size);
281 delete static_cast<const _Self*
>(p);
286 store_type& st =
get(blk).m_array;
291 if (new_size < (st.capacity() / 2))
295 #ifdef MDDS_UNIT_TEST 298 const store_type& blk2 =
get(blk).m_array;
299 std::for_each(blk2.begin(), blk2.end(), print_block_array());
300 std::cout << std::endl;
308 store_type& blk2 =
get(blk).m_array;
309 blk2.erase(blk2.begin()+pos);
314 store_type& blk2 =
get(blk).m_array;
315 blk2.erase(blk2.begin()+pos, blk2.begin()+pos+size);
320 store_type& d =
get(dest).m_array;
321 const store_type& s =
get(src).m_array;
322 d.insert(d.end(), s.begin(), s.end());
325 static void append_values_from_block(
328 store_type& d =
get(dest).m_array;
329 const store_type& s =
get(src).m_array;
330 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
331 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 332 d.reserve(d.size() + len);
334 d.insert(d.end(), its.first, its.second);
337 static void assign_values_from_block(
340 store_type& d =
get(dest).m_array;
341 const store_type& s =
get(src).m_array;
342 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
343 d.assign(its.first, its.second);
346 static void prepend_values_from_block(
349 store_type& d =
get(dest).m_array;
350 const store_type& s =
get(src).m_array;
351 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
352 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 353 d.reserve(d.size() + len);
355 d.insert(d.begin(), its.first, its.second);
358 static void swap_values(
361 store_type& st1 =
get(blk1).m_array;
362 store_type& st2 =
get(blk2).m_array;
363 assert(pos1 + len <= st1.size());
364 assert(pos2 + len <= st2.size());
366 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
367 std::advance(it1, pos1);
368 std::advance(it2, pos2);
369 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
371 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 372 std::swap(*it1, *it2);
374 value_type v1 = *it1, v2 = *it2;
381 template<
typename _Iter>
382 static void set_values(
385 store_type& d =
get(block).m_array;
386 typename store_type::iterator it_dest = d.begin();
387 std::advance(it_dest, pos);
388 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
392 template<
typename _Iter>
393 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
395 store_type& d =
get(block).m_array;
396 typename store_type::iterator it = d.end();
397 d.insert(it, it_begin, it_end);
400 template<
typename _Iter>
401 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
403 store_type& d =
get(block).m_array;
404 d.insert(d.begin(), it_begin, it_end);
407 template<
typename _Iter>
408 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
410 store_type& d =
get(dest).m_array;
411 d.assign(it_begin, it_end);
414 template<
typename _Iter>
415 static void insert_values(
418 store_type& blk =
get(block).m_array;
419 blk.insert(blk.begin()+pos, it_begin, it_end);
424 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 427 const store_type& blk =
get(block).m_array;
428 return blk.capacity();
434 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 435 get(block).m_array.shrink_to_fit();
440 static std::pair<const_iterator,const_iterator>
441 get_iterator_pair(
const store_type& array,
size_t begin_pos,
size_t len)
443 assert(begin_pos + len <= array.size());
444 const_iterator it = array.begin();
445 std::advance(it, begin_pos);
446 const_iterator it_end = it;
447 std::advance(it_end, len);
448 return std::pair<const_iterator,const_iterator>(it, it_end);
452 template<
typename _Self, element_t _TypeId,
typename _Data>
461 template<
typename _Iter>
465 using base_type::get;
470 return new _Self(
get(blk));
474 template<
typename _Self, element_t _TypeId,
typename _Data>
483 template<
typename _Iter>
512 template<element_t _TypeId,
typename _Data>
522 template<
typename _Iter>
525 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
527 return new self_type(init_size, val);
530 template<
typename _Iter>
531 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
533 return new self_type(it_begin, it_end);
546 template<element_t _TypeId,
typename _Data>
552 using base_type::get;
553 using base_type::set_value;
554 using base_type::m_array;
560 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 561 m_array.reserve(r.m_array.size());
563 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
564 for (; it != it_end; ++it)
565 m_array.push_back(
new _Data(**it));
568 template<
typename _Iter>
573 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
576 static self_type* create_block_with_value(
size_t init_size, _Data* val)
580 throw general_error(
"You can't create a managed block with initial value.");
582 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
584 set_value(*blk, 0, val);
586 return blk.release();
589 template<
typename _Iter>
590 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
592 return new self_type(it_begin, it_end);
598 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
599 typename managed_element_block::store_type::iterator it_end = it + len;
600 std::for_each(it, it_end, std::default_delete<_Data>());
604 template<element_t _TypeId,
typename _Data>
610 using base_type::get;
611 using base_type::m_array;
612 using base_type::set_value;
617 template<
typename _Iter>
622 std::for_each(m_array.begin(), m_array.end(), std::default_delete<_Data>());
625 static self_type* create_block_with_value(
size_t init_size, _Data* val)
629 throw general_error(
"You can't create a managed block with initial value.");
631 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
633 set_value(*blk, 0, val);
635 return blk.release();
638 template<
typename _Iter>
639 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
641 return new self_type(it_begin, it_end);
647 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
648 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
649 std::for_each(it, it_end, std::default_delete<_Data>());
Definition: multi_type_vector_types.hpp:74
Definition: multi_type_vector_types.hpp:605
Definition: multi_type_vector_types.hpp:87
Definition: multi_type_vector_types.hpp:453
Definition: multi_type_vector_types.hpp:547
Definition: multi_type_vector_types.hpp:96
Definition: multi_type_vector_types.hpp:513
Definition: global.hpp:58
Definition: flat_segment_tree.hpp:46
Definition: multi_type_vector_types.hpp:475