small_vector.hpp 30 KB

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