28 #ifndef MDDS_MULTI_TYPE_VECTOR_TYPES_HPP 29 #define MDDS_MULTI_TYPE_VECTOR_TYPES_HPP 31 #include "default_deleter.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 typedef int element_t;
56 const element_t element_type_empty = -1;
58 const element_t element_type_numeric = 0;
59 const element_t element_type_string = 1;
60 const element_t element_type_short = 2;
61 const element_t element_type_ushort = 3;
62 const element_t element_type_int = 4;
63 const element_t element_type_uint = 5;
64 const element_t element_type_long = 6;
65 const element_t element_type_ulong = 7;
66 const element_t element_type_boolean = 8;
67 const element_t element_type_char = 9;
68 const element_t element_type_uchar = 10;
70 const 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.begin();
161 return get(block).m_array.end();
166 return get(block).m_array.begin();
171 return get(block).m_array.end();
176 return get(block).m_array.rbegin();
181 return get(block).m_array.rend();
186 return get(block).m_array.rbegin();
191 return get(block).m_array.rend();
196 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 197 if (get_block_type(block) != _TypeId)
199 std::ostringstream os;
200 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
204 return static_cast<_Self&
>(block);
209 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 210 if (get_block_type(block) != _TypeId)
212 std::ostringstream os;
213 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
217 return static_cast<const _Self&
>(block);
222 get(blk).m_array[pos] = val;
227 val =
get(blk).m_array[pos];
232 get(blk).m_array.push_back(val);
237 store_type& blk2 =
get(blk).m_array;
238 blk2.insert(blk2.begin(), val);
241 static _Self* create_block(
size_t init_size)
243 return new _Self(init_size);
248 delete static_cast<const _Self*
>(p);
253 store_type& st =
get(blk).m_array;
258 if (new_size < (st.capacity() / 2))
262 #ifdef MDDS_UNIT_TEST 265 const store_type& blk2 =
get(blk).m_array;
266 std::for_each(blk2.begin(), blk2.end(), print_block_array());
267 std::cout << std::endl;
275 store_type& blk2 =
get(blk).m_array;
276 blk2.erase(blk2.begin()+pos);
281 store_type& blk2 =
get(blk).m_array;
282 blk2.erase(blk2.begin()+pos, blk2.begin()+pos+size);
287 store_type& d =
get(dest).m_array;
288 const store_type& s =
get(src).m_array;
289 d.insert(d.end(), s.begin(), s.end());
292 static void append_values_from_block(
295 store_type& d =
get(dest).m_array;
296 const store_type& s =
get(src).m_array;
297 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
298 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 299 d.reserve(d.size() + len);
301 d.insert(d.end(), its.first, its.second);
304 static void assign_values_from_block(
307 store_type& d =
get(dest).m_array;
308 const store_type& s =
get(src).m_array;
309 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
310 d.assign(its.first, its.second);
313 static void prepend_values_from_block(
316 store_type& d =
get(dest).m_array;
317 const store_type& s =
get(src).m_array;
318 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
319 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 320 d.reserve(d.size() + len);
322 d.insert(d.begin(), its.first, its.second);
325 static void swap_values(
328 store_type& st1 =
get(blk1).m_array;
329 store_type& st2 =
get(blk2).m_array;
330 assert(pos1 + len <= st1.size());
331 assert(pos2 + len <= st2.size());
333 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
334 std::advance(it1, pos1);
335 std::advance(it2, pos2);
336 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
338 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 339 std::swap(*it1, *it2);
341 value_type v1 = *it1, v2 = *it2;
348 template<
typename _Iter>
349 static void set_values(
352 store_type& d =
get(block).m_array;
353 typename store_type::iterator it_dest = d.begin();
354 std::advance(it_dest, pos);
355 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
359 template<
typename _Iter>
360 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
362 store_type& d =
get(block).m_array;
363 typename store_type::iterator it = d.end();
364 d.insert(it, it_begin, it_end);
367 template<
typename _Iter>
368 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
370 store_type& d =
get(block).m_array;
371 d.insert(d.begin(), it_begin, it_end);
374 template<
typename _Iter>
375 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
377 store_type& d =
get(dest).m_array;
378 d.assign(it_begin, it_end);
381 template<
typename _Iter>
382 static void insert_values(
385 store_type& blk =
get(block).m_array;
386 blk.insert(blk.begin()+pos, it_begin, it_end);
391 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 394 const store_type& blk =
get(block).m_array;
395 return blk.capacity();
401 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 402 get(block).m_array.shrink_to_fit();
407 static std::pair<const_iterator,const_iterator>
408 get_iterator_pair(
const store_type& array,
size_t begin_pos,
size_t len)
410 assert(begin_pos + len <= array.size());
411 const_iterator it = array.begin();
412 std::advance(it, begin_pos);
413 const_iterator it_end = it;
414 std::advance(it_end, len);
415 return std::pair<const_iterator,const_iterator>(it, it_end);
419 template<
typename _Self, element_t _TypeId,
typename _Data>
428 template<
typename _Iter>
432 using base_type::get;
437 return new _Self(
get(blk));
441 template<
typename _Self, element_t _TypeId,
typename _Data>
450 template<
typename _Iter>
479 template<element_t _TypeId,
typename _Data>
489 template<
typename _Iter>
492 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
494 return new self_type(init_size, val);
497 template<
typename _Iter>
498 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
500 return new self_type(it_begin, it_end);
513 template<element_t _TypeId,
typename _Data>
519 using base_type::get;
520 using base_type::set_value;
521 using base_type::m_array;
527 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 528 m_array.reserve(r.m_array.size());
530 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
531 for (; it != it_end; ++it)
532 m_array.push_back(
new _Data(**it));
535 template<
typename _Iter>
543 static self_type* create_block_with_value(
size_t init_size, _Data* val)
547 throw general_error(
"You can't create a managed block with initial value.");
549 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
551 set_value(*blk, 0, val);
553 return blk.release();
556 template<
typename _Iter>
557 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
559 return new self_type(it_begin, it_end);
565 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
566 typename managed_element_block::store_type::iterator it_end = it + len;
571 template<element_t _TypeId,
typename _Data>
577 using base_type::get;
578 using base_type::m_array;
579 using base_type::set_value;
584 template<
typename _Iter>
592 static self_type* create_block_with_value(
size_t init_size, _Data* val)
596 throw general_error(
"You can't create a managed block with initial value.");
598 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
600 set_value(*blk, 0, val);
602 return blk.release();
605 template<
typename _Iter>
606 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
608 return new self_type(it_begin, it_end);
614 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
615 typename noncopyable_managed_element_block::store_type::iterator it_end = it + len;
Definition: multi_type_vector_types.hpp:75
Definition: multi_type_vector_types.hpp:572
Definition: multi_type_vector_types.hpp:88
Definition: multi_type_vector_types.hpp:420
Definition: multi_type_vector_types.hpp:514
Definition: default_deleter.hpp:36
Definition: multi_type_vector_types.hpp:98
Definition: multi_type_vector_types.hpp:480
Definition: global.hpp:58
Definition: default_deleter.hpp:33
Definition: multi_type_vector_types.hpp:442