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.size();
161 return get(block).m_array.begin();
166 return get(block).m_array.end();
171 return get(block).m_array.begin();
176 return get(block).m_array.end();
181 return get(block).m_array.rbegin();
186 return get(block).m_array.rend();
191 return get(block).m_array.rbegin();
196 return get(block).m_array.rend();
201 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 202 if (get_block_type(block) != _TypeId)
204 std::ostringstream os;
205 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
209 return static_cast<_Self&
>(block);
214 #ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG 215 if (get_block_type(block) != _TypeId)
217 std::ostringstream os;
218 os <<
"incorrect block type: expected block type=" << _TypeId <<
", passed block type=" << get_block_type(block);
222 return static_cast<const _Self&
>(block);
227 get(blk).m_array[pos] = val;
232 val =
get(blk).m_array[pos];
237 return get(blk).m_array[pos];
242 get(blk).m_array.push_back(val);
247 store_type& blk2 =
get(blk).m_array;
248 blk2.insert(blk2.begin(), val);
251 static _Self* create_block(
size_t init_size)
253 return new _Self(init_size);
258 delete static_cast<const _Self*
>(p);
263 store_type& st =
get(blk).m_array;
268 if (new_size < (st.capacity() / 2))
272 #ifdef MDDS_UNIT_TEST 275 const store_type& blk2 =
get(blk).m_array;
276 std::for_each(blk2.begin(), blk2.end(), print_block_array());
277 std::cout << std::endl;
285 store_type& blk2 =
get(blk).m_array;
286 blk2.erase(blk2.begin()+pos);
291 store_type& blk2 =
get(blk).m_array;
292 blk2.erase(blk2.begin()+pos, blk2.begin()+pos+size);
297 store_type& d =
get(dest).m_array;
298 const store_type& s =
get(src).m_array;
299 d.insert(d.end(), s.begin(), s.end());
302 static void append_values_from_block(
305 store_type& d =
get(dest).m_array;
306 const store_type& s =
get(src).m_array;
307 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
308 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 309 d.reserve(d.size() + len);
311 d.insert(d.end(), its.first, its.second);
314 static void assign_values_from_block(
317 store_type& d =
get(dest).m_array;
318 const store_type& s =
get(src).m_array;
319 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
320 d.assign(its.first, its.second);
323 static void prepend_values_from_block(
326 store_type& d =
get(dest).m_array;
327 const store_type& s =
get(src).m_array;
328 std::pair<const_iterator,const_iterator> its = get_iterator_pair(s, begin_pos, len);
329 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 330 d.reserve(d.size() + len);
332 d.insert(d.begin(), its.first, its.second);
335 static void swap_values(
338 store_type& st1 =
get(blk1).m_array;
339 store_type& st2 =
get(blk2).m_array;
340 assert(pos1 + len <= st1.size());
341 assert(pos2 + len <= st2.size());
343 typename store_type::iterator it1 = st1.begin(), it2 = st2.begin();
344 std::advance(it1, pos1);
345 std::advance(it2, pos2);
346 for (
size_t i = 0; i < len; ++i, ++it1, ++it2)
348 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 349 std::swap(*it1, *it2);
351 value_type v1 = *it1, v2 = *it2;
358 template<
typename _Iter>
359 static void set_values(
362 store_type& d =
get(block).m_array;
363 typename store_type::iterator it_dest = d.begin();
364 std::advance(it_dest, pos);
365 for (_Iter it = it_begin; it != it_end; ++it, ++it_dest)
369 template<
typename _Iter>
370 static void append_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
372 store_type& d =
get(block).m_array;
373 typename store_type::iterator it = d.end();
374 d.insert(it, it_begin, it_end);
377 template<
typename _Iter>
378 static void prepend_values(
base_element_block& block,
const _Iter& it_begin,
const _Iter& it_end)
380 store_type& d =
get(block).m_array;
381 d.insert(d.begin(), it_begin, it_end);
384 template<
typename _Iter>
385 static void assign_values(
base_element_block& dest,
const _Iter& it_begin,
const _Iter& it_end)
387 store_type& d =
get(dest).m_array;
388 d.assign(it_begin, it_end);
391 template<
typename _Iter>
392 static void insert_values(
395 store_type& blk =
get(block).m_array;
396 blk.insert(blk.begin()+pos, it_begin, it_end);
401 #ifdef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 404 const store_type& blk =
get(block).m_array;
405 return blk.capacity();
411 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 412 get(block).m_array.shrink_to_fit();
417 static std::pair<const_iterator,const_iterator>
418 get_iterator_pair(
const store_type& array,
size_t begin_pos,
size_t len)
420 assert(begin_pos + len <= array.size());
421 const_iterator it = array.begin();
422 std::advance(it, begin_pos);
423 const_iterator it_end = it;
424 std::advance(it_end, len);
425 return std::pair<const_iterator,const_iterator>(it, it_end);
429 template<
typename _Self, element_t _TypeId,
typename _Data>
438 template<
typename _Iter>
442 using base_type::get;
447 return new _Self(
get(blk));
451 template<
typename _Self, element_t _TypeId,
typename _Data>
460 template<
typename _Iter>
489 template<element_t _TypeId,
typename _Data>
499 template<
typename _Iter>
502 static self_type* create_block_with_value(
size_t init_size,
const _Data& val)
504 return new self_type(init_size, val);
507 template<
typename _Iter>
508 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
510 return new self_type(it_begin, it_end);
523 template<element_t _TypeId,
typename _Data>
529 using base_type::get;
530 using base_type::set_value;
531 using base_type::m_array;
537 #ifndef MDDS_MULTI_TYPE_VECTOR_USE_DEQUE 538 m_array.reserve(r.m_array.size());
540 typename managed_element_block::store_type::const_iterator it = r.m_array.begin(), it_end = r.m_array.end();
541 for (; it != it_end; ++it)
542 m_array.push_back(
new _Data(**it));
545 template<
typename _Iter>
553 static self_type* create_block_with_value(
size_t init_size, _Data* val)
557 throw general_error(
"You can't create a managed block with initial value.");
559 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
561 set_value(*blk, 0, val);
563 return blk.release();
566 template<
typename _Iter>
567 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
569 return new self_type(it_begin, it_end);
575 typename managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
576 typename managed_element_block::store_type::iterator it_end = it + len;
581 template<element_t _TypeId,
typename _Data>
587 using base_type::get;
588 using base_type::m_array;
589 using base_type::set_value;
594 template<
typename _Iter>
602 static self_type* create_block_with_value(
size_t init_size, _Data* val)
606 throw general_error(
"You can't create a managed block with initial value.");
608 std::unique_ptr<self_type> blk = make_unique<self_type>(init_size);
610 set_value(*blk, 0, val);
612 return blk.release();
615 template<
typename _Iter>
616 static self_type* create_block_with_values(
const _Iter& it_begin,
const _Iter& it_end)
618 return new self_type(it_begin, it_end);
624 typename noncopyable_managed_element_block::store_type::iterator it = blk.m_array.begin() + pos;
625 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:582
Definition: multi_type_vector_types.hpp:88
Definition: multi_type_vector_types.hpp:430
Definition: multi_type_vector_types.hpp:524
Definition: default_deleter.hpp:36
Definition: multi_type_vector_types.hpp:98
Definition: multi_type_vector_types.hpp:490
Definition: global.hpp:58
Definition: default_deleter.hpp:33
Definition: multi_type_vector_types.hpp:452