small_vector.hpp 29 KB

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