static_vector.hpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 bounds 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. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_assignable<value_type>::value)
  331. {
  332. return static_cast<static_vector&>(base_t::operator=(BOOST_MOVE_BASE(base_t, other)));
  333. }
  334. //! @pre <tt>other.size() <= capacity()</tt>
  335. //!
  336. //! @brief Move assignment. Moves Values stored in the other static_vector to this one.
  337. //!
  338. //! @param other The static_vector which content will be moved to this one.
  339. //!
  340. //! @par Throws
  341. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws.
  342. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws.
  343. //!
  344. //! @par Complexity
  345. //! Linear O(N).
  346. template <std::size_t C>
  347. BOOST_CONTAINER_FORCEINLINE static_vector & operator=(BOOST_RV_REF_BEG static_vector<value_type, C> BOOST_RV_REF_END other)
  348. {
  349. return static_cast<static_vector&>(base_t::operator=
  350. (BOOST_MOVE_BASE(typename static_vector<value_type BOOST_MOVE_I C>::base_t, other)));
  351. }
  352. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  353. //! @brief Destructor. Destroys Values stored in this container.
  354. //!
  355. //! @par Throws
  356. //! Nothing
  357. //!
  358. //! @par Complexity
  359. //! Linear O(N).
  360. ~static_vector();
  361. //! @brief Swaps contents of the other static_vector and this one.
  362. //!
  363. //! @param other The static_vector which content will be swapped with this one's content.
  364. //!
  365. //! @par Throws
  366. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  367. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  368. //!
  369. //! @par Complexity
  370. //! Linear O(N).
  371. void swap(static_vector & other);
  372. //! @pre <tt>other.size() <= capacity() && size() <= other.capacity()</tt>
  373. //!
  374. //! @brief Swaps contents of the other static_vector and this one.
  375. //!
  376. //! @param other The static_vector which content will be swapped with this one's content.
  377. //!
  378. //! @par Throws
  379. //! @li If \c has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  380. //! @li If \c has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  381. //!
  382. //! @par Complexity
  383. //! Linear O(N).
  384. template <std::size_t C>
  385. void swap(static_vector<value_type, C> & other);
  386. //! @pre <tt>count <= capacity()</tt>
  387. //!
  388. //! @brief Inserts or erases elements at the end such that
  389. //! the size becomes count. New elements are value initialized.
  390. //!
  391. //! @param count The number of elements which will be stored in the container.
  392. //!
  393. //! @par Throws
  394. //! If Value's value initialization throws.
  395. //!
  396. //! @par Complexity
  397. //! Linear O(N).
  398. void resize(size_type count);
  399. //! @pre <tt>count <= capacity()</tt>
  400. //!
  401. //! @brief Inserts or erases elements at the end such that
  402. //! the size becomes count. New elements are default initialized.
  403. //!
  404. //! @param count The number of elements which will be stored in the container.
  405. //!
  406. //! @par Throws
  407. //! If Value's default initialization throws.
  408. //!
  409. //! @par Complexity
  410. //! Linear O(N).
  411. //!
  412. //! @par Note
  413. //! Non-standard extension
  414. void resize(size_type count, default_init_t);
  415. //! @pre <tt>count <= capacity()</tt>
  416. //!
  417. //! @brief Inserts or erases elements at the end such that
  418. //! the size becomes count. New elements are copy constructed from value.
  419. //!
  420. //! @param count The number of elements which will be stored in the container.
  421. //! @param value The value used to copy construct the new element.
  422. //!
  423. //! @par Throws
  424. //! If Value's copy constructor throws.
  425. //!
  426. //! @par Complexity
  427. //! Linear O(N).
  428. void resize(size_type count, value_type const& value);
  429. //! @pre <tt>count <= capacity()</tt>
  430. //!
  431. //! @brief This call has no effect because the Capacity of this container is constant.
  432. //!
  433. //! @param count The number of elements which the container should be able to contain.
  434. //!
  435. //! @par Throws
  436. //! Nothing.
  437. //!
  438. //! @par Complexity
  439. //! Linear O(N).
  440. void reserve(size_type count) BOOST_NOEXCEPT_OR_NOTHROW;
  441. //! @pre <tt>size() < capacity()</tt>
  442. //!
  443. //! @brief Adds a copy of value at the end.
  444. //!
  445. //! @param value The value used to copy construct the new element.
  446. //!
  447. //! @par Throws
  448. //! If Value's copy constructor throws.
  449. //!
  450. //! @par Complexity
  451. //! Constant O(1).
  452. void push_back(value_type const& value);
  453. //! @pre <tt>size() < capacity()</tt>
  454. //!
  455. //! @brief Moves value to the end.
  456. //!
  457. //! @param value The value to move construct the new element.
  458. //!
  459. //! @par Throws
  460. //! If Value's move constructor throws.
  461. //!
  462. //! @par Complexity
  463. //! Constant O(1).
  464. void push_back(BOOST_RV_REF(value_type) value);
  465. //! @pre <tt>!empty()</tt>
  466. //!
  467. //! @brief Destroys last value and decreases the size.
  468. //!
  469. //! @par Throws
  470. //! Nothing by default.
  471. //!
  472. //! @par Complexity
  473. //! Constant O(1).
  474. void pop_back();
  475. //! @pre
  476. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  477. //! @li <tt>size() < capacity()</tt>
  478. //!
  479. //! @brief Inserts a copy of element at p.
  480. //!
  481. //! @param p The position at which the new value will be inserted.
  482. //! @param value The value used to copy construct the new element.
  483. //!
  484. //! @par Throws
  485. //! @li If Value's copy constructor or copy assignment throws
  486. //! @li If Value's move constructor or move assignment throws.
  487. //!
  488. //! @par Complexity
  489. //! Constant or linear.
  490. iterator insert(const_iterator p, value_type const& value);
  491. //! @pre
  492. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  493. //! @li <tt>size() < capacity()</tt>
  494. //!
  495. //! @brief Inserts a move-constructed element at p.
  496. //!
  497. //! @param p The position at which the new value will be inserted.
  498. //! @param value The value used to move construct the new element.
  499. //!
  500. //! @par Throws
  501. //! If Value's move constructor or move assignment throws.
  502. //!
  503. //! @par Complexity
  504. //! Constant or linear.
  505. iterator insert(const_iterator p, BOOST_RV_REF(value_type) value);
  506. //! @pre
  507. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  508. //! @li <tt>size() + count <= capacity()</tt>
  509. //!
  510. //! @brief Inserts a count copies of value at p.
  511. //!
  512. //! @param p The position at which new elements will be inserted.
  513. //! @param count The number of new elements which will be inserted.
  514. //! @param value The value used to copy construct new elements.
  515. //!
  516. //! @par Throws
  517. //! @li If Value's copy constructor or copy assignment throws.
  518. //! @li If Value's move constructor or move assignment throws.
  519. //!
  520. //! @par Complexity
  521. //! Linear O(N).
  522. iterator insert(const_iterator p, size_type count, value_type const& value);
  523. //! @pre
  524. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  525. //! @li <tt>distance(first, last) <= capacity()</tt>
  526. //! @li \c Iterator must meet the \c ForwardTraversalIterator concept.
  527. //!
  528. //! @brief Inserts a copy of a range <tt>[first, last)</tt> at p.
  529. //!
  530. //! @param p The position at which new elements will be inserted.
  531. //! @param first The iterator to the first element of a range used to construct new elements.
  532. //! @param last The iterator to the one after the last element of a range used to construct new elements.
  533. //!
  534. //! @par Throws
  535. //! @li If Value's constructor and assignment taking a dereferenced \c Iterator.
  536. //! @li If Value's move constructor or move assignment throws.
  537. //!
  538. //! @par Complexity
  539. //! Linear O(N).
  540. template <typename Iterator>
  541. iterator insert(const_iterator p, Iterator first, Iterator last);
  542. //! @pre
  543. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  544. //! @li <tt>distance(il.begin(), il.end()) <= capacity()</tt>
  545. //!
  546. //! @brief Inserts a copy of a range <tt>[il.begin(), il.end())</tt> at p.
  547. //!
  548. //! @param p The position at which new elements will be inserted.
  549. //! @param il The std::initializer_list which contains elements that will be inserted.
  550. //!
  551. //! @par Throws
  552. //! @li If Value's constructor and assignment taking a dereferenced std::initializer_list iterator.
  553. //!
  554. //! @par Complexity
  555. //! Linear O(N).
  556. iterator insert(const_iterator p, std::initializer_list<value_type> il);
  557. //! @pre \c p must be a valid iterator of \c *this in range <tt>[begin(), end())</tt>
  558. //!
  559. //! @brief Erases Value from p.
  560. //!
  561. //! @param p The position of the element which will be erased from the container.
  562. //!
  563. //! @par Throws
  564. //! If Value's move assignment throws.
  565. //!
  566. //! @par Complexity
  567. //! Linear O(N).
  568. iterator erase(const_iterator p);
  569. //! @pre
  570. //! @li \c first and \c last must define a valid range
  571. //! @li iterators must be in range <tt>[begin(), end()]</tt>
  572. //!
  573. //! @brief Erases Values from a range <tt>[first, last)</tt>.
  574. //!
  575. //! @param first The position of the first element of a range which will be erased from the container.
  576. //! @param last The position of the one after the last element of a range which will be erased from the container.
  577. //!
  578. //! @par Throws
  579. //! If Value's move assignment throws.
  580. //!
  581. //! @par Complexity
  582. //! Linear O(N).
  583. iterator erase(const_iterator first, const_iterator last);
  584. //! @pre <tt>distance(first, last) <= capacity()</tt>
  585. //!
  586. //! @brief Assigns a range <tt>[first, last)</tt> of Values to this container.
  587. //!
  588. //! @param first The iterator to the first element of a range used to construct new content of this container.
  589. //! @param last The iterator to the one after the last element of a range used to construct new content of this container.
  590. //!
  591. //! @par Throws
  592. //! If Value's copy constructor or copy assignment throws,
  593. //!
  594. //! @par Complexity
  595. //! Linear O(N).
  596. template <typename Iterator>
  597. void assign(Iterator first, Iterator last);
  598. //! @pre <tt>distance(il.begin(), il.end()) <= capacity()</tt>
  599. //!
  600. //! @brief Assigns a range <tt>[il.begin(), il.end())</tt> of Values to this container.
  601. //!
  602. //! @param il std::initializer_list with values used to construct new content of this container.
  603. //!
  604. //! @par Throws
  605. //! If Value's copy constructor or copy assignment throws,
  606. //!
  607. //! @par Complexity
  608. //! Linear O(N).
  609. void assign(std::initializer_list<value_type> il);
  610. //! @pre <tt>count <= capacity()</tt>
  611. //!
  612. //! @brief Assigns a count copies of value to this container.
  613. //!
  614. //! @param count The new number of elements which will be container in the container.
  615. //! @param value The value which will be used to copy construct the new content.
  616. //!
  617. //! @par Throws
  618. //! If Value's copy constructor or copy assignment throws.
  619. //!
  620. //! @par Complexity
  621. //! Linear O(N).
  622. void assign(size_type count, value_type const& value);
  623. //! @pre <tt>size() < capacity()</tt>
  624. //!
  625. //! @brief Inserts a Value constructed with
  626. //! \c std::forward<Args>(args)... in the end of the container.
  627. //!
  628. //! @return A reference to the created object.
  629. //!
  630. //! @param args The arguments of the constructor of the new element which will be created at the end of the container.
  631. //!
  632. //! @par Throws
  633. //! If in-place constructor throws or Value's move constructor throws.
  634. //!
  635. //! @par Complexity
  636. //! Constant O(1).
  637. template<class ...Args>
  638. reference emplace_back(Args &&...args);
  639. //! @pre
  640. //! @li \c p must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>
  641. //! @li <tt>size() < capacity()</tt>
  642. //!
  643. //! @brief Inserts a Value constructed with
  644. //! \c std::forward<Args>(args)... before p
  645. //!
  646. //! @param p The position at which new elements will be inserted.
  647. //! @param args The arguments of the constructor of the new element.
  648. //!
  649. //! @par Throws
  650. //! If in-place constructor throws or if Value's move constructor or move assignment throws.
  651. //!
  652. //! @par Complexity
  653. //! Constant or linear.
  654. template<class ...Args>
  655. iterator emplace(const_iterator p, Args &&...args);
  656. //! @brief Removes all elements from the container.
  657. //!
  658. //! @par Throws
  659. //! Nothing.
  660. //!
  661. //! @par Complexity
  662. //! Constant O(1).
  663. void clear() BOOST_NOEXCEPT_OR_NOTHROW;
  664. //! @pre <tt>i < size()</tt>
  665. //!
  666. //! @brief Returns reference to the i-th element.
  667. //!
  668. //! @param i The element's index.
  669. //!
  670. //! @return reference to the i-th element
  671. //! from the beginning of the container.
  672. //!
  673. //! @par Throws
  674. //! \c std::out_of_range exception by default.
  675. //!
  676. //! @par Complexity
  677. //! Constant O(1).
  678. reference at(size_type i);
  679. //! @pre <tt>i < size()</tt>
  680. //!
  681. //! @brief Returns const reference to the i-th element.
  682. //!
  683. //! @param i The element's index.
  684. //!
  685. //! @return const reference to the i-th element
  686. //! from the beginning of the container.
  687. //!
  688. //! @par Throws
  689. //! \c std::out_of_range exception by default.
  690. //!
  691. //! @par Complexity
  692. //! Constant O(1).
  693. const_reference at(size_type i) const;
  694. //! @pre <tt>i < size()</tt>
  695. //!
  696. //! @brief Returns reference to the i-th element.
  697. //!
  698. //! @param i The element's index.
  699. //!
  700. //! @return reference to the i-th element
  701. //! from the beginning of the container.
  702. //!
  703. //! @par Throws
  704. //! Nothing by default.
  705. //!
  706. //! @par Complexity
  707. //! Constant O(1).
  708. reference operator[](size_type i);
  709. //! @pre <tt>i < size()</tt>
  710. //!
  711. //! @brief Returns const reference to the i-th element.
  712. //!
  713. //! @param i The element's index.
  714. //!
  715. //! @return const reference to the i-th element
  716. //! from the beginning of the container.
  717. //!
  718. //! @par Throws
  719. //! Nothing by default.
  720. //!
  721. //! @par Complexity
  722. //! Constant O(1).
  723. const_reference operator[](size_type i) const;
  724. //! @pre <tt>i =< size()</tt>
  725. //!
  726. //! @brief Returns a iterator to the i-th element.
  727. //!
  728. //! @param i The element's index.
  729. //!
  730. //! @return a iterator to the i-th element.
  731. //!
  732. //! @par Throws
  733. //! Nothing by default.
  734. //!
  735. //! @par Complexity
  736. //! Constant O(1).
  737. iterator nth(size_type i);
  738. //! @pre <tt>i =< size()</tt>
  739. //!
  740. //! @brief Returns a const_iterator to the i-th element.
  741. //!
  742. //! @param i The element's index.
  743. //!
  744. //! @return a const_iterator to the i-th element.
  745. //!
  746. //! @par Throws
  747. //! Nothing by default.
  748. //!
  749. //! @par Complexity
  750. //! Constant O(1).
  751. const_iterator nth(size_type i) const;
  752. //! @pre <tt>begin() <= p <= end()</tt>
  753. //!
  754. //! @brief Returns the index of the element pointed by p.
  755. //!
  756. //! @param p An iterator to the element.
  757. //!
  758. //! @return The index of the element pointed by p.
  759. //!
  760. //! @par Throws
  761. //! Nothing by default.
  762. //!
  763. //! @par Complexity
  764. //! Constant O(1).
  765. size_type index_of(iterator p);
  766. //! @pre <tt>begin() <= p <= end()</tt>
  767. //!
  768. //! @brief Returns the index of the element pointed by p.
  769. //!
  770. //! @param p A const_iterator to the element.
  771. //!
  772. //! @return a const_iterator to the i-th element.
  773. //!
  774. //! @par Throws
  775. //! Nothing by default.
  776. //!
  777. //! @par Complexity
  778. //! Constant O(1).
  779. size_type index_of(const_iterator p) const;
  780. //! @pre \c !empty()
  781. //!
  782. //! @brief Returns reference to the first element.
  783. //!
  784. //! @return reference to the first element
  785. //! from the beginning of the container.
  786. //!
  787. //! @par Throws
  788. //! Nothing by default.
  789. //!
  790. //! @par Complexity
  791. //! Constant O(1).
  792. reference front();
  793. //! @pre \c !empty()
  794. //!
  795. //! @brief Returns const reference to the first element.
  796. //!
  797. //! @return const reference to the first element
  798. //! from the beginning of the container.
  799. //!
  800. //! @par Throws
  801. //! Nothing by default.
  802. //!
  803. //! @par Complexity
  804. //! Constant O(1).
  805. const_reference front() const;
  806. //! @pre \c !empty()
  807. //!
  808. //! @brief Returns reference to the last element.
  809. //!
  810. //! @return reference to the last element
  811. //! from the beginning of the container.
  812. //!
  813. //! @par Throws
  814. //! Nothing by default.
  815. //!
  816. //! @par Complexity
  817. //! Constant O(1).
  818. reference back();
  819. //! @pre \c !empty()
  820. //!
  821. //! @brief Returns const reference to the first element.
  822. //!
  823. //! @return const reference to the last element
  824. //! from the beginning of the container.
  825. //!
  826. //! @par Throws
  827. //! Nothing by default.
  828. //!
  829. //! @par Complexity
  830. //! Constant O(1).
  831. const_reference back() const;
  832. //! @brief Pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  833. //! For a non-empty vector <tt>data() == &front()</tt>.
  834. //!
  835. //! @par Throws
  836. //! Nothing.
  837. //!
  838. //! @par Complexity
  839. //! Constant O(1).
  840. Value * data() BOOST_NOEXCEPT_OR_NOTHROW;
  841. //! @brief Const pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  842. //! For a non-empty vector <tt>data() == &front()</tt>.
  843. //!
  844. //! @par Throws
  845. //! Nothing.
  846. //!
  847. //! @par Complexity
  848. //! Constant O(1).
  849. const Value * data() const BOOST_NOEXCEPT_OR_NOTHROW;
  850. //! @brief Returns iterator to the first element.
  851. //!
  852. //! @return iterator to the first element contained in the vector.
  853. //!
  854. //! @par Throws
  855. //! Nothing.
  856. //!
  857. //! @par Complexity
  858. //! Constant O(1).
  859. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW;
  860. //! @brief Returns const iterator to the first element.
  861. //!
  862. //! @return const_iterator to the first element contained in the vector.
  863. //!
  864. //! @par Throws
  865. //! Nothing.
  866. //!
  867. //! @par Complexity
  868. //! Constant O(1).
  869. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW;
  870. //! @brief Returns const iterator to the first element.
  871. //!
  872. //! @return const_iterator to the first element contained in the vector.
  873. //!
  874. //! @par Throws
  875. //! Nothing.
  876. //!
  877. //! @par Complexity
  878. //! Constant O(1).
  879. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  880. //! @brief Returns iterator to the one after the last element.
  881. //!
  882. //! @return iterator pointing to the one after the last element contained in the vector.
  883. //!
  884. //! @par Throws
  885. //! Nothing.
  886. //!
  887. //! @par Complexity
  888. //! Constant O(1).
  889. iterator end() BOOST_NOEXCEPT_OR_NOTHROW;
  890. //! @brief Returns const iterator to the one after the last element.
  891. //!
  892. //! @return const_iterator pointing to the one after the last element contained in the vector.
  893. //!
  894. //! @par Throws
  895. //! Nothing.
  896. //!
  897. //! @par Complexity
  898. //! Constant O(1).
  899. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW;
  900. //! @brief Returns const iterator to the one after the last element.
  901. //!
  902. //! @return const_iterator pointing to the one after the last element contained in the vector.
  903. //!
  904. //! @par Throws
  905. //! Nothing.
  906. //!
  907. //! @par Complexity
  908. //! Constant O(1).
  909. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW;
  910. //! @brief Returns reverse iterator to the first element of the reversed container.
  911. //!
  912. //! @return reverse_iterator pointing to the beginning
  913. //! of the reversed static_vector.
  914. //!
  915. //! @par Throws
  916. //! Nothing.
  917. //!
  918. //! @par Complexity
  919. //! Constant O(1).
  920. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW;
  921. //! @brief Returns const reverse iterator to the first element of the reversed container.
  922. //!
  923. //! @return const_reverse_iterator pointing to the beginning
  924. //! of the reversed static_vector.
  925. //!
  926. //! @par Throws
  927. //! Nothing.
  928. //!
  929. //! @par Complexity
  930. //! Constant O(1).
  931. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  932. //! @brief Returns const reverse iterator to the first element of the reversed container.
  933. //!
  934. //! @return const_reverse_iterator pointing to the beginning
  935. //! of the reversed static_vector.
  936. //!
  937. //! @par Throws
  938. //! Nothing.
  939. //!
  940. //! @par Complexity
  941. //! Constant O(1).
  942. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW;
  943. //! @brief Returns reverse iterator to the one after the last element of the reversed container.
  944. //!
  945. //! @return reverse_iterator pointing to the one after the last element
  946. //! of the reversed static_vector.
  947. //!
  948. //! @par Throws
  949. //! Nothing.
  950. //!
  951. //! @par Complexity
  952. //! Constant O(1).
  953. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW;
  954. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  955. //!
  956. //! @return const_reverse_iterator pointing to the one after the last element
  957. //! of the reversed static_vector.
  958. //!
  959. //! @par Throws
  960. //! Nothing.
  961. //!
  962. //! @par Complexity
  963. //! Constant O(1).
  964. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW;
  965. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  966. //!
  967. //! @return const_reverse_iterator pointing to the one after the last element
  968. //! of the reversed static_vector.
  969. //!
  970. //! @par Throws
  971. //! Nothing.
  972. //!
  973. //! @par Complexity
  974. //! Constant O(1).
  975. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW;
  976. //! @brief Returns container's capacity.
  977. //!
  978. //! @return container's capacity.
  979. //!
  980. //! @par Throws
  981. //! Nothing.
  982. //!
  983. //! @par Complexity
  984. //! Constant O(1).
  985. static size_type capacity() BOOST_NOEXCEPT_OR_NOTHROW;
  986. //! @brief Returns container's capacity.
  987. //!
  988. //! @return container's capacity.
  989. //!
  990. //! @par Throws
  991. //! Nothing.
  992. //!
  993. //! @par Complexity
  994. //! Constant O(1).
  995. static size_type max_size() BOOST_NOEXCEPT_OR_NOTHROW;
  996. //! @brief Returns the number of stored elements.
  997. //!
  998. //! @return Number of elements contained in the container.
  999. //!
  1000. //! @par Throws
  1001. //! Nothing.
  1002. //!
  1003. //! @par Complexity
  1004. //! Constant O(1).
  1005. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW;
  1006. //! @brief Queries if the container contains elements.
  1007. //!
  1008. //! @return true if the number of elements contained in the
  1009. //! container is equal to 0.
  1010. //!
  1011. //! @par Throws
  1012. //! Nothing.
  1013. //!
  1014. //! @par Complexity
  1015. //! Constant O(1).
  1016. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW;
  1017. #else
  1018. BOOST_CONTAINER_FORCEINLINE friend void swap(static_vector &x, static_vector &y)
  1019. {
  1020. x.swap(y);
  1021. }
  1022. #endif // BOOST_CONTAINER_DOXYGEN_INVOKED
  1023. };
  1024. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  1025. //! @brief Checks if contents of two static_vectors are equal.
  1026. //!
  1027. //! @ingroup static_vector_non_member
  1028. //!
  1029. //! @param x The first static_vector.
  1030. //! @param y The second static_vector.
  1031. //!
  1032. //! @return \c true if containers have the same size and elements in both containers are equal.
  1033. //!
  1034. //! @par Complexity
  1035. //! Linear O(N).
  1036. template<typename V, std::size_t C1, std::size_t C2>
  1037. bool operator== (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1038. //! @brief Checks if contents of two static_vectors are not equal.
  1039. //!
  1040. //! @ingroup static_vector_non_member
  1041. //!
  1042. //! @param x The first static_vector.
  1043. //! @param y The second static_vector.
  1044. //!
  1045. //! @return \c true if containers have different size or elements in both containers are not equal.
  1046. //!
  1047. //! @par Complexity
  1048. //! Linear O(N).
  1049. template<typename V, std::size_t C1, std::size_t C2>
  1050. bool operator!= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1051. //! @brief Lexicographically compares static_vectors.
  1052. //!
  1053. //! @ingroup static_vector_non_member
  1054. //!
  1055. //! @param x The first static_vector.
  1056. //! @param y The second static_vector.
  1057. //!
  1058. //! @return \c true if x compares lexicographically less than y.
  1059. //!
  1060. //! @par Complexity
  1061. //! Linear O(N).
  1062. template<typename V, std::size_t C1, std::size_t C2>
  1063. bool operator< (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1064. //! @brief Lexicographically compares static_vectors.
  1065. //!
  1066. //! @ingroup static_vector_non_member
  1067. //!
  1068. //! @param x The first static_vector.
  1069. //! @param y The second static_vector.
  1070. //!
  1071. //! @return \c true if y compares lexicographically less than x.
  1072. //!
  1073. //! @par Complexity
  1074. //! Linear O(N).
  1075. template<typename V, std::size_t C1, std::size_t C2>
  1076. bool operator> (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1077. //! @brief Lexicographically compares static_vectors.
  1078. //!
  1079. //! @ingroup static_vector_non_member
  1080. //!
  1081. //! @param x The first static_vector.
  1082. //! @param y The second static_vector.
  1083. //!
  1084. //! @return \c true if y don't compare lexicographically less than x.
  1085. //!
  1086. //! @par Complexity
  1087. //! Linear O(N).
  1088. template<typename V, std::size_t C1, std::size_t C2>
  1089. bool operator<= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1090. //! @brief Lexicographically compares static_vectors.
  1091. //!
  1092. //! @ingroup static_vector_non_member
  1093. //!
  1094. //! @param x The first static_vector.
  1095. //! @param y The second static_vector.
  1096. //!
  1097. //! @return \c true if x don't compare lexicographically less than y.
  1098. //!
  1099. //! @par Complexity
  1100. //! Linear O(N).
  1101. template<typename V, std::size_t C1, std::size_t C2>
  1102. bool operator>= (static_vector<V, C1> const& x, static_vector<V, C2> const& y);
  1103. //! @brief Swaps contents of two static_vectors.
  1104. //!
  1105. //! This function calls static_vector::swap().
  1106. //!
  1107. //! @ingroup static_vector_non_member
  1108. //!
  1109. //! @param x The first static_vector.
  1110. //! @param y The second static_vector.
  1111. //!
  1112. //! @par Complexity
  1113. //! Linear O(N).
  1114. template<typename V, std::size_t C1, std::size_t C2>
  1115. inline void swap(static_vector<V, C1> & x, static_vector<V, C2> & y);
  1116. #else
  1117. template<typename V, std::size_t C1, std::size_t C2>
  1118. inline void swap(static_vector<V, C1> & x, static_vector<V, C2> & y
  1119. , typename dtl::enable_if_c< C1 != C2>::type * = 0)
  1120. {
  1121. x.swap(y);
  1122. }
  1123. #endif // BOOST_CONTAINER_DOXYGEN_INVOKED
  1124. }} // namespace boost::container
  1125. #include <boost/container/detail/config_end.hpp>
  1126. #endif // BOOST_CONTAINER_STATIC_VECTOR_HPP