base.hpp 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124
  1. // Implementation of the base circular buffer.
  2. // Copyright (c) 2003-2008 Jan Gaspar
  3. // Copyright (c) 2013 Paul A. Bristow // Doxygen comments changed.
  4. // Copyright (c) 2013 Antony Polukhin // Move semantics implementation.
  5. // Copyright (c) 2014 Glen Fernandes // C++11 allocator model support.
  6. // Use, modification, and distribution is subject to the Boost Software
  7. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)
  10. #define BOOST_CIRCULAR_BUFFER_BASE_HPP
  11. #if defined(_MSC_VER)
  12. #pragma once
  13. #endif
  14. #include <boost/config.hpp>
  15. #include <boost/call_traits.hpp>
  16. #include <boost/concept_check.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/container/allocator_traits.hpp>
  19. #include <boost/iterator/reverse_iterator.hpp>
  20. #include <boost/iterator/iterator_traits.hpp>
  21. #include <boost/type_traits/is_stateless.hpp>
  22. #include <boost/type_traits/is_integral.hpp>
  23. #include <boost/type_traits/is_scalar.hpp>
  24. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  25. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  26. #include <boost/type_traits/is_copy_constructible.hpp>
  27. #include <boost/type_traits/conditional.hpp>
  28. #include <boost/move/adl_move_swap.hpp>
  29. #include <boost/move/move.hpp>
  30. #include <boost/utility/addressof.hpp>
  31. #include <algorithm>
  32. #include <utility>
  33. #include <deque>
  34. #include <stdexcept>
  35. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  36. #include <stddef.h>
  37. #endif
  38. namespace boost {
  39. /*!
  40. \class circular_buffer
  41. \brief Circular buffer - a STL compliant container.
  42. \tparam T The type of the elements stored in the <code>circular_buffer</code>.
  43. \par Type Requirements T
  44. The <code>T</code> has to be <a href="http://www.sgi.com/tech/stl/Assignable.html">
  45. SGIAssignable</a> (SGI STL defined combination of <a href="../../../utility/Assignable.html">
  46. Assignable</a> and <a href="../../../utility/CopyConstructible.html">CopyConstructible</a>).
  47. Moreover <code>T</code> has to be <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html">
  48. DefaultConstructible</a> if supplied as a default parameter when invoking some of the
  49. <code>circular_buffer</code>'s methods e.g.
  50. <code>insert(iterator pos, const value_type& item = %value_type())</code>. And
  51. <a href="http://www.sgi.com/tech/stl/EqualityComparable.html">EqualityComparable</a> and/or
  52. <a href="../../../utility/LessThanComparable.html">LessThanComparable</a> if the <code>circular_buffer</code>
  53. will be compared with another container.
  54. \tparam Alloc The allocator type used for all internal memory management.
  55. \par Type Requirements Alloc
  56. The <code>Alloc</code> has to meet the allocator requirements imposed by STL.
  57. \par Default Alloc
  58. std::allocator<T>
  59. For detailed documentation of the circular_buffer visit:
  60. http://www.boost.org/libs/circular_buffer/doc/circular_buffer.html
  61. */
  62. template <class T, class Alloc>
  63. class circular_buffer
  64. /*! \cond */
  65. #if BOOST_CB_ENABLE_DEBUG
  66. : public cb_details::debug_iterator_registry
  67. #endif
  68. /*! \endcond */
  69. {
  70. // Requirements
  71. //BOOST_CLASS_REQUIRE(T, boost, SGIAssignableConcept);
  72. //BOOST_CONCEPT_ASSERT((Assignable<T>));
  73. //BOOST_CONCEPT_ASSERT((CopyConstructible<T>));
  74. //BOOST_CONCEPT_ASSERT((DefaultConstructible<T>));
  75. // Required if the circular_buffer will be compared with anther container.
  76. //BOOST_CONCEPT_ASSERT((EqualityComparable<T>));
  77. //BOOST_CONCEPT_ASSERT((LessThanComparable<T>));
  78. public:
  79. // Basic types
  80. //! The type of this <code>circular_buffer</code>.
  81. typedef circular_buffer<T, Alloc> this_type;
  82. //! The type of elements stored in the <code>circular_buffer</code>.
  83. typedef typename boost::container::allocator_traits<Alloc>::value_type value_type;
  84. //! A pointer to an element.
  85. typedef typename boost::container::allocator_traits<Alloc>::pointer pointer;
  86. //! A const pointer to the element.
  87. typedef typename boost::container::allocator_traits<Alloc>::const_pointer const_pointer;
  88. //! A reference to an element.
  89. typedef typename boost::container::allocator_traits<Alloc>::reference reference;
  90. //! A const reference to an element.
  91. typedef typename boost::container::allocator_traits<Alloc>::const_reference const_reference;
  92. //! The distance type.
  93. /*!
  94. (A signed integral type used to represent the distance between two iterators.)
  95. */
  96. typedef typename boost::container::allocator_traits<Alloc>::difference_type difference_type;
  97. //! The size type.
  98. /*!
  99. (An unsigned integral type that can represent any non-negative value of the container's distance type.)
  100. */
  101. typedef typename boost::container::allocator_traits<Alloc>::size_type size_type;
  102. //! The type of an allocator used in the <code>circular_buffer</code>.
  103. typedef Alloc allocator_type;
  104. // Iterators
  105. //! A const (random access) iterator used to iterate through the <code>circular_buffer</code>.
  106. typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::const_traits<boost::container::allocator_traits<Alloc> > > const_iterator;
  107. //! A (random access) iterator used to iterate through the <code>circular_buffer</code>.
  108. typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::nonconst_traits<boost::container::allocator_traits<Alloc> > > iterator;
  109. //! A const iterator used to iterate backwards through a <code>circular_buffer</code>.
  110. typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
  111. //! An iterator used to iterate backwards through a <code>circular_buffer</code>.
  112. typedef boost::reverse_iterator<iterator> reverse_iterator;
  113. // Container specific types
  114. //! An array range.
  115. /*!
  116. (A typedef for the <a href="http://www.sgi.com/tech/stl/pair.html"><code>std::pair</code></a> where
  117. its first element is a pointer to a beginning of an array and its second element represents
  118. a size of the array.)
  119. */
  120. typedef std::pair<pointer, size_type> array_range;
  121. //! A range of a const array.
  122. /*!
  123. (A typedef for the <a href="http://www.sgi.com/tech/stl/pair.html"><code>std::pair</code></a> where
  124. its first element is a pointer to a beginning of a const array and its second element represents
  125. a size of the const array.)
  126. */
  127. typedef std::pair<const_pointer, size_type> const_array_range;
  128. //! The capacity type.
  129. /*!
  130. (Same as <code>size_type</code> - defined for consistency with the __cbso class.
  131. */
  132. // <a href="space_optimized.html"><code>circular_buffer_space_optimized</code></a>.)
  133. typedef size_type capacity_type;
  134. // Helper types
  135. //! A type representing the "best" way to pass the value_type to a method.
  136. typedef const value_type& param_value_type;
  137. //! A type representing rvalue from param type.
  138. //! On compilers without rvalue references support this type is the Boost.Moves type used for emulation.
  139. typedef BOOST_RV_REF(value_type) rvalue_type;
  140. private:
  141. // Member variables
  142. //! The internal buffer used for storing elements in the circular buffer.
  143. pointer m_buff;
  144. //! The internal buffer's end (end of the storage space).
  145. pointer m_end;
  146. //! The virtual beginning of the circular buffer.
  147. pointer m_first;
  148. //! The virtual end of the circular buffer (one behind the last element).
  149. pointer m_last;
  150. //! The number of items currently stored in the circular buffer.
  151. size_type m_size;
  152. //! The allocator.
  153. allocator_type m_alloc;
  154. // Friends
  155. #if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  156. friend iterator;
  157. friend const_iterator;
  158. #else
  159. template <class Buff, class Traits> friend struct cb_details::iterator;
  160. #endif
  161. public:
  162. // Allocator
  163. //! Get the allocator.
  164. /*!
  165. \return The allocator.
  166. \throws Nothing.
  167. \par Exception Safety
  168. No-throw.
  169. \par Iterator Invalidation
  170. Does not invalidate any iterators.
  171. \par Complexity
  172. Constant (in the size of the <code>circular_buffer</code>).
  173. \sa <code>get_allocator()</code> for obtaining an allocator %reference.
  174. */
  175. allocator_type get_allocator() const BOOST_NOEXCEPT { return m_alloc; }
  176. //! Get the allocator reference.
  177. /*!
  178. \return A reference to the allocator.
  179. \throws Nothing.
  180. \par Exception Safety
  181. No-throw.
  182. \par Iterator Invalidation
  183. Does not invalidate any iterators.
  184. \par Complexity
  185. Constant (in the size of the <code>circular_buffer</code>).
  186. \note This method was added in order to optimize obtaining of the allocator with a state,
  187. although use of stateful allocators in STL is discouraged.
  188. \sa <code>get_allocator() const</code>
  189. */
  190. allocator_type& get_allocator() BOOST_NOEXCEPT { return m_alloc; }
  191. // Element access
  192. //! Get the iterator pointing to the beginning of the <code>circular_buffer</code>.
  193. /*!
  194. \return A random access iterator pointing to the first element of the <code>circular_buffer</code>. If the
  195. <code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
  196. <code>end()</code>.
  197. \throws Nothing.
  198. \par Exception Safety
  199. No-throw.
  200. \par Iterator Invalidation
  201. Does not invalidate any iterators.
  202. \par Complexity
  203. Constant (in the size of the <code>circular_buffer</code>).
  204. \sa <code>end()</code>, <code>rbegin()</code>, <code>rend()</code>
  205. */
  206. iterator begin() BOOST_NOEXCEPT { return iterator(this, empty() ? 0 : m_first); }
  207. //! Get the iterator pointing to the end of the <code>circular_buffer</code>.
  208. /*!
  209. \return A random access iterator pointing to the element "one behind" the last element of the <code>
  210. circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
  211. the one returned by <code>begin()</code>.
  212. \throws Nothing.
  213. \par Exception Safety
  214. No-throw.
  215. \par Iterator Invalidation
  216. Does not invalidate any iterators.
  217. \par Complexity
  218. Constant (in the size of the <code>circular_buffer</code>).
  219. \sa <code>begin()</code>, <code>rbegin()</code>, <code>rend()</code>
  220. */
  221. iterator end() BOOST_NOEXCEPT { return iterator(this, 0); }
  222. //! Get the const iterator pointing to the beginning of the <code>circular_buffer</code>.
  223. /*!
  224. \return A const random access iterator pointing to the first element of the <code>circular_buffer</code>. If
  225. the <code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
  226. <code>end() const</code>.
  227. \throws Nothing.
  228. \par Exception Safety
  229. No-throw.
  230. \par Iterator Invalidation
  231. Does not invalidate any iterators.
  232. \par Complexity
  233. Constant (in the size of the <code>circular_buffer</code>).
  234. \sa <code>end() const</code>, <code>rbegin() const</code>, <code>rend() const</code>
  235. */
  236. const_iterator begin() const BOOST_NOEXCEPT { return const_iterator(this, empty() ? 0 : m_first); }
  237. //! Get the const iterator pointing to the end of the <code>circular_buffer</code>.
  238. /*!
  239. \return A const random access iterator pointing to the element "one behind" the last element of the <code>
  240. circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
  241. the one returned by <code>begin() const</code> const.
  242. \throws Nothing.
  243. \par Exception Safety
  244. No-throw.
  245. \par Iterator Invalidation
  246. Does not invalidate any iterators.
  247. \par Complexity
  248. Constant (in the size of the <code>circular_buffer</code>).
  249. \sa <code>begin() const</code>, <code>rbegin() const</code>, <code>rend() const</code>
  250. */
  251. const_iterator end() const BOOST_NOEXCEPT { return const_iterator(this, 0); }
  252. //! Get the iterator pointing to the beginning of the "reversed" <code>circular_buffer</code>.
  253. /*!
  254. \return A reverse random access iterator pointing to the last element of the <code>circular_buffer</code>.
  255. If the <code>circular_buffer</code> is empty it returns an iterator equal to the one returned by
  256. <code>rend()</code>.
  257. \throws Nothing.
  258. \par Exception Safety
  259. No-throw.
  260. \par Iterator Invalidation
  261. Does not invalidate any iterators.
  262. \par Complexity
  263. Constant (in the size of the <code>circular_buffer</code>).
  264. \sa <code>rend()</code>, <code>begin()</code>, <code>end()</code>
  265. */
  266. reverse_iterator rbegin() BOOST_NOEXCEPT { return reverse_iterator(end()); }
  267. //! Get the iterator pointing to the end of the "reversed" <code>circular_buffer</code>.
  268. /*!
  269. \return A reverse random access iterator pointing to the element "one before" the first element of the <code>
  270. circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal to
  271. the one returned by <code>rbegin()</code>.
  272. \throws Nothing.
  273. \par Exception Safety
  274. No-throw.
  275. \par Iterator Invalidation
  276. Does not invalidate any iterators.
  277. \par Complexity
  278. Constant (in the size of the <code>circular_buffer</code>).
  279. \sa <code>rbegin()</code>, <code>begin()</code>, <code>end()</code>
  280. */
  281. reverse_iterator rend() BOOST_NOEXCEPT { return reverse_iterator(begin()); }
  282. //! Get the const iterator pointing to the beginning of the "reversed" <code>circular_buffer</code>.
  283. /*!
  284. \return A const reverse random access iterator pointing to the last element of the
  285. <code>circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal
  286. to the one returned by <code>rend() const</code>.
  287. \throws Nothing.
  288. \par Exception Safety
  289. No-throw.
  290. \par Iterator Invalidation
  291. Does not invalidate any iterators.
  292. \par Complexity
  293. Constant (in the size of the <code>circular_buffer</code>).
  294. \sa <code>rend() const</code>, <code>begin() const</code>, <code>end() const</code>
  295. */
  296. const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
  297. //! Get the const iterator pointing to the end of the "reversed" <code>circular_buffer</code>.
  298. /*!
  299. \return A const reverse random access iterator pointing to the element "one before" the first element of the
  300. <code>circular_buffer</code>. If the <code>circular_buffer</code> is empty it returns an iterator equal
  301. to the one returned by <code>rbegin() const</code>.
  302. \throws Nothing.
  303. \par Exception Safety
  304. No-throw.
  305. \par Iterator Invalidation
  306. Does not invalidate any iterators.
  307. \par Complexity
  308. Constant (in the size of the <code>circular_buffer</code>).
  309. \sa <code>rbegin() const</code>, <code>begin() const</code>, <code>end() const</code>
  310. */
  311. const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
  312. //! Get the element at the <code>index</code> position.
  313. /*!
  314. \pre <code>0 \<= index \&\& index \< size()</code>
  315. \param index The position of the element.
  316. \return A reference to the element at the <code>index</code> position.
  317. \throws Nothing.
  318. \par Exception Safety
  319. No-throw.
  320. \par Iterator Invalidation
  321. Does not invalidate any iterators.
  322. \par Complexity
  323. Constant (in the size of the <code>circular_buffer</code>).
  324. \sa <code>at()</code>
  325. */
  326. reference operator [] (size_type index) {
  327. BOOST_CB_ASSERT(index < size()); // check for invalid index
  328. return *add(m_first, index);
  329. }
  330. //! Get the element at the <code>index</code> position.
  331. /*!
  332. \pre <code>0 \<= index \&\& index \< size()</code>
  333. \param index The position of the element.
  334. \return A const reference to the element at the <code>index</code> position.
  335. \throws Nothing.
  336. \par Exception Safety
  337. No-throw.
  338. \par Iterator Invalidation
  339. Does not invalidate any iterators.
  340. \par Complexity
  341. Constant (in the size of the <code>circular_buffer</code>).
  342. \sa <code>\link at(size_type)const at() const \endlink</code>
  343. */
  344. const_reference operator [] (size_type index) const {
  345. BOOST_CB_ASSERT(index < size()); // check for invalid index
  346. return *add(m_first, index);
  347. }
  348. //! Get the element at the <code>index</code> position.
  349. /*!
  350. \param index The position of the element.
  351. \return A reference to the element at the <code>index</code> position.
  352. \throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
  353. <code>index >= size()</code>).
  354. \par Exception Safety
  355. Strong.
  356. \par Iterator Invalidation
  357. Does not invalidate any iterators.
  358. \par Complexity
  359. Constant (in the size of the <code>circular_buffer</code>).
  360. \sa <code>\link operator[](size_type) operator[] \endlink</code>
  361. */
  362. reference at(size_type index) {
  363. check_position(index);
  364. return (*this)[index];
  365. }
  366. //! Get the element at the <code>index</code> position.
  367. /*!
  368. \param index The position of the element.
  369. \return A const reference to the element at the <code>index</code> position.
  370. \throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
  371. <code>index >= size()</code>).
  372. \par Exception Safety
  373. Strong.
  374. \par Iterator Invalidation
  375. Does not invalidate any iterators.
  376. \par Complexity
  377. Constant (in the size of the <code>circular_buffer</code>).
  378. \sa <code>\link operator[](size_type)const operator[] const \endlink</code>
  379. */
  380. const_reference at(size_type index) const {
  381. check_position(index);
  382. return (*this)[index];
  383. }
  384. //! Get the first element.
  385. /*!
  386. \pre <code>!empty()</code>
  387. \return A reference to the first element of the <code>circular_buffer</code>.
  388. \throws Nothing.
  389. \par Exception Safety
  390. No-throw.
  391. \par Iterator Invalidation
  392. Does not invalidate any iterators.
  393. \par Complexity
  394. Constant (in the size of the <code>circular_buffer</code>).
  395. \sa <code>back()</code>
  396. */
  397. reference front() {
  398. BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
  399. return *m_first;
  400. }
  401. //! Get the last element.
  402. /*!
  403. \pre <code>!empty()</code>
  404. \return A reference to the last element of the <code>circular_buffer</code>.
  405. \throws Nothing.
  406. \par Exception Safety
  407. No-throw.
  408. \par Iterator Invalidation
  409. Does not invalidate any iterators.
  410. \par Complexity
  411. Constant (in the size of the <code>circular_buffer</code>).
  412. \sa <code>front()</code>
  413. */
  414. reference back() {
  415. BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
  416. return *((m_last == m_buff ? m_end : m_last) - 1);
  417. }
  418. //! Get the first element.
  419. /*!
  420. \pre <code>!empty()</code>
  421. \return A const reference to the first element of the <code>circular_buffer</code>.
  422. \throws Nothing.
  423. \par Exception Safety
  424. No-throw.
  425. \par Iterator Invalidation
  426. Does not invalidate any iterators.
  427. \par Complexity
  428. Constant (in the size of the <code>circular_buffer</code>).
  429. \sa <code>back() const</code>
  430. */
  431. const_reference front() const {
  432. BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
  433. return *m_first;
  434. }
  435. //! Get the last element.
  436. /*!
  437. \pre <code>!empty()</code>
  438. \return A const reference to the last element of the <code>circular_buffer</code>.
  439. \throws Nothing.
  440. \par Exception Safety
  441. No-throw.
  442. \par Iterator Invalidation
  443. Does not invalidate any iterators.
  444. \par Complexity
  445. Constant (in the size of the <code>circular_buffer</code>).
  446. \sa <code>front() const</code>
  447. */
  448. const_reference back() const {
  449. BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
  450. return *((m_last == m_buff ? m_end : m_last) - 1);
  451. }
  452. //! Get the first continuous array of the internal buffer.
  453. /*!
  454. This method in combination with <code>array_two()</code> can be useful when passing the stored data into
  455. a legacy C API as an array. Suppose there is a <code>circular_buffer</code> of capacity 10, containing 7
  456. characters <code>'a', 'b', ..., 'g'</code> where <code>buff[0] == 'a'</code>, <code>buff[1] == 'b'</code>,
  457. ... and <code>buff[6] == 'g'</code>:<br><br>
  458. <code>circular_buffer<char> buff(10);</code><br><br>
  459. The internal representation is often not linear and the state of the internal buffer may look like this:<br>
  460. <br><code>
  461. |e|f|g| | | |a|b|c|d|<br>
  462. end ___^<br>
  463. begin _______^</code><br><br>
  464. where <code>|a|b|c|d|</code> represents the "array one", <code>|e|f|g|</code> represents the "array two" and
  465. <code>| | | |</code> is a free space.<br>
  466. Now consider a typical C style function for writing data into a file:<br><br>
  467. <code>int write(int file_desc, char* buff, int num_bytes);</code><br><br>
  468. There are two ways how to write the content of the <code>circular_buffer</code> into a file. Either relying
  469. on <code>array_one()</code> and <code>array_two()</code> methods and calling the write function twice:<br><br>
  470. <code>array_range ar = buff.array_one();<br>
  471. write(file_desc, ar.first, ar.second);<br>
  472. ar = buff.array_two();<br>
  473. write(file_desc, ar.first, ar.second);</code><br><br>
  474. Or relying on the <code>linearize()</code> method:<br><br><code>
  475. write(file_desc, buff.linearize(), buff.size());</code><br><br>
  476. Since the complexity of <code>array_one()</code> and <code>array_two()</code> methods is constant the first
  477. option is suitable when calling the write method is "cheap". On the other hand the second option is more
  478. suitable when calling the write method is more "expensive" than calling the <code>linearize()</code> method
  479. whose complexity is linear.
  480. \return The array range of the first continuous array of the internal buffer. In the case the
  481. <code>circular_buffer</code> is empty the size of the returned array is <code>0</code>.
  482. \throws Nothing.
  483. \par Exception Safety
  484. No-throw.
  485. \par Iterator Invalidation
  486. Does not invalidate any iterators.
  487. \par Complexity
  488. Constant (in the size of the <code>circular_buffer</code>).
  489. \warning In general invoking any method which modifies the internal state of the circular_buffer may
  490. delinearize the internal buffer and invalidate the array ranges returned by <code>array_one()</code>
  491. and <code>array_two()</code> (and their const versions).
  492. \note In the case the internal buffer is linear e.g. <code>|a|b|c|d|e|f|g| | | |</code> the "array one" is
  493. represented by <code>|a|b|c|d|e|f|g|</code> and the "array two" does not exist (the
  494. <code>array_two()</code> method returns an array with the size <code>0</code>).
  495. \sa <code>array_two()</code>, <code>linearize()</code>
  496. */
  497. array_range array_one() {
  498. return array_range(m_first, (m_last <= m_first && !empty() ? m_end : m_last) - m_first);
  499. }
  500. //! Get the second continuous array of the internal buffer.
  501. /*!
  502. This method in combination with <code>array_one()</code> can be useful when passing the stored data into
  503. a legacy C API as an array.
  504. \return The array range of the second continuous array of the internal buffer. In the case the internal buffer
  505. is linear or the <code>circular_buffer</code> is empty the size of the returned array is
  506. <code>0</code>.
  507. \throws Nothing.
  508. \par Exception Safety
  509. No-throw.
  510. \par Iterator Invalidation
  511. Does not invalidate any iterators.
  512. \par Complexity
  513. Constant (in the size of the <code>circular_buffer</code>).
  514. \sa <code>array_one()</code>
  515. */
  516. array_range array_two() {
  517. return array_range(m_buff, m_last <= m_first && !empty() ? m_last - m_buff : 0);
  518. }
  519. //! Get the first continuous array of the internal buffer.
  520. /*!
  521. This method in combination with <code>array_two() const</code> can be useful when passing the stored data into
  522. a legacy C API as an array.
  523. \return The array range of the first continuous array of the internal buffer. In the case the
  524. <code>circular_buffer</code> is empty the size of the returned array is <code>0</code>.
  525. \throws Nothing.
  526. \par Exception Safety
  527. No-throw.
  528. \par Iterator Invalidation
  529. Does not invalidate any iterators.
  530. \par Complexity
  531. Constant (in the size of the <code>circular_buffer</code>).
  532. \sa <code>array_two() const</code>; <code>array_one()</code> for more details how to pass data into a legacy C
  533. API.
  534. */
  535. const_array_range array_one() const {
  536. return const_array_range(m_first, (m_last <= m_first && !empty() ? m_end : m_last) - m_first);
  537. }
  538. //! Get the second continuous array of the internal buffer.
  539. /*!
  540. This method in combination with <code>array_one() const</code> can be useful when passing the stored data into
  541. a legacy C API as an array.
  542. \return The array range of the second continuous array of the internal buffer. In the case the internal buffer
  543. is linear or the <code>circular_buffer</code> is empty the size of the returned array is
  544. <code>0</code>.
  545. \throws Nothing.
  546. \par Exception Safety
  547. No-throw.
  548. \par Iterator Invalidation
  549. Does not invalidate any iterators.
  550. \par Complexity
  551. Constant (in the size of the <code>circular_buffer</code>).
  552. \sa <code>array_one() const</code>
  553. */
  554. const_array_range array_two() const {
  555. return const_array_range(m_buff, m_last <= m_first && !empty() ? m_last - m_buff : 0);
  556. }
  557. //! Linearize the internal buffer into a continuous array.
  558. /*!
  559. This method can be useful when passing the stored data into a legacy C API as an array.
  560. \post <code>\&(*this)[0] \< \&(*this)[1] \< ... \< \&(*this)[size() - 1]</code>
  561. \return A pointer to the beginning of the array or <code>0</code> if empty.
  562. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  563. \par Exception Safety
  564. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  565. \par Iterator Invalidation
  566. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  567. <code>end()</code>); does not invalidate any iterators if the postcondition (the <i>Effect</i>) is already
  568. met prior calling this method.
  569. \par Complexity
  570. Linear (in the size of the <code>circular_buffer</code>); constant if the postcondition (the
  571. <i>Effect</i>) is already met.
  572. \warning In general invoking any method which modifies the internal state of the <code>circular_buffer</code>
  573. may delinearize the internal buffer and invalidate the returned pointer.
  574. \sa <code>array_one()</code> and <code>array_two()</code> for the other option how to pass data into a legacy
  575. C API; <code>is_linearized()</code>, <code>rotate(const_iterator)</code>
  576. */
  577. pointer linearize() {
  578. if (empty())
  579. return 0;
  580. if (m_first < m_last || m_last == m_buff)
  581. return m_first;
  582. pointer src = m_first;
  583. pointer dest = m_buff;
  584. size_type moved = 0;
  585. size_type constructed = 0;
  586. BOOST_TRY {
  587. for (pointer first = m_first; dest < src; src = first) {
  588. for (size_type ii = 0; src < m_end; ++src, ++dest, ++moved, ++ii) {
  589. if (moved == size()) {
  590. first = dest;
  591. break;
  592. }
  593. if (dest == first) {
  594. first += ii;
  595. break;
  596. }
  597. if (is_uninitialized(dest)) {
  598. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(dest), boost::move_if_noexcept(*src));
  599. ++constructed;
  600. } else {
  601. value_type tmp = boost::move_if_noexcept(*src);
  602. replace(src, boost::move_if_noexcept(*dest));
  603. replace(dest, boost::move(tmp));
  604. }
  605. }
  606. }
  607. } BOOST_CATCH(...) {
  608. m_last += constructed;
  609. m_size += constructed;
  610. BOOST_RETHROW
  611. }
  612. BOOST_CATCH_END
  613. for (src = m_end - constructed; src < m_end; ++src)
  614. destroy_item(src);
  615. m_first = m_buff;
  616. m_last = add(m_buff, size());
  617. #if BOOST_CB_ENABLE_DEBUG
  618. invalidate_iterators_except(end());
  619. #endif
  620. return m_buff;
  621. }
  622. //! Is the <code>circular_buffer</code> linearized?
  623. /*!
  624. \return <code>true</code> if the internal buffer is linearized into a continuous array (i.e. the
  625. <code>circular_buffer</code> meets a condition
  626. <code>\&(*this)[0] \< \&(*this)[1] \< ... \< \&(*this)[size() - 1]</code>);
  627. <code>false</code> otherwise.
  628. \throws Nothing.
  629. \par Exception Safety
  630. No-throw.
  631. \par Iterator Invalidation
  632. Does not invalidate any iterators.
  633. \par Complexity
  634. Constant (in the size of the <code>circular_buffer</code>).
  635. \sa <code>linearize()</code>, <code>array_one()</code>, <code>array_two()</code>
  636. */
  637. bool is_linearized() const BOOST_NOEXCEPT { return m_first < m_last || m_last == m_buff; }
  638. //! Rotate elements in the <code>circular_buffer</code>.
  639. /*!
  640. A more effective implementation of
  641. <code><a href="http://www.sgi.com/tech/stl/rotate.html">std::rotate</a></code>.
  642. \pre <code>new_begin</code> is a valid iterator pointing to the <code>circular_buffer</code> <b>except</b> its
  643. end.
  644. \post Before calling the method suppose:<br><br>
  645. <code>m == std::distance(new_begin, end())</code><br><code>n == std::distance(begin(), new_begin)</code>
  646. <br><code>val_0 == *new_begin, val_1 == *(new_begin + 1), ... val_m == *(new_begin + m)</code><br>
  647. <code>val_r1 == *(new_begin - 1), val_r2 == *(new_begin - 2), ... val_rn == *(new_begin - n)</code><br>
  648. <br>then after call to the method:<br><br>
  649. <code>val_0 == (*this)[0] \&\& val_1 == (*this)[1] \&\& ... \&\& val_m == (*this)[m - 1] \&\& val_r1 ==
  650. (*this)[m + n - 1] \&\& val_r2 == (*this)[m + n - 2] \&\& ... \&\& val_rn == (*this)[m]</code>
  651. \param new_begin The new beginning.
  652. \throws See <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  653. \par Exception Safety
  654. Basic; no-throw if the <code>circular_buffer</code> is full or <code>new_begin</code> points to
  655. <code>begin()</code> or if the operations in the <i>Throws</i> section do not throw anything.
  656. \par Iterator Invalidation
  657. If <code>m \< n</code> invalidates iterators pointing to the last <code>m</code> elements
  658. (<b>including</b> <code>new_begin</code>, but not iterators equal to <code>end()</code>) else invalidates
  659. iterators pointing to the first <code>n</code> elements; does not invalidate any iterators if the
  660. <code>circular_buffer</code> is full.
  661. \par Complexity
  662. Linear (in <code>(std::min)(m, n)</code>); constant if the <code>circular_buffer</code> is full.
  663. \sa <code><a href="http://www.sgi.com/tech/stl/rotate.html">std::rotate</a></code>
  664. */
  665. void rotate(const_iterator new_begin) {
  666. BOOST_CB_ASSERT(new_begin.is_valid(this)); // check for uninitialized or invalidated iterator
  667. BOOST_CB_ASSERT(new_begin.m_it != 0); // check for iterator pointing to end()
  668. if (full()) {
  669. m_first = m_last = const_cast<pointer>(new_begin.m_it);
  670. } else {
  671. difference_type m = end() - new_begin;
  672. difference_type n = new_begin - begin();
  673. if (m < n) {
  674. for (; m > 0; --m) {
  675. push_front(boost::move_if_noexcept(back()));
  676. pop_back();
  677. }
  678. } else {
  679. for (; n > 0; --n) {
  680. push_back(boost::move_if_noexcept(front()));
  681. pop_front();
  682. }
  683. }
  684. }
  685. }
  686. // Size and capacity
  687. //! Get the number of elements currently stored in the <code>circular_buffer</code>.
  688. /*!
  689. \return The number of elements stored in the <code>circular_buffer</code>.
  690. \throws Nothing.
  691. \par Exception Safety
  692. No-throw.
  693. \par Iterator Invalidation
  694. Does not invalidate any iterators.
  695. \par Complexity
  696. Constant (in the size of the <code>circular_buffer</code>).
  697. \sa <code>capacity()</code>, <code>max_size()</code>, <code>reserve()</code>,
  698. <code>\link resize() resize(size_type, const_reference)\endlink</code>
  699. */
  700. size_type size() const BOOST_NOEXCEPT { return m_size; }
  701. /*! \brief Get the largest possible size or capacity of the <code>circular_buffer</code>. (It depends on
  702. allocator's %max_size()).
  703. \return The maximum size/capacity the <code>circular_buffer</code> can be set to.
  704. \throws Nothing.
  705. \par Exception Safety
  706. No-throw.
  707. \par Iterator Invalidation
  708. Does not invalidate any iterators.
  709. \par Complexity
  710. Constant (in the size of the <code>circular_buffer</code>).
  711. \sa <code>size()</code>, <code>capacity()</code>, <code>reserve()</code>
  712. */
  713. size_type max_size() const BOOST_NOEXCEPT {
  714. return (std::min<size_type>)(boost::container::allocator_traits<Alloc>::max_size(m_alloc), (std::numeric_limits<difference_type>::max)());
  715. }
  716. //! Is the <code>circular_buffer</code> empty?
  717. /*!
  718. \return <code>true</code> if there are no elements stored in the <code>circular_buffer</code>;
  719. <code>false</code> otherwise.
  720. \throws Nothing.
  721. \par Exception Safety
  722. No-throw.
  723. \par Iterator Invalidation
  724. Does not invalidate any iterators.
  725. \par Complexity
  726. Constant (in the size of the <code>circular_buffer</code>).
  727. \sa <code>full()</code>
  728. */
  729. bool empty() const BOOST_NOEXCEPT { return size() == 0; }
  730. //! Is the <code>circular_buffer</code> full?
  731. /*!
  732. \return <code>true</code> if the number of elements stored in the <code>circular_buffer</code>
  733. equals the capacity of the <code>circular_buffer</code>; <code>false</code> otherwise.
  734. \throws Nothing.
  735. \par Exception Safety
  736. No-throw.
  737. \par Iterator Invalidation
  738. Does not invalidate any iterators.
  739. \par Complexity
  740. Constant (in the size of the <code>circular_buffer</code>).
  741. \sa <code>empty()</code>
  742. */
  743. bool full() const BOOST_NOEXCEPT { return capacity() == size(); }
  744. /*! \brief Get the maximum number of elements which can be inserted into the <code>circular_buffer</code> without
  745. overwriting any of already stored elements.
  746. \return <code>capacity() - size()</code>
  747. \throws Nothing.
  748. \par Exception Safety
  749. No-throw.
  750. \par Iterator Invalidation
  751. Does not invalidate any iterators.
  752. \par Complexity
  753. Constant (in the size of the <code>circular_buffer</code>).
  754. \sa <code>capacity()</code>, <code>size()</code>, <code>max_size()</code>
  755. */
  756. size_type reserve() const BOOST_NOEXCEPT { return capacity() - size(); }
  757. //! Get the capacity of the <code>circular_buffer</code>.
  758. /*!
  759. \return The maximum number of elements which can be stored in the <code>circular_buffer</code>.
  760. \throws Nothing.
  761. \par Exception Safety
  762. No-throw.
  763. \par Iterator Invalidation
  764. Does not invalidate any iterators.
  765. \par Complexity
  766. Constant (in the size of the <code>circular_buffer</code>).
  767. \sa <code>reserve()</code>, <code>size()</code>, <code>max_size()</code>,
  768. <code>set_capacity(capacity_type)</code>
  769. */
  770. capacity_type capacity() const BOOST_NOEXCEPT { return m_end - m_buff; }
  771. //! Change the capacity of the <code>circular_buffer</code>.
  772. /*!
  773. \pre If <code>T</code> is a move only type, then compiler shall support <code>noexcept</code> modifiers
  774. and move constructor of <code>T</code> must be marked with it (must not throw exceptions).
  775. \post <code>capacity() == new_capacity \&\& size() \<= new_capacity</code><br><br>
  776. If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
  777. new capacity then number of <code>[size() - new_capacity]</code> <b>last</b> elements will be removed and
  778. the new size will be equal to <code>new_capacity</code>.
  779. \param new_capacity The new capacity.
  780. \throws "An allocation error" if memory is exhausted, (<code>std::bad_alloc</code> if the standard allocator is
  781. used).
  782. Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
  783. \par Exception Safety
  784. Strong.
  785. \par Iterator Invalidation
  786. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  787. <code>end()</code>) if the new capacity is different from the original.
  788. \par Complexity
  789. Linear (in <code>min[size(), new_capacity]</code>).
  790. \sa <code>rset_capacity(capacity_type)</code>,
  791. <code>\link resize() resize(size_type, const_reference)\endlink</code>
  792. */
  793. void set_capacity(capacity_type new_capacity) {
  794. if (new_capacity == capacity())
  795. return;
  796. pointer buff = allocate(new_capacity);
  797. iterator b = begin();
  798. BOOST_TRY {
  799. reset(buff,
  800. cb_details::uninitialized_move_if_noexcept(b, b + (std::min)(new_capacity, size()), buff, m_alloc),
  801. new_capacity);
  802. } BOOST_CATCH(...) {
  803. deallocate(buff, new_capacity);
  804. BOOST_RETHROW
  805. }
  806. BOOST_CATCH_END
  807. }
  808. //! Change the size of the <code>circular_buffer</code>.
  809. /*!
  810. \post <code>size() == new_size \&\& capacity() >= new_size</code><br><br>
  811. If the new size is greater than the current size, copies of <code>item</code> will be inserted at the
  812. <b>back</b> of the of the <code>circular_buffer</code> in order to achieve the desired size. In the case
  813. the resulting size exceeds the current capacity the capacity will be set to <code>new_size</code>.<br>
  814. If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
  815. new size then number of <code>[size() - new_size]</code> <b>last</b> elements will be removed. (The
  816. capacity will remain unchanged.)
  817. \param new_size The new size.
  818. \param item The element the <code>circular_buffer</code> will be filled with in order to gain the requested
  819. size. (See the <i>Effect</i>.)
  820. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  821. used).
  822. Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
  823. \par Exception Safety
  824. Basic.
  825. \par Iterator Invalidation
  826. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  827. <code>end()</code>) if the new size is greater than the current capacity. Invalidates iterators pointing
  828. to the removed elements if the new size is lower that the original size. Otherwise it does not invalidate
  829. any iterator.
  830. \par Complexity
  831. Linear (in the new size of the <code>circular_buffer</code>).
  832. \sa <code>\link rresize() rresize(size_type, const_reference)\endlink</code>,
  833. <code>set_capacity(capacity_type)</code>
  834. */
  835. void resize(size_type new_size, param_value_type item = value_type()) {
  836. if (new_size > size()) {
  837. if (new_size > capacity())
  838. set_capacity(new_size);
  839. insert(end(), new_size - size(), item);
  840. } else {
  841. iterator e = end();
  842. erase(e - (size() - new_size), e);
  843. }
  844. }
  845. //! Change the capacity of the <code>circular_buffer</code>.
  846. /*!
  847. \pre If <code>T</code> is a move only type, then compiler shall support <code>noexcept</code> modifiers
  848. and move constructor of <code>T</code> must be marked with it (must not throw exceptions).
  849. \post <code>capacity() == new_capacity \&\& size() \<= new_capacity</code><br><br>
  850. If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
  851. new capacity then number of <code>[size() - new_capacity]</code> <b>first</b> elements will be removed
  852. and the new size will be equal to <code>new_capacity</code>.
  853. \param new_capacity The new capacity.
  854. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  855. used).
  856. Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
  857. \par Exception Safety
  858. Strong.
  859. \par Iterator Invalidation
  860. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  861. <code>end()</code>) if the new capacity is different from the original.
  862. \par Complexity
  863. Linear (in <code>min[size(), new_capacity]</code>).
  864. \sa <code>set_capacity(capacity_type)</code>,
  865. <code>\link rresize() rresize(size_type, const_reference)\endlink</code>
  866. */
  867. void rset_capacity(capacity_type new_capacity) {
  868. if (new_capacity == capacity())
  869. return;
  870. pointer buff = allocate(new_capacity);
  871. iterator e = end();
  872. BOOST_TRY {
  873. reset(buff, cb_details::uninitialized_move_if_noexcept(e - (std::min)(new_capacity, size()),
  874. e, buff, m_alloc), new_capacity);
  875. } BOOST_CATCH(...) {
  876. deallocate(buff, new_capacity);
  877. BOOST_RETHROW
  878. }
  879. BOOST_CATCH_END
  880. }
  881. //! Change the size of the <code>circular_buffer</code>.
  882. /*!
  883. \post <code>size() == new_size \&\& capacity() >= new_size</code><br><br>
  884. If the new size is greater than the current size, copies of <code>item</code> will be inserted at the
  885. <b>front</b> of the of the <code>circular_buffer</code> in order to achieve the desired size. In the case
  886. the resulting size exceeds the current capacity the capacity will be set to <code>new_size</code>.<br>
  887. If the current number of elements stored in the <code>circular_buffer</code> is greater than the desired
  888. new size then number of <code>[size() - new_size]</code> <b>first</b> elements will be removed. (The
  889. capacity will remain unchanged.)
  890. \param new_size The new size.
  891. \param item The element the <code>circular_buffer</code> will be filled with in order to gain the requested
  892. size. (See the <i>Effect</i>.)
  893. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  894. used).
  895. Whatever <code>T::T(const T&)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
  896. \par Exception Safety
  897. Basic.
  898. \par Iterator Invalidation
  899. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  900. <code>end()</code>) if the new size is greater than the current capacity. Invalidates iterators pointing
  901. to the removed elements if the new size is lower that the original size. Otherwise it does not invalidate
  902. any iterator.
  903. \par Complexity
  904. Linear (in the new size of the <code>circular_buffer</code>).
  905. \sa <code>\link resize() resize(size_type, const_reference)\endlink</code>,
  906. <code>rset_capacity(capacity_type)</code>
  907. */
  908. void rresize(size_type new_size, param_value_type item = value_type()) {
  909. if (new_size > size()) {
  910. if (new_size > capacity())
  911. set_capacity(new_size);
  912. rinsert(begin(), new_size - size(), item);
  913. } else {
  914. rerase(begin(), end() - new_size);
  915. }
  916. }
  917. // Construction/Destruction
  918. //! Create an empty <code>circular_buffer</code> with zero capacity.
  919. /*!
  920. \post <code>capacity() == 0 \&\& size() == 0</code>
  921. \param alloc The allocator.
  922. \throws Nothing.
  923. \par Complexity
  924. Constant.
  925. \warning Since Boost version 1.36 the behaviour of this constructor has changed. Now the constructor does not
  926. allocate any memory and both capacity and size are set to zero. Also note when inserting an element
  927. into a <code>circular_buffer</code> with zero capacity (e.g. by
  928. <code>\link push_back() push_back(const_reference)\endlink</code> or
  929. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>) nothing
  930. will be inserted and the size (as well as capacity) remains zero.
  931. \note You can explicitly set the capacity by calling the <code>set_capacity(capacity_type)</code> method or you
  932. can use the other constructor with the capacity specified.
  933. \sa <code>circular_buffer(capacity_type, const allocator_type& alloc)</code>,
  934. <code>set_capacity(capacity_type)</code>
  935. */
  936. explicit circular_buffer(const allocator_type& alloc = allocator_type()) BOOST_NOEXCEPT
  937. : m_buff(0), m_end(0), m_first(0), m_last(0), m_size(0), m_alloc(alloc) {}
  938. //! Create an empty <code>circular_buffer</code> with the specified capacity.
  939. /*!
  940. \post <code>capacity() == buffer_capacity \&\& size() == 0</code>
  941. \param buffer_capacity The maximum number of elements which can be stored in the <code>circular_buffer</code>.
  942. \param alloc The allocator.
  943. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  944. used).
  945. \par Complexity
  946. Constant.
  947. */
  948. explicit circular_buffer(capacity_type buffer_capacity, const allocator_type& alloc = allocator_type())
  949. : m_size(0), m_alloc(alloc) {
  950. initialize_buffer(buffer_capacity);
  951. m_first = m_last = m_buff;
  952. }
  953. /*! \brief Create a full <code>circular_buffer</code> with the specified capacity and filled with <code>n</code>
  954. copies of <code>item</code>.
  955. \post <code>capacity() == n \&\& full() \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... \&\&
  956. (*this)[n - 1] == item </code>
  957. \param n The number of elements the created <code>circular_buffer</code> will be filled with.
  958. \param item The element the created <code>circular_buffer</code> will be filled with.
  959. \param alloc The allocator.
  960. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  961. used).
  962. Whatever <code>T::T(const T&)</code> throws.
  963. \par Complexity
  964. Linear (in the <code>n</code>).
  965. */
  966. circular_buffer(size_type n, param_value_type item, const allocator_type& alloc = allocator_type())
  967. : m_size(n), m_alloc(alloc) {
  968. initialize_buffer(n, item);
  969. m_first = m_last = m_buff;
  970. }
  971. /*! \brief Create a <code>circular_buffer</code> with the specified capacity and filled with <code>n</code>
  972. copies of <code>item</code>.
  973. \pre <code>buffer_capacity >= n</code>
  974. \post <code>capacity() == buffer_capacity \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item
  975. \&\& ... \&\& (*this)[n - 1] == item</code>
  976. \param buffer_capacity The capacity of the created <code>circular_buffer</code>.
  977. \param n The number of elements the created <code>circular_buffer</code> will be filled with.
  978. \param item The element the created <code>circular_buffer</code> will be filled with.
  979. \param alloc The allocator.
  980. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  981. used).
  982. Whatever <code>T::T(const T&)</code> throws.
  983. \par Complexity
  984. Linear (in the <code>n</code>).
  985. */
  986. circular_buffer(capacity_type buffer_capacity, size_type n, param_value_type item,
  987. const allocator_type& alloc = allocator_type())
  988. : m_size(n), m_alloc(alloc) {
  989. BOOST_CB_ASSERT(buffer_capacity >= size()); // check for capacity lower than size
  990. initialize_buffer(buffer_capacity, item);
  991. m_first = m_buff;
  992. m_last = buffer_capacity == n ? m_buff : m_buff + n;
  993. }
  994. //! The copy constructor.
  995. /*!
  996. Creates a copy of the specified <code>circular_buffer</code>.
  997. \post <code>*this == cb</code>
  998. \param cb The <code>circular_buffer</code> to be copied.
  999. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1000. used).
  1001. Whatever <code>T::T(const T&)</code> throws.
  1002. \par Complexity
  1003. Linear (in the size of <code>cb</code>).
  1004. */
  1005. circular_buffer(const circular_buffer<T, Alloc>& cb)
  1006. :
  1007. #if BOOST_CB_ENABLE_DEBUG
  1008. debug_iterator_registry(),
  1009. #endif
  1010. m_size(cb.size()), m_alloc(cb.get_allocator()) {
  1011. initialize_buffer(cb.capacity());
  1012. m_first = m_buff;
  1013. BOOST_TRY {
  1014. m_last = cb_details::uninitialized_copy(cb.begin(), cb.end(), m_buff, m_alloc);
  1015. } BOOST_CATCH(...) {
  1016. deallocate(m_buff, cb.capacity());
  1017. BOOST_RETHROW
  1018. }
  1019. BOOST_CATCH_END
  1020. if (m_last == m_end)
  1021. m_last = m_buff;
  1022. }
  1023. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1024. //! The move constructor.
  1025. /*! \brief Move constructs a <code>circular_buffer</code> from <code>cb</code>, leaving <code>cb</code> empty.
  1026. \pre C++ compiler with rvalue references support.
  1027. \post <code>cb.empty()</code>
  1028. \param cb <code>circular_buffer</code> to 'steal' value from.
  1029. \throws Nothing.
  1030. \par Constant.
  1031. */
  1032. circular_buffer(circular_buffer<T, Alloc>&& cb) BOOST_NOEXCEPT
  1033. : m_buff(0), m_end(0), m_first(0), m_last(0), m_size(0), m_alloc(cb.get_allocator()) {
  1034. cb.swap(*this);
  1035. }
  1036. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1037. //! Create a full <code>circular_buffer</code> filled with a copy of the range.
  1038. /*!
  1039. \pre Valid range <code>[first, last)</code>.<br>
  1040. <code>first</code> and <code>last</code> have to meet the requirements of
  1041. <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1042. \post <code>capacity() == std::distance(first, last) \&\& full() \&\& (*this)[0]== *first \&\&
  1043. (*this)[1] == *(first + 1) \&\& ... \&\& (*this)[std::distance(first, last) - 1] == *(last - 1)</code>
  1044. \param first The beginning of the range to be copied.
  1045. \param last The end of the range to be copied.
  1046. \param alloc The allocator.
  1047. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1048. used).
  1049. Whatever <code>T::T(const T&)</code> throws.
  1050. \par Complexity
  1051. Linear (in the <code>std::distance(first, last)</code>).
  1052. */
  1053. template <class InputIterator>
  1054. circular_buffer(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type())
  1055. : m_alloc(alloc) {
  1056. initialize(first, last, is_integral<InputIterator>());
  1057. }
  1058. //! Create a <code>circular_buffer</code> with the specified capacity and filled with a copy of the range.
  1059. /*!
  1060. \pre Valid range <code>[first, last)</code>.<br>
  1061. <code>first</code> and <code>last</code> have to meet the requirements of
  1062. <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1063. \post <code>capacity() == buffer_capacity \&\& size() \<= std::distance(first, last) \&\&
  1064. (*this)[0]== *(last - buffer_capacity) \&\& (*this)[1] == *(last - buffer_capacity + 1) \&\& ... \&\&
  1065. (*this)[buffer_capacity - 1] == *(last - 1)</code><br><br>
  1066. If the number of items to be copied from the range <code>[first, last)</code> is greater than the
  1067. specified <code>buffer_capacity</code> then only elements from the range
  1068. <code>[last - buffer_capacity, last)</code> will be copied.
  1069. \param buffer_capacity The capacity of the created <code>circular_buffer</code>.
  1070. \param first The beginning of the range to be copied.
  1071. \param last The end of the range to be copied.
  1072. \param alloc The allocator.
  1073. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1074. used).
  1075. Whatever <code>T::T(const T&)</code> throws.
  1076. \par Complexity
  1077. Linear (in <code>std::distance(first, last)</code>; in
  1078. <code>min[capacity, std::distance(first, last)]</code> if the <code>InputIterator</code> is a
  1079. <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1080. */
  1081. template <class InputIterator>
  1082. circular_buffer(capacity_type buffer_capacity, InputIterator first, InputIterator last,
  1083. const allocator_type& alloc = allocator_type())
  1084. : m_alloc(alloc) {
  1085. initialize(buffer_capacity, first, last, is_integral<InputIterator>());
  1086. }
  1087. //! The destructor.
  1088. /*!
  1089. Destroys the <code>circular_buffer</code>.
  1090. \throws Nothing.
  1091. \par Iterator Invalidation
  1092. Invalidates all iterators pointing to the <code>circular_buffer</code> (including iterators equal to
  1093. <code>end()</code>).
  1094. \par Complexity
  1095. Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
  1096. \sa <code>clear()</code>
  1097. */
  1098. ~circular_buffer() BOOST_NOEXCEPT {
  1099. destroy();
  1100. #if BOOST_CB_ENABLE_DEBUG
  1101. invalidate_all_iterators();
  1102. #endif
  1103. }
  1104. public:
  1105. // Assign methods
  1106. //! The assign operator.
  1107. /*!
  1108. Makes this <code>circular_buffer</code> to become a copy of the specified <code>circular_buffer</code>.
  1109. \post <code>*this == cb</code>
  1110. \param cb The <code>circular_buffer</code> to be copied.
  1111. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1112. used).
  1113. Whatever <code>T::T(const T&)</code> throws.
  1114. \par Exception Safety
  1115. Strong.
  1116. \par Iterator Invalidation
  1117. Invalidates all iterators pointing to this <code>circular_buffer</code> (except iterators equal to
  1118. <code>end()</code>).
  1119. \par Complexity
  1120. Linear (in the size of <code>cb</code>).
  1121. \sa <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1122. <code>\link assign(capacity_type, size_type, param_value_type)
  1123. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1124. <code>assign(InputIterator, InputIterator)</code>,
  1125. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1126. */
  1127. circular_buffer<T, Alloc>& operator = (const circular_buffer<T, Alloc>& cb) {
  1128. if (this == &cb)
  1129. return *this;
  1130. pointer buff = allocate(cb.capacity());
  1131. BOOST_TRY {
  1132. reset(buff, cb_details::uninitialized_copy(cb.begin(), cb.end(), buff, m_alloc), cb.capacity());
  1133. } BOOST_CATCH(...) {
  1134. deallocate(buff, cb.capacity());
  1135. BOOST_RETHROW
  1136. }
  1137. BOOST_CATCH_END
  1138. return *this;
  1139. }
  1140. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1141. /*! \brief Move assigns content of <code>cb</code> to <code>*this</code>, leaving <code>cb</code> empty.
  1142. \pre C++ compiler with rvalue references support.
  1143. \post <code>cb.empty()</code>
  1144. \param cb <code>circular_buffer</code> to 'steal' value from.
  1145. \throws Nothing.
  1146. \par Complexity
  1147. Constant.
  1148. */
  1149. circular_buffer<T, Alloc>& operator = (circular_buffer<T, Alloc>&& cb) BOOST_NOEXCEPT {
  1150. cb.swap(*this); // now `this` holds `cb`
  1151. circular_buffer<T, Alloc>(get_allocator()) // temprary that holds initial `cb` allocator
  1152. .swap(cb); // makes `cb` empty
  1153. return *this;
  1154. }
  1155. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1156. //! Assign <code>n</code> items into the <code>circular_buffer</code>.
  1157. /*!
  1158. The content of the <code>circular_buffer</code> will be removed and replaced with <code>n</code> copies of the
  1159. <code>item</code>.
  1160. \post <code>capacity() == n \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... \&\&
  1161. (*this) [n - 1] == item</code>
  1162. \param n The number of elements the <code>circular_buffer</code> will be filled with.
  1163. \param item The element the <code>circular_buffer</code> will be filled with.
  1164. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1165. used).
  1166. Whatever <code>T::T(const T&)</code> throws.
  1167. \par Exception Safety
  1168. Basic.
  1169. \par Iterator Invalidation
  1170. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1171. <code>end()</code>).
  1172. \par Complexity
  1173. Linear (in the <code>n</code>).
  1174. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1175. <code>\link assign(capacity_type, size_type, param_value_type)
  1176. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1177. <code>assign(InputIterator, InputIterator)</code>,
  1178. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1179. */
  1180. void assign(size_type n, param_value_type item) {
  1181. assign_n(n, n, cb_details::assign_n<param_value_type, allocator_type>(n, item, m_alloc));
  1182. }
  1183. //! Assign <code>n</code> items into the <code>circular_buffer</code> specifying the capacity.
  1184. /*!
  1185. The capacity of the <code>circular_buffer</code> will be set to the specified value and the content of the
  1186. <code>circular_buffer</code> will be removed and replaced with <code>n</code> copies of the <code>item</code>.
  1187. \pre <code>capacity >= n</code>
  1188. \post <code>capacity() == buffer_capacity \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item
  1189. \&\& ... \&\& (*this) [n - 1] == item </code>
  1190. \param buffer_capacity The new capacity.
  1191. \param n The number of elements the <code>circular_buffer</code> will be filled with.
  1192. \param item The element the <code>circular_buffer</code> will be filled with.
  1193. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1194. used).
  1195. Whatever <code>T::T(const T&)</code> throws.
  1196. \par Exception Safety
  1197. Basic.
  1198. \par Iterator Invalidation
  1199. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1200. <code>end()</code>).
  1201. \par Complexity
  1202. Linear (in the <code>n</code>).
  1203. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1204. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1205. <code>assign(InputIterator, InputIterator)</code>,
  1206. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1207. */
  1208. void assign(capacity_type buffer_capacity, size_type n, param_value_type item) {
  1209. BOOST_CB_ASSERT(buffer_capacity >= n); // check for new capacity lower than n
  1210. assign_n(buffer_capacity, n, cb_details::assign_n<param_value_type, allocator_type>(n, item, m_alloc));
  1211. }
  1212. //! Assign a copy of the range into the <code>circular_buffer</code>.
  1213. /*!
  1214. The content of the <code>circular_buffer</code> will be removed and replaced with copies of elements from the
  1215. specified range.
  1216. \pre Valid range <code>[first, last)</code>.<br>
  1217. <code>first</code> and <code>last</code> have to meet the requirements of
  1218. <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1219. \post <code>capacity() == std::distance(first, last) \&\& size() == std::distance(first, last) \&\&
  1220. (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... \&\& (*this)[std::distance(first, last) - 1]
  1221. == *(last - 1)</code>
  1222. \param first The beginning of the range to be copied.
  1223. \param last The end of the range to be copied.
  1224. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1225. used).
  1226. Whatever <code>T::T(const T&)</code> throws.
  1227. \par Exception Safety
  1228. Basic.
  1229. \par Iterator Invalidation
  1230. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1231. <code>end()</code>).
  1232. \par Complexity
  1233. Linear (in the <code>std::distance(first, last)</code>).
  1234. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1235. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1236. <code>\link assign(capacity_type, size_type, param_value_type)
  1237. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1238. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1239. */
  1240. template <class InputIterator>
  1241. void assign(InputIterator first, InputIterator last) {
  1242. assign(first, last, is_integral<InputIterator>());
  1243. }
  1244. //! Assign a copy of the range into the <code>circular_buffer</code> specifying the capacity.
  1245. /*!
  1246. The capacity of the <code>circular_buffer</code> will be set to the specified value and the content of the
  1247. <code>circular_buffer</code> will be removed and replaced with copies of elements from the specified range.
  1248. \pre Valid range <code>[first, last)</code>.<br>
  1249. <code>first</code> and <code>last</code> have to meet the requirements of
  1250. <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1251. \post <code>capacity() == buffer_capacity \&\& size() \<= std::distance(first, last) \&\&
  1252. (*this)[0]== *(last - buffer_capacity) \&\& (*this)[1] == *(last - buffer_capacity + 1) \&\& ... \&\&
  1253. (*this)[buffer_capacity - 1] == *(last - 1)</code><br><br>
  1254. If the number of items to be copied from the range <code>[first, last)</code> is greater than the
  1255. specified <code>buffer_capacity</code> then only elements from the range
  1256. <code>[last - buffer_capacity, last)</code> will be copied.
  1257. \param buffer_capacity The new capacity.
  1258. \param first The beginning of the range to be copied.
  1259. \param last The end of the range to be copied.
  1260. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1261. used).
  1262. Whatever <code>T::T(const T&)</code> throws.
  1263. \par Exception Safety
  1264. Basic.
  1265. \par Iterator Invalidation
  1266. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1267. <code>end()</code>).
  1268. \par Complexity
  1269. Linear (in <code>std::distance(first, last)</code>; in
  1270. <code>min[capacity, std::distance(first, last)]</code> if the <code>InputIterator</code> is a
  1271. <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1272. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1273. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1274. <code>\link assign(capacity_type, size_type, param_value_type)
  1275. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1276. <code>assign(InputIterator, InputIterator)</code>
  1277. */
  1278. template <class InputIterator>
  1279. void assign(capacity_type buffer_capacity, InputIterator first, InputIterator last) {
  1280. assign(buffer_capacity, first, last, is_integral<InputIterator>());
  1281. }
  1282. //! Swap the contents of two <code>circular_buffer</code>s.
  1283. /*!
  1284. \post <code>this</code> contains elements of <code>cb</code> and vice versa; the capacity of <code>this</code>
  1285. equals to the capacity of <code>cb</code> and vice versa.
  1286. \param cb The <code>circular_buffer</code> whose content will be swapped.
  1287. \throws Nothing.
  1288. \par Exception Safety
  1289. No-throw.
  1290. \par Iterator Invalidation
  1291. Invalidates all iterators of both <code>circular_buffer</code>s. (On the other hand the iterators still
  1292. point to the same elements but within another container. If you want to rely on this feature you have to
  1293. turn the <a href="#debug">Debug Support</a> off otherwise an assertion will report an error if such
  1294. invalidated iterator is used.)
  1295. \par Complexity
  1296. Constant (in the size of the <code>circular_buffer</code>).
  1297. \sa <code>swap(circular_buffer<T, Alloc>&, circular_buffer<T, Alloc>&)</code>
  1298. */
  1299. void swap(circular_buffer<T, Alloc>& cb) BOOST_NOEXCEPT {
  1300. swap_allocator(cb, is_stateless<allocator_type>());
  1301. adl_move_swap(m_buff, cb.m_buff);
  1302. adl_move_swap(m_end, cb.m_end);
  1303. adl_move_swap(m_first, cb.m_first);
  1304. adl_move_swap(m_last, cb.m_last);
  1305. adl_move_swap(m_size, cb.m_size);
  1306. #if BOOST_CB_ENABLE_DEBUG
  1307. invalidate_all_iterators();
  1308. cb.invalidate_all_iterators();
  1309. #endif
  1310. }
  1311. // push and pop
  1312. private:
  1313. template <class ValT>
  1314. void push_back_impl(ValT item) {
  1315. if (full()) {
  1316. if (empty())
  1317. return;
  1318. replace(m_last, static_cast<ValT>(item));
  1319. increment(m_last);
  1320. m_first = m_last;
  1321. } else {
  1322. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(m_last), static_cast<ValT>(item));
  1323. increment(m_last);
  1324. ++m_size;
  1325. }
  1326. }
  1327. template <class ValT>
  1328. void push_front_impl(ValT item) {
  1329. BOOST_TRY {
  1330. if (full()) {
  1331. if (empty())
  1332. return;
  1333. decrement(m_first);
  1334. replace(m_first, static_cast<ValT>(item));
  1335. m_last = m_first;
  1336. } else {
  1337. decrement(m_first);
  1338. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(m_first), static_cast<ValT>(item));
  1339. ++m_size;
  1340. }
  1341. } BOOST_CATCH(...) {
  1342. increment(m_first);
  1343. BOOST_RETHROW
  1344. }
  1345. BOOST_CATCH_END
  1346. }
  1347. public:
  1348. //! Insert a new element at the end of the <code>circular_buffer</code>.
  1349. /*!
  1350. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1351. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1352. <code>0</code>, nothing will be inserted.
  1353. \param item The element to be inserted.
  1354. \throws Whatever <code>T::T(const T&)</code> throws.
  1355. Whatever <code>T::operator = (const T&)</code> throws.
  1356. \par Exception Safety
  1357. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1358. \par Iterator Invalidation
  1359. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1360. \par Complexity
  1361. Constant (in the size of the <code>circular_buffer</code>).
  1362. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1363. <code>pop_back()</code>, <code>pop_front()</code>
  1364. */
  1365. void push_back(param_value_type item) {
  1366. push_back_impl<param_value_type>(item);
  1367. }
  1368. //! Insert a new element at the end of the <code>circular_buffer</code> using rvalue references or rvalues references emulation.
  1369. /*!
  1370. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1371. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1372. <code>0</code>, nothing will be inserted.
  1373. \param item The element to be inserted.
  1374. \throws Whatever <code>T::T(T&&)</code> throws.
  1375. Whatever <code>T::operator = (T&&)</code> throws.
  1376. \par Exception Safety
  1377. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1378. \par Iterator Invalidation
  1379. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1380. \par Complexity
  1381. Constant (in the size of the <code>circular_buffer</code>).
  1382. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1383. <code>pop_back()</code>, <code>pop_front()</code>
  1384. */
  1385. void push_back(rvalue_type item) {
  1386. push_back_impl<rvalue_type>(boost::move(item));
  1387. }
  1388. //! Insert a new default-constructed element at the end of the <code>circular_buffer</code>.
  1389. /*!
  1390. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1391. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1392. <code>0</code>, nothing will be inserted.
  1393. \throws Whatever <code>T::T()</code> throws.
  1394. Whatever <code>T::T(T&&)</code> throws.
  1395. Whatever <code>T::operator = (T&&)</code> throws.
  1396. \par Exception Safety
  1397. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1398. \par Iterator Invalidation
  1399. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1400. \par Complexity
  1401. Constant (in the size of the <code>circular_buffer</code>).
  1402. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1403. <code>pop_back()</code>, <code>pop_front()</code>
  1404. */
  1405. void push_back() {
  1406. value_type temp;
  1407. push_back(boost::move(temp));
  1408. }
  1409. //! Insert a new element at the beginning of the <code>circular_buffer</code>.
  1410. /*!
  1411. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1412. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1413. <code>0</code>, nothing will be inserted.
  1414. \param item The element to be inserted.
  1415. \throws Whatever <code>T::T(const T&)</code> throws.
  1416. Whatever <code>T::operator = (const T&)</code> throws.
  1417. \par Exception Safety
  1418. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1419. \par Iterator Invalidation
  1420. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1421. \par Complexity
  1422. Constant (in the size of the <code>circular_buffer</code>).
  1423. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1424. <code>pop_back()</code>, <code>pop_front()</code>
  1425. */
  1426. void push_front(param_value_type item) {
  1427. push_front_impl<param_value_type>(item);
  1428. }
  1429. //! Insert a new element at the beginning of the <code>circular_buffer</code> using rvalue references or rvalues references emulation.
  1430. /*!
  1431. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1432. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1433. <code>0</code>, nothing will be inserted.
  1434. \param item The element to be inserted.
  1435. \throws Whatever <code>T::T(T&&)</code> throws.
  1436. Whatever <code>T::operator = (T&&)</code> throws.
  1437. \par Exception Safety
  1438. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1439. \par Iterator Invalidation
  1440. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1441. \par Complexity
  1442. Constant (in the size of the <code>circular_buffer</code>).
  1443. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1444. <code>pop_back()</code>, <code>pop_front()</code>
  1445. */
  1446. void push_front(rvalue_type item) {
  1447. push_front_impl<rvalue_type>(boost::move(item));
  1448. }
  1449. //! Insert a new default-constructed element at the beginning of the <code>circular_buffer</code>.
  1450. /*!
  1451. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1452. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1453. <code>0</code>, nothing will be inserted.
  1454. \throws Whatever <code>T::T()</code> throws.
  1455. Whatever <code>T::T(T&&)</code> throws.
  1456. Whatever <code>T::operator = (T&&)</code> throws.
  1457. \par Exception Safety
  1458. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1459. \par Iterator Invalidation
  1460. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1461. \par Complexity
  1462. Constant (in the size of the <code>circular_buffer</code>).
  1463. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1464. <code>pop_back()</code>, <code>pop_front()</code>
  1465. */
  1466. void push_front() {
  1467. value_type temp;
  1468. push_front(boost::move(temp));
  1469. }
  1470. //! Remove the last element from the <code>circular_buffer</code>.
  1471. /*!
  1472. \pre <code>!empty()</code>
  1473. \post The last element is removed from the <code>circular_buffer</code>.
  1474. \throws Nothing.
  1475. \par Exception Safety
  1476. No-throw.
  1477. \par Iterator Invalidation
  1478. Invalidates only iterators pointing to the removed element.
  1479. \par Complexity
  1480. Constant (in the size of the <code>circular_buffer</code>).
  1481. \sa <code>pop_front()</code>, <code>\link push_back() push_back(const_reference)\endlink</code>,
  1482. <code>\link push_front() push_front(const_reference)\endlink</code>
  1483. */
  1484. void pop_back() {
  1485. BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
  1486. decrement(m_last);
  1487. destroy_item(m_last);
  1488. --m_size;
  1489. }
  1490. //! Remove the first element from the <code>circular_buffer</code>.
  1491. /*!
  1492. \pre <code>!empty()</code>
  1493. \post The first element is removed from the <code>circular_buffer</code>.
  1494. \throws Nothing.
  1495. \par Exception Safety
  1496. No-throw.
  1497. \par Iterator Invalidation
  1498. Invalidates only iterators pointing to the removed element.
  1499. \par Complexity
  1500. Constant (in the size of the <code>circular_buffer</code>).
  1501. \sa <code>pop_back()</code>, <code>\link push_back() push_back(const_reference)\endlink</code>,
  1502. <code>\link push_front() push_front(const_reference)\endlink</code>
  1503. */
  1504. void pop_front() {
  1505. BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
  1506. destroy_item(m_first);
  1507. increment(m_first);
  1508. --m_size;
  1509. }
  1510. private:
  1511. template <class ValT>
  1512. iterator insert_impl(iterator pos, ValT item) {
  1513. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1514. iterator b = begin();
  1515. if (full() && pos == b)
  1516. return b;
  1517. return insert_item<ValT>(pos, static_cast<ValT>(item));
  1518. }
  1519. public:
  1520. // Insert
  1521. //! Insert an element at the specified position.
  1522. /*!
  1523. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1524. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1525. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1526. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1527. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1528. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1529. \param item The element to be inserted.
  1530. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1531. the <i>Effect</i>.)
  1532. \throws Whatever <code>T::T(const T&)</code> throws.
  1533. Whatever <code>T::operator = (const T&)</code> throws.
  1534. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1535. \par Exception Safety
  1536. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1537. \par Iterator Invalidation
  1538. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1539. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1540. also invalidates iterators pointing to the overwritten element.
  1541. \par Complexity
  1542. Linear (in <code>std::distance(pos, end())</code>).
  1543. \sa <code>\link insert(iterator, size_type, param_value_type)
  1544. insert(iterator, size_type, value_type)\endlink</code>,
  1545. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1546. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1547. <code>\link rinsert(iterator, size_type, param_value_type)
  1548. rinsert(iterator, size_type, value_type)\endlink</code>,
  1549. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1550. */
  1551. iterator insert(iterator pos, param_value_type item) {
  1552. return insert_impl<param_value_type>(pos, item);
  1553. }
  1554. //! Insert an element at the specified position.
  1555. /*!
  1556. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1557. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1558. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1559. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1560. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1561. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1562. \param item The element to be inserted.
  1563. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1564. the <i>Effect</i>.)
  1565. \throws Whatever <code>T::T(T&&)</code> throws.
  1566. Whatever <code>T::operator = (T&&)</code> throws.
  1567. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1568. \par Exception Safety
  1569. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1570. \par Iterator Invalidation
  1571. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1572. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1573. also invalidates iterators pointing to the overwritten element.
  1574. \par Complexity
  1575. Linear (in <code>std::distance(pos, end())</code>).
  1576. \sa <code>\link insert(iterator, size_type, param_value_type)
  1577. insert(iterator, size_type, value_type)\endlink</code>,
  1578. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1579. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1580. <code>\link rinsert(iterator, size_type, param_value_type)
  1581. rinsert(iterator, size_type, value_type)\endlink</code>,
  1582. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1583. */
  1584. iterator insert(iterator pos, rvalue_type item) {
  1585. return insert_impl<rvalue_type>(pos, boost::move(item));
  1586. }
  1587. //! Insert a default-constructed element at the specified position.
  1588. /*!
  1589. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1590. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1591. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1592. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1593. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1594. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1595. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1596. the <i>Effect</i>.)
  1597. \throws Whatever <code>T::T()</code> throws.
  1598. Whatever <code>T::T(T&&)</code> throws.
  1599. Whatever <code>T::operator = (T&&)</code> throws.
  1600. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1601. \par Exception Safety
  1602. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1603. \par Iterator Invalidation
  1604. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1605. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1606. also invalidates iterators pointing to the overwritten element.
  1607. \par Complexity
  1608. Linear (in <code>std::distance(pos, end())</code>).
  1609. \sa <code>\link insert(iterator, size_type, param_value_type)
  1610. insert(iterator, size_type, value_type)\endlink</code>,
  1611. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1612. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1613. <code>\link rinsert(iterator, size_type, param_value_type)
  1614. rinsert(iterator, size_type, value_type)\endlink</code>,
  1615. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1616. */
  1617. iterator insert(iterator pos) {
  1618. value_type temp;
  1619. return insert(pos, boost::move(temp));
  1620. }
  1621. //! Insert <code>n</code> copies of the <code>item</code> at the specified position.
  1622. /*!
  1623. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1624. \post The number of <code>min[n, (pos - begin()) + reserve()]</code> elements will be inserted at the position
  1625. <code>pos</code>.<br>The number of <code>min[pos - begin(), max[0, n - reserve()]]</code> elements will
  1626. be overwritten at the beginning of the <code>circular_buffer</code>.<br>(See <i>Example</i> for the
  1627. explanation.)
  1628. \param pos An iterator specifying the position where the <code>item</code>s will be inserted.
  1629. \param n The number of <code>item</code>s the to be inserted.
  1630. \param item The element whose copies will be inserted.
  1631. \throws Whatever <code>T::T(const T&)</code> throws.
  1632. Whatever <code>T::operator = (const T&)</code> throws.
  1633. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1634. \par Exception Safety
  1635. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1636. \par Iterator Invalidation
  1637. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1638. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1639. also invalidates iterators pointing to the overwritten elements.
  1640. \par Complexity
  1641. Linear (in <code>min[capacity(), std::distance(pos, end()) + n]</code>).
  1642. \par Example
  1643. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1644. look like the one below.<br><br>
  1645. <code>|1|2|3|4| | |</code><br>
  1646. <code>p ___^</code><br><br>After inserting 5 elements at the position <code>p</code>:<br><br>
  1647. <code>insert(p, (size_t)5, 0);</code><br><br>actually only 4 elements get inserted and elements
  1648. <code>1</code> and <code>2</code> are overwritten. This is due to the fact the insert operation preserves
  1649. the capacity. After insertion the internal buffer looks like this:<br><br><code>|0|0|0|0|3|4|</code><br>
  1650. <br>For comparison if the capacity would not be preserved the internal buffer would then result in
  1651. <code>|1|2|0|0|0|0|0|3|4|</code>.
  1652. \sa <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1653. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1654. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1655. <code>\link rinsert(iterator, size_type, param_value_type)
  1656. rinsert(iterator, size_type, value_type)\endlink</code>,
  1657. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1658. */
  1659. void insert(iterator pos, size_type n, param_value_type item) {
  1660. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1661. if (n == 0)
  1662. return;
  1663. size_type copy = capacity() - (end() - pos);
  1664. if (copy == 0)
  1665. return;
  1666. if (n > copy)
  1667. n = copy;
  1668. insert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
  1669. }
  1670. //! Insert the range <code>[first, last)</code> at the specified position.
  1671. /*!
  1672. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.<br>
  1673. Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
  1674. requirements of an <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1675. \post Elements from the range
  1676. <code>[first + max[0, distance(first, last) - (pos - begin()) - reserve()], last)</code> will be
  1677. inserted at the position <code>pos</code>.<br>The number of <code>min[pos - begin(), max[0,
  1678. distance(first, last) - reserve()]]</code> elements will be overwritten at the beginning of the
  1679. <code>circular_buffer</code>.<br>(See <i>Example</i> for the explanation.)
  1680. \param pos An iterator specifying the position where the range will be inserted.
  1681. \param first The beginning of the range to be inserted.
  1682. \param last The end of the range to be inserted.
  1683. \throws Whatever <code>T::T(const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1684. Whatever <code>T::operator = (const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1685. Whatever <code>T::T(T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1686. Whatever <code>T::operator = (T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1687. \par Exception Safety
  1688. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1689. \par Iterator Invalidation
  1690. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1691. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1692. also invalidates iterators pointing to the overwritten elements.
  1693. \par Complexity
  1694. Linear (in <code>[std::distance(pos, end()) + std::distance(first, last)]</code>; in
  1695. <code>min[capacity(), std::distance(pos, end()) + std::distance(first, last)]</code> if the
  1696. <code>InputIterator</code> is a
  1697. <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1698. \par Example
  1699. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1700. look like the one below.<br><br>
  1701. <code>|1|2|3|4| | |</code><br>
  1702. <code>p ___^</code><br><br>After inserting a range of elements at the position <code>p</code>:<br><br>
  1703. <code>int array[] = { 5, 6, 7, 8, 9 };</code><br><code>insert(p, array, array + 5);</code><br><br>
  1704. actually only elements <code>6</code>, <code>7</code>, <code>8</code> and <code>9</code> from the
  1705. specified range get inserted and elements <code>1</code> and <code>2</code> are overwritten. This is due
  1706. to the fact the insert operation preserves the capacity. After insertion the internal buffer looks like
  1707. this:<br><br><code>|6|7|8|9|3|4|</code><br><br>For comparison if the capacity would not be preserved the
  1708. internal buffer would then result in <code>|1|2|5|6|7|8|9|3|4|</code>.
  1709. \sa <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1710. <code>\link insert(iterator, size_type, param_value_type)
  1711. insert(iterator, size_type, value_type)\endlink</code>, <code>\link rinsert(iterator, param_value_type)
  1712. rinsert(iterator, value_type)\endlink</code>, <code>\link rinsert(iterator, size_type, param_value_type)
  1713. rinsert(iterator, size_type, value_type)\endlink</code>,
  1714. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1715. */
  1716. template <class InputIterator>
  1717. void insert(iterator pos, InputIterator first, InputIterator last) {
  1718. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1719. insert(pos, first, last, is_integral<InputIterator>());
  1720. }
  1721. private:
  1722. template <class ValT>
  1723. iterator rinsert_impl(iterator pos, ValT item) {
  1724. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1725. if (full() && pos.m_it == 0)
  1726. return end();
  1727. if (pos == begin()) {
  1728. BOOST_TRY {
  1729. decrement(m_first);
  1730. construct_or_replace(!full(), m_first, static_cast<ValT>(item));
  1731. } BOOST_CATCH(...) {
  1732. increment(m_first);
  1733. BOOST_RETHROW
  1734. }
  1735. BOOST_CATCH_END
  1736. pos.m_it = m_first;
  1737. } else {
  1738. pointer src = m_first;
  1739. pointer dest = m_first;
  1740. decrement(dest);
  1741. pos.m_it = map_pointer(pos.m_it);
  1742. bool construct = !full();
  1743. BOOST_TRY {
  1744. while (src != pos.m_it) {
  1745. construct_or_replace(construct, dest, boost::move_if_noexcept(*src));
  1746. increment(src);
  1747. increment(dest);
  1748. construct = false;
  1749. }
  1750. decrement(pos.m_it);
  1751. replace(pos.m_it, static_cast<ValT>(item));
  1752. } BOOST_CATCH(...) {
  1753. if (!construct && !full()) {
  1754. decrement(m_first);
  1755. ++m_size;
  1756. }
  1757. BOOST_RETHROW
  1758. }
  1759. BOOST_CATCH_END
  1760. decrement(m_first);
  1761. }
  1762. if (full())
  1763. m_last = m_first;
  1764. else
  1765. ++m_size;
  1766. return iterator(this, pos.m_it);
  1767. }
  1768. public:
  1769. //! Insert an element before the specified position.
  1770. /*!
  1771. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1772. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1773. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1774. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1775. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1776. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1777. \param item The element to be inserted.
  1778. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1779. the <i>Effect</i>.)
  1780. \throws Whatever <code>T::T(const T&)</code> throws.
  1781. Whatever <code>T::operator = (const T&)</code> throws.
  1782. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1783. \par Exception Safety
  1784. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1785. \par Iterator Invalidation
  1786. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1787. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1788. \par Complexity
  1789. Linear (in <code>std::distance(begin(), pos)</code>).
  1790. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1791. rinsert(iterator, size_type, value_type)\endlink</code>,
  1792. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1793. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1794. <code>\link insert(iterator, size_type, param_value_type)
  1795. insert(iterator, size_type, value_type)\endlink</code>,
  1796. <code>insert(iterator, InputIterator, InputIterator)</code>
  1797. */
  1798. iterator rinsert(iterator pos, param_value_type item) {
  1799. return rinsert_impl<param_value_type>(pos, item);
  1800. }
  1801. //! Insert an element before the specified position.
  1802. /*!
  1803. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1804. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1805. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1806. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1807. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1808. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1809. \param item The element to be inserted.
  1810. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1811. the <i>Effect</i>.)
  1812. \throws Whatever <code>T::T(T&&)</code> throws.
  1813. Whatever <code>T::operator = (T&&)</code> throws.
  1814. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1815. \par Exception Safety
  1816. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1817. \par Iterator Invalidation
  1818. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1819. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1820. \par Complexity
  1821. Linear (in <code>std::distance(begin(), pos)</code>).
  1822. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1823. rinsert(iterator, size_type, value_type)\endlink</code>,
  1824. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1825. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1826. <code>\link insert(iterator, size_type, param_value_type)
  1827. insert(iterator, size_type, value_type)\endlink</code>,
  1828. <code>insert(iterator, InputIterator, InputIterator)</code>
  1829. */
  1830. iterator rinsert(iterator pos, rvalue_type item) {
  1831. return rinsert_impl<rvalue_type>(pos, boost::move(item));
  1832. }
  1833. //! Insert an element before the specified position.
  1834. /*!
  1835. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1836. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1837. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1838. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1839. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1840. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1841. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1842. the <i>Effect</i>.)
  1843. \throws Whatever <code>T::T()</code> throws.
  1844. Whatever <code>T::T(T&&)</code> throws.
  1845. Whatever <code>T::operator = (T&&)</code> throws.
  1846. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1847. \par Exception Safety
  1848. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1849. \par Iterator Invalidation
  1850. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1851. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1852. \par Complexity
  1853. Linear (in <code>std::distance(begin(), pos)</code>).
  1854. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1855. rinsert(iterator, size_type, value_type)\endlink</code>,
  1856. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1857. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1858. <code>\link insert(iterator, size_type, param_value_type)
  1859. insert(iterator, size_type, value_type)\endlink</code>,
  1860. <code>insert(iterator, InputIterator, InputIterator)</code>
  1861. */
  1862. iterator rinsert(iterator pos) {
  1863. value_type temp;
  1864. return rinsert(pos, boost::move(temp));
  1865. }
  1866. //! Insert <code>n</code> copies of the <code>item</code> before the specified position.
  1867. /*!
  1868. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1869. \post The number of <code>min[n, (end() - pos) + reserve()]</code> elements will be inserted before the
  1870. position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0, n - reserve()]]</code> elements
  1871. will be overwritten at the end of the <code>circular_buffer</code>.<br>(See <i>Example</i> for the
  1872. explanation.)
  1873. \param pos An iterator specifying the position where the <code>item</code>s will be inserted.
  1874. \param n The number of <code>item</code>s the to be inserted.
  1875. \param item The element whose copies will be inserted.
  1876. \throws Whatever <code>T::T(const T&)</code> throws.
  1877. Whatever <code>T::operator = (const T&)</code> throws.
  1878. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1879. \par Exception Safety
  1880. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1881. \par Iterator Invalidation
  1882. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1883. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten elements.
  1884. \par Complexity
  1885. Linear (in <code>min[capacity(), std::distance(begin(), pos) + n]</code>).
  1886. \par Example
  1887. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1888. look like the one below.<br><br>
  1889. <code>|1|2|3|4| | |</code><br>
  1890. <code>p ___^</code><br><br>After inserting 5 elements before the position <code>p</code>:<br><br>
  1891. <code>rinsert(p, (size_t)5, 0);</code><br><br>actually only 4 elements get inserted and elements
  1892. <code>3</code> and <code>4</code> are overwritten. This is due to the fact the rinsert operation preserves
  1893. the capacity. After insertion the internal buffer looks like this:<br><br><code>|1|2|0|0|0|0|</code><br>
  1894. <br>For comparison if the capacity would not be preserved the internal buffer would then result in
  1895. <code>|1|2|0|0|0|0|0|3|4|</code>.
  1896. \sa <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1897. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1898. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1899. <code>\link insert(iterator, size_type, param_value_type)
  1900. insert(iterator, size_type, value_type)\endlink</code>,
  1901. <code>insert(iterator, InputIterator, InputIterator)</code>
  1902. */
  1903. void rinsert(iterator pos, size_type n, param_value_type item) {
  1904. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1905. rinsert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
  1906. }
  1907. //! Insert the range <code>[first, last)</code> before the specified position.
  1908. /*!
  1909. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.<br>
  1910. Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
  1911. requirements of an <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
  1912. \post Elements from the range
  1913. <code>[first, last - max[0, distance(first, last) - (end() - pos) - reserve()])</code> will be inserted
  1914. before the position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0,
  1915. distance(first, last) - reserve()]]</code> elements will be overwritten at the end of the
  1916. <code>circular_buffer</code>.<br>(See <i>Example</i> for the explanation.)
  1917. \param pos An iterator specifying the position where the range will be inserted.
  1918. \param first The beginning of the range to be inserted.
  1919. \param last The end of the range to be inserted.
  1920. \throws Whatever <code>T::T(const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1921. Whatever <code>T::operator = (const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1922. Whatever <code>T::T(T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1923. Whatever <code>T::operator = (T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1924. \par Exception Safety
  1925. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1926. \par Iterator Invalidation
  1927. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1928. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten elements.
  1929. \par Complexity
  1930. Linear (in <code>[std::distance(begin(), pos) + std::distance(first, last)]</code>; in
  1931. <code>min[capacity(), std::distance(begin(), pos) + std::distance(first, last)]</code> if the
  1932. <code>InputIterator</code> is a
  1933. <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1934. \par Example
  1935. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1936. look like the one below.<br><br>
  1937. <code>|1|2|3|4| | |</code><br>
  1938. <code>p ___^</code><br><br>After inserting a range of elements before the position <code>p</code>:<br><br>
  1939. <code>int array[] = { 5, 6, 7, 8, 9 };</code><br><code>insert(p, array, array + 5);</code><br><br>
  1940. actually only elements <code>5</code>, <code>6</code>, <code>7</code> and <code>8</code> from the
  1941. specified range get inserted and elements <code>3</code> and <code>4</code> are overwritten. This is due
  1942. to the fact the rinsert operation preserves the capacity. After insertion the internal buffer looks like
  1943. this:<br><br><code>|1|2|5|6|7|8|</code><br><br>For comparison if the capacity would not be preserved the
  1944. internal buffer would then result in <code>|1|2|5|6|7|8|9|3|4|</code>.
  1945. \sa <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1946. <code>\link rinsert(iterator, size_type, param_value_type)
  1947. rinsert(iterator, size_type, value_type)\endlink</code>, <code>\link insert(iterator, param_value_type)
  1948. insert(iterator, value_type)\endlink</code>, <code>\link insert(iterator, size_type, param_value_type)
  1949. insert(iterator, size_type, value_type)\endlink</code>,
  1950. <code>insert(iterator, InputIterator, InputIterator)</code>
  1951. */
  1952. template <class InputIterator>
  1953. void rinsert(iterator pos, InputIterator first, InputIterator last) {
  1954. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1955. rinsert(pos, first, last, is_integral<InputIterator>());
  1956. }
  1957. // Erase
  1958. //! Remove an element at the specified position.
  1959. /*!
  1960. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> (but not an
  1961. <code>end()</code>).
  1962. \post The element at the position <code>pos</code> is removed.
  1963. \param pos An iterator pointing at the element to be removed.
  1964. \return Iterator to the first element remaining beyond the removed element or <code>end()</code> if no such
  1965. element exists.
  1966. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1967. \par Exception Safety
  1968. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1969. \par Iterator Invalidation
  1970. Invalidates iterators pointing to the erased element and iterators pointing to the elements behind
  1971. the erased element (towards the end; except iterators equal to <code>end()</code>).
  1972. \par Complexity
  1973. Linear (in <code>std::distance(pos, end())</code>).
  1974. \sa <code>erase(iterator, iterator)</code>, <code>rerase(iterator)</code>,
  1975. <code>rerase(iterator, iterator)</code>, <code>erase_begin(size_type)</code>,
  1976. <code>erase_end(size_type)</code>, <code>clear()</code>
  1977. */
  1978. iterator erase(iterator pos) {
  1979. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1980. BOOST_CB_ASSERT(pos.m_it != 0); // check for iterator pointing to end()
  1981. pointer next = pos.m_it;
  1982. increment(next);
  1983. for (pointer p = pos.m_it; next != m_last; p = next, increment(next))
  1984. replace(p, boost::move_if_noexcept(*next));
  1985. decrement(m_last);
  1986. destroy_item(m_last);
  1987. --m_size;
  1988. #if BOOST_CB_ENABLE_DEBUG
  1989. return m_last == pos.m_it ? end() : iterator(this, pos.m_it);
  1990. #else
  1991. return m_last == pos.m_it ? end() : pos;
  1992. #endif
  1993. }
  1994. //! Erase the range <code>[first, last)</code>.
  1995. /*!
  1996. \pre Valid range <code>[first, last)</code>.
  1997. \post The elements from the range <code>[first, last)</code> are removed. (If <code>first == last</code>
  1998. nothing is removed.)
  1999. \param first The beginning of the range to be removed.
  2000. \param last The end of the range to be removed.
  2001. \return Iterator to the first element remaining beyond the removed elements or <code>end()</code> if no such
  2002. element exists.
  2003. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2004. \par Exception Safety
  2005. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2006. \par Iterator Invalidation
  2007. Invalidates iterators pointing to the erased elements and iterators pointing to the elements behind
  2008. the erased range (towards the end; except iterators equal to <code>end()</code>).
  2009. \par Complexity
  2010. Linear (in <code>std::distance(first, end())</code>).
  2011. \sa <code>erase(iterator)</code>, <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2012. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>, <code>clear()</code>
  2013. */
  2014. iterator erase(iterator first, iterator last) {
  2015. BOOST_CB_ASSERT(first.is_valid(this)); // check for uninitialized or invalidated iterator
  2016. BOOST_CB_ASSERT(last.is_valid(this)); // check for uninitialized or invalidated iterator
  2017. BOOST_CB_ASSERT(first <= last); // check for wrong range
  2018. if (first == last)
  2019. return first;
  2020. pointer p = first.m_it;
  2021. while (last.m_it != 0)
  2022. replace((first++).m_it, boost::move_if_noexcept(*last++));
  2023. do {
  2024. decrement(m_last);
  2025. destroy_item(m_last);
  2026. --m_size;
  2027. } while(m_last != first.m_it);
  2028. return m_last == p ? end() : iterator(this, p);
  2029. }
  2030. //! Remove an element at the specified position.
  2031. /*!
  2032. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> (but not an
  2033. <code>end()</code>).
  2034. \post The element at the position <code>pos</code> is removed.
  2035. \param pos An iterator pointing at the element to be removed.
  2036. \return Iterator to the first element remaining in front of the removed element or <code>begin()</code> if no
  2037. such element exists.
  2038. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2039. \par Exception Safety
  2040. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2041. \par Iterator Invalidation
  2042. Invalidates iterators pointing to the erased element and iterators pointing to the elements in front of
  2043. the erased element (towards the beginning).
  2044. \par Complexity
  2045. Linear (in <code>std::distance(begin(), pos)</code>).
  2046. \note This method is symetric to the <code>erase(iterator)</code> method and is more effective than
  2047. <code>erase(iterator)</code> if the iterator <code>pos</code> is close to the beginning of the
  2048. <code>circular_buffer</code>. (See the <i>Complexity</i>.)
  2049. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2050. <code>rerase(iterator, iterator)</code>, <code>erase_begin(size_type)</code>,
  2051. <code>erase_end(size_type)</code>, <code>clear()</code>
  2052. */
  2053. iterator rerase(iterator pos) {
  2054. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  2055. BOOST_CB_ASSERT(pos.m_it != 0); // check for iterator pointing to end()
  2056. pointer prev = pos.m_it;
  2057. pointer p = prev;
  2058. for (decrement(prev); p != m_first; p = prev, decrement(prev))
  2059. replace(p, boost::move_if_noexcept(*prev));
  2060. destroy_item(m_first);
  2061. increment(m_first);
  2062. --m_size;
  2063. #if BOOST_CB_ENABLE_DEBUG
  2064. return p == pos.m_it ? begin() : iterator(this, pos.m_it);
  2065. #else
  2066. return p == pos.m_it ? begin() : pos;
  2067. #endif
  2068. }
  2069. //! Erase the range <code>[first, last)</code>.
  2070. /*!
  2071. \pre Valid range <code>[first, last)</code>.
  2072. \post The elements from the range <code>[first, last)</code> are removed. (If <code>first == last</code>
  2073. nothing is removed.)
  2074. \param first The beginning of the range to be removed.
  2075. \param last The end of the range to be removed.
  2076. \return Iterator to the first element remaining in front of the removed elements or <code>begin()</code> if no
  2077. such element exists.
  2078. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2079. \par Exception Safety
  2080. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2081. \par Iterator Invalidation
  2082. Invalidates iterators pointing to the erased elements and iterators pointing to the elements in front of
  2083. the erased range (towards the beginning).
  2084. \par Complexity
  2085. Linear (in <code>std::distance(begin(), last)</code>).
  2086. \note This method is symetric to the <code>erase(iterator, iterator)</code> method and is more effective than
  2087. <code>erase(iterator, iterator)</code> if <code>std::distance(begin(), first)</code> is lower that
  2088. <code>std::distance(last, end())</code>.
  2089. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>, <code>rerase(iterator)</code>,
  2090. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>, <code>clear()</code>
  2091. */
  2092. iterator rerase(iterator first, iterator last) {
  2093. BOOST_CB_ASSERT(first.is_valid(this)); // check for uninitialized or invalidated iterator
  2094. BOOST_CB_ASSERT(last.is_valid(this)); // check for uninitialized or invalidated iterator
  2095. BOOST_CB_ASSERT(first <= last); // check for wrong range
  2096. if (first == last)
  2097. return first;
  2098. pointer p = map_pointer(last.m_it);
  2099. last.m_it = p;
  2100. while (first.m_it != m_first) {
  2101. decrement(first.m_it);
  2102. decrement(p);
  2103. replace(p, boost::move_if_noexcept(*first.m_it));
  2104. }
  2105. do {
  2106. destroy_item(m_first);
  2107. increment(m_first);
  2108. --m_size;
  2109. } while(m_first != p);
  2110. if (m_first == last.m_it)
  2111. return begin();
  2112. decrement(last.m_it);
  2113. return iterator(this, last.m_it);
  2114. }
  2115. //! Remove first <code>n</code> elements (with constant complexity for scalar types).
  2116. /*!
  2117. \pre <code>n \<= size()</code>
  2118. \post The <code>n</code> elements at the beginning of the <code>circular_buffer</code> will be removed.
  2119. \param n The number of elements to be removed.
  2120. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2121. \par Exception Safety
  2122. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
  2123. case of scalars.)
  2124. \par Iterator Invalidation
  2125. Invalidates iterators pointing to the first <code>n</code> erased elements.
  2126. \par Complexity
  2127. Constant (in <code>n</code>) for scalar types; linear for other types.
  2128. \note This method has been specially designed for types which do not require an explicit destructruction (e.g.
  2129. integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
  2130. it possible to implement the "erase from beginning" operation with a constant complexity. For non-sacalar
  2131. types the complexity is linear (hence the explicit destruction is needed) and the implementation is
  2132. actually equivalent to
  2133. <code>\link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink</code>.
  2134. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2135. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2136. <code>erase_end(size_type)</code>, <code>clear()</code>
  2137. */
  2138. void erase_begin(size_type n) {
  2139. BOOST_CB_ASSERT(n <= size()); // check for n greater than size
  2140. #if BOOST_CB_ENABLE_DEBUG
  2141. erase_begin(n, false_type());
  2142. #else
  2143. erase_begin(n, is_scalar<value_type>());
  2144. #endif
  2145. }
  2146. //! Remove last <code>n</code> elements (with constant complexity for scalar types).
  2147. /*!
  2148. \pre <code>n \<= size()</code>
  2149. \post The <code>n</code> elements at the end of the <code>circular_buffer</code> will be removed.
  2150. \param n The number of elements to be removed.
  2151. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2152. \par Exception Safety
  2153. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
  2154. case of scalars.)
  2155. \par Iterator Invalidation
  2156. Invalidates iterators pointing to the last <code>n</code> erased elements.
  2157. \par Complexity
  2158. Constant (in <code>n</code>) for scalar types; linear for other types.
  2159. \note This method has been specially designed for types which do not require an explicit destructruction (e.g.
  2160. integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
  2161. it possible to implement the "erase from end" operation with a constant complexity. For non-sacalar
  2162. types the complexity is linear (hence the explicit destruction is needed) and the implementation is
  2163. actually equivalent to
  2164. <code>\link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink</code>.
  2165. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2166. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2167. <code>erase_begin(size_type)</code>, <code>clear()</code>
  2168. */
  2169. void erase_end(size_type n) {
  2170. BOOST_CB_ASSERT(n <= size()); // check for n greater than size
  2171. #if BOOST_CB_ENABLE_DEBUG
  2172. erase_end(n, false_type());
  2173. #else
  2174. erase_end(n, is_scalar<value_type>());
  2175. #endif
  2176. }
  2177. //! Remove all stored elements from the <code>circular_buffer</code>.
  2178. /*!
  2179. \post <code>size() == 0</code>
  2180. \throws Nothing.
  2181. \par Exception Safety
  2182. No-throw.
  2183. \par Iterator Invalidation
  2184. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  2185. <code>end()</code>).
  2186. \par Complexity
  2187. Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
  2188. \sa <code>~circular_buffer()</code>, <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2189. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2190. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>
  2191. */
  2192. void clear() BOOST_NOEXCEPT {
  2193. destroy_content();
  2194. m_size = 0;
  2195. }
  2196. private:
  2197. // Helper methods
  2198. //! Check if the <code>index</code> is valid.
  2199. void check_position(size_type index) const {
  2200. if (index >= size())
  2201. throw_exception(std::out_of_range("circular_buffer"));
  2202. }
  2203. //! Increment the pointer.
  2204. template <class Pointer>
  2205. void increment(Pointer& p) const {
  2206. if (++p == m_end)
  2207. p = m_buff;
  2208. }
  2209. //! Decrement the pointer.
  2210. template <class Pointer>
  2211. void decrement(Pointer& p) const {
  2212. if (p == m_buff)
  2213. p = m_end;
  2214. --p;
  2215. }
  2216. //! Add <code>n</code> to the pointer.
  2217. template <class Pointer>
  2218. Pointer add(Pointer p, difference_type n) const {
  2219. return p + (n < (m_end - p) ? n : n - capacity());
  2220. }
  2221. //! Subtract <code>n</code> from the pointer.
  2222. template <class Pointer>
  2223. Pointer sub(Pointer p, difference_type n) const {
  2224. return p - (n > (p - m_buff) ? n - capacity() : n);
  2225. }
  2226. //! Map the null pointer to virtual end of circular buffer.
  2227. pointer map_pointer(pointer p) const { return p == 0 ? m_last : p; }
  2228. //! Allocate memory.
  2229. pointer allocate(size_type n) {
  2230. if (n > max_size())
  2231. throw_exception(std::length_error("circular_buffer"));
  2232. #if BOOST_CB_ENABLE_DEBUG
  2233. pointer p = (n == 0) ? 0 : m_alloc.allocate(n);
  2234. cb_details::do_fill_uninitialized_memory(p, sizeof(value_type) * n);
  2235. return p;
  2236. #else
  2237. return (n == 0) ? 0 : m_alloc.allocate(n);
  2238. #endif
  2239. }
  2240. //! Deallocate memory.
  2241. void deallocate(pointer p, size_type n) {
  2242. if (p != 0)
  2243. m_alloc.deallocate(p, n);
  2244. }
  2245. //! Does the pointer point to the uninitialized memory?
  2246. bool is_uninitialized(const_pointer p) const BOOST_NOEXCEPT {
  2247. return p >= m_last && (m_first < m_last || p < m_first);
  2248. }
  2249. //! Replace an element.
  2250. void replace(pointer pos, param_value_type item) {
  2251. *pos = item;
  2252. #if BOOST_CB_ENABLE_DEBUG
  2253. invalidate_iterators(iterator(this, pos));
  2254. #endif
  2255. }
  2256. //! Replace an element.
  2257. void replace(pointer pos, rvalue_type item) {
  2258. *pos = boost::move(item);
  2259. #if BOOST_CB_ENABLE_DEBUG
  2260. invalidate_iterators(iterator(this, pos));
  2261. #endif
  2262. }
  2263. //! Construct or replace an element.
  2264. /*!
  2265. <code>construct</code> has to be set to <code>true</code> if and only if
  2266. <code>pos</code> points to an uninitialized memory.
  2267. */
  2268. void construct_or_replace(bool construct, pointer pos, param_value_type item) {
  2269. if (construct)
  2270. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(pos), item);
  2271. else
  2272. replace(pos, item);
  2273. }
  2274. //! Construct or replace an element.
  2275. /*!
  2276. <code>construct</code> has to be set to <code>true</code> if and only if
  2277. <code>pos</code> points to an uninitialized memory.
  2278. */
  2279. void construct_or_replace(bool construct, pointer pos, rvalue_type item) {
  2280. if (construct)
  2281. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(pos), boost::move(item));
  2282. else
  2283. replace(pos, boost::move(item));
  2284. }
  2285. //! Destroy an item.
  2286. void destroy_item(pointer p) {
  2287. boost::container::allocator_traits<Alloc>::destroy(m_alloc, boost::to_address(p));
  2288. #if BOOST_CB_ENABLE_DEBUG
  2289. invalidate_iterators(iterator(this, p));
  2290. cb_details::do_fill_uninitialized_memory(p, sizeof(value_type));
  2291. #endif
  2292. }
  2293. //! Destroy an item only if it has been constructed.
  2294. void destroy_if_constructed(pointer pos) {
  2295. if (is_uninitialized(pos))
  2296. destroy_item(pos);
  2297. }
  2298. //! Destroy the whole content of the circular buffer.
  2299. void destroy_content() {
  2300. #if BOOST_CB_ENABLE_DEBUG
  2301. destroy_content(false_type());
  2302. #else
  2303. destroy_content(is_scalar<value_type>());
  2304. #endif
  2305. }
  2306. //! Specialized destroy_content method.
  2307. void destroy_content(const true_type&) {
  2308. m_first = add(m_first, size());
  2309. }
  2310. //! Specialized destroy_content method.
  2311. void destroy_content(const false_type&) {
  2312. for (size_type ii = 0; ii < size(); ++ii, increment(m_first))
  2313. destroy_item(m_first);
  2314. }
  2315. //! Destroy content and free allocated memory.
  2316. void destroy() BOOST_NOEXCEPT {
  2317. destroy_content();
  2318. deallocate(m_buff, capacity());
  2319. #if BOOST_CB_ENABLE_DEBUG
  2320. m_buff = 0;
  2321. m_first = 0;
  2322. m_last = 0;
  2323. m_end = 0;
  2324. #endif
  2325. }
  2326. //! Initialize the internal buffer.
  2327. void initialize_buffer(capacity_type buffer_capacity) {
  2328. m_buff = allocate(buffer_capacity);
  2329. m_end = m_buff + buffer_capacity;
  2330. }
  2331. //! Initialize the internal buffer.
  2332. void initialize_buffer(capacity_type buffer_capacity, param_value_type item) {
  2333. initialize_buffer(buffer_capacity);
  2334. BOOST_TRY {
  2335. cb_details::uninitialized_fill_n_with_alloc(m_buff, size(), item, m_alloc);
  2336. } BOOST_CATCH(...) {
  2337. deallocate(m_buff, size());
  2338. BOOST_RETHROW
  2339. }
  2340. BOOST_CATCH_END
  2341. }
  2342. //! Specialized initialize method.
  2343. template <class IntegralType>
  2344. void initialize(IntegralType n, IntegralType item, const true_type&) {
  2345. m_size = static_cast<size_type>(n);
  2346. initialize_buffer(size(), item);
  2347. m_first = m_last = m_buff;
  2348. }
  2349. //! Specialized initialize method.
  2350. template <class Iterator>
  2351. void initialize(Iterator first, Iterator last, const false_type&) {
  2352. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2353. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2354. initialize(first, last, iterator_category<Iterator>::type());
  2355. #else
  2356. initialize(first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2357. #endif
  2358. }
  2359. //! Specialized initialize method.
  2360. template <class InputIterator>
  2361. void initialize(InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2362. BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS // check if the STL provides templated iterator constructors
  2363. // for containers
  2364. std::deque<value_type, allocator_type> tmp(first, last, m_alloc);
  2365. size_type distance = tmp.size();
  2366. initialize(distance, boost::make_move_iterator(tmp.begin()), boost::make_move_iterator(tmp.end()), distance);
  2367. }
  2368. //! Specialized initialize method.
  2369. template <class ForwardIterator>
  2370. void initialize(ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2371. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2372. size_type distance = std::distance(first, last);
  2373. initialize(distance, first, last, distance);
  2374. }
  2375. //! Specialized initialize method.
  2376. template <class IntegralType>
  2377. void initialize(capacity_type buffer_capacity, IntegralType n, IntegralType item, const true_type&) {
  2378. BOOST_CB_ASSERT(buffer_capacity >= static_cast<size_type>(n)); // check for capacity lower than n
  2379. m_size = static_cast<size_type>(n);
  2380. initialize_buffer(buffer_capacity, item);
  2381. m_first = m_buff;
  2382. m_last = buffer_capacity == size() ? m_buff : m_buff + size();
  2383. }
  2384. //! Specialized initialize method.
  2385. template <class Iterator>
  2386. void initialize(capacity_type buffer_capacity, Iterator first, Iterator last, const false_type&) {
  2387. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2388. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2389. initialize(buffer_capacity, first, last, iterator_category<Iterator>::type());
  2390. #else
  2391. initialize(buffer_capacity, first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2392. #endif
  2393. }
  2394. //! Specialized initialize method.
  2395. template <class InputIterator>
  2396. void initialize(capacity_type buffer_capacity,
  2397. InputIterator first,
  2398. InputIterator last,
  2399. const std::input_iterator_tag&) {
  2400. initialize_buffer(buffer_capacity);
  2401. m_first = m_last = m_buff;
  2402. m_size = 0;
  2403. if (buffer_capacity == 0)
  2404. return;
  2405. while (first != last && !full()) {
  2406. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(m_last), *first++);
  2407. increment(m_last);
  2408. ++m_size;
  2409. }
  2410. while (first != last) {
  2411. replace(m_last, *first++);
  2412. increment(m_last);
  2413. m_first = m_last;
  2414. }
  2415. }
  2416. //! Specialized initialize method.
  2417. template <class ForwardIterator>
  2418. void initialize(capacity_type buffer_capacity,
  2419. ForwardIterator first,
  2420. ForwardIterator last,
  2421. const std::forward_iterator_tag&) {
  2422. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2423. initialize(buffer_capacity, first, last, std::distance(first, last));
  2424. }
  2425. //! Initialize the circular buffer.
  2426. template <class ForwardIterator>
  2427. void initialize(capacity_type buffer_capacity,
  2428. ForwardIterator first,
  2429. ForwardIterator last,
  2430. size_type distance) {
  2431. initialize_buffer(buffer_capacity);
  2432. m_first = m_buff;
  2433. if (distance > buffer_capacity) {
  2434. std::advance(first, distance - buffer_capacity);
  2435. m_size = buffer_capacity;
  2436. } else {
  2437. m_size = distance;
  2438. }
  2439. BOOST_TRY {
  2440. m_last = cb_details::uninitialized_copy(first, last, m_buff, m_alloc);
  2441. } BOOST_CATCH(...) {
  2442. deallocate(m_buff, buffer_capacity);
  2443. BOOST_RETHROW
  2444. }
  2445. BOOST_CATCH_END
  2446. if (m_last == m_end)
  2447. m_last = m_buff;
  2448. }
  2449. //! Reset the circular buffer.
  2450. void reset(pointer buff, pointer last, capacity_type new_capacity) {
  2451. destroy();
  2452. m_size = last - buff;
  2453. m_first = m_buff = buff;
  2454. m_end = m_buff + new_capacity;
  2455. m_last = last == m_end ? m_buff : last;
  2456. }
  2457. //! Specialized method for swapping the allocator.
  2458. void swap_allocator(circular_buffer<T, Alloc>&, const true_type&) {
  2459. // Swap is not needed because allocators have no state.
  2460. }
  2461. //! Specialized method for swapping the allocator.
  2462. void swap_allocator(circular_buffer<T, Alloc>& cb, const false_type&) {
  2463. adl_move_swap(m_alloc, cb.m_alloc);
  2464. }
  2465. //! Specialized assign method.
  2466. template <class IntegralType>
  2467. void assign(IntegralType n, IntegralType item, const true_type&) {
  2468. assign(static_cast<size_type>(n), static_cast<value_type>(item));
  2469. }
  2470. //! Specialized assign method.
  2471. template <class Iterator>
  2472. void assign(Iterator first, Iterator last, const false_type&) {
  2473. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2474. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2475. assign(first, last, iterator_category<Iterator>::type());
  2476. #else
  2477. assign(first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2478. #endif
  2479. }
  2480. //! Specialized assign method.
  2481. template <class InputIterator>
  2482. void assign(InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2483. BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS // check if the STL provides templated iterator constructors
  2484. // for containers
  2485. std::deque<value_type, allocator_type> tmp(first, last, m_alloc);
  2486. size_type distance = tmp.size();
  2487. assign_n(distance, distance,
  2488. cb_details::make_assign_range
  2489. (boost::make_move_iterator(tmp.begin()), boost::make_move_iterator(tmp.end()), m_alloc));
  2490. }
  2491. //! Specialized assign method.
  2492. template <class ForwardIterator>
  2493. void assign(ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2494. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2495. size_type distance = std::distance(first, last);
  2496. assign_n(distance, distance, cb_details::make_assign_range(first, last, m_alloc));
  2497. }
  2498. //! Specialized assign method.
  2499. template <class IntegralType>
  2500. void assign(capacity_type new_capacity, IntegralType n, IntegralType item, const true_type&) {
  2501. assign(new_capacity, static_cast<size_type>(n), static_cast<value_type>(item));
  2502. }
  2503. //! Specialized assign method.
  2504. template <class Iterator>
  2505. void assign(capacity_type new_capacity, Iterator first, Iterator last, const false_type&) {
  2506. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2507. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2508. assign(new_capacity, first, last, iterator_category<Iterator>::type());
  2509. #else
  2510. assign(new_capacity, first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2511. #endif
  2512. }
  2513. //! Specialized assign method.
  2514. template <class InputIterator>
  2515. void assign(capacity_type new_capacity, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2516. if (new_capacity == capacity()) {
  2517. clear();
  2518. insert(begin(), first, last);
  2519. } else {
  2520. circular_buffer<value_type, allocator_type> tmp(new_capacity, first, last, m_alloc);
  2521. tmp.swap(*this);
  2522. }
  2523. }
  2524. //! Specialized assign method.
  2525. template <class ForwardIterator>
  2526. void assign(capacity_type new_capacity, ForwardIterator first, ForwardIterator last,
  2527. const std::forward_iterator_tag&) {
  2528. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2529. size_type distance = std::distance(first, last);
  2530. if (distance > new_capacity) {
  2531. std::advance(first, distance - new_capacity);
  2532. distance = new_capacity;
  2533. }
  2534. assign_n(new_capacity, distance,
  2535. cb_details::make_assign_range(first, last, m_alloc));
  2536. }
  2537. //! Helper assign method.
  2538. template <class Functor>
  2539. void assign_n(capacity_type new_capacity, size_type n, const Functor& fnc) {
  2540. if (new_capacity == capacity()) {
  2541. destroy_content();
  2542. BOOST_TRY {
  2543. fnc(m_buff);
  2544. } BOOST_CATCH(...) {
  2545. m_size = 0;
  2546. BOOST_RETHROW
  2547. }
  2548. BOOST_CATCH_END
  2549. } else {
  2550. pointer buff = allocate(new_capacity);
  2551. BOOST_TRY {
  2552. fnc(buff);
  2553. } BOOST_CATCH(...) {
  2554. deallocate(buff, new_capacity);
  2555. BOOST_RETHROW
  2556. }
  2557. BOOST_CATCH_END
  2558. destroy();
  2559. m_buff = buff;
  2560. m_end = m_buff + new_capacity;
  2561. }
  2562. m_size = n;
  2563. m_first = m_buff;
  2564. m_last = add(m_buff, size());
  2565. }
  2566. //! Helper insert method.
  2567. template <class ValT>
  2568. iterator insert_item(const iterator& pos, ValT item) {
  2569. pointer p = pos.m_it;
  2570. if (p == 0) {
  2571. construct_or_replace(!full(), m_last, static_cast<ValT>(item));
  2572. p = m_last;
  2573. } else {
  2574. pointer src = m_last;
  2575. pointer dest = m_last;
  2576. bool construct = !full();
  2577. BOOST_TRY {
  2578. while (src != p) {
  2579. decrement(src);
  2580. construct_or_replace(construct, dest, boost::move_if_noexcept(*src));
  2581. decrement(dest);
  2582. construct = false;
  2583. }
  2584. replace(p, static_cast<ValT>(item));
  2585. } BOOST_CATCH(...) {
  2586. if (!construct && !full()) {
  2587. increment(m_last);
  2588. ++m_size;
  2589. }
  2590. BOOST_RETHROW
  2591. }
  2592. BOOST_CATCH_END
  2593. }
  2594. increment(m_last);
  2595. if (full())
  2596. m_first = m_last;
  2597. else
  2598. ++m_size;
  2599. return iterator(this, p);
  2600. }
  2601. //! Specialized insert method.
  2602. template <class IntegralType>
  2603. void insert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) {
  2604. insert(pos, static_cast<size_type>(n), static_cast<value_type>(item));
  2605. }
  2606. //! Specialized insert method.
  2607. template <class Iterator>
  2608. void insert(const iterator& pos, Iterator first, Iterator last, const false_type&) {
  2609. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2610. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2611. insert(pos, first, last, iterator_category<Iterator>::type());
  2612. #else
  2613. insert(pos, first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2614. #endif
  2615. }
  2616. //! Specialized insert method.
  2617. template <class InputIterator>
  2618. void insert(iterator pos, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2619. if (!full() || pos != begin()) {
  2620. for (;first != last; ++pos)
  2621. pos = insert(pos, *first++);
  2622. }
  2623. }
  2624. //! Specialized insert method.
  2625. template <class ForwardIterator>
  2626. void insert(const iterator& pos, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2627. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2628. size_type n = std::distance(first, last);
  2629. if (n == 0)
  2630. return;
  2631. size_type copy = capacity() - (end() - pos);
  2632. if (copy == 0)
  2633. return;
  2634. if (n > copy) {
  2635. std::advance(first, n - copy);
  2636. n = copy;
  2637. }
  2638. insert_n(pos, n, cb_details::iterator_wrapper<ForwardIterator>(first));
  2639. }
  2640. //! Helper insert method.
  2641. template <class Wrapper>
  2642. void insert_n(const iterator& pos, size_type n, const Wrapper& wrapper) {
  2643. size_type construct = reserve();
  2644. if (construct > n)
  2645. construct = n;
  2646. if (pos.m_it == 0) {
  2647. size_type ii = 0;
  2648. pointer p = m_last;
  2649. BOOST_TRY {
  2650. for (; ii < construct; ++ii, increment(p))
  2651. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(p), *wrapper());
  2652. for (;ii < n; ++ii, increment(p))
  2653. replace(p, *wrapper());
  2654. } BOOST_CATCH(...) {
  2655. size_type constructed = (std::min)(ii, construct);
  2656. m_last = add(m_last, constructed);
  2657. m_size += constructed;
  2658. BOOST_RETHROW
  2659. }
  2660. BOOST_CATCH_END
  2661. } else {
  2662. pointer src = m_last;
  2663. pointer dest = add(m_last, n - 1);
  2664. pointer p = pos.m_it;
  2665. size_type ii = 0;
  2666. BOOST_TRY {
  2667. while (src != pos.m_it) {
  2668. decrement(src);
  2669. construct_or_replace(is_uninitialized(dest), dest, *src);
  2670. decrement(dest);
  2671. }
  2672. for (; ii < n; ++ii, increment(p))
  2673. construct_or_replace(is_uninitialized(p), p, *wrapper());
  2674. } BOOST_CATCH(...) {
  2675. for (p = add(m_last, n - 1); p != dest; decrement(p))
  2676. destroy_if_constructed(p);
  2677. for (n = 0, p = pos.m_it; n < ii; ++n, increment(p))
  2678. destroy_if_constructed(p);
  2679. BOOST_RETHROW
  2680. }
  2681. BOOST_CATCH_END
  2682. }
  2683. m_last = add(m_last, n);
  2684. m_first = add(m_first, n - construct);
  2685. m_size += construct;
  2686. }
  2687. //! Specialized rinsert method.
  2688. template <class IntegralType>
  2689. void rinsert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) {
  2690. rinsert(pos, static_cast<size_type>(n), static_cast<value_type>(item));
  2691. }
  2692. //! Specialized rinsert method.
  2693. template <class Iterator>
  2694. void rinsert(const iterator& pos, Iterator first, Iterator last, const false_type&) {
  2695. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2696. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2697. rinsert(pos, first, last, iterator_category<Iterator>::type());
  2698. #else
  2699. rinsert(pos, first, last, BOOST_DEDUCED_TYPENAME iterator_category<Iterator>::type());
  2700. #endif
  2701. }
  2702. //! Specialized insert method.
  2703. template <class InputIterator>
  2704. void rinsert(iterator pos, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2705. if (!full() || pos.m_it != 0) {
  2706. for (;first != last; ++pos) {
  2707. pos = rinsert(pos, *first++);
  2708. if (pos.m_it == 0)
  2709. break;
  2710. }
  2711. }
  2712. }
  2713. //! Specialized rinsert method.
  2714. template <class ForwardIterator>
  2715. void rinsert(const iterator& pos, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2716. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2717. rinsert_n(pos, std::distance(first, last), cb_details::iterator_wrapper<ForwardIterator>(first));
  2718. }
  2719. //! Helper rinsert method.
  2720. template <class Wrapper>
  2721. void rinsert_n(const iterator& pos, size_type n, const Wrapper& wrapper) {
  2722. if (n == 0)
  2723. return;
  2724. iterator b = begin();
  2725. size_type copy = capacity() - (pos - b);
  2726. if (copy == 0)
  2727. return;
  2728. if (n > copy)
  2729. n = copy;
  2730. size_type construct = reserve();
  2731. if (construct > n)
  2732. construct = n;
  2733. if (pos == b) {
  2734. pointer p = sub(m_first, n);
  2735. size_type ii = n;
  2736. BOOST_TRY {
  2737. for (;ii > construct; --ii, increment(p))
  2738. replace(p, *wrapper());
  2739. for (; ii > 0; --ii, increment(p))
  2740. boost::container::allocator_traits<Alloc>::construct(m_alloc, boost::to_address(p), *wrapper());
  2741. } BOOST_CATCH(...) {
  2742. size_type constructed = ii < construct ? construct - ii : 0;
  2743. m_last = add(m_last, constructed);
  2744. m_size += constructed;
  2745. BOOST_RETHROW
  2746. }
  2747. BOOST_CATCH_END
  2748. } else {
  2749. pointer src = m_first;
  2750. pointer dest = sub(m_first, n);
  2751. pointer p = map_pointer(pos.m_it);
  2752. BOOST_TRY {
  2753. while (src != p) {
  2754. construct_or_replace(is_uninitialized(dest), dest, *src);
  2755. increment(src);
  2756. increment(dest);
  2757. }
  2758. for (size_type ii = 0; ii < n; ++ii, increment(dest))
  2759. construct_or_replace(is_uninitialized(dest), dest, *wrapper());
  2760. } BOOST_CATCH(...) {
  2761. for (src = sub(m_first, n); src != dest; increment(src))
  2762. destroy_if_constructed(src);
  2763. BOOST_RETHROW
  2764. }
  2765. BOOST_CATCH_END
  2766. }
  2767. m_first = sub(m_first, n);
  2768. m_last = sub(m_last, n - construct);
  2769. m_size += construct;
  2770. }
  2771. //! Specialized erase_begin method.
  2772. void erase_begin(size_type n, const true_type&) {
  2773. m_first = add(m_first, n);
  2774. m_size -= n;
  2775. }
  2776. //! Specialized erase_begin method.
  2777. void erase_begin(size_type n, const false_type&) {
  2778. iterator b = begin();
  2779. rerase(b, b + n);
  2780. }
  2781. //! Specialized erase_end method.
  2782. void erase_end(size_type n, const true_type&) {
  2783. m_last = sub(m_last, n);
  2784. m_size -= n;
  2785. }
  2786. //! Specialized erase_end method.
  2787. void erase_end(size_type n, const false_type&) {
  2788. iterator e = end();
  2789. erase(e - n, e);
  2790. }
  2791. };
  2792. // Non-member functions
  2793. //! Compare two <code>circular_buffer</code>s element-by-element to determine if they are equal.
  2794. /*!
  2795. \param lhs The <code>circular_buffer</code> to compare.
  2796. \param rhs The <code>circular_buffer</code> to compare.
  2797. \return <code>lhs.\link circular_buffer::size() size()\endlink == rhs.\link circular_buffer::size() size()\endlink
  2798. && <a href="http://www.sgi.com/tech/stl/equal.html">std::equal</a>(lhs.\link circular_buffer::begin()
  2799. begin()\endlink, lhs.\link circular_buffer::end() end()\endlink,
  2800. rhs.\link circular_buffer::begin() begin()\endlink)</code>
  2801. \throws Nothing.
  2802. \par Complexity
  2803. Linear (in the size of the <code>circular_buffer</code>s).
  2804. \par Iterator Invalidation
  2805. Does not invalidate any iterators.
  2806. */
  2807. template <class T, class Alloc>
  2808. inline bool operator == (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2809. return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
  2810. }
  2811. /*!
  2812. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is lesser than the
  2813. right one.
  2814. \param lhs The <code>circular_buffer</code> to compare.
  2815. \param rhs The <code>circular_buffer</code> to compare.
  2816. \return <code><a href="http://www.sgi.com/tech/stl/lexicographical_compare.html">
  2817. std::lexicographical_compare</a>(lhs.\link circular_buffer::begin() begin()\endlink,
  2818. lhs.\link circular_buffer::end() end()\endlink, rhs.\link circular_buffer::begin() begin()\endlink,
  2819. rhs.\link circular_buffer::end() end()\endlink)</code>
  2820. \throws Nothing.
  2821. \par Complexity
  2822. Linear (in the size of the <code>circular_buffer</code>s).
  2823. \par Iterator Invalidation
  2824. Does not invalidate any iterators.
  2825. */
  2826. template <class T, class Alloc>
  2827. inline bool operator < (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2828. return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  2829. }
  2830. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
  2831. //! Compare two <code>circular_buffer</code>s element-by-element to determine if they are non-equal.
  2832. /*!
  2833. \param lhs The <code>circular_buffer</code> to compare.
  2834. \param rhs The <code>circular_buffer</code> to compare.
  2835. \return <code>!(lhs == rhs)</code>
  2836. \throws Nothing.
  2837. \par Complexity
  2838. Linear (in the size of the <code>circular_buffer</code>s).
  2839. \par Iterator Invalidation
  2840. Does not invalidate any iterators.
  2841. \sa <code>operator==(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2842. */
  2843. template <class T, class Alloc>
  2844. inline bool operator != (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2845. return !(lhs == rhs);
  2846. }
  2847. /*!
  2848. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is greater than
  2849. the right one.
  2850. \param lhs The <code>circular_buffer</code> to compare.
  2851. \param rhs The <code>circular_buffer</code> to compare.
  2852. \return <code>rhs \< lhs</code>
  2853. \throws Nothing.
  2854. \par Complexity
  2855. Linear (in the size of the <code>circular_buffer</code>s).
  2856. \par Iterator Invalidation
  2857. Does not invalidate any iterators.
  2858. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2859. */
  2860. template <class T, class Alloc>
  2861. inline bool operator > (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2862. return rhs < lhs;
  2863. }
  2864. /*!
  2865. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is lesser or equal
  2866. to the right one.
  2867. \param lhs The <code>circular_buffer</code> to compare.
  2868. \param rhs The <code>circular_buffer</code> to compare.
  2869. \return <code>!(rhs \< lhs)</code>
  2870. \throws Nothing.
  2871. \par Complexity
  2872. Linear (in the size of the <code>circular_buffer</code>s).
  2873. \par Iterator Invalidation
  2874. Does not invalidate any iterators.
  2875. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2876. */
  2877. template <class T, class Alloc>
  2878. inline bool operator <= (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2879. return !(rhs < lhs);
  2880. }
  2881. /*!
  2882. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is greater or
  2883. equal to the right one.
  2884. \param lhs The <code>circular_buffer</code> to compare.
  2885. \param rhs The <code>circular_buffer</code> to compare.
  2886. \return <code>!(lhs < rhs)</code>
  2887. \throws Nothing.
  2888. \par Complexity
  2889. Linear (in the size of the <code>circular_buffer</code>s).
  2890. \par Iterator Invalidation
  2891. Does not invalidate any iterators.
  2892. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2893. */
  2894. template <class T, class Alloc>
  2895. inline bool operator >= (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2896. return !(lhs < rhs);
  2897. }
  2898. //! Swap the contents of two <code>circular_buffer</code>s.
  2899. /*!
  2900. \post <code>lhs</code> contains elements of <code>rhs</code> and vice versa.
  2901. \param lhs The <code>circular_buffer</code> whose content will be swapped with <code>rhs</code>.
  2902. \param rhs The <code>circular_buffer</code> whose content will be swapped with <code>lhs</code>.
  2903. \throws Nothing.
  2904. \par Complexity
  2905. Constant (in the size of the <code>circular_buffer</code>s).
  2906. \par Iterator Invalidation
  2907. Invalidates all iterators of both <code>circular_buffer</code>s. (On the other hand the iterators still
  2908. point to the same elements but within another container. If you want to rely on this feature you have to
  2909. turn the <a href="#debug">Debug Support</a> off otherwise an assertion will report an error if such
  2910. invalidated iterator is used.)
  2911. \sa <code>\link circular_buffer::swap(circular_buffer<T, Alloc>&) swap(circular_buffer<T, Alloc>&)\endlink</code>
  2912. */
  2913. template <class T, class Alloc>
  2914. inline void swap(circular_buffer<T, Alloc>& lhs, circular_buffer<T, Alloc>& rhs) BOOST_NOEXCEPT {
  2915. lhs.swap(rhs);
  2916. }
  2917. #endif // #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
  2918. } // namespace boost
  2919. #endif // #if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)