packed_segment.hpp 9.7 KB

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