static_vector.hpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. // Boost.Container static_vector
  2. //
  3. // Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland.
  4. // Copyright (c) 2011-2013 Andrew Hundt.
  5. // Copyright (c) 2013-2014 Ion Gaztanaga
  6. //
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_CONTAINER_STATIC_VECTOR_HPP
  11. #define BOOST_CONTAINER_STATIC_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. #include <boost/container/detail/type_traits.hpp>
  21. #include <boost/container/vector.hpp>
  22. #include <cstddef>
  23. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  24. #include <initializer_list>
  25. #endif
  26. namespace boost { namespace container {
  27. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  28. namespace dtl {
  29. template<class T, std::size_t N>
  30. class static_storage_allocator
  31. {
  32. public:
  33. typedef T value_type;
  34. BOOST_CONTAINER_FORCEINLINE static_storage_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  35. {}
  36. BOOST_CONTAINER_FORCEINLINE static_storage_allocator(const static_storage_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  37. {}
  38. BOOST_CONTAINER_FORCEINLINE static_storage_allocator & operator=(const static_storage_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  39. { return *this; }
  40. BOOST_CONTAINER_FORCEINLINE T* internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
  41. { return const_cast<T*>(static_cast<const T*>(static_cast<const void*>(storage.data))); }
  42. BOOST_CONTAINER_FORCEINLINE T* internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
  43. { return static_cast<T*>(static_cast<void*>(storage.data)); }
  44. static const std::size_t internal_capacity = N;
  45. std::size_t max_size() const
  46. { return N; }
  47. typedef boost::container::dtl::version_type<static_storage_allocator, 0> version;
  48. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const static_storage_allocator &, const static_storage_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  49. { return false; }
  50. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const static_storage_allocator &, const static_storage_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  51. { return true; }
  52. private:
  53. typename aligned_storage<sizeof(T)*N, alignment_of<T>::value>::type storage;
  54. };
  55. } //namespace dtl {
  56. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  57. //!
  58. //!@brief A variable-size array container with fixed capacity.
  59. //!
  60. //!static_vector is a sequence container like boost::container::vector with contiguous storage that can
  61. //!change in size, along with the static allocation, low overhead, and fixed capacity of boost::array.
  62. //!
  63. //!A static_vector is a sequence that supports random access to elements, constant time insertion and
  64. //!removal of elements at the end, and linear time insertion and removal of elements at the beginning or
  65. //!in the middle. The number of elements in a static_vector may vary dynamically up to a fixed capacity
  66. //!because elements are stored within the object itself similarly to an array. However, objects are
  67. //!initialized as they are inserted into static_vector unlike C arrays or std::array which must construct
  68. //!all elements on instantiation. The behavior of static_vector enables the use of statically allocated
  69. //!elements in cases with complex object lifetime requirements that would otherwise not be trivially
  70. //!possible.
  71. //!
  72. //!@par Error Handling
  73. //! Insertion beyond the capacity result in throwing std::bad_alloc() if exceptions are enabled or
  74. //! calling throw_bad_alloc() if not enabled.
  75. //!
  76. //! std::out_of_range is thrown if out of bound access is performed in <code>at()</code> if exceptions are
  77. //! enabled, throw_out_of_range() if not enabled.
  78. //!
  79. //!@tparam Value The type of element that will be stored.
  80. //!@tparam Capacity The maximum number of elements static_vector can store, fixed at compile time.
  81. template <typename Value, std::size_t Capacity>
  82. class static_vector
  83. : public vector<Value, dtl::static_storage_allocator<Value, Capacity> >
  84. {
  85. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  86. typedef vector<Value, dtl::static_storage_allocator<Value, Capacity> > base_t;
  87. BOOST_COPYABLE_AND_MOVABLE(static_vector)
  88. template<class U, std::size_t OtherCapacity>
  89. friend class static_vector;
  90. public:
  91. typedef dtl::static_storage_allocator<Value, Capacity> allocator_type;
  92. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  93. public:
  94. //! @brief The type of elements stored in the container.
  95. typedef typename base_t::value_type value_type;
  96. //! @brief The unsigned integral type used by the container.
  97. typedef typename base_t::size_type size_type;
  98. //! @brief The pointers difference type.
  99. typedef typename base_t::difference_type difference_type;
  100. //! @brief The pointer type.
  101. typedef typename base_t::pointer pointer;
  102. //! @brief The const pointer type.
  103. typedef typename base_t::const_pointer const_pointer;
  104. //! @brief The value reference type.
  105. typedef typename base_t::reference reference;
  106. //! @brief The value const reference type.
  107. typedef typename base_t::const_reference const_reference;
  108. //! @brief The iterator type.
  109. typedef typename base_t::iterator iterator;
  110. //! @brief The const iterator type.
  111. typedef typename base_t::const_iterator const_iterator;
  112. //! @brief The reverse iterator type.
  113. typedef typename base_t::reverse_iterator reverse_iterator;
  114. //! @brief The const reverse iterator.
  115. typedef typename base_t::const_reverse_iterator const_reverse_iterator;
  116. //! @brief The capacity/max size of the container
  117. static const size_type static_capacity = Capacity;
  118. //! @brief Constructs an empty static_vector.
  119. //!
  120. //! @par Throws
  121. //! Nothing.
  122. //!
  123. //! @par Complexity
  124. //! Constant O(1).
  125. BOOST_CONTAINER_FORCEINLINE static_vector() BOOST_NOEXCEPT_OR_NOTHROW
  126. : base_t()
  127. {}
  128. //! @pre <tt>count <= capacity()</tt>
  129. //!
  130. //! @brief Constructs a static_vector containing count value initialized values.
  131. //!
  132. //! @param count The number of values which will be contained in the container.
  133. //!
  134. //! @par Throws
  135. //! If Value's value initialization throws.
  136. //!
  137. //! @par Complexity
  138. //! Linear O(N).
  139. BOOST_CONTAINER_FORCEINLINE explicit static_vector(size_type count)
  140. : base_t(count)
  141. {}
  142. //! @pre <tt>count <= capacity()</tt>
  143. //!
  144. //! @brief Constructs a static_vector containing count default initialized values.
  145. //!
  146. //! @param count The number of values which will be contained in the container.
  147. //!
  148. //! @par Throws
  149. //! If Value's default initialization throws.
  150. //!
  151. //! @par Complexity
  152. //! Linear O(N).
  153. //!
  154. //! @par Note
  155. //! Non-standard extension
  156. BOOST_CONTAINER_FORCEINLINE static_vector(size_type count, default_init_t)
  157. : base_t(count, default_init_t())
  158. {}
  159. //! @pre <tt>count <= capacity()</tt>
  160. //!
  161. //! @brief Constructs a static_vector containing count copies of value.
  162. //!
  163. //! @param count The number of copies of a values that will be contained in the container.
  164. //! @param value The value which will be used to copy construct values.
  165. //!
  166. //! @par Throws
  167. //! If Value's copy constructor throws.
  168. //!
  169. //! @par Complexity
  170. //! Linear O(N).
  171. BOOST_CONTAINER_FORCEINLINE static_vector(size_type count, value_type const& value)
  172. : base_t(count, value)
  173. {}
  174. //! @pre
  175. //! @li <tt>distance(first, last) <= capacity()</tt>
  176. //! @li Iterator must meet the \c ForwardTraversalIterator concept.
  177. //!
  178. //! @brief Constructs a static_vector containing copy of a range <tt>[first, last)</tt>.
  179. //!
  180. //! @param first The iterator to the first element in range.
  181. //! @param last The iterator to the one after the last element in range.
  182. //!
  183. //! @par Throws
  184. //! If Value's constructor taking a dereferenced Iterator throws.
  185. //!
  186. //! @par Complexity
  187. //! Linear O(N).
  188. template <typename Iterator>
  189. BOOST_CONTAINER_FORCEINLINE static_vector(Iterator first, Iterator last)
  190. : base_t(first, last)
  191. {}
  192. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  193. //! @pre
  194. //! @li <tt>distance(il.begin(), il.end()) <= capacity()</tt>
  195. //!
  196. //! @brief Constructs a static_vector containing copy of a range <tt>[il.begin(), il.end())</tt>.
  197. //!
  198. //! @param il std::initializer_list with values to initialize vector.
  199. //!
  200. //! @par Throws
  201. //! If Value's constructor taking a dereferenced std::initializer_list throws.
  202. //!
  203. //! @par Complexity
  204. //! Linear O(N).
  205. BOOST_CONTAINER_FORCEINLINE static_vector(std::initializer_list<value_type> il)
  206. : base_t(il)
  207. {}
  208. #endif
  209. //! @brief Constructs a copy of other static_vector.
  210. //!
  211. //! @param other The static_vector which content will be copied to this one.
  212. //!
  213. //! @par Throws
  214. //! If Value's copy constructor throws.
  215. //!
  216. //! @par Complexity
  217. //! Linear O(N).
  218. BOOST_CONTAINER_FORCEINLINE static_vector(static_vector const& other)
  219. : base_t(other)
  220. {}
  221. BOOST_CONTAINER_FORCEINLINE static_vector(static_vector const& other, const allocator_type &)
  222. : base_t(other)
  223. {}
  224. BOOST_CONTAINER_FORCEINLINE static_vector(BOOST_RV_REF(static_vector) other, const allocator_type &)
  225. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<value_type>::value)
  226. : base_t(BOOST_MOVE_BASE(base_t, other))
  227. {}
  228. BOOST_CONTAINER_FORCEINLINE explicit static_vector(const allocator_type &)
  229. : base_t()
  230. {}
  231. //! @pre <tt>other.size() <= capacity()</tt>.
  232. //!
  233. //! @brief Constructs a copy of other static_vector.
  234. //!
  235. //! @param other The static_vector which content will be copied to this one.
  236. //!
  237. //! @par Throws
  238. //! If Value's copy constructor throws.
  239. //!
  240. //! @par Complexity
  241. //! Linear O(N).
  242. template <std::size_t C>
  243. BOOST_CONTAINER_FORCEINLINE static_vector(static_vector<value_type, C> const& other)
  244. : base_t(other)
  245. {}
  246. //! @brief Move constructor. Moves Values stored in the other static_vector to this one.
  247. //!
  248. //! @param other The static_vector which content will be moved to this one.
  249. //!
  250. //! @par Throws
  251. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor throws.
  252. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor throws.
  253. //!
  254. //! @par Complexity
  255. //! Linear O(N).
  256. BOOST_CONTAINER_FORCEINLINE static_vector(BOOST_RV_REF(static_vector) other)
  257. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<value_type>::value)
  258. : base_t(BOOST_MOVE_BASE(base_t, other))
  259. {}
  260. //! @pre <tt>other.size() <= capacity()</tt>
  261. //!
  262. //! @brief Move constructor. Moves Values stored in the other static_vector to this one.
  263. //!
  264. //! @param other The static_vector which content will be moved to this one.
  265. //!
  266. //! @par Throws
  267. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor throws.
  268. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor throws.
  269. //!
  270. //! @par Complexity
  271. //! Linear O(N).
  272. template <std::size_t C>
  273. BOOST_CONTAINER_FORCEINLINE static_vector(BOOST_RV_REF_BEG static_vector<value_type, C> BOOST_RV_REF_END other)
  274. : base_t(BOOST_MOVE_BASE(typename static_vector<value_type BOOST_MOVE_I C>::base_t, other))
  275. {}
  276. //! @brief Copy assigns Values stored in the other static_vector to this one.
  277. //!
  278. //! @param other The static_vector which content will be copied to this one.
  279. //!
  280. //! @par Throws
  281. //! If Value's copy constructor or copy assignment throws.
  282. //!
  283. //! @par Complexity
  284. //! Linear O(N).
  285. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(BOOST_COPY_ASSIGN_REF(static_vector) other)
  286. {
  287. return static_cast<static_vector&>(base_t::operator=(static_cast<base_t const&>(other)));
  288. }
  289. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  290. //! @brief Copy assigns Values stored in std::initializer_list to *this.
  291. //!
  292. //! @param il The std::initializer_list which content will be copied to this one.
  293. //!
  294. //! @par Throws
  295. //! If Value's copy constructor or copy assignment throws.
  296. //!
  297. //! @par Complexity
  298. //! Linear O(N).
  299. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(std::initializer_list<value_type> il)
  300. { return static_cast<static_vector&>(base_t::operator=(il)); }
  301. #endif
  302. //! @pre <tt>other.size() <= capacity()</tt>
  303. //!
  304. //! @brief Copy assigns Values stored in the other static_vector to this one.
  305. //!
  306. //! @param other The static_vector which content will be copied to this one.
  307. //!
  308. //! @par Throws
  309. //! If Value's copy constructor or copy assignment throws.
  310. //!
  311. //! @par Complexity
  312. //! Linear O(N).
  313. template <std::size_t C>
  314. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(static_vector<value_type, C> const& other)
  315. {
  316. return static_cast<static_vector&>(base_t::operator=
  317. (static_cast<typename static_vector<value_type, C>::base_t const&>(other)));
  318. }
  319. //! @brief Move assignment. Moves Values stored in the other static_vector to this one.
  320. //!
  321. //! @param other The static_vector which content will be moved to this one.
  322. //!
  323. //! @par Throws
  324. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws.
  325. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws.
  326. //!
  327. //! @par Complexity
  328. //! Linear O(N).
  329. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(BOOST_RV_REF(static_vector) other)
  330. {
  331. return static_cast<static_vector&>(base_t::operator=(BOOST_MOVE_BASE(base_t, other)));
  332. }
  333. //! @pre <tt>other.size() <= capacity()</tt>
  334. //!
  335. //! @brief Move assignment. Moves Values stored in the other static_vector to this one.
  336. //!
  337. //! @param other The static_vector which content will be moved to this one.
  338. //!
  339. //! @par Throws
  340. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws.
  341. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws.
  342. //!
  343. //! @par Complexity
  344. //! Linear O(N).
  345. template <std::size_t C>
  346. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(BOOST_RV_REF_BEG static_vector<value_type, C> BOOST_RV_REF_END other)
  347. {
  348. return static_cast<static_vector&>(base_t::operator=
  349. (BOOST_MOVE_BASE(typename static_vector<value_type BOOST_MOVE_I C>::base_t, other)));
  350. }
  351. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  352. //! @brief Destructor. Destroys Values stored in this container.
  353. //!
  354. //! @par Throws
  355. //! Nothing
  356. //!
  357. //! @par Complexity
  358. //! Linear O(N).
  359. ~static_vector();
  360. //! @brief Swaps contents of the other static_vector and this one.
  361. //!
  362. //! @param other The static_vector which content will be swapped with this one's content.
  363. //!
  364. //! @par Throws
  365. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  366. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  367. //!
  368. //! @par Complexity
  369. //! Linear O(N).
  370. void swap(static_vector & other);
  371. //! @pre <tt>other.size() <= capacity() && size() <= other.capacity()</tt>
  372. //!
  373. //! @brief Swaps contents of the other static_vector and this one.
  374. //!
  375. //! @param other The static_vector which content will be swapped with this one's content.
  376. //!
  377. //! @par Throws
  378. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  379. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  380. //!
  381. //! @par Complexity
  382. //! Linear O(N).
  383. template <std::size_t C>
  384. void swap(static_vector<value_type, C> & other);
  385. //! @pre <tt>count <= capacity()</tt>
  386. //!
  387. //! @brief Inserts or erases elements at the end such that
  388. //! the size becomes count. New elements are value initialized.
  389. //!
  390. //! @param count The number of elements which will be stored in the container.
  391. //!
  392. //! @par Throws
  393. //! If Value's value initialization throws.
  394. //!
  395. //! @par Complexity
  396. //! Linear O(N).
  397. void resize(size_type count);
  398. //! @pre <tt>count <= capacity()</tt>
  399. //!
  400. //! @brief Inserts or erases elements at the end such that
  401. //! the size becomes count. New elements are default initialized.
  402. //!
  403. //! @param count The number of elements which will be stored in the container.
  404. //!
  405. //! @par Throws
  406. //! If Value's default initialization throws.
  407. //!
  408. //! @par Complexity
  409. //! Linear O(N).
  410. //!
  411. //! @par Note
  412. //! Non-standard extension
  413. void resize(size_type count, default_init_t);
  414. //! @pre <tt>count <= capacity()</tt>
  415. //!
  416. //! @brief Inserts or erases elements at the end such that
  417. //! the size becomes count. New elements are copy constructed from value.
  418. //!
  419. //! @param count The number of elements which will be stored in the container.
  420. //! @param value The value used to copy construct the new element.
  421. //!
  422. //! @par Throws
  423. //! If Value's copy constructor throws.
  424. //!
  425. //! @par Complexity
  426. //! Linear O(N).
  427. void resize(size_type count, value_type const& value);
  428. //! @pre <tt>count <= capacity()</tt>
  429. //!
  430. //! @brief This call has no effect because the Capacity of this container is constant.
  431. //!
  432. //! @param count The number of elements which the container should be able to contain.
  433. //!
  434. //! @par Throws
  435. //! Nothing.
  436. //!
  437. //! @par Complexity
  438. //! Linear O(N).
  439. void reserve(size_type count) BOOST_NOEXCEPT_OR_NOTHROW;
  440. //! @pre <tt>size() < capacity()</tt>
  441. //!
  442. //! @brief Adds a copy of value at the end.
  443. //!
  444. //! @param value The value used to copy construct the new element.
  445. //!
  446. //! @par Throws
  447. //! If Value's copy constructor throws.
  448. //!
  449. //! @par Complexity
  450. //! Constant O(1).
  451. void push_back(value_type const& value);
  452. //! @pre <tt>size() < capacity()</tt>
  453. //!
  454. //! @brief Moves value to the end.
  455. //!
  456. //! @param value The value to move construct the new element.
  457. //!
  458. //! @par Throws
  459. //! If Value's move constructor throws.
  460. //!
  461. //! @par Complexity
  462. //! Constant O(1).
  463. void push_back(BOOST_RV_REF(value_type) value);
  464. //! @pre <tt>!empty()</tt>
  465. //!
  466. //! @brief Destroys last value and decreases the size.
  467. //!
  468. //! @par Throws
  469. //! Nothing by default.
  470. //!
  471. //! @par Complexity
  472. //! Constant O(1).
  473. void pop_back();
  474. //! @pre
  475. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  476. //! @li <tt>size() < capacity()</tt>
  477. //!
  478. //! @brief Inserts a copy of element at p.
  479. //!
  480. //! @param p The position at which the new value will be inserted.
  481. //! @param value The value used to copy construct the new element.
  482. //!
  483. //! @par Throws
  484. //! @li If Value's copy constructor or copy assignment throws
  485. //! @li If Value's move constructor or move assignment throws.
  486. //!
  487. //! @par Complexity
  488. //! Constant or linear.
  489. iterator insert(const_iterator p, value_type const& value);
  490. //! @pre
  491. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  492. //! @li <tt>size() < capacity()</tt>
  493. //!
  494. //! @brief Inserts a move-constructed element at p.
  495. //!
  496. //! @param p The position at which the new value will be inserted.
  497. //! @param value The value used to move construct the new element.
  498. //!
  499. //! @par Throws
  500. //! If Value's move constructor or move assignment throws.
  501. //!
  502. //! @par Complexity
  503. //! Constant or linear.
  504. iterator insert(const_iterator p, BOOST_RV_REF(value_type) value);
  505. //! @pre
  506. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  507. //! @li <tt>size() + count <= capacity()</tt>
  508. //!
  509. //! @brief Inserts a count copies of value at p.
  510. //!
  511. //! @param p The position at which new elements will be inserted.
  512. //! @param count The number of new elements which will be inserted.
  513. //! @param value The value used to copy construct new elements.
  514. //!
  515. //! @par Throws
  516. //! @li If Value's copy constructor or copy assignment throws.
  517. //! @li If Value's move constructor or move assignment throws.
  518. //!
  519. //! @par Complexity
  520. //! Linear O(N).
  521. iterator insert(const_iterator p, size_type count, value_type const& value);
  522. //! @pre
  523. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  524. //! @li <tt>distance(first, last) <= capacity()</tt>
  525. //! @li \c Iterator must meet the \c ForwardTraversalIterator concept.
  526. //!
  527. //! @brief Inserts a copy of a range <tt>[first, last)</tt> at p.
  528. //!
  529. //! @param p The position at which new elements will be inserted.
  530. //! @param first The iterator to the first element of a range used to construct new elements.
  531. //! @param last The iterator to the one after the last element of a range used to construct new elements.
  532. //!
  533. //! @par Throws
  534. //! @li If Value's constructor and assignment taking a dereferenced \c Iterator.
  535. //! @li If Value's move constructor or move assignment throws.
  536. //!
  537. //! @par Complexity
  538. //! Linear O(N).
  539. template <typename Iterator>
  540. iterator insert(const_iterator p, Iterator first, Iterator last);
  541. //! @pre
  542. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  543. //! @li <tt>distance(il.begin(), il.end()) <= capacity()</tt>
  544. //!
  545. //! @brief Inserts a copy of a range <tt>[il.begin(), il.end())</tt> at p.
  546. //!
  547. //! @param p The position at which new elements will be inserted.
  548. //! @param il The std::initializer_list which contains elements that will be inserted.
  549. //!
  550. //! @par Throws
  551. //! @li If Value's constructor and assignment taking a dereferenced std::initializer_list iterator.
  552. //!
  553. //! @par Complexity
  554. //! Linear O(N).
  555. iterator insert(const_iterator p, std::initializer_list<value_type> il);
  556. //! @pre \c p must be a valid iterator of \c *this in range <tt>[begin(), end())</tt>
  557. //!
  558. //! @brief Erases Value from p.
  559. //!
  560. //! @param p The position of the element which will be erased from the container.
  561. //!
  562. //! @par Throws
  563. //! If Value's move assignment throws.
  564. //!
  565. //! @par Complexity
  566. //! Linear O(N).
  567. iterator erase(const_iterator p);
  568. //! @pre
  569. //! @li \c first and \c last must define a valid range
  570. //! @li iterators must be in range <tt>[begin(), end()]</tt>
  571. //!
  572. //! @brief Erases Values from a range <tt>[first, last)</tt>.
  573. //!
  574. //! @param first The position of the first element of a range which will be erased from the container.
  575. //! @param last The position of the one after the last element of a range which will be erased from the container.
  576. //!
  577. //! @par Throws
  578. //! If Value's move assignment throws.
  579. //!
  580. //! @par Complexity
  581. //! Linear O(N).
  582. iterator erase(const_iterator first, const_iterator last);
  583. //! @pre <tt>distance(first, last) <= capacity()</tt>
  584. //!
  585. //! @brief Assigns a range <tt>[first, last)</tt> of Values to this container.
  586. //!
  587. //! @param first The iterator to the first element of a range used to construct new content of this container.
  588. //! @param last The iterator to the one after the last element of a range used to construct new content of this container.
  589. //!
  590. //! @par Throws
  591. //! If Value's copy constructor or copy assignment throws,
  592. //!
  593. //! @par Complexity
  594. //! Linear O(N).
  595. template <typename Iterator>
  596. void assign(Iterator first, Iterator last);
  597. //! @pre <tt>distance(il.begin(), il.end()) <= capacity()</tt>
  598. //!
  599. //! @brief Assigns a range <tt>[il.begin(), il.end())</tt> of Values to this container.
  600. //!
  601. //! @param il std::initializer_list with values used to construct new content of this container.
  602. //!
  603. //! @par Throws
  604. //! If Value's copy constructor or copy assignment throws,
  605. //!
  606. //! @par Complexity
  607. //! Linear O(N).
  608. void assign(std::initializer_list<value_type> il);
  609. //! @pre <tt>count <= capacity()</tt>
  610. //!
  611. //! @brief Assigns a count copies of value to this container.
  612. //!
  613. //! @param count The new number of elements which will be container in the container.
  614. //! @param value The value which will be used to copy construct the new content.
  615. //!
  616. //! @par Throws
  617. //! If Value's copy constructor or copy assignment throws.
  618. //!
  619. //! @par Complexity
  620. //! Linear O(N).
  621. void assign(size_type count, value_type const& value);
  622. //! @pre <tt>size() < capacity()</tt>
  623. //!
  624. //! @brief Inserts a Value constructed with
  625. //! \c std::forward<Args>(args)... in the end of the container.
  626. //!
  627. //! @return A reference to the created object.
  628. //!
  629. //! @param args The arguments of the constructor of the new element which will be created at the end of the container.
  630. //!
  631. //! @par Throws
  632. //! If in-place constructor throws or Value's move constructor throws.
  633. //!
  634. //! @par Complexity
  635. //! Constant O(1).
  636. template<class ...Args>
  637. reference emplace_back(Args &&...args);
  638. //! @pre
  639. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>
  640. //! @li <tt>size() < capacity()</tt>
  641. //!
  642. //! @brief Inserts a Value constructed with
  643. //! \c std::forward<Args>(args)... before p
  644. //!
  645. //! @param p The position at which new elements will be inserted.
  646. //! @param args The arguments of the constructor of the new element.
  647. //!
  648. //! @par Throws
  649. //! If in-place constructor throws or if Value's move constructor or move assignment throws.
  650. //!
  651. //! @par Complexity
  652. //! Constant or linear.
  653. template<class ...Args>
  654. iterator emplace(const_iterator p, Args &&...args);
  655. //! @brief Removes all elements from the container.
  656. //!
  657. //! @par Throws
  658. //! Nothing.
  659. //!
  660. //! @par Complexity
  661. //! Constant O(1).
  662. void clear() BOOST_NOEXCEPT_OR_NOTHROW;
  663. //! @pre <tt>i < size()</tt>
  664. //!
  665. //! @brief Returns reference to the i-th element.
  666. //!
  667. //! @param i The element's index.
  668. //!
  669. //! @return reference to the i-th element
  670. //! from the beginning of the container.
  671. //!
  672. //! @par Throws
  673. //! \c std::out_of_range exception by default.
  674. //!
  675. //! @par Complexity
  676. //! Constant O(1).
  677. reference at(size_type i);
  678. //! @pre <tt>i < size()</tt>
  679. //!
  680. //! @brief Returns const reference to the i-th element.
  681. //!
  682. //! @param i The element's index.
  683. //!
  684. //! @return const reference to the i-th element
  685. //! from the beginning of the container.
  686. //!
  687. //! @par Throws
  688. //! \c std::out_of_range exception by default.
  689. //!
  690. //! @par Complexity
  691. //! Constant O(1).
  692. const_reference at(size_type i) const;
  693. //! @pre <tt>i < size()</tt>
  694. //!
  695. //! @brief Returns reference to the i-th element.
  696. //!
  697. //! @param i The element's index.
  698. //!
  699. //! @return reference to the i-th element
  700. //! from the beginning of the container.
  701. //!
  702. //! @par Throws
  703. //! Nothing by default.
  704. //!
  705. //! @par Complexity
  706. //! Constant O(1).
  707. reference operator[](size_type i);
  708. //! @pre <tt>i < size()</tt>
  709. //!
  710. //! @brief Returns const reference to the i-th element.
  711. //!
  712. //! @param i The element's index.
  713. //!
  714. //! @return const reference to the i-th element
  715. //! from the beginning of the container.
  716. //!
  717. //! @par Throws
  718. //! Nothing by default.
  719. //!
  720. //! @par Complexity
  721. //! Constant O(1).
  722. const_reference operator[](size_type i) const;
  723. //! @pre <tt>i =< size()</tt>
  724. //!
  725. //! @brief Returns a iterator to the i-th element.
  726. //!
  727. //! @param i The element's index.
  728. //!
  729. //! @return a iterator to the i-th element.
  730. //!
  731. //! @par Throws
  732. //! Nothing by default.
  733. //!
  734. //! @par Complexity
  735. //! Constant O(1).
  736. iterator nth(size_type i);
  737. //! @pre <tt>i =< size()</tt>
  738. //!
  739. //! @brief Returns a const_iterator to the i-th element.
  740. //!
  741. //! @param i The element's index.
  742. //!
  743. //! @return a const_iterator to the i-th element.
  744. //!
  745. //! @par Throws
  746. //! Nothing by default.
  747. //!
  748. //! @par Complexity
  749. //! Constant O(1).
  750. const_iterator nth(size_type i) const;
  751. //! @pre <tt>begin() <= p <= end()</tt>
  752. //!
  753. //! @brief Returns the index of the element pointed by p.
  754. //!
  755. //! @param p An iterator to the element.
  756. //!
  757. //! @return The index of the element pointed by p.
  758. //!
  759. //! @par Throws
  760. //! Nothing by default.
  761. //!
  762. //! @par Complexity
  763. //! Constant O(1).
  764. size_type index_of(iterator p);
  765. //! @pre <tt>begin() <= p <= end()</tt>
  766. //!
  767. //! @brief Returns the index of the element pointed by p.
  768. //!
  769. //! @param p A const_iterator to the element.
  770. //!
  771. //! @return a const_iterator to the i-th element.
  772. //!
  773. //! @par Throws
  774. //! Nothing by default.
  775. //!
  776. //! @par Complexity
  777. //! Constant O(1).
  778. size_type index_of(const_iterator p) const;
  779. //! @pre \c !empty()
  780. //!
  781. //! @brief Returns reference to the first element.
  782. //!
  783. //! @return reference to the first element
  784. //! from the beginning of the container.
  785. //!
  786. //! @par Throws
  787. //! Nothing by default.
  788. //!
  789. //! @par Complexity
  790. //! Constant O(1).
  791. reference front();
  792. //! @pre \c !empty()
  793. //!
  794. //! @brief Returns const reference to the first element.
  795. //!
  796. //! @return const reference to the first element
  797. //! from the beginning of the container.
  798. //!
  799. //! @par Throws
  800. //! Nothing by default.
  801. //!
  802. //! @par Complexity
  803. //! Constant O(1).
  804. const_reference front() const;
  805. //! @pre \c !empty()
  806. //!
  807. //! @brief Returns reference to the last element.
  808. //!
  809. //! @return reference to the last element
  810. //! from the beginning of the container.
  811. //!
  812. //! @par Throws
  813. //! Nothing by default.
  814. //!
  815. //! @par Complexity
  816. //! Constant O(1).
  817. reference back();
  818. //! @pre \c !empty()
  819. //!
  820. //! @brief Returns const reference to the first element.
  821. //!
  822. //! @return const reference to the last element
  823. //! from the beginning of the container.
  824. //!
  825. //! @par Throws
  826. //! Nothing by default.
  827. //!
  828. //! @par Complexity
  829. //! Constant O(1).
  830. const_reference back() const;
  831. //! @brief Pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  832. //! For a non-empty vector <tt>data() == &front()</tt>.
  833. //!
  834. //! @par Throws
  835. //! Nothing.
  836. //!
  837. //! @par Complexity
  838. //! Constant O(1).
  839. Value * data() BOOST_NOEXCEPT_OR_NOTHROW;
  840. //! @brief Const pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  841. //! For a non-empty vector <tt>data() == &front()</tt>.
  842. //!
  843. //! @par Throws
  844. //! Nothing.
  845. //!
  846. //! @par Complexity
  847. //! Constant O(1).
  848. const Value * data() const BOOST_NOEXCEPT_OR_NOTHROW;
  849. //! @brief Returns iterator to the first element.
  850. //!
  851. //! @return iterator to the first element contained in the vector.
  852. //!
  853. //! @par Throws
  854. //! Nothing.
  855. //!
  856. //! @par Complexity
  857. //! Constant O(1).
  858. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW;
  859. //! @brief Returns const iterator to the first element.
  860. //!
  861. //! @return const_iterator to the first element contained in the vector.
  862. //!
  863. //! @par Throws
  864. //! Nothing.
  865. //!
  866. //! @par Complexity
  867. //! Constant O(1).
  868. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW;
  869. //! @brief Returns const iterator to the first element.
  870. //!
  871. //! @return const_iterator to the first element contained in the vector.
  872. //!
  873. //! @par Throws
  874. //! Nothing.
  875. //!
  876. //! @par Complexity
  877. //! Constant O(1).
  878. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  879. //! @brief Returns iterator to the one after the last element.
  880. //!
  881. //! @return iterator pointing to the one after the last element contained in the vector.
  882. //!
  883. //! @par Throws
  884. //! Nothing.
  885. //!
  886. //! @par Complexity
  887. //! Constant O(1).
  888. iterator end() BOOST_NOEXCEPT_OR_NOTHROW;
  889. //! @brief Returns const iterator to the one after the last element.
  890. //!
  891. //! @return const_iterator pointing to the one after the last element contained in the vector.
  892. //!
  893. //! @par Throws
  894. //! Nothing.
  895. //!
  896. //! @par Complexity
  897. //! Constant O(1).
  898. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW;
  899. //! @brief Returns const iterator to the one after the last element.
  900. //!
  901. //! @return const_iterator pointing to the one after the last element contained in the vector.
  902. //!
  903. //! @par Throws
  904. //! Nothing.
  905. //!
  906. //! @par Complexity
  907. //! Constant O(1).
  908. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW;
  909. //! @brief Returns reverse iterator to the first element of the reversed container.
  910. //!
  911. //! @return reverse_iterator pointing to the beginning
  912. //! of the reversed static_vector.
  913. //!
  914. //! @par Throws
  915. //! Nothing.
  916. //!
  917. //! @par Complexity
  918. //! Constant O(1).
  919. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW;
  920. //! @brief Returns const reverse iterator to the first element of the reversed container.
  921. //!
  922. //! @return const_reverse_iterator pointing to the beginning
  923. //! of the reversed static_vector.
  924. //!
  925. //! @par Throws
  926. //! Nothing.
  927. //!
  928. //! @par Complexity
  929. //! Constant O(1).
  930. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  931. //! @brief Returns const reverse iterator to the first element of the reversed container.
  932. //!
  933. //! @return const_reverse_iterator pointing to the beginning
  934. //! of the reversed static_vector.
  935. //!
  936. //! @par Throws
  937. //! Nothing.
  938. //!
  939. //! @par Complexity
  940. //! Constant O(1).
  941. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  942. //! @brief Returns reverse iterator to the one after the last element of the reversed container.
  943. //!
  944. //! @return reverse_iterator pointing to the one after the last element
  945. //! of the reversed static_vector.
  946. //!
  947. //! @par Throws
  948. //! Nothing.
  949. //!
  950. //! @par Complexity
  951. //! Constant O(1).
  952. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW;
  953. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  954. //!
  955. //! @return const_reverse_iterator pointing to the one after the last element
  956. //! of the reversed static_vector.
  957. //!
  958. //! @par Throws
  959. //! Nothing.
  960. //!
  961. //! @par Complexity
  962. //! Constant O(1).
  963. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW;
  964. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  965. //!
  966. //! @return const_reverse_iterator pointing to the one after the last element
  967. //! of the reversed static_vector.
  968. //!
  969. //! @par Throws
  970. //! Nothing.
  971. //!
  972. //! @par Complexity
  973. //! Constant O(1).
  974. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW;
  975. //! @brief Returns container's capacity.
  976. //!
  977. //! @return container's capacity.
  978. //!
  979. //! @par Throws
  980. //! Nothing.
  981. //!
  982. //! @par Complexity
  983. //! Constant O(1).
  984. static size_type capacity() BOOST_NOEXCEPT_OR_NOTHROW;
  985. //! @brief Returns container's capacity.
  986. //!
  987. //! @return container's capacity.
  988. //!
  989. //! @par Throws
  990. //! Nothing.
  991. //!
  992. //! @par Complexity
  993. //! Constant O(1).
  994. static size_type max_size() BOOST_NOEXCEPT_OR_NOTHROW;
  995. //! @brief Returns the number of stored elements.
  996. //!
  997. //! @return Number of elements contained in the container.
  998. //!
  999. //! @par Throws
  1000. //! Nothing.
  1001. //!
  1002. //! @par Complexity
  1003. //! Constant O(1).
  1004. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW;
  1005. //! @brief Queries if the container contains elements.
  1006. //!
  1007. //! @return true if the number of elements contained in the
  1008. //! container is equal to 0.
  1009. //!
  1010. //! @par Throws
  1011. //! Nothing.
  1012. //!
  1013. //! @par Complexity
  1014. //! Constant O(1).
  1015. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW;
  1016. #else
  1017. BOOST_CONTAINER_FORCEINLINE friend void swap(static_vector &x, static_vector &y)
  1018. {
  1019. x.swap(y);
  1020. }
  1021. #endif // BOOST_CONTAINER_DOXYGEN_INVOKED
  1022. };
  1023. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  1024. //! @brief Checks if contents of two static_vectors are equal.
  1025. //!
  1026. //! @ingroup static_vector_non_member
  1027. //!
  1028. //! @param x The first static_vector.
  1029. //! @param y The second static_vector.
  1030. //!
  1031. //! @return \c true if containers have the same size and elements in both containers are equal.
  1032. //!
  1033. //! @par Complexity
  1034. //! Linear O(N).
  1035. template<typename V, std::size_t C1, std::size_t C2>
  1036. bool operator== (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1037. //! @brief Checks if contents of two static_vectors are not equal.
  1038. //!
  1039. //! @ingroup static_vector_non_member
  1040. //!
  1041. //! @param x The first static_vector.
  1042. //! @param y The second static_vector.
  1043. //!
  1044. //! @return \c true if containers have different size or elements in both containers are not equal.
  1045. //!
  1046. //! @par Complexity
  1047. //! Linear O(N).
  1048. template<typename V, std::size_t C1, std::size_t C2>
  1049. bool operator!= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1050. //! @brief Lexicographically compares static_vectors.
  1051. //!
  1052. //! @ingroup static_vector_non_member
  1053. //!
  1054. //! @param x The first static_vector.
  1055. //! @param y The second static_vector.
  1056. //!
  1057. //! @return \c true if x compares lexicographically less than y.
  1058. //!
  1059. //! @par Complexity
  1060. //! Linear O(N).
  1061. template<typename V, std::size_t C1, std::size_t C2>
  1062. bool operator< (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1063. //! @brief Lexicographically compares static_vectors.
  1064. //!
  1065. //! @ingroup static_vector_non_member
  1066. //!
  1067. //! @param x The first static_vector.
  1068. //! @param y The second static_vector.
  1069. //!
  1070. //! @return \c true if y compares lexicographically less than x.
  1071. //!
  1072. //! @par Complexity
  1073. //! Linear O(N).
  1074. template<typename V, std::size_t C1, std::size_t C2>
  1075. bool operator> (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1076. //! @brief Lexicographically compares static_vectors.
  1077. //!
  1078. //! @ingroup static_vector_non_member
  1079. //!
  1080. //! @param x The first static_vector.
  1081. //! @param y The second static_vector.
  1082. //!
  1083. //! @return \c true if y don't compare lexicographically less than x.
  1084. //!
  1085. //! @par Complexity
  1086. //! Linear O(N).
  1087. template<typename V, std::size_t C1, std::size_t C2>
  1088. bool operator<= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1089. //! @brief Lexicographically compares static_vectors.
  1090. //!
  1091. //! @ingroup static_vector_non_member
  1092. //!
  1093. //! @param x The first static_vector.
  1094. //! @param y The second static_vector.
  1095. //!
  1096. //! @return \c true if x don't compare lexicographically less than y.
  1097. //!
  1098. //! @par Complexity
  1099. //! Linear O(N).
  1100. template<typename V, std::size_t C1, std::size_t C2>
  1101. bool operator>= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1102. //! @brief Swaps contents of two static_vectors.
  1103. //!
  1104. //! This function calls static_vector::swap().
  1105. //!
  1106. //! @ingroup static_vector_non_member
  1107. //!
  1108. //! @param x The first static_vector.
  1109. //! @param y The second static_vector.
  1110. //!
  1111. //! @par Complexity
  1112. //! Linear O(N).
  1113. template<typename V, std::size_t C1, std::size_t C2>
  1114. inline void swap(static_vector<V, C1> & x, static_vector<V, C2> & y);
  1115. #else
  1116. template<typename V, std::size_t C1, std::size_t C2>
  1117. inline void swap(static_vector<V, C1> & x, static_vector<V, C2> & y
  1118. , typename dtl::enable_if_c< C1 != C2>::type * = 0)
  1119. {
  1120. x.swap(y);
  1121. }
  1122. #endif // BOOST_CONTAINER_DOXYGEN_INVOKED
  1123. }} // namespace boost::container
  1124. #include <boost/container/detail/config_end.hpp>
  1125. #endif // BOOST_CONTAINER_STATIC_VECTOR_HPP