small_vector.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. 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_CONTAINER_SMALL_VECTOR_HPP
  11. #define BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_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. #include <boost/container/vector.hpp>
  23. #include <boost/container/allocator_traits.hpp>
  24. #include <boost/container/new_allocator.hpp> //new_allocator
  25. // container/detail
  26. #include <boost/container/detail/type_traits.hpp>
  27. #include <boost/container/detail/version_type.hpp>
  28. //move
  29. #include <boost/move/adl_move_swap.hpp>
  30. #include <boost/move/iterator.hpp>
  31. //move/detail
  32. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  33. #include <boost/move/detail/fwd_macros.hpp>
  34. #endif
  35. //std
  36. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  37. #include <initializer_list> //for std::initializer_list
  38. #endif
  39. namespace boost {
  40. namespace container {
  41. namespace dtl{
  42. template<class Options>
  43. struct get_small_vector_opt
  44. {
  45. typedef Options type;
  46. };
  47. template<>
  48. struct get_small_vector_opt<void>
  49. {
  50. typedef small_vector_null_opt type;
  51. };
  52. template<class Options>
  53. struct get_vopt_from_svopt
  54. : get_small_vector_opt<Options>::type
  55. {
  56. typedef typename get_small_vector_opt<Options>::type options_t;
  57. typedef vector_opt< typename options_t::growth_factor_type, void> type;
  58. };
  59. template<>
  60. struct get_vopt_from_svopt<void>
  61. {
  62. typedef void type;
  63. };
  64. template <class T, class SecondaryAllocator, class Options>
  65. struct vector_for_small_vector
  66. {
  67. typedef vector
  68. < T
  69. , small_vector_allocator
  70. < T
  71. , typename allocator_traits<typename real_allocator<T, SecondaryAllocator>::type>::template portable_rebind_alloc<void>::type
  72. , Options>
  73. , typename dtl::get_vopt_from_svopt<Options>::type
  74. > type;
  75. };
  76. } //namespace dtl
  77. //! A non-standard allocator used to implement `small_vector`.
  78. //! Users should never use it directly. It is described here
  79. //! for documentation purposes.
  80. //!
  81. //! This allocator inherits from a standard-conforming allocator
  82. //! and forwards member functions to the standard allocator except
  83. //! when internal storage is being used as memory source.
  84. //!
  85. //! This allocator is a "partially_propagable" allocator and
  86. //! defines `is_partially_propagable` as true_type.
  87. //!
  88. //! A partially propagable allocator means that not all storage
  89. //! allocatod by an instance of `small_vector_allocator` can be
  90. //! deallocated by another instance of this type, even if both
  91. //! instances compare equal or an instance is propagated to another
  92. //! one using the copy/move constructor or assignment. The storage that
  93. //! can never be propagated is identified by `storage_is_unpropagable(p)`.
  94. //!
  95. //! `boost::container::vector` supports partially propagable allocators
  96. //! fallbacking to deep copy/swap/move operations when internal storage
  97. //! is being used to store vector elements.
  98. //!
  99. //! `small_vector_allocator` assumes that will be instantiated as
  100. //! `boost::container::vector< T, small_vector_allocator<T, Allocator> >`
  101. //! and internal storage can be obtained downcasting that vector
  102. //! to `small_vector_base<T>`.
  103. template<class T, class VoidAllocator BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void)>
  104. class small_vector_allocator
  105. : public allocator_traits<typename real_allocator<T, VoidAllocator>::type>::template portable_rebind_alloc<T>::type
  106. {
  107. typedef unsigned int allocation_type;
  108. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  109. private:
  110. typedef typename allocator_traits<typename real_allocator<T, VoidAllocator>::type>::template portable_rebind_alloc<T>::type allocator_type;
  111. BOOST_COPYABLE_AND_MOVABLE(small_vector_allocator)
  112. BOOST_CONTAINER_FORCEINLINE const allocator_type &as_base() const BOOST_NOEXCEPT
  113. { return static_cast<const allocator_type&>(*this); }
  114. BOOST_CONTAINER_FORCEINLINE allocator_type &as_base() BOOST_NOEXCEPT
  115. { return static_cast<allocator_type&>(*this); }
  116. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  117. public:
  118. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  119. typedef allocator_traits<allocator_type> allocator_traits_type;
  120. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  121. typedef typename allocator_traits<allocator_type>::value_type value_type;
  122. typedef typename allocator_traits<allocator_type>::pointer pointer;
  123. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  124. typedef typename allocator_traits<allocator_type>::reference reference;
  125. typedef typename allocator_traits<allocator_type>::const_reference const_reference;
  126. typedef typename allocator_traits<allocator_type>::size_type size_type;
  127. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  128. typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
  129. typedef typename allocator_traits<allocator_type>::const_void_pointer const_void_pointer;
  130. typedef typename allocator_traits<allocator_type>::propagate_on_container_copy_assignment propagate_on_container_copy_assignment;
  131. typedef typename allocator_traits<allocator_type>::propagate_on_container_move_assignment propagate_on_container_move_assignment;
  132. typedef typename allocator_traits<allocator_type>::propagate_on_container_swap propagate_on_container_swap;
  133. //! An integral constant with member `value == false`
  134. typedef BOOST_CONTAINER_IMPDEF(dtl::bool_<false>) is_always_equal;
  135. //! An integral constant with member `value == true`
  136. typedef BOOST_CONTAINER_IMPDEF(dtl::bool_<true>) is_partially_propagable;
  137. BOOST_CONTAINER_DOCIGN(typedef dtl::version_type<small_vector_allocator BOOST_CONTAINER_I 1> version;)
  138. //!Obtains an small_vector_allocator that allocates
  139. //!objects of type T2
  140. template<class T2>
  141. struct rebind
  142. {
  143. typedef typename allocator_traits<allocator_type>::template portable_rebind_alloc<T2>::type other;
  144. };
  145. BOOST_CONTAINER_FORCEINLINE small_vector_allocator() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  146. {}
  147. //!Constructor from other small_vector_allocator.
  148. //!Never throws
  149. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  150. (const small_vector_allocator &other) BOOST_NOEXCEPT_OR_NOTHROW
  151. : allocator_type(other.as_base())
  152. {}
  153. //!Move constructor from small_vector_allocator.
  154. //!Never throws
  155. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  156. (BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  157. : allocator_type(::boost::move(other.as_base()))
  158. {}
  159. //!Constructor from related small_vector_allocator.
  160. //!Never throws
  161. template<class U, class OtherVoidAllocator, class OtherOptions>
  162. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  163. (const small_vector_allocator<U, OtherVoidAllocator, OtherOptions> &other) BOOST_NOEXCEPT_OR_NOTHROW
  164. : allocator_type(other.as_base())
  165. {}
  166. //!Move constructor from related small_vector_allocator.
  167. //!Never throws
  168. template<class U, class OtherVoidAllocator, class OtherOptions>
  169. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  170. (BOOST_RV_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I OtherOptions>) other) BOOST_NOEXCEPT_OR_NOTHROW
  171. : allocator_type(::boost::move(other.as_base()))
  172. {}
  173. //!Constructor from allocator_type.
  174. //!Never throws
  175. BOOST_CONTAINER_FORCEINLINE explicit small_vector_allocator
  176. (const allocator_type &other) BOOST_NOEXCEPT_OR_NOTHROW
  177. : allocator_type(other)
  178. {}
  179. //!Assignment from other small_vector_allocator.
  180. //!Never throws
  181. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  182. operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  183. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other.as_base())); }
  184. //!Move assignment from other small_vector_allocator.
  185. //!Never throws
  186. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  187. operator=(BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  188. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(::boost::move(other.as_base()))); }
  189. //!Assignment from related small_vector_allocator.
  190. //!Never throws
  191. template<class U, class OtherVoidAllocator>
  192. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  193. operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I Options>) other) BOOST_NOEXCEPT_OR_NOTHROW
  194. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other.as_base())); }
  195. //!Move assignment from related small_vector_allocator.
  196. //!Never throws
  197. template<class U, class OtherVoidAllocator>
  198. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  199. operator=(BOOST_RV_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I Options>) other) BOOST_NOEXCEPT_OR_NOTHROW
  200. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(::boost::move(other.as_base()))); }
  201. //!Move assignment from allocator_type.
  202. //!Never throws
  203. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  204. operator=(const allocator_type &other) BOOST_NOEXCEPT_OR_NOTHROW
  205. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other)); }
  206. //!Allocates storage from the standard-conforming allocator
  207. BOOST_CONTAINER_FORCEINLINE pointer allocate(size_type count, const_void_pointer hint = const_void_pointer())
  208. { return allocator_traits_type::allocate(this->as_base(), count, hint); }
  209. //!Deallocates previously allocated memory.
  210. //!Never throws
  211. void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  212. {
  213. if(!this->is_internal_storage(ptr))
  214. allocator_traits_type::deallocate(this->as_base(), ptr, n);
  215. }
  216. //!Returns the maximum number of elements that could be allocated.
  217. //!Never throws
  218. BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  219. { return allocator_traits_type::max_size(this->as_base()); }
  220. small_vector_allocator select_on_container_copy_construction() const
  221. { return small_vector_allocator(allocator_traits_type::select_on_container_copy_construction(this->as_base())); }
  222. bool storage_is_unpropagable(pointer p) const
  223. { return this->is_internal_storage(p) || allocator_traits_type::storage_is_unpropagable(this->as_base(), p); }
  224. //!Swaps two allocators, does nothing
  225. //!because this small_vector_allocator is stateless
  226. BOOST_CONTAINER_FORCEINLINE friend void swap(small_vector_allocator &l, small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  227. { boost::adl_move_swap(l.as_base(), r.as_base()); }
  228. //!An small_vector_allocator always compares to true, as memory allocated with one
  229. //!instance can be deallocated by another instance (except for unpropagable storage)
  230. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const small_vector_allocator &l, const small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  231. { return allocator_traits_type::equal(l.as_base(), r.as_base()); }
  232. //!An small_vector_allocator always compares to false, as memory allocated with one
  233. //!instance can be deallocated by another instance
  234. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const small_vector_allocator &l, const small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  235. { return !(l == r); }
  236. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  237. /*
  238. //!An advanced function that offers in-place expansion shrink to fit and new allocation
  239. //!capabilities. Memory allocated with this function can only be deallocated with deallocate()
  240. //!or deallocate_many().
  241. //!This function is available only with Version == 2
  242. pointer allocation_command(allocation_type command,
  243. size_type limit_size,
  244. size_type &prefer_in_recvd_out_size,
  245. pointer &reuse)
  246. { return allocator_traits_type::allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); }
  247. //!Returns maximum the number of objects the previously allocated memory
  248. //!pointed by p can hold.
  249. //!Memory must not have been allocated with
  250. //!allocate_one or allocate_individual.
  251. //!This function is available only with Version == 2
  252. size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
  253. { return allocator_traits_type::size(p); }
  254. */
  255. private:
  256. /*
  257. //!Allocates just one object. Memory allocated with this function
  258. //!must be deallocated only with deallocate_one().
  259. //!Throws bad_alloc if there is no enough memory
  260. //!This function is available only with Version == 2
  261. using allocator_type::allocate_one;
  262. using allocator_type::allocate_individual;
  263. using allocator_type::deallocate_one;
  264. using allocator_type::deallocate_individual;
  265. using allocator_type::allocate_many;
  266. using allocator_type::deallocate_many;*/
  267. typedef vector_alloc_holder< small_vector_allocator, size_type > vector_alloc_holder_t;
  268. typedef typename dtl::vector_for_small_vector<T, allocator_type, Options>::type vector_base;
  269. typedef small_vector_base<value_type, allocator_type, Options> derived_type;
  270. BOOST_CONTAINER_FORCEINLINE bool is_internal_storage(const_pointer p) const
  271. { return this->internal_storage() == p; }
  272. BOOST_CONTAINER_FORCEINLINE
  273. const_pointer internal_storage() const
  274. {
  275. const vector_alloc_holder_t &v_holder = static_cast<const vector_alloc_holder_t &>(*this);
  276. const vector_base &v_base = reinterpret_cast<const vector_base &>(v_holder);
  277. const derived_type &d_base = static_cast<const derived_type &>(v_base);
  278. return d_base.internal_storage();
  279. }
  280. BOOST_CONTAINER_FORCEINLINE
  281. pointer internal_storage()
  282. {
  283. vector_alloc_holder_t &v_holder = static_cast<vector_alloc_holder_t &>(*this);
  284. vector_base &v_base = reinterpret_cast<vector_base &>(v_holder);
  285. derived_type &d_base = static_cast<derived_type &>(v_base);
  286. return d_base.internal_storage();
  287. }
  288. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  289. };
  290. //! This class consists of common code from all small_vector<T, N> types that don't depend on the
  291. //! "N" template parameter. This class is non-copyable and non-destructible, so this class typically
  292. //! used as reference argument to functions that read or write small vectors. Since `small_vector<T, N>`
  293. //! derives from `small_vector_base<T>`, the conversion to `small_vector_base` is implicit
  294. //! <pre>
  295. //!
  296. //! //Clients can pass any small_vector<Foo, N>.
  297. //! void read_any_small_vector_of_foo(const small_vector_base<Foo> &in_parameter);
  298. //!
  299. //! void modify_any_small_vector_of_foo(small_vector_base<Foo> &in_out_parameter);
  300. //!
  301. //! void some_function()
  302. //! {
  303. //!
  304. //! small_vector<Foo, 8> myvector;
  305. //!
  306. //! read_any_small_vector_of_foo(myvector); // Reads myvector
  307. //!
  308. //! modify_any_small_vector_of_foo(myvector); // Modifies myvector
  309. //!
  310. //! }
  311. //! </pre>
  312. //!
  313. //! All `boost::container:vector` member functions are inherited. See `vector` documentation for details.
  314. //!
  315. template <class T, class SecondaryAllocator, class Options>
  316. class small_vector_base
  317. : public dtl::vector_for_small_vector<T, SecondaryAllocator, Options>::type
  318. {
  319. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKEDVECTOR
  320. public:
  321. //Make it public as it will be inherited by small_vector and container
  322. //must have this public member
  323. typedef typename real_allocator<T, SecondaryAllocator>::type secondary_allocator_t;
  324. typedef typename allocator_traits<secondary_allocator_t>::
  325. template portable_rebind_alloc<void>::type void_allocator_t;
  326. typedef typename dtl::get_small_vector_opt<Options>::type options_t;
  327. typedef typename dtl::vector_for_small_vector
  328. <T, SecondaryAllocator, Options>::type base_type;
  329. typedef typename allocator_traits<secondary_allocator_t>::pointer pointer;
  330. typedef typename allocator_traits<secondary_allocator_t>::const_pointer const_pointer;
  331. typedef typename allocator_traits<secondary_allocator_t>::void_pointer void_pointer;
  332. typedef typename allocator_traits<secondary_allocator_t>::const_void_pointer const_void_pointer;
  333. typedef small_vector_allocator<T, void_allocator_t, Options> allocator_type;
  334. private:
  335. BOOST_COPYABLE_AND_MOVABLE(small_vector_base)
  336. friend class small_vector_allocator<T, void_allocator_t, Options>;
  337. BOOST_CONTAINER_FORCEINLINE
  338. const_pointer internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
  339. {
  340. typedef typename boost::intrusive::pointer_traits<const_pointer>::template
  341. rebind_pointer<const unsigned char>::type const_char_pointer;
  342. const_void_pointer void_p = boost::intrusive::pointer_traits<const_char_pointer>::
  343. pointer_to(*m_storage_start.data);
  344. return boost::intrusive::pointer_traits<const_pointer>::static_cast_from(void_p);
  345. }
  346. BOOST_CONTAINER_FORCEINLINE
  347. pointer internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
  348. {
  349. typedef typename boost::intrusive::pointer_traits<pointer>::template
  350. rebind_pointer<unsigned char>::type char_pointer;
  351. void_pointer void_p = boost::intrusive::pointer_traits<char_pointer>::
  352. pointer_to(*m_storage_start.data);
  353. return boost::intrusive::pointer_traits<pointer>::static_cast_from(void_p);
  354. }
  355. base_type &as_base() { return static_cast<base_type&>(*this); }
  356. const base_type &as_base() const { return static_cast<const base_type&>(*this); }
  357. static const std::size_t final_alignment =
  358. options_t::inplace_alignment ? options_t::inplace_alignment : dtl::alignment_of<T>::value;
  359. public:
  360. typedef typename dtl::aligned_storage
  361. <sizeof(T), final_alignment>::type storage_type;
  362. protected:
  363. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(initial_capacity_t, std::size_t initial_capacity)
  364. : base_type(initial_capacity_t(), this->internal_storage(), initial_capacity)
  365. {}
  366. template<class AllocFwd>
  367. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(initial_capacity_t, std::size_t capacity, BOOST_FWD_REF(AllocFwd) a)
  368. : base_type(initial_capacity_t(), this->internal_storage(), capacity, ::boost::forward<AllocFwd>(a))
  369. {}
  370. //~small_vector_base(){}
  371. private:
  372. //The only member
  373. storage_type m_storage_start;
  374. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  375. public:
  376. BOOST_CONTAINER_FORCEINLINE small_vector_base& operator=(BOOST_COPY_ASSIGN_REF(small_vector_base) other)
  377. { return static_cast<small_vector_base&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
  378. BOOST_CONTAINER_FORCEINLINE small_vector_base& operator=(BOOST_RV_REF(small_vector_base) other)
  379. { return static_cast<small_vector_base&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
  380. BOOST_CONTAINER_FORCEINLINE void swap(small_vector_base &other)
  381. { return this->base_type::swap(other); }
  382. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  383. protected:
  384. void move_construct_impl(base_type &x, const allocator_type &a)
  385. {
  386. if(base_type::is_propagable_from(x.get_stored_allocator(), x.data(), a, true)){
  387. this->steal_resources(x);
  388. }
  389. else{
  390. this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
  391. , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end ()))
  392. );
  393. }
  394. }
  395. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  396. };
  397. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  398. /////////////////////////////////////////////////////
  399. //
  400. // small_vector_storage_calculator
  401. //
  402. /////////////////////////////////////////////////////
  403. template<std::size_t Needed, std::size_t Hdr, std::size_t SSize, bool NeedsZero = (0u == Needed || Needed <= Hdr)>
  404. struct small_vector_storage_calculator_helper
  405. {
  406. static const std::size_t value = (Needed - Hdr - 1u)/SSize + 1u;
  407. };
  408. template<std::size_t Needed, std::size_t Hdr, std::size_t SSize>
  409. struct small_vector_storage_calculator_helper<Needed, Hdr, SSize, true>
  410. {
  411. static const std::size_t value = 0u;
  412. };
  413. template<class Storage, class Allocator, class T, std::size_t N, class Options>
  414. struct small_vector_storage_calculator
  415. {
  416. typedef small_vector_base<T, Allocator, Options> svh_type;
  417. typedef typename real_allocator<T, Allocator>::type value_allocator_t;
  418. typedef typename allocator_traits<value_allocator_t>::template portable_rebind_alloc<void>::type void_allocator_t;
  419. typedef typename dtl::vector_for_small_vector<T, void_allocator_t, Options>::type svhb_type;
  420. static const std::size_t s_align = dtl::alignment_of<Storage>::value;
  421. static const std::size_t s_size = sizeof(Storage);
  422. static const std::size_t svh_sizeof = sizeof(svh_type);
  423. static const std::size_t svhb_sizeof = sizeof(svhb_type);
  424. static const std::size_t s_start = ((svhb_sizeof-1)/s_align+1)*s_align;
  425. static const std::size_t header_bytes = svh_sizeof-s_start;
  426. static const std::size_t needed_bytes = sizeof(T)*N;
  427. static const std::size_t needed_extra_storages =
  428. small_vector_storage_calculator_helper<needed_bytes, header_bytes, s_size>::value;
  429. };
  430. /////////////////////////////////////////////////////
  431. //
  432. // small_vector_storage_definer
  433. //
  434. /////////////////////////////////////////////////////
  435. template<class Storage, std::size_t N>
  436. struct small_vector_storage
  437. {
  438. Storage m_rest_of_storage[N];
  439. };
  440. template<class Storage>
  441. struct small_vector_storage<Storage, 0>
  442. {};
  443. template<class T, class Allocator, std::size_t N, class Options>
  444. struct small_vector_storage_definer
  445. {
  446. typedef T value_type;
  447. typedef typename small_vector_base<value_type, Allocator, Options>::storage_type storage_type;
  448. static const std::size_t needed_extra_storages =
  449. small_vector_storage_calculator<storage_type, Allocator, value_type, N, Options>::needed_extra_storages;
  450. typedef small_vector_storage<storage_type, needed_extra_storages> type;
  451. };
  452. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  453. //! small_vector is a vector-like container optimized for the case when it contains few elements.
  454. //! It contains some preallocated elements in-place, which can avoid the use of dynamic storage allocation
  455. //! when the actual number of elements is below that preallocated threshold.
  456. //!
  457. //! `small_vector<T, N, Allocator, Options>` is convertible to `small_vector_base<T, Allocator, Options>` that is independent
  458. //! from the preallocated element capacity, so client code does not need to be templated on that N argument.
  459. //!
  460. //! All `boost::container::vector` member functions are inherited. See `vector` documentation for details.
  461. //!
  462. //! \tparam T The type of object that is stored in the small_vector
  463. //! \tparam N The number of preallocated elements stored inside small_vector. It shall be less than Allocator::max_size();
  464. //! \tparam Allocator The allocator used for memory management when the number of elements exceeds N. Use void
  465. //! for the default allocator
  466. //! |tparam Options A type produced from \c boost::container::small_vector_options.
  467. template <class T, std::size_t N, class Allocator BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void) >
  468. class small_vector : public small_vector_base<T, Allocator, Options>
  469. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  470. , private small_vector_storage_definer<T, Allocator, N, Options>::type
  471. #endif
  472. {
  473. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  474. typedef small_vector_base<T, Allocator, Options> base_type;
  475. typedef typename small_vector_storage_definer
  476. <T, Allocator, N, Options>::type remaining_storage_holder;
  477. BOOST_COPYABLE_AND_MOVABLE(small_vector)
  478. typedef allocator_traits<typename base_type::allocator_type> allocator_traits_type;
  479. public:
  480. typedef small_vector_storage_calculator
  481. < typename small_vector_base<T, Allocator, Options>::storage_type
  482. , Allocator, T, N, Options> storage_test;
  483. static const std::size_t needed_extra_storages = storage_test::needed_extra_storages;
  484. static const std::size_t needed_bytes = storage_test::needed_bytes;
  485. static const std::size_t header_bytes = storage_test::header_bytes;
  486. static const std::size_t s_start = storage_test::s_start;
  487. typedef typename base_type::allocator_type allocator_type;
  488. typedef typename base_type::size_type size_type;
  489. typedef typename base_type::value_type value_type;
  490. BOOST_CONTAINER_FORCEINLINE static std::size_t internal_capacity()
  491. { return (sizeof(small_vector) - storage_test::s_start)/sizeof(T); }
  492. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  493. //! @brief The capacity/max size of the container
  494. static const size_type static_capacity = N;
  495. public:
  496. BOOST_CONTAINER_FORCEINLINE small_vector()
  497. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  498. : base_type(initial_capacity_t(), internal_capacity())
  499. {}
  500. BOOST_CONTAINER_FORCEINLINE explicit small_vector(const allocator_type &a)
  501. : base_type(initial_capacity_t(), internal_capacity(), a)
  502. {}
  503. BOOST_CONTAINER_FORCEINLINE explicit small_vector(size_type n)
  504. : base_type(initial_capacity_t(), internal_capacity())
  505. { this->resize(n); }
  506. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const allocator_type &a)
  507. : base_type(initial_capacity_t(), internal_capacity(), a)
  508. { this->resize(n); }
  509. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, default_init_t)
  510. : base_type(initial_capacity_t(), internal_capacity())
  511. { this->resize(n, default_init_t()); }
  512. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, default_init_t, const allocator_type &a)
  513. : base_type(initial_capacity_t(), internal_capacity(), a)
  514. { this->resize(n, default_init_t()); }
  515. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const value_type &v)
  516. : base_type(initial_capacity_t(), internal_capacity())
  517. { this->resize(n, v); }
  518. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const value_type &v, const allocator_type &a)
  519. : base_type(initial_capacity_t(), internal_capacity(), a)
  520. { this->resize(n, v); }
  521. template <class InIt>
  522. BOOST_CONTAINER_FORCEINLINE small_vector(InIt first, InIt last
  523. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  524. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  525. BOOST_MOVE_I dtl::nat >::type * = 0)
  526. )
  527. : base_type(initial_capacity_t(), internal_capacity())
  528. { this->assign(first, last); }
  529. template <class InIt>
  530. BOOST_CONTAINER_FORCEINLINE small_vector(InIt first, InIt last, const allocator_type& a
  531. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  532. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  533. BOOST_MOVE_I dtl::nat >::type * = 0)
  534. )
  535. : base_type(initial_capacity_t(), internal_capacity(), a)
  536. { this->assign(first, last); }
  537. BOOST_CONTAINER_FORCEINLINE small_vector(const small_vector &other)
  538. : base_type( initial_capacity_t(), internal_capacity()
  539. , allocator_traits_type::select_on_container_copy_construction(other.get_stored_allocator()))
  540. { this->assign(other.cbegin(), other.cend()); }
  541. BOOST_CONTAINER_FORCEINLINE small_vector(const small_vector &other, const allocator_type &a)
  542. : base_type(initial_capacity_t(), internal_capacity(), a)
  543. { this->assign(other.cbegin(), other.cend()); }
  544. BOOST_CONTAINER_FORCEINLINE explicit small_vector(const base_type &other)
  545. : base_type( initial_capacity_t(), internal_capacity()
  546. , allocator_traits_type::select_on_container_copy_construction(other.get_stored_allocator()))
  547. { this->assign(other.cbegin(), other.cend()); }
  548. BOOST_CONTAINER_FORCEINLINE explicit small_vector(BOOST_RV_REF(base_type) other)
  549. : base_type(initial_capacity_t(), internal_capacity(), ::boost::move(other.get_stored_allocator()))
  550. { this->move_construct_impl(other, other.get_stored_allocator()); }
  551. BOOST_CONTAINER_FORCEINLINE small_vector(BOOST_RV_REF(small_vector) other)
  552. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_assignable<value_type>::value)
  553. : base_type(initial_capacity_t(), internal_capacity(), ::boost::move(other.get_stored_allocator()))
  554. { this->move_construct_impl(other, other.get_stored_allocator()); }
  555. BOOST_CONTAINER_FORCEINLINE small_vector(BOOST_RV_REF(small_vector) other, const allocator_type &a)
  556. : base_type(initial_capacity_t(), internal_capacity(), a)
  557. { this->move_construct_impl(other, a); }
  558. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  559. BOOST_CONTAINER_FORCEINLINE small_vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  560. : base_type(initial_capacity_t(), internal_capacity(), a)
  561. {
  562. this->assign(il.begin(), il.end());
  563. }
  564. #endif
  565. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_COPY_ASSIGN_REF(small_vector) other)
  566. { return static_cast<small_vector&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
  567. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_RV_REF(small_vector) other)
  568. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_assignable<value_type>::value
  569. && (allocator_traits_type::propagate_on_container_move_assignment::value
  570. || allocator_traits_type::is_always_equal::value))
  571. { return static_cast<small_vector&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
  572. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(const base_type &other)
  573. { return static_cast<small_vector&>(this->base_type::operator=(other)); }
  574. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_RV_REF(base_type) other)
  575. { return static_cast<small_vector&>(this->base_type::operator=(boost::move(other))); }
  576. BOOST_CONTAINER_FORCEINLINE void swap(small_vector &other)
  577. { return this->base_type::swap(other); }
  578. };
  579. }}
  580. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  581. /*
  582. namespace boost {
  583. //!has_trivial_destructor_after_move<> == true_type
  584. //!specialization for optimizations
  585. template <class T, class Allocator>
  586. struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
  587. {
  588. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  589. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  590. ::boost::has_trivial_destructor_after_move<pointer>::value;
  591. };
  592. }
  593. */
  594. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  595. #include <boost/container/detail/config_end.hpp>
  596. #endif // #ifndef BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP