multiallocation_chain.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP
  11. #define BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/container_fwd.hpp>
  22. // container/detail
  23. #include <boost/move/detail/to_raw_pointer.hpp>
  24. #include <boost/container/detail/transform_iterator.hpp>
  25. #include <boost/container/detail/type_traits.hpp>
  26. // intrusive
  27. #include <boost/intrusive/slist.hpp>
  28. #include <boost/intrusive/pointer_traits.hpp>
  29. // move
  30. #include <boost/move/utility_core.hpp>
  31. namespace boost {
  32. namespace container {
  33. namespace dtl {
  34. template<class VoidPointer>
  35. class basic_multiallocation_chain
  36. {
  37. private:
  38. typedef bi::slist_base_hook<bi::void_pointer<VoidPointer>
  39. ,bi::link_mode<bi::normal_link>
  40. > node;
  41. typedef typename boost::intrusive::pointer_traits
  42. <VoidPointer>::template rebind_pointer<char>::type char_ptr;
  43. typedef typename boost::intrusive::
  44. pointer_traits<char_ptr>::difference_type difference_type;
  45. typedef bi::slist< node
  46. , bi::linear<true>
  47. , bi::cache_last<true>
  48. , bi::size_type<typename boost::container::dtl::make_unsigned<difference_type>::type>
  49. > slist_impl_t;
  50. slist_impl_t slist_impl_;
  51. typedef typename boost::intrusive::pointer_traits
  52. <VoidPointer>::template rebind_pointer<node>::type node_ptr;
  53. typedef typename boost::intrusive::
  54. pointer_traits<node_ptr> node_ptr_traits;
  55. static node & to_node(const VoidPointer &p)
  56. { return *static_cast<node*>(static_cast<void*>(boost::movelib::to_raw_pointer(p))); }
  57. static VoidPointer from_node(node &n)
  58. { return node_ptr_traits::pointer_to(n); }
  59. static node_ptr to_node_ptr(const VoidPointer &p)
  60. { return node_ptr_traits::static_cast_from(p); }
  61. BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_multiallocation_chain)
  62. public:
  63. typedef VoidPointer void_pointer;
  64. typedef typename slist_impl_t::iterator iterator;
  65. typedef typename slist_impl_t::size_type size_type;
  66. basic_multiallocation_chain()
  67. : slist_impl_()
  68. {}
  69. basic_multiallocation_chain(const void_pointer &b, const void_pointer &before_e, size_type n)
  70. : slist_impl_(to_node_ptr(b), to_node_ptr(before_e), n)
  71. {}
  72. basic_multiallocation_chain(BOOST_RV_REF(basic_multiallocation_chain) other)
  73. : slist_impl_(::boost::move(other.slist_impl_))
  74. {}
  75. basic_multiallocation_chain& operator=(BOOST_RV_REF(basic_multiallocation_chain) other)
  76. {
  77. slist_impl_ = ::boost::move(other.slist_impl_);
  78. return *this;
  79. }
  80. bool empty() const
  81. { return slist_impl_.empty(); }
  82. size_type size() const
  83. { return slist_impl_.size(); }
  84. iterator before_begin()
  85. { return slist_impl_.before_begin(); }
  86. iterator begin()
  87. { return slist_impl_.begin(); }
  88. iterator end()
  89. { return slist_impl_.end(); }
  90. iterator last()
  91. { return slist_impl_.last(); }
  92. void clear()
  93. { slist_impl_.clear(); }
  94. iterator insert_after(iterator it, void_pointer m)
  95. { return slist_impl_.insert_after(it, to_node(m)); }
  96. void push_front(const void_pointer &m)
  97. { return slist_impl_.push_front(to_node(m)); }
  98. void push_back(const void_pointer &m)
  99. { return slist_impl_.push_back(to_node(m)); }
  100. void_pointer pop_front()
  101. {
  102. node & n = slist_impl_.front();
  103. void_pointer ret = from_node(n);
  104. slist_impl_.pop_front();
  105. return ret;
  106. }
  107. void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
  108. { slist_impl_.splice_after(after_this, x.slist_impl_, before_b, before_e, n); }
  109. void splice_after(iterator after_this, basic_multiallocation_chain &x)
  110. { slist_impl_.splice_after(after_this, x.slist_impl_); }
  111. void erase_after(iterator before_b, iterator e, size_type n)
  112. { slist_impl_.erase_after(before_b, e, n); }
  113. void_pointer incorporate_after(iterator after_this, const void_pointer &b, size_type unit_bytes, size_type num_units)
  114. {
  115. typedef typename boost::intrusive::pointer_traits<char_ptr> char_pointer_traits;
  116. char_ptr elem = char_pointer_traits::static_cast_from(b);
  117. if(num_units){
  118. char_ptr prev_elem = elem;
  119. elem += unit_bytes;
  120. for(size_type i = 0; i != num_units-1; ++i, elem += unit_bytes){
  121. ::new (boost::movelib::to_raw_pointer(prev_elem)) void_pointer(elem);
  122. prev_elem = elem;
  123. }
  124. slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(prev_elem), num_units);
  125. }
  126. return elem;
  127. }
  128. void incorporate_after(iterator after_this, void_pointer b, void_pointer before_e, size_type n)
  129. { slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(before_e), n); }
  130. void swap(basic_multiallocation_chain &x)
  131. { slist_impl_.swap(x.slist_impl_); }
  132. static iterator iterator_to(const void_pointer &p)
  133. { return slist_impl_t::s_iterator_to(to_node(p)); }
  134. std::pair<void_pointer, void_pointer> extract_data()
  135. {
  136. if(BOOST_LIKELY(!slist_impl_.empty())){
  137. std::pair<void_pointer, void_pointer> ret
  138. (slist_impl_.begin().operator->()
  139. ,slist_impl_.last().operator->());
  140. slist_impl_.clear();
  141. return ret;
  142. }
  143. else {
  144. return std::pair<void_pointer, void_pointer>();
  145. }
  146. }
  147. };
  148. template<class T>
  149. struct cast_functor
  150. {
  151. typedef typename dtl::add_reference<T>::type result_type;
  152. template<class U>
  153. result_type operator()(U &ptr) const
  154. { return *static_cast<T*>(static_cast<void*>(&ptr)); }
  155. };
  156. template<class MultiallocationChain, class T>
  157. class transform_multiallocation_chain
  158. : public MultiallocationChain
  159. {
  160. private:
  161. BOOST_MOVABLE_BUT_NOT_COPYABLE(transform_multiallocation_chain)
  162. //transform_multiallocation_chain(const transform_multiallocation_chain &);
  163. //transform_multiallocation_chain & operator=(const transform_multiallocation_chain &);
  164. typedef typename MultiallocationChain::void_pointer void_pointer;
  165. typedef typename boost::intrusive::pointer_traits
  166. <void_pointer> void_pointer_traits;
  167. typedef typename void_pointer_traits::template
  168. rebind_pointer<T>::type pointer;
  169. typedef typename boost::intrusive::pointer_traits
  170. <pointer> pointer_traits;
  171. static pointer cast(const void_pointer &p)
  172. { return pointer_traits::static_cast_from(p); }
  173. public:
  174. typedef transform_iterator
  175. < typename MultiallocationChain::iterator
  176. , dtl::cast_functor <T> > iterator;
  177. typedef typename MultiallocationChain::size_type size_type;
  178. transform_multiallocation_chain()
  179. : MultiallocationChain()
  180. {}
  181. transform_multiallocation_chain(BOOST_RV_REF(transform_multiallocation_chain) other)
  182. : MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
  183. {}
  184. transform_multiallocation_chain(BOOST_RV_REF(MultiallocationChain) other)
  185. : MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
  186. {}
  187. transform_multiallocation_chain& operator=(BOOST_RV_REF(transform_multiallocation_chain) other)
  188. {
  189. return static_cast<MultiallocationChain&>
  190. (this->MultiallocationChain::operator=(::boost::move(static_cast<MultiallocationChain&>(other))));
  191. }
  192. void push_front(const pointer &mem)
  193. { this->MultiallocationChain::push_front(mem); }
  194. void push_back(const pointer &mem)
  195. { return this->MultiallocationChain::push_back(mem); }
  196. void swap(transform_multiallocation_chain &other_chain)
  197. { this->MultiallocationChain::swap(other_chain); }
  198. void splice_after(iterator after_this, transform_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
  199. { this->MultiallocationChain::splice_after(after_this.base(), x, before_b.base(), before_e.base(), n); }
  200. void incorporate_after(iterator after_this, pointer b, pointer before_e, size_type n)
  201. { this->MultiallocationChain::incorporate_after(after_this.base(), b, before_e, n); }
  202. pointer pop_front()
  203. { return cast(this->MultiallocationChain::pop_front()); }
  204. bool empty() const
  205. { return this->MultiallocationChain::empty(); }
  206. iterator before_begin()
  207. { return iterator(this->MultiallocationChain::before_begin()); }
  208. iterator begin()
  209. { return iterator(this->MultiallocationChain::begin()); }
  210. iterator last()
  211. { return iterator(this->MultiallocationChain::last()); }
  212. iterator end()
  213. { return iterator(this->MultiallocationChain::end()); }
  214. size_type size() const
  215. { return this->MultiallocationChain::size(); }
  216. void clear()
  217. { this->MultiallocationChain::clear(); }
  218. iterator insert_after(iterator it, pointer m)
  219. { return iterator(this->MultiallocationChain::insert_after(it.base(), m)); }
  220. static iterator iterator_to(const pointer &p)
  221. { return iterator(MultiallocationChain::iterator_to(p)); }
  222. std::pair<pointer, pointer> extract_data()
  223. {
  224. std::pair<void_pointer, void_pointer> data(this->MultiallocationChain::extract_data());
  225. return std::pair<pointer, pointer>(cast(data.first), cast(data.second));
  226. }
  227. /*
  228. MultiallocationChain &extract_multiallocation_chain()
  229. { return holder_; }*/
  230. };
  231. }}}
  232. // namespace dtl {
  233. // namespace container {
  234. // namespace boost {
  235. #include <boost/container/detail/config_end.hpp>
  236. #endif //BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP