small_vector.hpp 29 KB

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