varray.hpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. // Boost.Container varray
  2. //
  3. // Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland.
  4. // Copyright (c) 2011-2013 Andrew Hundt.
  5. // Copyright (c) 2014-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_DETAIL_VARRAY_HPP
  11. #define BOOST_CONTAINER_DETAIL_VARRAY_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/addressof.hpp>
  21. #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
  22. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  23. #include <boost/move/detail/fwd_macros.hpp>
  24. #endif
  25. #include <boost/container/detail/iterator.hpp>
  26. #include <boost/container/detail/iterators.hpp>
  27. #include <boost/container/detail/mpl.hpp>
  28. #include <boost/container/detail/type_traits.hpp>
  29. #include <boost/move/adl_move_swap.hpp> //adl_move_swap
  30. #include "varray_util.hpp"
  31. #include <boost/assert.hpp>
  32. #include <boost/config.hpp>
  33. #include <boost/static_assert.hpp>
  34. #ifndef BOOST_NO_EXCEPTIONS
  35. #include <stdexcept>
  36. #endif // BOOST_NO_EXCEPTIONS
  37. /**
  38. * @defgroup varray_non_member varray non-member functions
  39. */
  40. namespace boost { namespace container { namespace dtl {
  41. // Forward declaration
  42. template <typename Value, std::size_t Capacity, typename Strategy>
  43. class varray;
  44. namespace strategy {
  45. // TODO: Improve error messages
  46. // possibly include N in the strategy, and provide size as an optoinal allocate_failed parameter?
  47. // Example of current error with reserve(4) when capacity is 3:
  48. // "boost/container/varray.hpp(66): size can't exceed the capacity"
  49. // Could say
  50. // "cannot reserve(4) due to fixed capacity of 3 elements"
  51. //! @brief The default strategy.
  52. //!
  53. //! @tparam Value Type of element stored in the container.
  54. template <typename Value>
  55. struct def
  56. {
  57. typedef Value value_type;
  58. typedef std::size_t size_type;
  59. typedef std::ptrdiff_t difference_type;
  60. typedef Value* pointer;
  61. typedef const Value* const_pointer;
  62. typedef Value& reference;
  63. typedef const Value& const_reference;
  64. static void allocate_failed()
  65. {
  66. BOOST_ASSERT_MSG(false, "size can't exceed the capacity");
  67. }
  68. };
  69. //! @brief The strategy adapting info from passed Allocator.
  70. //!
  71. //! This strategy defines the same types that are defined in the Allocator.
  72. //!
  73. //! @tparam Allocator The Allocator which will be adapted.
  74. template <typename Allocator>
  75. struct allocator_adaptor
  76. {
  77. typedef typename Allocator::value_type value_type;
  78. typedef typename Allocator::size_type size_type;
  79. typedef typename Allocator::difference_type difference_type;
  80. typedef typename Allocator::pointer pointer;
  81. typedef typename Allocator::const_pointer const_pointer;
  82. typedef typename Allocator::reference reference;
  83. typedef typename Allocator::const_reference const_reference;
  84. static void allocate_failed()
  85. {
  86. BOOST_ASSERT_MSG(false, "size can't exceed the capacity");
  87. }
  88. };
  89. } // namespace strategy
  90. struct varray_error_handler
  91. {
  92. template <typename V, std::size_t Capacity, typename S>
  93. static void check_capacity(varray<V, Capacity, S> const&, std::size_t s)
  94. {
  95. if ( Capacity < s )
  96. S::allocate_failed();
  97. }
  98. template <typename V, std::size_t C, typename S>
  99. static void check_at(varray<V, C, S> const& v,
  100. typename varray<V, C, S>::size_type i)
  101. {
  102. (void)v;
  103. (void)i;
  104. // TODO - use BOOST_THROW_EXCEPTION here?
  105. #ifndef BOOST_NO_EXCEPTIONS
  106. if ( v.size() <= i )
  107. throw std::out_of_range("index out of bounds");
  108. #else // BOOST_NO_EXCEPTIONS
  109. BOOST_ASSERT_MSG(i < v.size(), "index out of bounds");
  110. #endif // BOOST_NO_EXCEPTIONS
  111. }
  112. template <typename V, std::size_t C, typename S>
  113. static void check_operator_brackets(varray<V, C, S> const& v,
  114. typename varray<V, C, S>::size_type i)
  115. {
  116. (void)v;
  117. (void)i;
  118. BOOST_ASSERT_MSG(i < v.size(), "index out of bounds");
  119. }
  120. template <typename V, std::size_t C, typename S>
  121. static void check_empty(varray<V, C, S> const& v)
  122. {
  123. (void)v;
  124. BOOST_ASSERT_MSG(0 < v.size(), "the container is empty");
  125. }
  126. template <typename V, std::size_t C, typename S>
  127. static void check_iterator_end_neq(varray<V, C, S> const& v,
  128. typename varray<V, C, S>::const_iterator position)
  129. {
  130. (void)v;
  131. (void)position;
  132. BOOST_ASSERT_MSG(v.begin() <= position && position < v.end(), "iterator out of bounds");
  133. }
  134. template <typename V, std::size_t C, typename S>
  135. static void check_iterator_end_eq(varray<V, C, S> const& v,
  136. typename varray<V, C, S>::const_iterator position)
  137. {
  138. (void)v;
  139. (void)position;
  140. BOOST_ASSERT_MSG(v.begin() <= position && position <= v.end(), "iterator out of bounds");
  141. }
  142. };
  143. template <typename Value, std::size_t Capacity, typename Strategy>
  144. struct varray_traits
  145. {
  146. typedef typename Strategy::value_type value_type;
  147. typedef typename Strategy::size_type size_type;
  148. typedef typename Strategy::difference_type difference_type;
  149. typedef typename Strategy::pointer pointer;
  150. typedef typename Strategy::const_pointer const_pointer;
  151. typedef typename Strategy::reference reference;
  152. typedef typename Strategy::const_reference const_reference;
  153. typedef varray_error_handler error_handler;
  154. typedef false_type use_memop_in_swap_and_move;
  155. typedef false_type use_optimized_swap;
  156. typedef false_type disable_trivial_init;
  157. };
  158. /**
  159. * @brief A variable-size array container with fixed capacity.
  160. *
  161. * varray is a sequence container like boost::container::vector with contiguous storage that can
  162. * change in size, along with the static allocation, low overhead, and fixed capacity of boost::array.
  163. *
  164. * A varray is a sequence that supports random access to elements, constant time insertion and
  165. * removal of elements at the end, and linear time insertion and removal of elements at the beginning or
  166. * in the middle. The number of elements in a varray may vary dynamically up to a fixed capacity
  167. * because elements are stored within the object itself similarly to an array. However, objects are
  168. * initialized as they are inserted into varray unlike C arrays or std::array which must construct
  169. * all elements on instantiation. The behavior of varray enables the use of statically allocated
  170. * elements in cases with complex object lifetime requirements that would otherwise not be trivially
  171. * possible.
  172. *
  173. * @par Error Handling
  174. * Insertion beyond the capacity and out of bounds errors result in undefined behavior unless
  175. * otherwise specified. In this respect if size() == capacity(), then varray::push_back()
  176. * behaves like std::vector pop_front() if size() == empty(). The reason for this difference
  177. * is because unlike vectors, varray does not perform allocation.
  178. *
  179. * @par Advanced Usage
  180. * Error handling behavior can be modified to more closely match std::vector exception behavior
  181. * when exceeding bounds by providing an alternate Strategy and varray_traits instantiation.
  182. *
  183. * @tparam Value The type of element that will be stored.
  184. * @tparam Capacity The maximum number of elements varray can store, fixed at compile time.
  185. * @tparam Strategy Defines the public typedefs and error handlers,
  186. * implements StaticVectorStrategy and has some similarities
  187. * to an Allocator.
  188. */
  189. template <typename Value, std::size_t Capacity, typename Strategy = strategy::def<Value> >
  190. class varray
  191. {
  192. typedef dtl::varray_traits<
  193. Value, Capacity, Strategy
  194. > vt;
  195. typedef typename vt::error_handler errh;
  196. typedef typename aligned_storage<
  197. sizeof(Value[Capacity]),
  198. boost::container::dtl::alignment_of<Value[Capacity]>::value
  199. >::type aligned_storage_type;
  200. template <typename V, std::size_t C, typename S>
  201. friend class varray;
  202. BOOST_COPYABLE_AND_MOVABLE(varray)
  203. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  204. public:
  205. template <std::size_t C, typename S>
  206. varray & operator=(varray<Value, C, S> & sv)
  207. {
  208. typedef varray<Value, C, S> other;
  209. this->operator=(static_cast<const ::boost::rv<other> &>(const_cast<const other &>(sv)));
  210. return *this;
  211. }
  212. #endif
  213. public:
  214. //! @brief The type of elements stored in the container.
  215. typedef typename vt::value_type value_type;
  216. //! @brief The unsigned integral type used by the container.
  217. typedef typename vt::size_type size_type;
  218. //! @brief The pointers difference type.
  219. typedef typename vt::difference_type difference_type;
  220. //! @brief The pointer type.
  221. typedef typename vt::pointer pointer;
  222. //! @brief The const pointer type.
  223. typedef typename vt::const_pointer const_pointer;
  224. //! @brief The value reference type.
  225. typedef typename vt::reference reference;
  226. //! @brief The value const reference type.
  227. typedef typename vt::const_reference const_reference;
  228. //! @brief The iterator type.
  229. typedef pointer iterator;
  230. //! @brief The const iterator type.
  231. typedef const_pointer const_iterator;
  232. //! @brief The reverse iterator type.
  233. typedef boost::container::reverse_iterator<iterator> reverse_iterator;
  234. //! @brief The const reverse iterator.
  235. typedef boost::container::reverse_iterator<const_iterator> const_reverse_iterator;
  236. //! @brief The type of a strategy used by the varray.
  237. typedef Strategy strategy_type;
  238. //! @brief Constructs an empty varray.
  239. //!
  240. //! @par Throws
  241. //! Nothing.
  242. //!
  243. //! @par Complexity
  244. //! Constant O(1).
  245. varray()
  246. : m_size(0)
  247. {}
  248. //! @pre <tt>count <= capacity()</tt>
  249. //!
  250. //! @brief Constructs a varray containing count value initialized Values.
  251. //!
  252. //! @param count The number of values which will be contained in the container.
  253. //!
  254. //! @par Throws
  255. //! If Value's value initialization throws.
  256. //! @internal
  257. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  258. //! @endinternal
  259. //!
  260. //! @par Complexity
  261. //! Linear O(N).
  262. explicit varray(size_type count)
  263. : m_size(0)
  264. {
  265. this->resize(count); // may throw
  266. }
  267. //! @pre <tt>count <= capacity()</tt>
  268. //!
  269. //! @brief Constructs a varray containing count copies of value.
  270. //!
  271. //! @param count The number of copies of a values that will be contained in the container.
  272. //! @param value The value which will be used to copy construct values.
  273. //!
  274. //! @par Throws
  275. //! If Value's copy constructor throws.
  276. //! @internal
  277. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  278. //! @endinternal
  279. //!
  280. //! @par Complexity
  281. //! Linear O(N).
  282. varray(size_type count, value_type const& value)
  283. : m_size(0)
  284. {
  285. this->resize(count, value); // may throw
  286. }
  287. //! @pre
  288. //! @li <tt>distance(first, last) <= capacity()</tt>
  289. //! @li Iterator must meet the \c ForwardIterator.
  290. //!
  291. //! @brief Constructs a varray containing copy of a range <tt>[first, last)</tt>.
  292. //!
  293. //! @param first The iterator to the first element in range.
  294. //! @param last The iterator to the one after the last element in range.
  295. //!
  296. //! @par Throws
  297. //! If Value's constructor taking a dereferenced Iterator throws.
  298. //! @internal
  299. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  300. //! @endinternal
  301. //!
  302. //! @par Complexity
  303. //! Linear O(N).
  304. template <typename Iterator>
  305. varray(Iterator first, Iterator last)
  306. : m_size(0)
  307. {
  308. this->assign(first, last); // may throw
  309. }
  310. //! @brief Constructs a copy of other varray.
  311. //!
  312. //! @param other The varray which content will be copied to this one.
  313. //!
  314. //! @par Throws
  315. //! If Value's copy constructor throws.
  316. //!
  317. //! @par Complexity
  318. //! Linear O(N).
  319. varray(varray const& other)
  320. : m_size(other.size())
  321. {
  322. namespace sv = varray_detail;
  323. sv::uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
  324. }
  325. //! @pre <tt>other.size() <= capacity()</tt>.
  326. //!
  327. //! @brief Constructs a copy of other varray.
  328. //!
  329. //! @param other The varray which content will be copied to this one.
  330. //!
  331. //! @par Throws
  332. //! If Value's copy constructor throws.
  333. //! @internal
  334. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  335. //! @endinternal
  336. //!
  337. //! @par Complexity
  338. //! Linear O(N).
  339. template <std::size_t C, typename S>
  340. varray(varray<value_type, C, S> const& other)
  341. : m_size(other.size())
  342. {
  343. errh::check_capacity(*this, other.size()); // may throw
  344. namespace sv = varray_detail;
  345. sv::uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
  346. }
  347. //! @brief Copy assigns Values stored in the other varray to this one.
  348. //!
  349. //! @param other The varray which content will be copied to this one.
  350. //!
  351. //! @par Throws
  352. //! If Value's copy constructor or copy assignment throws.
  353. //!
  354. //! @par Complexity
  355. //! Linear O(N).
  356. varray & operator=(BOOST_COPY_ASSIGN_REF(varray) other)
  357. {
  358. this->assign(other.begin(), other.end()); // may throw
  359. return *this;
  360. }
  361. //! @pre <tt>other.size() <= capacity()</tt>
  362. //!
  363. //! @brief Copy assigns Values stored in the other varray to this one.
  364. //!
  365. //! @param other The varray which content will be copied to this one.
  366. //!
  367. //! @par Throws
  368. //! If Value's copy constructor or copy assignment throws.
  369. //! @internal
  370. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  371. //! @endinternal
  372. //!
  373. //! @par Complexity
  374. //! Linear O(N).
  375. template <std::size_t C, typename S>
  376. // TEMPORARY WORKAROUND
  377. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  378. varray & operator=(::boost::rv< varray<value_type, C, S> > const& other)
  379. #else
  380. varray & operator=(varray<value_type, C, S> const& other)
  381. #endif
  382. {
  383. this->assign(other.begin(), other.end()); // may throw
  384. return *this;
  385. }
  386. //! @brief Move constructor. Moves Values stored in the other varray to this one.
  387. //!
  388. //! @param other The varray which content will be moved to this one.
  389. //!
  390. //! @par Throws
  391. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor throws.
  392. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor throws.
  393. //! @internal
  394. //! @li It throws only if \c use_memop_in_swap_and_move is \c false_type - default.
  395. //! @endinternal
  396. //!
  397. //! @par Complexity
  398. //! Linear O(N).
  399. varray(BOOST_RV_REF(varray) other)
  400. {
  401. typedef typename
  402. vt::use_memop_in_swap_and_move use_memop_in_swap_and_move;
  403. this->move_ctor_dispatch(other, use_memop_in_swap_and_move());
  404. }
  405. //! @pre <tt>other.size() <= capacity()</tt>
  406. //!
  407. //! @brief Move constructor. Moves Values stored in the other varray to this one.
  408. //!
  409. //! @param other The varray which content will be moved to this one.
  410. //!
  411. //! @par Throws
  412. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor throws.
  413. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor throws.
  414. //! @internal
  415. //! @li It throws only if \c use_memop_in_swap_and_move is false_type - default.
  416. //! @endinternal
  417. //! @internal
  418. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  419. //! @endinternal
  420. //!
  421. //! @par Complexity
  422. //! Linear O(N).
  423. template <std::size_t C, typename S>
  424. varray(BOOST_RV_REF_3_TEMPL_ARGS(varray, value_type, C, S) other)
  425. : m_size(other.m_size)
  426. {
  427. errh::check_capacity(*this, other.size()); // may throw
  428. typedef typename
  429. vt::use_memop_in_swap_and_move use_memop_in_swap_and_move;
  430. this->move_ctor_dispatch(other, use_memop_in_swap_and_move());
  431. }
  432. //! @brief Move assignment. Moves Values stored in the other varray to this one.
  433. //!
  434. //! @param other The varray which content will be moved to this one.
  435. //!
  436. //! @par Throws
  437. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws.
  438. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws.
  439. //! @internal
  440. //! @li It throws only if \c use_memop_in_swap_and_move is \c false_type - default.
  441. //! @endinternal
  442. //!
  443. //! @par Complexity
  444. //! Linear O(N).
  445. varray & operator=(BOOST_RV_REF(varray) other)
  446. {
  447. if ( &other == this )
  448. return *this;
  449. typedef typename
  450. vt::use_memop_in_swap_and_move use_memop_in_swap_and_move;
  451. this->move_assign_dispatch(other, use_memop_in_swap_and_move());
  452. return *this;
  453. }
  454. //! @pre <tt>other.size() <= capacity()</tt>
  455. //!
  456. //! @brief Move assignment. Moves Values stored in the other varray to this one.
  457. //!
  458. //! @param other The varray which content will be moved to this one.
  459. //!
  460. //! @par Throws
  461. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws.
  462. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws.
  463. //! @internal
  464. //! @li It throws only if \c use_memop_in_swap_and_move is \c false_type - default.
  465. //! @endinternal
  466. //! @internal
  467. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  468. //! @endinternal
  469. //!
  470. //! @par Complexity
  471. //! Linear O(N).
  472. template <std::size_t C, typename S>
  473. varray & operator=(BOOST_RV_REF_3_TEMPL_ARGS(varray, value_type, C, S) other)
  474. {
  475. errh::check_capacity(*this, other.size()); // may throw
  476. typedef typename
  477. vt::use_memop_in_swap_and_move use_memop_in_swap_and_move;
  478. this->move_assign_dispatch(other, use_memop_in_swap_and_move());
  479. return *this;
  480. }
  481. //! @brief Destructor. Destroys Values stored in this container.
  482. //!
  483. //! @par Throws
  484. //! Nothing
  485. //!
  486. //! @par Complexity
  487. //! Linear O(N).
  488. ~varray()
  489. {
  490. namespace sv = varray_detail;
  491. sv::destroy(this->begin(), this->end());
  492. }
  493. //! @brief Swaps contents of the other varray and this one.
  494. //!
  495. //! @param other The varray which content will be swapped with this one's content.
  496. //!
  497. //! @par Throws
  498. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  499. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  500. //! @internal
  501. //! @li It throws only if \c use_memop_in_swap_and_move and \c use_optimized_swap are \c false_type - default.
  502. //! @endinternal
  503. //!
  504. //! @par Complexity
  505. //! Linear O(N).
  506. void swap(varray & other)
  507. {
  508. typedef typename
  509. vt::use_optimized_swap use_optimized_swap;
  510. this->swap_dispatch(other, use_optimized_swap());
  511. }
  512. //! @pre <tt>other.size() <= capacity() && size() <= other.capacity()</tt>
  513. //!
  514. //! @brief Swaps contents of the other varray and this one.
  515. //!
  516. //! @param other The varray which content will be swapped with this one's content.
  517. //!
  518. //! @par Throws
  519. //! @li If \c boost::has_nothrow_move<Value>::value is \c true and Value's move constructor or move assignment throws,
  520. //! @li If \c boost::has_nothrow_move<Value>::value is \c false and Value's copy constructor or copy assignment throws,
  521. //! @internal
  522. //! @li It throws only if \c use_memop_in_swap_and_move and \c use_optimized_swap are \c false_type - default.
  523. //! @endinternal
  524. //! @internal
  525. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  526. //! @endinternal
  527. //!
  528. //! @par Complexity
  529. //! Linear O(N).
  530. template <std::size_t C, typename S>
  531. void swap(varray<value_type, C, S> & other)
  532. {
  533. errh::check_capacity(*this, other.size());
  534. errh::check_capacity(other, this->size());
  535. typedef typename
  536. vt::use_optimized_swap use_optimized_swap;
  537. this->swap_dispatch(other, use_optimized_swap());
  538. }
  539. //! @pre <tt>count <= capacity()</tt>
  540. //!
  541. //! @brief Inserts or erases elements at the end such that
  542. //! the size becomes count. New elements are value initialized.
  543. //!
  544. //! @param count The number of elements which will be stored in the container.
  545. //!
  546. //! @par Throws
  547. //! If Value's value initialization throws.
  548. //! @internal
  549. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  550. //! @endinternal
  551. //!
  552. //! @par Complexity
  553. //! Linear O(N).
  554. void resize(size_type count)
  555. {
  556. namespace sv = varray_detail;
  557. typedef typename vt::disable_trivial_init dti;
  558. if ( count < m_size )
  559. {
  560. sv::destroy(this->begin() + count, this->end());
  561. }
  562. else
  563. {
  564. errh::check_capacity(*this, count); // may throw
  565. sv::uninitialized_fill(this->end(), this->begin() + count, dti()); // may throw
  566. }
  567. m_size = count; // update end
  568. }
  569. //! @pre <tt>count <= capacity()</tt>
  570. //!
  571. //! @brief Inserts or erases elements at the end such that
  572. //! the size becomes count. New elements are copy constructed from value.
  573. //!
  574. //! @param count The number of elements which will be stored in the container.
  575. //! @param value The value used to copy construct the new element.
  576. //!
  577. //! @par Throws
  578. //! If Value's copy constructor throws.
  579. //! @internal
  580. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  581. //! @endinternal
  582. //!
  583. //! @par Complexity
  584. //! Linear O(N).
  585. void resize(size_type count, value_type const& value)
  586. {
  587. if ( count < m_size )
  588. {
  589. namespace sv = varray_detail;
  590. sv::destroy(this->begin() + count, this->end());
  591. }
  592. else
  593. {
  594. errh::check_capacity(*this, count); // may throw
  595. std::uninitialized_fill(this->end(), this->begin() + count, value); // may throw
  596. }
  597. m_size = count; // update end
  598. }
  599. //! @pre <tt>count <= capacity()</tt>
  600. //!
  601. //! @brief This call has no effect because the Capacity of this container is constant.
  602. //!
  603. //! @param count The number of elements which the container should be able to contain.
  604. //!
  605. //! @par Throws
  606. //! Nothing.
  607. //! @internal
  608. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  609. //! @endinternal
  610. //!
  611. //! @par Complexity
  612. //! Linear O(N).
  613. void reserve(size_type count)
  614. {
  615. errh::check_capacity(*this, count); // may throw
  616. }
  617. //! @pre <tt>size() < capacity()</tt>
  618. //!
  619. //! @brief Adds a copy of value at the end.
  620. //!
  621. //! @param value The value used to copy construct the new element.
  622. //!
  623. //! @par Throws
  624. //! If Value's copy constructor throws.
  625. //! @internal
  626. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  627. //! @endinternal
  628. //!
  629. //! @par Complexity
  630. //! Constant O(1).
  631. void push_back(value_type const& value)
  632. {
  633. typedef typename vt::disable_trivial_init dti;
  634. errh::check_capacity(*this, m_size + 1); // may throw
  635. namespace sv = varray_detail;
  636. sv::construct(dti(), this->end(), value); // may throw
  637. ++m_size; // update end
  638. }
  639. //! @pre <tt>size() < capacity()</tt>
  640. //!
  641. //! @brief Moves value to the end.
  642. //!
  643. //! @param value The value to move construct the new element.
  644. //!
  645. //! @par Throws
  646. //! If Value's move constructor throws.
  647. //! @internal
  648. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  649. //! @endinternal
  650. //!
  651. //! @par Complexity
  652. //! Constant O(1).
  653. void push_back(BOOST_RV_REF(value_type) value)
  654. {
  655. typedef typename vt::disable_trivial_init dti;
  656. errh::check_capacity(*this, m_size + 1); // may throw
  657. namespace sv = varray_detail;
  658. sv::construct(dti(), this->end(), ::boost::move(value)); // may throw
  659. ++m_size; // update end
  660. }
  661. //! @pre <tt>!empty()</tt>
  662. //!
  663. //! @brief Destroys last value and decreases the size.
  664. //!
  665. //! @par Throws
  666. //! Nothing by default.
  667. //!
  668. //! @par Complexity
  669. //! Constant O(1).
  670. void pop_back()
  671. {
  672. errh::check_empty(*this);
  673. namespace sv = varray_detail;
  674. sv::destroy(this->end() - 1);
  675. --m_size; // update end
  676. }
  677. //! @pre
  678. //! @li \c position must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  679. //! @li <tt>size() < capacity()</tt>
  680. //!
  681. //! @brief Inserts a copy of element at position.
  682. //!
  683. //! @param position The position at which the new value will be inserted.
  684. //! @param value The value used to copy construct the new element.
  685. //!
  686. //! @par Throws
  687. //! @li If Value's copy constructor or copy assignment throws
  688. //! @li If Value's move constructor or move assignment throws.
  689. //! @internal
  690. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  691. //! @endinternal
  692. //!
  693. //! @par Complexity
  694. //! Constant or linear.
  695. iterator insert(iterator position, value_type const& value)
  696. {
  697. return this->priv_insert(position, value);
  698. }
  699. //! @pre
  700. //! @li \c position must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  701. //! @li <tt>size() < capacity()</tt>
  702. //!
  703. //! @brief Inserts a move-constructed element at position.
  704. //!
  705. //! @param position The position at which the new value will be inserted.
  706. //! @param value The value used to move construct the new element.
  707. //!
  708. //! @par Throws
  709. //! If Value's move constructor or move assignment throws.
  710. //! @internal
  711. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  712. //! @endinternal
  713. //!
  714. //! @par Complexity
  715. //! Constant or linear.
  716. iterator insert(iterator position, BOOST_RV_REF(value_type) value)
  717. {
  718. return this->priv_insert(position, boost::move(value));
  719. }
  720. //! @pre
  721. //! @li \c position must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  722. //! @li <tt>size() + count <= capacity()</tt>
  723. //!
  724. //! @brief Inserts a count copies of value at position.
  725. //!
  726. //! @param position The position at which new elements will be inserted.
  727. //! @param count The number of new elements which will be inserted.
  728. //! @param value The value used to copy construct new elements.
  729. //!
  730. //! @par Throws
  731. //! @li If Value's copy constructor or copy assignment throws.
  732. //! @li If Value's move constructor or move assignment throws.
  733. //! @internal
  734. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  735. //! @endinternal
  736. //!
  737. //! @par Complexity
  738. //! Linear O(N).
  739. iterator insert(iterator position, size_type count, value_type const& value)
  740. {
  741. errh::check_iterator_end_eq(*this, position);
  742. errh::check_capacity(*this, m_size + count); // may throw
  743. if ( position == this->end() )
  744. {
  745. std::uninitialized_fill(position, position + count, value); // may throw
  746. m_size += count; // update end
  747. }
  748. else
  749. {
  750. namespace sv = varray_detail;
  751. difference_type to_move = boost::container::iterator_distance(position, this->end());
  752. // TODO - should following lines check for exception and revert to the old size?
  753. if ( count < static_cast<size_type>(to_move) )
  754. {
  755. sv::uninitialized_move(this->end() - count, this->end(), this->end()); // may throw
  756. m_size += count; // update end
  757. sv::move_backward(position, position + to_move - count, this->end() - count); // may throw
  758. std::fill_n(position, count, value); // may throw
  759. }
  760. else
  761. {
  762. std::uninitialized_fill(this->end(), position + count, value); // may throw
  763. m_size += count - to_move; // update end
  764. sv::uninitialized_move(position, position + to_move, position + count); // may throw
  765. m_size += to_move; // update end
  766. std::fill_n(position, to_move, value); // may throw
  767. }
  768. }
  769. return position;
  770. }
  771. //! @pre
  772. //! @li \c position must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>.
  773. //! @li <tt>distance(first, last) <= capacity()</tt>
  774. //! @li \c Iterator must meet the \c ForwardIterator.
  775. //!
  776. //! @brief Inserts a copy of a range <tt>[first, last)</tt> at position.
  777. //!
  778. //! @param position The position at which new elements will be inserted.
  779. //! @param first The iterator to the first element of a range used to construct new elements.
  780. //! @param last The iterator to the one after the last element of a range used to construct new elements.
  781. //!
  782. //! @par Throws
  783. //! @li If Value's constructor and assignment taking a dereferenced \c Iterator.
  784. //! @li If Value's move constructor or move assignment throws.
  785. //! @internal
  786. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  787. //! @endinternal
  788. //!
  789. //! @par Complexity
  790. //! Linear O(N).
  791. template <typename Iterator>
  792. iterator insert(iterator position, Iterator first, Iterator last)
  793. {
  794. this->insert_dispatch(position, first, last);
  795. return position;
  796. }
  797. //! @pre \c position must be a valid iterator of \c *this in range <tt>[begin(), end())</tt>
  798. //!
  799. //! @brief Erases Value from position.
  800. //!
  801. //! @param position The position of the element which will be erased from the container.
  802. //!
  803. //! @par Throws
  804. //! If Value's move assignment throws.
  805. //!
  806. //! @par Complexity
  807. //! Linear O(N).
  808. iterator erase(iterator position)
  809. {
  810. namespace sv = varray_detail;
  811. errh::check_iterator_end_neq(*this, position);
  812. //TODO - add empty check?
  813. //errh::check_empty(*this);
  814. sv::move(position + 1, this->end(), position); // may throw
  815. sv::destroy(this->end() - 1);
  816. --m_size;
  817. return position;
  818. }
  819. //! @pre
  820. //! @li \c first and \c last must define a valid range
  821. //! @li iterators must be in range <tt>[begin(), end()]</tt>
  822. //!
  823. //! @brief Erases Values from a range <tt>[first, last)</tt>.
  824. //!
  825. //! @param first The position of the first element of a range which will be erased from the container.
  826. //! @param last The position of the one after the last element of a range which will be erased from the container.
  827. //!
  828. //! @par Throws
  829. //! If Value's move assignment throws.
  830. //!
  831. //! @par Complexity
  832. //! Linear O(N).
  833. iterator erase(iterator first, iterator last)
  834. {
  835. namespace sv = varray_detail;
  836. errh::check_iterator_end_eq(*this, first);
  837. errh::check_iterator_end_eq(*this, last);
  838. difference_type n = boost::container::iterator_distance(first, last);
  839. //TODO - add invalid range check?
  840. //BOOST_ASSERT_MSG(0 <= n, "invalid range");
  841. //TODO - add this->size() check?
  842. //BOOST_ASSERT_MSG(n <= this->size(), "invalid range");
  843. sv::move(last, this->end(), first); // may throw
  844. sv::destroy(this->end() - n, this->end());
  845. m_size -= n;
  846. return first;
  847. }
  848. //! @pre <tt>distance(first, last) <= capacity()</tt>
  849. //!
  850. //! @brief Assigns a range <tt>[first, last)</tt> of Values to this container.
  851. //!
  852. //! @param first The iterator to the first element of a range used to construct new content of this container.
  853. //! @param last The iterator to the one after the last element of a range used to construct new content of this container.
  854. //!
  855. //! @par Throws
  856. //! If Value's copy constructor or copy assignment throws,
  857. //!
  858. //! @par Complexity
  859. //! Linear O(N).
  860. template <typename Iterator>
  861. void assign(Iterator first, Iterator last)
  862. {
  863. this->assign_dispatch(first, last); // may throw
  864. }
  865. //! @pre <tt>count <= capacity()</tt>
  866. //!
  867. //! @brief Assigns a count copies of value to this container.
  868. //!
  869. //! @param count The new number of elements which will be container in the container.
  870. //! @param value The value which will be used to copy construct the new content.
  871. //!
  872. //! @par Throws
  873. //! If Value's copy constructor or copy assignment throws.
  874. //!
  875. //! @par Complexity
  876. //! Linear O(N).
  877. void assign(size_type count, value_type const& value)
  878. {
  879. if ( count < m_size )
  880. {
  881. namespace sv = varray_detail;
  882. std::fill_n(this->begin(), count, value); // may throw
  883. sv::destroy(this->begin() + count, this->end());
  884. }
  885. else
  886. {
  887. errh::check_capacity(*this, count); // may throw
  888. std::fill_n(this->begin(), m_size, value); // may throw
  889. std::uninitialized_fill(this->end(), this->begin() + count, value); // may throw
  890. }
  891. m_size = count; // update end
  892. }
  893. #if !defined(BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE)
  894. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  895. //! @pre <tt>size() < capacity()</tt>
  896. //!
  897. //! @brief Inserts a Value constructed with
  898. //! \c std::forward<Args>(args)... in the end of the container.
  899. //!
  900. //! @param args The arguments of the constructor of the new element which will be created at the end of the container.
  901. //!
  902. //! @par Throws
  903. //! If in-place constructor throws or Value's move constructor throws.
  904. //! @internal
  905. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  906. //! @endinternal
  907. //!
  908. //! @par Complexity
  909. //! Constant O(1).
  910. template<class ...Args>
  911. void emplace_back(BOOST_FWD_REF(Args) ...args)
  912. {
  913. typedef typename vt::disable_trivial_init dti;
  914. errh::check_capacity(*this, m_size + 1); // may throw
  915. namespace sv = varray_detail;
  916. sv::construct(dti(), this->end(), ::boost::forward<Args>(args)...); // may throw
  917. ++m_size; // update end
  918. }
  919. //! @pre
  920. //! @li \c position must be a valid iterator of \c *this in range <tt>[begin(), end()]</tt>
  921. //! @li <tt>size() < capacity()</tt>
  922. //!
  923. //! @brief Inserts a Value constructed with
  924. //! \c std::forward<Args>(args)... before position
  925. //!
  926. //! @param position The position at which new elements will be inserted.
  927. //! @param args The arguments of the constructor of the new element.
  928. //!
  929. //! @par Throws
  930. //! If in-place constructor throws or if Value's move constructor or move assignment throws.
  931. //! @internal
  932. //! @li If a throwing error handler is specified, throws when the capacity is exceeded. (not by default).
  933. //! @endinternal
  934. //!
  935. //! @par Complexity
  936. //! Constant or linear.
  937. template<class ...Args>
  938. iterator emplace(iterator position, BOOST_FWD_REF(Args) ...args)
  939. {
  940. typedef typename vt::disable_trivial_init dti;
  941. namespace sv = varray_detail;
  942. errh::check_iterator_end_eq(*this, position);
  943. errh::check_capacity(*this, m_size + 1); // may throw
  944. if ( position == this->end() )
  945. {
  946. sv::construct(dti(), position, ::boost::forward<Args>(args)...); // may throw
  947. ++m_size; // update end
  948. }
  949. else
  950. {
  951. // TODO - should following lines check for exception and revert to the old size?
  952. // TODO - should move be used only if it's nonthrowing?
  953. value_type & r = *(this->end() - 1);
  954. sv::construct(dti(), this->end(), boost::move(r)); // may throw
  955. ++m_size; // update end
  956. sv::move_backward(position, this->end() - 2, this->end() - 1); // may throw
  957. typename aligned_storage
  958. <sizeof(value_type), alignment_of<value_type>::value>::type temp_storage;
  959. value_type * val_p = static_cast<value_type*>(static_cast<void*>(&temp_storage));
  960. sv::construct(dti(), val_p, ::boost::forward<Args>(args)...); // may throw
  961. sv::scoped_destructor<value_type> d(val_p);
  962. sv::assign(position, ::boost::move(*val_p)); // may throw
  963. }
  964. return position;
  965. }
  966. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || BOOST_CONTAINER_DOXYGEN_INVOKED
  967. #define BOOST_CONTAINER_VARRAY_EMPLACE_CODE(N) \
  968. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  969. void emplace_back(BOOST_MOVE_UREF##N)\
  970. {\
  971. typedef typename vt::disable_trivial_init dti;\
  972. errh::check_capacity(*this, m_size + 1);/*may throw*/\
  973. \
  974. namespace sv = varray_detail;\
  975. sv::construct(dti(), this->end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N ); /*may throw*/\
  976. ++m_size; /*update end*/\
  977. }\
  978. \
  979. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  980. iterator emplace(iterator position BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  981. {\
  982. typedef typename vt::disable_trivial_init dti;\
  983. namespace sv = varray_detail;\
  984. errh::check_iterator_end_eq(*this, position);\
  985. errh::check_capacity(*this, m_size + 1); /*may throw*/\
  986. if ( position == this->end() ){\
  987. sv::construct(dti(), position BOOST_MOVE_I##N BOOST_MOVE_FWD##N ); /*may throw*/\
  988. ++m_size; /*update end*/\
  989. }\
  990. else{\
  991. /* TODO - should following lines check for exception and revert to the old size? */\
  992. /* TODO - should move be used only if it's nonthrowing? */\
  993. value_type & r = *(this->end() - 1);\
  994. sv::construct(dti(), this->end(), boost::move(r));/*may throw*/\
  995. ++m_size; /*update end*/\
  996. sv::move_backward(position, this->end() - 2, this->end() - 1);/*may throw*/\
  997. typename aligned_storage\
  998. <sizeof(value_type), alignment_of<value_type>::value>::type temp_storage;\
  999. value_type * val_p = static_cast<value_type*>(static_cast<void*>(&temp_storage));\
  1000. sv::construct(dti(), val_p BOOST_MOVE_I##N BOOST_MOVE_FWD##N ); /*may throw*/\
  1001. sv::scoped_destructor<value_type> d(val_p);\
  1002. sv::assign(position, ::boost::move(*val_p));/*may throw*/\
  1003. }\
  1004. return position;\
  1005. }\
  1006. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VARRAY_EMPLACE_CODE)
  1007. #undef BOOST_CONTAINER_VARRAY_EMPLACE_CODE
  1008. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || BOOST_CONTAINER_DOXYGEN_INVOKED
  1009. #endif // !BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE
  1010. //! @brief Removes all elements from the container.
  1011. //!
  1012. //! @par Throws
  1013. //! Nothing.
  1014. //!
  1015. //! @par Complexity
  1016. //! Constant O(1).
  1017. void clear()
  1018. {
  1019. namespace sv = varray_detail;
  1020. sv::destroy(this->begin(), this->end());
  1021. m_size = 0; // update end
  1022. }
  1023. //! @pre <tt>i < size()</tt>
  1024. //!
  1025. //! @brief Returns reference to the i-th element.
  1026. //!
  1027. //! @param i The element's index.
  1028. //!
  1029. //! @return reference to the i-th element
  1030. //! from the beginning of the container.
  1031. //!
  1032. //! @par Throws
  1033. //! \c std::out_of_range exception by default.
  1034. //!
  1035. //! @par Complexity
  1036. //! Constant O(1).
  1037. reference at(size_type i)
  1038. {
  1039. errh::check_at(*this, i); // may throw
  1040. return *(this->begin() + i);
  1041. }
  1042. //! @pre <tt>i < size()</tt>
  1043. //!
  1044. //! @brief Returns const reference to the i-th element.
  1045. //!
  1046. //! @param i The element's index.
  1047. //!
  1048. //! @return const reference to the i-th element
  1049. //! from the beginning of the container.
  1050. //!
  1051. //! @par Throws
  1052. //! \c std::out_of_range exception by default.
  1053. //!
  1054. //! @par Complexity
  1055. //! Constant O(1).
  1056. const_reference at(size_type i) const
  1057. {
  1058. errh::check_at(*this, i); // may throw
  1059. return *(this->begin() + i);
  1060. }
  1061. //! @pre <tt>i < size()</tt>
  1062. //!
  1063. //! @brief Returns reference to the i-th element.
  1064. //!
  1065. //! @param i The element's index.
  1066. //!
  1067. //! @return reference to the i-th element
  1068. //! from the beginning of the container.
  1069. //!
  1070. //! @par Throws
  1071. //! Nothing by default.
  1072. //!
  1073. //! @par Complexity
  1074. //! Constant O(1).
  1075. reference operator[](size_type i)
  1076. {
  1077. // TODO: Remove bounds check? std::vector and std::array operator[] don't check.
  1078. errh::check_operator_brackets(*this, i);
  1079. return *(this->begin() + i);
  1080. }
  1081. //! @pre <tt>i < size()</tt>
  1082. //!
  1083. //! @brief Returns const reference to the i-th element.
  1084. //!
  1085. //! @param i The element's index.
  1086. //!
  1087. //! @return const reference to the i-th element
  1088. //! from the beginning of the container.
  1089. //!
  1090. //! @par Throws
  1091. //! Nothing by default.
  1092. //!
  1093. //! @par Complexity
  1094. //! Constant O(1).
  1095. const_reference operator[](size_type i) const
  1096. {
  1097. errh::check_operator_brackets(*this, i);
  1098. return *(this->begin() + i);
  1099. }
  1100. //! @pre \c !empty()
  1101. //!
  1102. //! @brief Returns reference to the first element.
  1103. //!
  1104. //! @return reference to the first element
  1105. //! from the beginning of the container.
  1106. //!
  1107. //! @par Throws
  1108. //! Nothing by default.
  1109. //!
  1110. //! @par Complexity
  1111. //! Constant O(1).
  1112. reference front()
  1113. {
  1114. errh::check_empty(*this);
  1115. return *(this->begin());
  1116. }
  1117. //! @pre \c !empty()
  1118. //!
  1119. //! @brief Returns const reference to the first element.
  1120. //!
  1121. //! @return const reference to the first element
  1122. //! from the beginning of the container.
  1123. //!
  1124. //! @par Throws
  1125. //! Nothing by default.
  1126. //!
  1127. //! @par Complexity
  1128. //! Constant O(1).
  1129. const_reference front() const
  1130. {
  1131. errh::check_empty(*this);
  1132. return *(this->begin());
  1133. }
  1134. //! @pre \c !empty()
  1135. //!
  1136. //! @brief Returns reference to the last element.
  1137. //!
  1138. //! @return reference to the last element
  1139. //! from the beginning of the container.
  1140. //!
  1141. //! @par Throws
  1142. //! Nothing by default.
  1143. //!
  1144. //! @par Complexity
  1145. //! Constant O(1).
  1146. reference back()
  1147. {
  1148. errh::check_empty(*this);
  1149. return *(this->end() - 1);
  1150. }
  1151. //! @pre \c !empty()
  1152. //!
  1153. //! @brief Returns const reference to the first element.
  1154. //!
  1155. //! @return const reference to the last element
  1156. //! from the beginning of the container.
  1157. //!
  1158. //! @par Throws
  1159. //! Nothing by default.
  1160. //!
  1161. //! @par Complexity
  1162. //! Constant O(1).
  1163. const_reference back() const
  1164. {
  1165. errh::check_empty(*this);
  1166. return *(this->end() - 1);
  1167. }
  1168. //! @brief Pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  1169. //! For a non-empty vector <tt>data() == &front()</tt>.
  1170. //!
  1171. //! @par Throws
  1172. //! Nothing.
  1173. //!
  1174. //! @par Complexity
  1175. //! Constant O(1).
  1176. Value * data()
  1177. {
  1178. return (addressof)(*(this->ptr()));
  1179. }
  1180. //! @brief Const pointer such that <tt>[data(), data() + size())</tt> is a valid range.
  1181. //! For a non-empty vector <tt>data() == &front()</tt>.
  1182. //!
  1183. //! @par Throws
  1184. //! Nothing.
  1185. //!
  1186. //! @par Complexity
  1187. //! Constant O(1).
  1188. const Value * data() const
  1189. {
  1190. return (addressof)(*(this->ptr()));
  1191. }
  1192. //! @brief Returns iterator to the first element.
  1193. //!
  1194. //! @return iterator to the first element contained in the vector.
  1195. //!
  1196. //! @par Throws
  1197. //! Nothing.
  1198. //!
  1199. //! @par Complexity
  1200. //! Constant O(1).
  1201. iterator begin() { return this->ptr(); }
  1202. //! @brief Returns const iterator to the first element.
  1203. //!
  1204. //! @return const_iterator to the first element contained in the vector.
  1205. //!
  1206. //! @par Throws
  1207. //! Nothing.
  1208. //!
  1209. //! @par Complexity
  1210. //! Constant O(1).
  1211. const_iterator begin() const { return this->ptr(); }
  1212. //! @brief Returns const iterator to the first element.
  1213. //!
  1214. //! @return const_iterator to the first element contained in the vector.
  1215. //!
  1216. //! @par Throws
  1217. //! Nothing.
  1218. //!
  1219. //! @par Complexity
  1220. //! Constant O(1).
  1221. const_iterator cbegin() const { return this->ptr(); }
  1222. //! @brief Returns iterator to the one after the last element.
  1223. //!
  1224. //! @return iterator pointing to the one after the last element contained in the vector.
  1225. //!
  1226. //! @par Throws
  1227. //! Nothing.
  1228. //!
  1229. //! @par Complexity
  1230. //! Constant O(1).
  1231. iterator end() { return this->begin() + m_size; }
  1232. //! @brief Returns const iterator to the one after the last element.
  1233. //!
  1234. //! @return const_iterator pointing to the one after the last element contained in the vector.
  1235. //!
  1236. //! @par Throws
  1237. //! Nothing.
  1238. //!
  1239. //! @par Complexity
  1240. //! Constant O(1).
  1241. const_iterator end() const { return this->begin() + m_size; }
  1242. //! @brief Returns const iterator to the one after the last element.
  1243. //!
  1244. //! @return const_iterator pointing to the one after the last element contained in the vector.
  1245. //!
  1246. //! @par Throws
  1247. //! Nothing.
  1248. //!
  1249. //! @par Complexity
  1250. //! Constant O(1).
  1251. const_iterator cend() const { return this->cbegin() + m_size; }
  1252. //! @brief Returns reverse iterator to the first element of the reversed container.
  1253. //!
  1254. //! @return reverse_iterator pointing to the beginning
  1255. //! of the reversed varray.
  1256. //!
  1257. //! @par Throws
  1258. //! Nothing.
  1259. //!
  1260. //! @par Complexity
  1261. //! Constant O(1).
  1262. reverse_iterator rbegin() { return reverse_iterator(this->end()); }
  1263. //! @brief Returns const reverse iterator to the first element of the reversed container.
  1264. //!
  1265. //! @return const_reverse_iterator pointing to the beginning
  1266. //! of the reversed varray.
  1267. //!
  1268. //! @par Throws
  1269. //! Nothing.
  1270. //!
  1271. //! @par Complexity
  1272. //! Constant O(1).
  1273. const_reverse_iterator rbegin() const { return reverse_iterator(this->end()); }
  1274. //! @brief Returns const reverse iterator to the first element of the reversed container.
  1275. //!
  1276. //! @return const_reverse_iterator pointing to the beginning
  1277. //! of the reversed varray.
  1278. //!
  1279. //! @par Throws
  1280. //! Nothing.
  1281. //!
  1282. //! @par Complexity
  1283. //! Constant O(1).
  1284. const_reverse_iterator crbegin() const { return reverse_iterator(this->end()); }
  1285. //! @brief Returns reverse iterator to the one after the last element of the reversed container.
  1286. //!
  1287. //! @return reverse_iterator pointing to the one after the last element
  1288. //! of the reversed varray.
  1289. //!
  1290. //! @par Throws
  1291. //! Nothing.
  1292. //!
  1293. //! @par Complexity
  1294. //! Constant O(1).
  1295. reverse_iterator rend() { return reverse_iterator(this->begin()); }
  1296. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  1297. //!
  1298. //! @return const_reverse_iterator pointing to the one after the last element
  1299. //! of the reversed varray.
  1300. //!
  1301. //! @par Throws
  1302. //! Nothing.
  1303. //!
  1304. //! @par Complexity
  1305. //! Constant O(1).
  1306. const_reverse_iterator rend() const { return reverse_iterator(this->begin()); }
  1307. //! @brief Returns const reverse iterator to the one after the last element of the reversed container.
  1308. //!
  1309. //! @return const_reverse_iterator pointing to the one after the last element
  1310. //! of the reversed varray.
  1311. //!
  1312. //! @par Throws
  1313. //! Nothing.
  1314. //!
  1315. //! @par Complexity
  1316. //! Constant O(1).
  1317. const_reverse_iterator crend() const { return reverse_iterator(this->begin()); }
  1318. //! @brief Returns container's capacity.
  1319. //!
  1320. //! @return container's capacity.
  1321. //!
  1322. //! @par Throws
  1323. //! Nothing.
  1324. //!
  1325. //! @par Complexity
  1326. //! Constant O(1).
  1327. static size_type capacity() { return Capacity; }
  1328. //! @brief Returns container's capacity.
  1329. //!
  1330. //! @return container's capacity.
  1331. //!
  1332. //! @par Throws
  1333. //! Nothing.
  1334. //!
  1335. //! @par Complexity
  1336. //! Constant O(1).
  1337. static size_type max_size() { return Capacity; }
  1338. //! @brief Returns the number of stored elements.
  1339. //!
  1340. //! @return Number of elements contained in the container.
  1341. //!
  1342. //! @par Throws
  1343. //! Nothing.
  1344. //!
  1345. //! @par Complexity
  1346. //! Constant O(1).
  1347. size_type size() const { return m_size; }
  1348. //! @brief Queries if the container contains elements.
  1349. //!
  1350. //! @return true if the number of elements contained in the
  1351. //! container is equal to 0.
  1352. //!
  1353. //! @par Throws
  1354. //! Nothing.
  1355. //!
  1356. //! @par Complexity
  1357. //! Constant O(1).
  1358. bool empty() const { return 0 == m_size; }
  1359. private:
  1360. // @par Throws
  1361. // Nothing.
  1362. // @par Complexity
  1363. // Linear O(N).
  1364. template <std::size_t C, typename S>
  1365. void move_ctor_dispatch(varray<value_type, C, S> & other, true_type /*use_memop*/)
  1366. {
  1367. ::memcpy(this->data(), other.data(), sizeof(Value) * other.m_size);
  1368. m_size = other.m_size;
  1369. }
  1370. // @par Throws
  1371. // @li If boost::has_nothrow_move<Value>::value is true and Value's move constructor throws
  1372. // @li If boost::has_nothrow_move<Value>::value is false and Value's copy constructor throws.
  1373. // @par Complexity
  1374. // Linear O(N).
  1375. template <std::size_t C, typename S>
  1376. void move_ctor_dispatch(varray<value_type, C, S> & other, false_type /*use_memop*/)
  1377. {
  1378. namespace sv = varray_detail;
  1379. sv::uninitialized_move_if_noexcept(other.begin(), other.end(), this->begin()); // may throw
  1380. m_size = other.m_size;
  1381. }
  1382. // @par Throws
  1383. // Nothing.
  1384. // @par Complexity
  1385. // Linear O(N).
  1386. template <std::size_t C, typename S>
  1387. void move_assign_dispatch(varray<value_type, C, S> & other, true_type /*use_memop*/)
  1388. {
  1389. this->clear();
  1390. ::memcpy(this->data(), other.data(), sizeof(Value) * other.m_size);
  1391. boost::adl_move_swap(m_size, other.m_size);
  1392. }
  1393. // @par Throws
  1394. // @li If boost::has_nothrow_move<Value>::value is true and Value's move constructor or move assignment throws
  1395. // @li If boost::has_nothrow_move<Value>::value is false and Value's copy constructor or move assignment throws.
  1396. // @par Complexity
  1397. // Linear O(N).
  1398. template <std::size_t C, typename S>
  1399. void move_assign_dispatch(varray<value_type, C, S> & other, false_type /*use_memop*/)
  1400. {
  1401. namespace sv = varray_detail;
  1402. if ( m_size <= static_cast<size_type>(other.size()) )
  1403. {
  1404. sv::move_if_noexcept(other.begin(), other.begin() + m_size, this->begin()); // may throw
  1405. // TODO - perform uninitialized_copy first?
  1406. sv::uninitialized_move_if_noexcept(other.begin() + m_size, other.end(), this->end()); // may throw
  1407. }
  1408. else
  1409. {
  1410. sv::move_if_noexcept(other.begin(), other.end(), this->begin()); // may throw
  1411. sv::destroy(this->begin() + other.size(), this->end());
  1412. }
  1413. m_size = other.size(); // update end
  1414. }
  1415. // @par Throws
  1416. // Nothing.
  1417. // @par Complexity
  1418. // Linear O(N).
  1419. template <std::size_t C, typename S>
  1420. void swap_dispatch(varray<value_type, C, S> & other, true_type const& /*use_optimized_swap*/)
  1421. {
  1422. typedef typename
  1423. if_c<
  1424. Capacity < C,
  1425. aligned_storage_type,
  1426. typename varray<value_type, C, S>::aligned_storage_type
  1427. >::type
  1428. storage_type;
  1429. storage_type temp_storage;
  1430. value_type * temp_ptr = static_cast<value_type*>(static_cast<void*>(&temp_storage));
  1431. ::memcpy(temp_ptr, this->data(), sizeof(Value) * this->size());
  1432. ::memcpy(this->data(), other.data(), sizeof(Value) * other.size());
  1433. ::memcpy(other.data(), temp_ptr, sizeof(Value) * this->size());
  1434. boost::adl_move_swap(m_size, other.m_size);
  1435. }
  1436. // @par Throws
  1437. // If Value's move constructor or move assignment throws
  1438. // but only if use_memop_in_swap_and_move is false_type - default.
  1439. // @par Complexity
  1440. // Linear O(N).
  1441. template <std::size_t C, typename S>
  1442. void swap_dispatch(varray<value_type, C, S> & other, false_type const& /*use_optimized_swap*/)
  1443. {
  1444. namespace sv = varray_detail;
  1445. typedef typename
  1446. vt::use_memop_in_swap_and_move use_memop_in_swap_and_move;
  1447. if ( this->size() < other.size() )
  1448. swap_dispatch_impl(this->begin(), this->end(), other.begin(), other.end(), use_memop_in_swap_and_move()); // may throw
  1449. else
  1450. swap_dispatch_impl(other.begin(), other.end(), this->begin(), this->end(), use_memop_in_swap_and_move()); // may throw
  1451. boost::adl_move_swap(m_size, other.m_size);
  1452. }
  1453. // @par Throws
  1454. // Nothing.
  1455. // @par Complexity
  1456. // Linear O(N).
  1457. void swap_dispatch_impl(iterator first_sm, iterator last_sm, iterator first_la, iterator last_la, true_type const& /*use_memop*/)
  1458. {
  1459. //BOOST_ASSERT_MSG(boost::container::iterator_distance(first_sm, last_sm) <= boost::container::iterator_distance(first_la, last_la));
  1460. namespace sv = varray_detail;
  1461. for (; first_sm != last_sm ; ++first_sm, ++first_la)
  1462. {
  1463. typename aligned_storage<
  1464. sizeof(value_type),
  1465. alignment_of<value_type>::value
  1466. >::type temp_storage;
  1467. value_type * temp_ptr = static_cast<value_type*>(static_cast<void*>(&temp_storage));
  1468. ::memcpy(temp_ptr, (addressof)(*first_sm), sizeof(value_type));
  1469. ::memcpy((addressof)(*first_sm), (addressof)(*first_la), sizeof(value_type));
  1470. ::memcpy((addressof)(*first_la), temp_ptr, sizeof(value_type));
  1471. }
  1472. ::memcpy(first_sm, first_la, sizeof(value_type) * boost::container::iterator_distance(first_la, last_la));
  1473. }
  1474. // @par Throws
  1475. // If Value's move constructor or move assignment throws.
  1476. // @par Complexity
  1477. // Linear O(N).
  1478. void swap_dispatch_impl(iterator first_sm, iterator last_sm, iterator first_la, iterator last_la, false_type const& /*use_memop*/)
  1479. {
  1480. //BOOST_ASSERT_MSG(boost::container::iterator_distance(first_sm, last_sm) <= boost::container::iterator_distance(first_la, last_la));
  1481. namespace sv = varray_detail;
  1482. for (; first_sm != last_sm ; ++first_sm, ++first_la)
  1483. {
  1484. //boost::adl_move_swap(*first_sm, *first_la); // may throw
  1485. value_type temp(boost::move(*first_sm)); // may throw
  1486. *first_sm = boost::move(*first_la); // may throw
  1487. *first_la = boost::move(temp); // may throw
  1488. }
  1489. sv::uninitialized_move(first_la, last_la, first_sm); // may throw
  1490. sv::destroy(first_la, last_la);
  1491. }
  1492. // insert
  1493. // @par Throws
  1494. // If Value's move constructor or move assignment throws
  1495. // or if Value's copy assignment throws.
  1496. // @par Complexity
  1497. // Linear O(N).
  1498. template <typename V>
  1499. iterator priv_insert(iterator position, V & value)
  1500. {
  1501. typedef typename vt::disable_trivial_init dti;
  1502. namespace sv = varray_detail;
  1503. errh::check_iterator_end_eq(*this, position);
  1504. errh::check_capacity(*this, m_size + 1); // may throw
  1505. if ( position == this->end() )
  1506. {
  1507. sv::construct(dti(), position, value); // may throw
  1508. ++m_size; // update end
  1509. }
  1510. else
  1511. {
  1512. // TODO - should following lines check for exception and revert to the old size?
  1513. // TODO - should move be used only if it's nonthrowing?
  1514. value_type & r = *(this->end() - 1);
  1515. sv::construct(dti(), this->end(), boost::move(r)); // may throw
  1516. ++m_size; // update end
  1517. sv::move_backward(position, this->end() - 2, this->end() - 1); // may throw
  1518. sv::assign(position, value); // may throw
  1519. }
  1520. return position;
  1521. }
  1522. // insert
  1523. // @par Throws
  1524. // If Value's move constructor, move assignment throws
  1525. // or if Value's copy constructor or copy assignment throws.
  1526. // @par Complexity
  1527. // Linear O(N).
  1528. template <typename Iterator>
  1529. typename iterator_enable_if_tag<Iterator, std::random_access_iterator_tag>::type
  1530. insert_dispatch(iterator position, Iterator first, Iterator last)
  1531. {
  1532. errh::check_iterator_end_eq(*this, position);
  1533. size_type count = boost::container::iterator_distance(first, last);
  1534. errh::check_capacity(*this, m_size + count); // may throw
  1535. if ( position == this->end() )
  1536. {
  1537. namespace sv = varray_detail;
  1538. sv::uninitialized_copy(first, last, position); // may throw
  1539. m_size += count; // update end
  1540. }
  1541. else
  1542. {
  1543. this->insert_in_the_middle(position, first, last, count); // may throw
  1544. }
  1545. }
  1546. // @par Throws
  1547. // If Value's move constructor, move assignment throws
  1548. // or if Value's copy constructor or copy assignment throws.
  1549. // @par Complexity
  1550. // Linear O(N).
  1551. template <typename Iterator, typename Category>
  1552. typename iterator_disable_if_tag<Iterator, std::random_access_iterator_tag>::type
  1553. insert_dispatch(iterator position, Iterator first, Iterator last)
  1554. {
  1555. errh::check_iterator_end_eq(*this, position);
  1556. if ( position == this->end() )
  1557. {
  1558. namespace sv = varray_detail;
  1559. std::ptrdiff_t d = boost::container::iterator_distance(position, this->begin() + Capacity);
  1560. std::size_t count = sv::uninitialized_copy_s(first, last, position, d); // may throw
  1561. errh::check_capacity(*this, count <= static_cast<std::size_t>(d) ? m_size + count : Capacity + 1); // may throw
  1562. m_size += count;
  1563. }
  1564. else
  1565. {
  1566. size_type count = boost::container::iterator_distance(first, last);
  1567. errh::check_capacity(*this, m_size + count); // may throw
  1568. this->insert_in_the_middle(position, first, last, count); // may throw
  1569. }
  1570. }
  1571. // @par Throws
  1572. // If Value's move constructor, move assignment throws
  1573. // or if Value's copy constructor or copy assignment throws.
  1574. // @par Complexity
  1575. // Linear O(N).
  1576. template <typename Iterator>
  1577. void insert_in_the_middle(iterator position, Iterator first, Iterator last, difference_type count)
  1578. {
  1579. namespace sv = varray_detail;
  1580. difference_type to_move = boost::container::iterator_distance(position, this->end());
  1581. // TODO - should following lines check for exception and revert to the old size?
  1582. if ( count < to_move )
  1583. {
  1584. sv::uninitialized_move(this->end() - count, this->end(), this->end()); // may throw
  1585. m_size += count; // update end
  1586. sv::move_backward(position, position + to_move - count, this->end() - count); // may throw
  1587. sv::copy(first, last, position); // may throw
  1588. }
  1589. else
  1590. {
  1591. Iterator middle_iter = first;
  1592. boost::container::iterator_advance(middle_iter, to_move);
  1593. sv::uninitialized_copy(middle_iter, last, this->end()); // may throw
  1594. m_size += count - to_move; // update end
  1595. sv::uninitialized_move(position, position + to_move, position + count); // may throw
  1596. m_size += to_move; // update end
  1597. sv::copy(first, middle_iter, position); // may throw
  1598. }
  1599. }
  1600. // assign
  1601. // @par Throws
  1602. // If Value's constructor or assignment taking dereferenced Iterator throws.
  1603. // @par Complexity
  1604. // Linear O(N).
  1605. template <typename Iterator>
  1606. typename iterator_enable_if_tag<Iterator, std::random_access_iterator_tag>::type
  1607. assign_dispatch(Iterator first, Iterator last)
  1608. {
  1609. namespace sv = varray_detail;
  1610. size_type s = boost::container::iterator_distance(first, last);
  1611. errh::check_capacity(*this, s); // may throw
  1612. if ( m_size <= static_cast<size_type>(s) )
  1613. {
  1614. sv::copy(first, first + m_size, this->begin()); // may throw
  1615. // TODO - perform uninitialized_copy first?
  1616. sv::uninitialized_copy(first + m_size, last, this->end()); // may throw
  1617. }
  1618. else
  1619. {
  1620. sv::copy(first, last, this->begin()); // may throw
  1621. sv::destroy(this->begin() + s, this->end());
  1622. }
  1623. m_size = s; // update end
  1624. }
  1625. // @par Throws
  1626. // If Value's constructor or assignment taking dereferenced Iterator throws.
  1627. // @par Complexity
  1628. // Linear O(N).
  1629. template <typename Iterator, typename Category>
  1630. typename iterator_disable_if_tag<Iterator, std::random_access_iterator_tag>::type
  1631. assign_dispatch(Iterator first, Iterator last)
  1632. {
  1633. namespace sv = varray_detail;
  1634. size_type s = 0;
  1635. iterator it = this->begin();
  1636. for ( ; it != this->end() && first != last ; ++it, ++first, ++s )
  1637. *it = *first; // may throw
  1638. sv::destroy(it, this->end());
  1639. std::ptrdiff_t d = boost::container::iterator_distance(it, this->begin() + Capacity);
  1640. std::size_t count = sv::uninitialized_copy_s(first, last, it, d); // may throw
  1641. s += count;
  1642. errh::check_capacity(*this, count <= static_cast<std::size_t>(d) ? s : Capacity + 1); // may throw
  1643. m_size = s; // update end
  1644. }
  1645. pointer ptr()
  1646. {
  1647. return pointer(static_cast<Value*>(static_cast<void*>(&m_storage)));
  1648. }
  1649. const_pointer ptr() const
  1650. {
  1651. return pointer(static_cast<const Value*>(static_cast<const void*>(&m_storage)));
  1652. }
  1653. size_type m_size;
  1654. aligned_storage_type m_storage;
  1655. };
  1656. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1657. template<typename Value, typename Strategy>
  1658. class varray<Value, 0, Strategy>
  1659. {
  1660. typedef varray_traits<
  1661. Value, 0, Strategy
  1662. > vt;
  1663. typedef typename vt::size_type stored_size_type;
  1664. typedef typename vt::error_handler errh;
  1665. public:
  1666. typedef typename vt::value_type value_type;
  1667. typedef stored_size_type size_type;
  1668. typedef typename vt::difference_type difference_type;
  1669. typedef typename vt::pointer pointer;
  1670. typedef typename vt::const_pointer const_pointer;
  1671. typedef typename vt::reference reference;
  1672. typedef typename vt::const_reference const_reference;
  1673. typedef pointer iterator;
  1674. typedef const_pointer const_iterator;
  1675. typedef boost::container::reverse_iterator<iterator> reverse_iterator;
  1676. typedef boost::container::reverse_iterator<const_iterator> const_reverse_iterator;
  1677. // nothrow
  1678. varray() {}
  1679. // strong
  1680. explicit varray(size_type count)
  1681. {
  1682. errh::check_capacity(*this, count); // may throw
  1683. }
  1684. // strong
  1685. varray(size_type count, value_type const&)
  1686. {
  1687. errh::check_capacity(*this, count); // may throw
  1688. }
  1689. // strong
  1690. varray(varray const& other)
  1691. {
  1692. errh::check_capacity(*this, other.size());
  1693. }
  1694. // strong
  1695. template <size_t C, typename S>
  1696. varray(varray<value_type, C, S> const& other)
  1697. {
  1698. errh::check_capacity(*this, other.size()); // may throw
  1699. }
  1700. // strong
  1701. template <typename Iterator>
  1702. varray(Iterator first, Iterator last)
  1703. {
  1704. errh::check_capacity(*this, boost::container::iterator_distance(first, last)); // may throw
  1705. }
  1706. // basic
  1707. varray & operator=(varray const& other)
  1708. {
  1709. errh::check_capacity(*this, other.size());
  1710. return *this;
  1711. }
  1712. // basic
  1713. template <size_t C, typename S>
  1714. varray & operator=(varray<value_type, C, S> const& other)
  1715. {
  1716. errh::check_capacity(*this, other.size()); // may throw
  1717. return *this;
  1718. }
  1719. // nothrow
  1720. ~varray() {}
  1721. // strong
  1722. void resize(size_type count)
  1723. {
  1724. errh::check_capacity(*this, count); // may throw
  1725. }
  1726. // strong
  1727. void resize(size_type count, value_type const&)
  1728. {
  1729. errh::check_capacity(*this, count); // may throw
  1730. }
  1731. // nothrow
  1732. void reserve(size_type count)
  1733. {
  1734. errh::check_capacity(*this, count); // may throw
  1735. }
  1736. // strong
  1737. void push_back(value_type const&)
  1738. {
  1739. errh::check_capacity(*this, 1); // may throw
  1740. }
  1741. // nothrow
  1742. void pop_back()
  1743. {
  1744. errh::check_empty(*this);
  1745. }
  1746. // basic
  1747. void insert(iterator position, value_type const&)
  1748. {
  1749. errh::check_iterator_end_eq(*this, position);
  1750. errh::check_capacity(*this, 1); // may throw
  1751. }
  1752. // basic
  1753. void insert(iterator position, size_type count, value_type const&)
  1754. {
  1755. errh::check_iterator_end_eq(*this, position);
  1756. errh::check_capacity(*this, count); // may throw
  1757. }
  1758. // basic
  1759. template <typename Iterator>
  1760. void insert(iterator, Iterator first, Iterator last)
  1761. {
  1762. errh::check_capacity(*this, boost::container::iterator_distance(first, last)); // may throw
  1763. }
  1764. // basic
  1765. void erase(iterator position)
  1766. {
  1767. errh::check_iterator_end_neq(*this, position);
  1768. }
  1769. // basic
  1770. void erase(iterator first, iterator last)
  1771. {
  1772. errh::check_iterator_end_eq(*this, first);
  1773. errh::check_iterator_end_eq(*this, last);
  1774. //BOOST_ASSERT_MSG(0 <= n, "invalid range");
  1775. }
  1776. // basic
  1777. template <typename Iterator>
  1778. void assign(Iterator first, Iterator last)
  1779. {
  1780. errh::check_capacity(*this, boost::container::iterator_distance(first, last)); // may throw
  1781. }
  1782. // basic
  1783. void assign(size_type count, value_type const&)
  1784. {
  1785. errh::check_capacity(*this, count); // may throw
  1786. }
  1787. // nothrow
  1788. void clear() {}
  1789. // strong
  1790. reference at(size_type i)
  1791. {
  1792. errh::check_at(*this, i); // may throw
  1793. return *(this->begin() + i);
  1794. }
  1795. // strong
  1796. const_reference at(size_type i) const
  1797. {
  1798. errh::check_at(*this, i); // may throw
  1799. return *(this->begin() + i);
  1800. }
  1801. // nothrow
  1802. reference operator[](size_type i)
  1803. {
  1804. errh::check_operator_brackets(*this, i);
  1805. return *(this->begin() + i);
  1806. }
  1807. // nothrow
  1808. const_reference operator[](size_type i) const
  1809. {
  1810. errh::check_operator_brackets(*this, i);
  1811. return *(this->begin() + i);
  1812. }
  1813. // nothrow
  1814. reference front()
  1815. {
  1816. errh::check_empty(*this);
  1817. return *(this->begin());
  1818. }
  1819. // nothrow
  1820. const_reference front() const
  1821. {
  1822. errh::check_empty(*this);
  1823. return *(this->begin());
  1824. }
  1825. // nothrow
  1826. reference back()
  1827. {
  1828. errh::check_empty(*this);
  1829. return *(this->end() - 1);
  1830. }
  1831. // nothrow
  1832. const_reference back() const
  1833. {
  1834. errh::check_empty(*this);
  1835. return *(this->end() - 1);
  1836. }
  1837. // nothrow
  1838. Value * data() { return (addressof)(*(this->ptr())); }
  1839. const Value * data() const { return (addressof)(*(this->ptr())); }
  1840. // nothrow
  1841. iterator begin() { return this->ptr(); }
  1842. const_iterator begin() const { return this->ptr(); }
  1843. const_iterator cbegin() const { return this->ptr(); }
  1844. iterator end() { return this->begin(); }
  1845. const_iterator end() const { return this->begin(); }
  1846. const_iterator cend() const { return this->cbegin(); }
  1847. // nothrow
  1848. reverse_iterator rbegin() { return reverse_iterator(this->end()); }
  1849. const_reverse_iterator rbegin() const { return reverse_iterator(this->end()); }
  1850. const_reverse_iterator crbegin() const { return reverse_iterator(this->end()); }
  1851. reverse_iterator rend() { return reverse_iterator(this->begin()); }
  1852. const_reverse_iterator rend() const { return reverse_iterator(this->begin()); }
  1853. const_reverse_iterator crend() const { return reverse_iterator(this->begin()); }
  1854. // nothrow
  1855. size_type capacity() const { return 0; }
  1856. size_type max_size() const { return 0; }
  1857. size_type size() const { return 0; }
  1858. bool empty() const { return true; }
  1859. private:
  1860. pointer ptr()
  1861. {
  1862. return pointer(reinterpret_cast<Value*>(this));
  1863. }
  1864. const_pointer ptr() const
  1865. {
  1866. return const_pointer(reinterpret_cast<const Value*>(this));
  1867. }
  1868. };
  1869. #endif // !BOOST_CONTAINER_DOXYGEN_INVOKED
  1870. //! @brief Checks if contents of two varrays are equal.
  1871. //!
  1872. //! @ingroup varray_non_member
  1873. //!
  1874. //! @param x The first varray.
  1875. //! @param y The second varray.
  1876. //!
  1877. //! @return \c true if containers have the same size and elements in both containers are equal.
  1878. //!
  1879. //! @par Complexity
  1880. //! Linear O(N).
  1881. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1882. bool operator== (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1883. {
  1884. return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin());
  1885. }
  1886. //! @brief Checks if contents of two varrays are not equal.
  1887. //!
  1888. //! @ingroup varray_non_member
  1889. //!
  1890. //! @param x The first varray.
  1891. //! @param y The second varray.
  1892. //!
  1893. //! @return \c true if containers have different size or elements in both containers are not equal.
  1894. //!
  1895. //! @par Complexity
  1896. //! Linear O(N).
  1897. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1898. bool operator!= (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1899. {
  1900. return !(x==y);
  1901. }
  1902. //! @brief Lexicographically compares varrays.
  1903. //!
  1904. //! @ingroup varray_non_member
  1905. //!
  1906. //! @param x The first varray.
  1907. //! @param y The second varray.
  1908. //!
  1909. //! @return \c true if x compares lexicographically less than y.
  1910. //!
  1911. //! @par Complexity
  1912. //! Linear O(N).
  1913. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1914. bool operator< (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1915. {
  1916. return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  1917. }
  1918. //! @brief Lexicographically compares varrays.
  1919. //!
  1920. //! @ingroup varray_non_member
  1921. //!
  1922. //! @param x The first varray.
  1923. //! @param y The second varray.
  1924. //!
  1925. //! @return \c true if y compares lexicographically less than x.
  1926. //!
  1927. //! @par Complexity
  1928. //! Linear O(N).
  1929. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1930. bool operator> (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1931. {
  1932. return y<x;
  1933. }
  1934. //! @brief Lexicographically compares varrays.
  1935. //!
  1936. //! @ingroup varray_non_member
  1937. //!
  1938. //! @param x The first varray.
  1939. //! @param y The second varray.
  1940. //!
  1941. //! @return \c true if y don't compare lexicographically less than x.
  1942. //!
  1943. //! @par Complexity
  1944. //! Linear O(N).
  1945. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1946. bool operator<= (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1947. {
  1948. return !(y<x);
  1949. }
  1950. //! @brief Lexicographically compares varrays.
  1951. //!
  1952. //! @ingroup varray_non_member
  1953. //!
  1954. //! @param x The first varray.
  1955. //! @param y The second varray.
  1956. //!
  1957. //! @return \c true if x don't compare lexicographically less than y.
  1958. //!
  1959. //! @par Complexity
  1960. //! Linear O(N).
  1961. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1962. bool operator>= (varray<V, C1, S1> const& x, varray<V, C2, S2> const& y)
  1963. {
  1964. return !(x<y);
  1965. }
  1966. //! @brief Swaps contents of two varrays.
  1967. //!
  1968. //! This function calls varray::swap().
  1969. //!
  1970. //! @ingroup varray_non_member
  1971. //!
  1972. //! @param x The first varray.
  1973. //! @param y The second varray.
  1974. //!
  1975. //! @par Complexity
  1976. //! Linear O(N).
  1977. template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
  1978. inline void swap(varray<V, C1, S1> & x, varray<V, C2, S2> & y)
  1979. {
  1980. x.swap(y);
  1981. }
  1982. }}} // namespace boost::container::dtl
  1983. #include <boost/container/detail/config_end.hpp>
  1984. #endif // BOOST_CONTAINER_DETAIL_VARRAY_HPP