packed_segment.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_PACKED_SEGMENT_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_PACKED_SEGMENT_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/poly_collection/detail/newdelete_allocator.hpp>
  15. #include <boost/poly_collection/detail/segment_backend.hpp>
  16. #include <boost/poly_collection/detail/value_holder.hpp>
  17. #include <memory>
  18. #include <new>
  19. #include <vector>
  20. #include <utility>
  21. namespace boost{
  22. namespace poly_collection{
  23. namespace detail{
  24. /* segment_backend implementation where value_type& and Concrete& actually refer
  25. * to the same stored entity.
  26. *
  27. * Requires:
  28. * - [const_]base_iterator is a stride iterator constructible from
  29. * {value_type*,sizeof(store_value_type)}.
  30. * - Model provides a function value_ptr for
  31. * const Concrete* -> const value_type* conversion.
  32. */
  33. template<typename Model,typename Concrete,typename Allocator>
  34. class packed_segment:public segment_backend<Model>
  35. {
  36. using value_type=typename Model::value_type;
  37. using store_value_type=value_holder<Concrete>;
  38. using store=std::vector<
  39. store_value_type,
  40. value_holder_allocator_adaptor<
  41. typename std::allocator_traits<Allocator>::
  42. template rebind_alloc<store_value_type>
  43. >
  44. >;
  45. using store_iterator=typename store::iterator;
  46. using const_store_iterator=typename store::const_iterator;
  47. using segment_backend=detail::segment_backend<Model>;
  48. using typename segment_backend::segment_backend_unique_ptr;
  49. using typename segment_backend::value_pointer;
  50. using typename segment_backend::const_value_pointer;
  51. using typename segment_backend::base_iterator;
  52. using typename segment_backend::const_base_iterator;
  53. using const_iterator=
  54. typename segment_backend::template const_iterator<Concrete>;
  55. using typename segment_backend::base_sentinel;
  56. using typename segment_backend::range;
  57. using segment_allocator_type=newdelete_allocator_adaptor<
  58. typename std::allocator_traits<Allocator>::
  59. template rebind_alloc<packed_segment>
  60. >;
  61. public:
  62. virtual ~packed_segment()=default;
  63. virtual segment_backend_unique_ptr copy()const
  64. {
  65. return new_(s.get_allocator(),store{s});
  66. }
  67. virtual segment_backend_unique_ptr empty_copy()const
  68. {
  69. return new_(s.get_allocator(),s.get_allocator());
  70. }
  71. virtual bool equal(const segment_backend& x)const
  72. {
  73. return s==static_cast<const packed_segment&>(x).s;
  74. }
  75. virtual base_iterator begin()const noexcept{return nv_begin();}
  76. base_iterator nv_begin()const noexcept
  77. {
  78. return {value_ptr(s.data()),sizeof(store_value_type)};
  79. }
  80. virtual base_iterator end()const noexcept{return nv_end();}
  81. base_iterator nv_end()const noexcept
  82. {
  83. return {value_ptr(s.data()+s.size()),sizeof(store_value_type)};
  84. }
  85. virtual bool empty()const noexcept{return nv_empty();}
  86. bool nv_empty()const noexcept{return s.empty();}
  87. virtual std::size_t size()const noexcept{return nv_size();}
  88. std::size_t nv_size()const noexcept{return s.size();}
  89. virtual std::size_t max_size()const noexcept{return nv_max_size();}
  90. std::size_t nv_max_size()const noexcept{return s.max_size();}
  91. virtual std::size_t capacity()const noexcept{return nv_capacity();}
  92. std::size_t nv_capacity()const noexcept{return s.capacity();}
  93. virtual base_sentinel reserve(std::size_t n){return nv_reserve(n);}
  94. base_sentinel nv_reserve(std::size_t n)
  95. {s.reserve(n);return sentinel();}
  96. virtual base_sentinel shrink_to_fit(){return nv_shrink_to_fit();}
  97. base_sentinel nv_shrink_to_fit()
  98. {s.shrink_to_fit();return sentinel();}
  99. template<typename Iterator,typename... Args>
  100. range nv_emplace(Iterator p,Args&&... args)
  101. {
  102. return range_from(
  103. s.emplace(
  104. iterator_from(p),
  105. value_holder_emplacing_ctor,std::forward<Args>(args)...));
  106. }
  107. template<typename... Args>
  108. range nv_emplace_back(Args&&... args)
  109. {
  110. s.emplace_back(value_holder_emplacing_ctor,std::forward<Args>(args)...);
  111. return range_from(s.size()-1);
  112. }
  113. virtual range push_back(const_value_pointer x)
  114. {return nv_push_back(const_concrete_ref(x));}
  115. range nv_push_back(const Concrete& x)
  116. {
  117. s.emplace_back(x);
  118. return range_from(s.size()-1);
  119. }
  120. virtual range push_back_move(value_pointer x)
  121. {return nv_push_back(std::move(concrete_ref(x)));}
  122. range nv_push_back(Concrete&& x)
  123. {
  124. s.emplace_back(std::move(x));
  125. return range_from(s.size()-1);
  126. }
  127. virtual range insert(const_base_iterator p,const_value_pointer x)
  128. {return nv_insert(const_iterator(p),const_concrete_ref(x));}
  129. range nv_insert(const_iterator p,const Concrete& x)
  130. {
  131. return range_from(s.emplace(iterator_from(p),x));
  132. }
  133. virtual range insert_move(const_base_iterator p,value_pointer x)
  134. {return nv_insert(const_iterator(p),std::move(concrete_ref(x)));}
  135. range nv_insert(const_iterator p,Concrete&& x)
  136. {
  137. return range_from(s.emplace(iterator_from(p),std::move(x)));
  138. }
  139. template<typename InputIterator>
  140. range nv_insert(InputIterator first,InputIterator last)
  141. {
  142. return nv_insert(
  143. const_iterator(concrete_ptr(s.data()+s.size())),first,last);
  144. }
  145. template<typename InputIterator>
  146. range nv_insert(const_iterator p,InputIterator first,InputIterator last)
  147. {
  148. #if BOOST_WORKAROUND(BOOST_LIBSTDCXX_VERSION,<40900)
  149. /* std::vector::insert(pos,first,last) returns void rather than iterator */
  150. auto n=const_store_value_type_ptr(p)-s.data();
  151. s.insert(s.begin()+n,first,last);
  152. return range_from(static_cast<std::size_t>(n));
  153. #else
  154. return range_from(s.insert(iterator_from(p),first,last));
  155. #endif
  156. }
  157. virtual range erase(const_base_iterator p)
  158. {return nv_erase(const_iterator(p));}
  159. range nv_erase(const_iterator p)
  160. {
  161. return range_from(s.erase(iterator_from(p)));
  162. }
  163. virtual range erase(const_base_iterator first,const_base_iterator last)
  164. {return nv_erase(const_iterator(first),const_iterator(last));}
  165. range nv_erase(const_iterator first,const_iterator last)
  166. {
  167. return range_from(s.erase(iterator_from(first),iterator_from(last)));
  168. }
  169. virtual range erase_till_end(const_base_iterator first)
  170. {
  171. return range_from(s.erase(iterator_from(first),s.end()));
  172. }
  173. virtual range erase_from_begin(const_base_iterator last)
  174. {
  175. return range_from(s.erase(s.begin(),iterator_from(last)));
  176. }
  177. virtual base_sentinel clear()noexcept{return nv_clear();}
  178. base_sentinel nv_clear()noexcept{s.clear();return sentinel();}
  179. private:
  180. friend Model;
  181. template<typename... Args>
  182. static segment_backend_unique_ptr new_(
  183. segment_allocator_type al,Args&&... args)
  184. {
  185. auto p=std::allocator_traits<segment_allocator_type>::allocate(al,1);
  186. try{
  187. ::new ((void*)p) packed_segment{std::forward<Args>(args)...};
  188. }
  189. catch(...){
  190. std::allocator_traits<segment_allocator_type>::deallocate(al,p,1);
  191. throw;
  192. }
  193. return {p,&delete_};
  194. }
  195. static void delete_(segment_backend* p)
  196. {
  197. auto q=static_cast<packed_segment*>(p);
  198. auto al=segment_allocator_type{q->s.get_allocator()};
  199. q->~packed_segment();
  200. std::allocator_traits<segment_allocator_type>::deallocate(al,q,1);
  201. }
  202. packed_segment(const Allocator& al):s{typename store::allocator_type{al}}{}
  203. packed_segment(store&& s):s{std::move(s)}{}
  204. static Concrete& concrete_ref(value_pointer p)noexcept
  205. {
  206. return *static_cast<Concrete*>(p);
  207. }
  208. static const Concrete& const_concrete_ref(const_value_pointer p)noexcept
  209. {
  210. return *static_cast<const Concrete*>(p);
  211. }
  212. static Concrete* concrete_ptr(store_value_type* p)noexcept
  213. {
  214. return reinterpret_cast<Concrete*>(
  215. static_cast<value_holder_base<Concrete>*>(p));
  216. }
  217. static const Concrete* const_concrete_ptr(const store_value_type* p)noexcept
  218. {
  219. return concrete_ptr(const_cast<store_value_type*>(p));
  220. }
  221. static value_type* value_ptr(const store_value_type* p)noexcept
  222. {
  223. return const_cast<value_type*>(Model::value_ptr(const_concrete_ptr(p)));
  224. }
  225. static const store_value_type* const_store_value_type_ptr(
  226. const Concrete* p)noexcept
  227. {
  228. return static_cast<const value_holder<Concrete>*>(
  229. reinterpret_cast<const value_holder_base<Concrete>*>(p));
  230. }
  231. /* It would have sufficed if iterator_from returned const_store_iterator
  232. * except for the fact that some old versions of libstdc++ claiming to be
  233. * C++11 compliant do not however provide std::vector modifier ops taking
  234. * const_iterator's.
  235. */
  236. store_iterator iterator_from(const_base_iterator p)
  237. {
  238. return iterator_from(static_cast<const_iterator>(p));
  239. }
  240. store_iterator iterator_from(const_iterator p)
  241. {
  242. return s.begin()+(const_store_value_type_ptr(p)-s.data());
  243. }
  244. base_sentinel sentinel()const noexcept
  245. {
  246. return base_iterator{
  247. value_ptr(s.data()+s.size()),
  248. sizeof(store_value_type)
  249. };
  250. }
  251. range range_from(const_store_iterator it)const
  252. {
  253. return {
  254. {value_ptr(s.data()+(it-s.begin())),sizeof(store_value_type)},
  255. sentinel()
  256. };
  257. }
  258. range range_from(std::size_t n)const
  259. {
  260. return {
  261. {value_ptr(s.data()+n),sizeof(store_value_type)},
  262. sentinel()
  263. };
  264. }
  265. store s;
  266. };
  267. } /* namespace poly_collection::detail */
  268. } /* namespace poly_collection */
  269. } /* namespace boost */
  270. #endif