static_vector.hpp 35 KB

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