base.hpp 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  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 2014,2018 Glen Joseph Fernandes
  6. // (glenjofe@gmail.com)
  7. // Use, modification, and distribution is subject to the Boost Software
  8. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)
  11. #define BOOST_CIRCULAR_BUFFER_BASE_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <boost/concept_check.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/circular_buffer/allocators.hpp>
  19. #include <boost/core/empty_value.hpp>
  20. #include <boost/type_traits/is_stateless.hpp>
  21. #include <boost/type_traits/is_integral.hpp>
  22. #include <boost/type_traits/is_scalar.hpp>
  23. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  24. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  25. #include <boost/type_traits/is_copy_constructible.hpp>
  26. #include <boost/type_traits/conditional.hpp>
  27. #include <boost/move/adl_move_swap.hpp>
  28. #include <boost/move/move.hpp>
  29. #include <algorithm>
  30. #include <iterator>
  31. #include <utility>
  32. #include <deque>
  33. #include <stdexcept>
  34. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  35. #include <stddef.h>
  36. #endif
  37. namespace boost {
  38. /*!
  39. \class circular_buffer
  40. \brief Circular buffer - a STL compliant container.
  41. \tparam T The type of the elements stored in the <code>circular_buffer</code>.
  42. \par Type Requirements T
  43. The <code>T</code> has to be <a href="https://www.boost.org/sgi/stl/Assignable.html">
  44. SGIAssignable</a> (SGI STL defined combination of <a href="../../../utility/Assignable.html">
  45. Assignable</a> and <a href="../../../utility/CopyConstructible.html">CopyConstructible</a>).
  46. Moreover <code>T</code> has to be <a href="https://www.boost.org/sgi/stl/DefaultConstructible.html">
  47. DefaultConstructible</a> if supplied as a default parameter when invoking some of the
  48. <code>circular_buffer</code>'s methods e.g.
  49. <code>insert(iterator pos, const value_type& item = %value_type())</code>. And
  50. <a href="https://www.boost.org/sgi/stl/EqualityComparable.html">EqualityComparable</a> and/or
  51. <a href="../../../utility/LessThanComparable.html">LessThanComparable</a> if the <code>circular_buffer</code>
  52. will be compared with another container.
  53. \tparam Alloc The allocator type used for all internal memory management.
  54. \par Type Requirements Alloc
  55. The <code>Alloc</code> has to meet the allocator requirements imposed by STL.
  56. \par Default Alloc
  57. std::allocator<T>
  58. For detailed documentation of the circular_buffer visit:
  59. http://www.boost.org/libs/circular_buffer/doc/circular_buffer.html
  60. */
  61. template <class T, class Alloc>
  62. class circular_buffer
  63. :
  64. /*! \cond */
  65. #if BOOST_CB_ENABLE_DEBUG
  66. public cb_details::debug_iterator_registry,
  67. #endif
  68. /*! \endcond */
  69. private empty_value<Alloc>
  70. {
  71. typedef empty_value<Alloc> base;
  72. // Requirements
  73. //BOOST_CLASS_REQUIRE(T, boost, SGIAssignableConcept);
  74. //BOOST_CONCEPT_ASSERT((Assignable<T>));
  75. //BOOST_CONCEPT_ASSERT((CopyConstructible<T>));
  76. //BOOST_CONCEPT_ASSERT((DefaultConstructible<T>));
  77. // Required if the circular_buffer will be compared with anther container.
  78. //BOOST_CONCEPT_ASSERT((EqualityComparable<T>));
  79. //BOOST_CONCEPT_ASSERT((LessThanComparable<T>));
  80. public:
  81. // Basic types
  82. //! The type of this <code>circular_buffer</code>.
  83. typedef circular_buffer<T, Alloc> this_type;
  84. //! The type of elements stored in the <code>circular_buffer</code>.
  85. typedef typename cb_details::allocator_traits<Alloc>::value_type value_type;
  86. //! A pointer to an element.
  87. typedef typename cb_details::allocator_traits<Alloc>::pointer pointer;
  88. //! A const pointer to the element.
  89. typedef typename cb_details::allocator_traits<Alloc>::const_pointer const_pointer;
  90. //! A reference to an element.
  91. typedef value_type& reference;
  92. //! A const reference to an element.
  93. typedef const value_type& const_reference;
  94. //! The distance type.
  95. /*!
  96. (A signed integral type used to represent the distance between two iterators.)
  97. */
  98. typedef typename cb_details::allocator_traits<Alloc>::difference_type difference_type;
  99. //! The size type.
  100. /*!
  101. (An unsigned integral type that can represent any non-negative value of the container's distance type.)
  102. */
  103. typedef typename cb_details::allocator_traits<Alloc>::size_type size_type;
  104. //! The type of an allocator used in the <code>circular_buffer</code>.
  105. typedef Alloc allocator_type;
  106. // Iterators
  107. //! A const (random access) iterator used to iterate through the <code>circular_buffer</code>.
  108. typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::const_traits<cb_details::allocator_traits<Alloc> > > const_iterator;
  109. //! A (random access) iterator used to iterate through the <code>circular_buffer</code>.
  110. typedef cb_details::iterator< circular_buffer<T, Alloc>, cb_details::nonconst_traits<cb_details::allocator_traits<Alloc> > > iterator;
  111. //! A const iterator used to iterate backwards through a <code>circular_buffer</code>.
  112. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  113. //! An iterator used to iterate backwards through a <code>circular_buffer</code>.
  114. typedef std::reverse_iterator<iterator> reverse_iterator;
  115. // Container specific types
  116. //! An array range.
  117. /*!
  118. (A typedef for the <a href="https://www.boost.org/sgi/stl/pair.html"><code>std::pair</code></a> where
  119. its first element is a pointer to a beginning of an array and its second element represents
  120. a size of the array.)
  121. */
  122. typedef std::pair<pointer, size_type> array_range;
  123. //! A range of a const array.
  124. /*!
  125. (A typedef for the <a href="https://www.boost.org/sgi/stl/pair.html"><code>std::pair</code></a> where
  126. its first element is a pointer to a beginning of a const array and its second element represents
  127. a size of the const array.)
  128. */
  129. typedef std::pair<const_pointer, size_type> const_array_range;
  130. //! The capacity type.
  131. /*!
  132. (Same as <code>size_type</code> - defined for consistency with the __cbso class.
  133. */
  134. // <a href="space_optimized.html"><code>circular_buffer_space_optimized</code></a>.)
  135. typedef size_type capacity_type;
  136. // Helper types
  137. //! A type representing the "best" way to pass the value_type to a method.
  138. typedef const value_type& param_value_type;
  139. //! A type representing rvalue from param type.
  140. //! On compilers without rvalue references support this type is the Boost.Moves type used for emulation.
  141. typedef BOOST_RV_REF(value_type) rvalue_type;
  142. private:
  143. // Member variables
  144. //! The internal buffer used for storing elements in the circular buffer.
  145. pointer m_buff;
  146. //! The internal buffer's end (end of the storage space).
  147. pointer m_end;
  148. //! The virtual beginning of the circular buffer.
  149. pointer m_first;
  150. //! The virtual end of the circular buffer (one behind the last element).
  151. pointer m_last;
  152. //! The number of items currently stored in the circular buffer.
  153. size_type m_size;
  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 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 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. cb_details::allocator_traits<Alloc>::construct(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="https://www.boost.org/sgi/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="https://www.boost.org/sgi/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>)(cb_details::allocator_traits<Alloc>::max_size(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, 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, 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. : base(boost::empty_init_t(), alloc), m_buff(0), m_end(0), m_first(0), m_last(0), m_size(0) {}
  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. : base(boost::empty_init_t(), alloc), m_size(0) {
  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. : base(boost::empty_init_t(), alloc), m_size(n) {
  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. : base(boost::empty_init_t(), alloc), m_size(n) {
  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. base(boost::empty_init_t(), cb.get_allocator()),
  1011. m_size(cb.size()) {
  1012. initialize_buffer(cb.capacity());
  1013. m_first = m_buff;
  1014. BOOST_TRY {
  1015. m_last = cb_details::uninitialized_copy(cb.begin(), cb.end(), m_buff, alloc());
  1016. } BOOST_CATCH(...) {
  1017. deallocate(m_buff, cb.capacity());
  1018. BOOST_RETHROW
  1019. }
  1020. BOOST_CATCH_END
  1021. if (m_last == m_end)
  1022. m_last = m_buff;
  1023. }
  1024. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1025. //! The move constructor.
  1026. /*! \brief Move constructs a <code>circular_buffer</code> from <code>cb</code>, leaving <code>cb</code> empty.
  1027. \pre C++ compiler with rvalue references support.
  1028. \post <code>cb.empty()</code>
  1029. \param cb <code>circular_buffer</code> to 'steal' value from.
  1030. \throws Nothing.
  1031. \par Constant.
  1032. */
  1033. circular_buffer(circular_buffer<T, Alloc>&& cb) BOOST_NOEXCEPT
  1034. : base(boost::empty_init_t(), cb.get_allocator()), m_buff(0), m_end(0), m_first(0), m_last(0), m_size(0) {
  1035. cb.swap(*this);
  1036. }
  1037. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1038. //! Create a full <code>circular_buffer</code> filled with a copy of the range.
  1039. /*!
  1040. \pre Valid range <code>[first, last)</code>.<br>
  1041. <code>first</code> and <code>last</code> have to meet the requirements of
  1042. <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1043. \post <code>capacity() == std::distance(first, last) \&\& full() \&\& (*this)[0]== *first \&\&
  1044. (*this)[1] == *(first + 1) \&\& ... \&\& (*this)[std::distance(first, last) - 1] == *(last - 1)</code>
  1045. \param first The beginning of the range to be copied.
  1046. \param last The end of the range to be copied.
  1047. \param alloc The allocator.
  1048. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1049. used).
  1050. Whatever <code>T::T(const T&)</code> throws.
  1051. \par Complexity
  1052. Linear (in the <code>std::distance(first, last)</code>).
  1053. */
  1054. template <class InputIterator>
  1055. circular_buffer(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type())
  1056. : base(boost::empty_init_t(), alloc) {
  1057. initialize(first, last, is_integral<InputIterator>());
  1058. }
  1059. //! Create a <code>circular_buffer</code> with the specified capacity and filled with a copy of the range.
  1060. /*!
  1061. \pre Valid range <code>[first, last)</code>.<br>
  1062. <code>first</code> and <code>last</code> have to meet the requirements of
  1063. <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1064. \post <code>capacity() == buffer_capacity \&\& size() \<= std::distance(first, last) \&\&
  1065. (*this)[0]== *(last - buffer_capacity) \&\& (*this)[1] == *(last - buffer_capacity + 1) \&\& ... \&\&
  1066. (*this)[buffer_capacity - 1] == *(last - 1)</code><br><br>
  1067. If the number of items to be copied from the range <code>[first, last)</code> is greater than the
  1068. specified <code>buffer_capacity</code> then only elements from the range
  1069. <code>[last - buffer_capacity, last)</code> will be copied.
  1070. \param buffer_capacity The capacity of the created <code>circular_buffer</code>.
  1071. \param first The beginning of the range to be copied.
  1072. \param last The end of the range to be copied.
  1073. \param alloc The allocator.
  1074. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1075. used).
  1076. Whatever <code>T::T(const T&)</code> throws.
  1077. \par Complexity
  1078. Linear (in <code>std::distance(first, last)</code>; in
  1079. <code>min[capacity, std::distance(first, last)]</code> if the <code>InputIterator</code> is a
  1080. <a href="https://www.boost.org/sgi/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1081. */
  1082. template <class InputIterator>
  1083. circular_buffer(capacity_type buffer_capacity, InputIterator first, InputIterator last,
  1084. const allocator_type& alloc = allocator_type())
  1085. : base(boost::empty_init_t(), alloc) {
  1086. initialize(buffer_capacity, first, last, is_integral<InputIterator>());
  1087. }
  1088. //! The destructor.
  1089. /*!
  1090. Destroys the <code>circular_buffer</code>.
  1091. \throws Nothing.
  1092. \par Iterator Invalidation
  1093. Invalidates all iterators pointing to the <code>circular_buffer</code> (including iterators equal to
  1094. <code>end()</code>).
  1095. \par Complexity
  1096. Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
  1097. \sa <code>clear()</code>
  1098. */
  1099. ~circular_buffer() BOOST_NOEXCEPT {
  1100. destroy();
  1101. #if BOOST_CB_ENABLE_DEBUG
  1102. invalidate_all_iterators();
  1103. #endif
  1104. }
  1105. public:
  1106. // Assign methods
  1107. //! The assign operator.
  1108. /*!
  1109. Makes this <code>circular_buffer</code> to become a copy of the specified <code>circular_buffer</code>.
  1110. \post <code>*this == cb</code>
  1111. \param cb The <code>circular_buffer</code> to be copied.
  1112. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1113. used).
  1114. Whatever <code>T::T(const T&)</code> throws.
  1115. \par Exception Safety
  1116. Strong.
  1117. \par Iterator Invalidation
  1118. Invalidates all iterators pointing to this <code>circular_buffer</code> (except iterators equal to
  1119. <code>end()</code>).
  1120. \par Complexity
  1121. Linear (in the size of <code>cb</code>).
  1122. \sa <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1123. <code>\link assign(capacity_type, size_type, param_value_type)
  1124. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1125. <code>assign(InputIterator, InputIterator)</code>,
  1126. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1127. */
  1128. circular_buffer<T, Alloc>& operator = (const circular_buffer<T, Alloc>& cb) {
  1129. if (this == &cb)
  1130. return *this;
  1131. pointer buff = allocate(cb.capacity());
  1132. BOOST_TRY {
  1133. reset(buff, cb_details::uninitialized_copy(cb.begin(), cb.end(), buff, alloc()), cb.capacity());
  1134. } BOOST_CATCH(...) {
  1135. deallocate(buff, cb.capacity());
  1136. BOOST_RETHROW
  1137. }
  1138. BOOST_CATCH_END
  1139. return *this;
  1140. }
  1141. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1142. /*! \brief Move assigns content of <code>cb</code> to <code>*this</code>, leaving <code>cb</code> empty.
  1143. \pre C++ compiler with rvalue references support.
  1144. \post <code>cb.empty()</code>
  1145. \param cb <code>circular_buffer</code> to 'steal' value from.
  1146. \throws Nothing.
  1147. \par Complexity
  1148. Constant.
  1149. */
  1150. circular_buffer<T, Alloc>& operator = (circular_buffer<T, Alloc>&& cb) BOOST_NOEXCEPT {
  1151. cb.swap(*this); // now `this` holds `cb`
  1152. circular_buffer<T, Alloc>(get_allocator()) // temporary that holds initial `cb` allocator
  1153. .swap(cb); // makes `cb` empty
  1154. return *this;
  1155. }
  1156. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1157. //! Assign <code>n</code> items into the <code>circular_buffer</code>.
  1158. /*!
  1159. The content of the <code>circular_buffer</code> will be removed and replaced with <code>n</code> copies of the
  1160. <code>item</code>.
  1161. \post <code>capacity() == n \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... \&\&
  1162. (*this) [n - 1] == item</code>
  1163. \param n The number of elements the <code>circular_buffer</code> will be filled with.
  1164. \param item The element the <code>circular_buffer</code> will be filled with.
  1165. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1166. used).
  1167. Whatever <code>T::T(const T&)</code> throws.
  1168. \par Exception Safety
  1169. Basic.
  1170. \par Iterator Invalidation
  1171. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1172. <code>end()</code>).
  1173. \par Complexity
  1174. Linear (in the <code>n</code>).
  1175. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1176. <code>\link assign(capacity_type, size_type, param_value_type)
  1177. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1178. <code>assign(InputIterator, InputIterator)</code>,
  1179. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1180. */
  1181. void assign(size_type n, param_value_type item) {
  1182. assign_n(n, n, cb_details::assign_n<param_value_type, allocator_type>(n, item, alloc()));
  1183. }
  1184. //! Assign <code>n</code> items into the <code>circular_buffer</code> specifying the capacity.
  1185. /*!
  1186. The capacity of the <code>circular_buffer</code> will be set to the specified value and the content of the
  1187. <code>circular_buffer</code> will be removed and replaced with <code>n</code> copies of the <code>item</code>.
  1188. \pre <code>capacity >= n</code>
  1189. \post <code>capacity() == buffer_capacity \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item
  1190. \&\& ... \&\& (*this) [n - 1] == item </code>
  1191. \param buffer_capacity The new capacity.
  1192. \param n The number of elements the <code>circular_buffer</code> will be filled with.
  1193. \param item The element the <code>circular_buffer</code> will be filled with.
  1194. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1195. used).
  1196. Whatever <code>T::T(const T&)</code> throws.
  1197. \par Exception Safety
  1198. Basic.
  1199. \par Iterator Invalidation
  1200. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1201. <code>end()</code>).
  1202. \par Complexity
  1203. Linear (in the <code>n</code>).
  1204. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1205. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1206. <code>assign(InputIterator, InputIterator)</code>,
  1207. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1208. */
  1209. void assign(capacity_type buffer_capacity, size_type n, param_value_type item) {
  1210. BOOST_CB_ASSERT(buffer_capacity >= n); // check for new capacity lower than n
  1211. assign_n(buffer_capacity, n, cb_details::assign_n<param_value_type, allocator_type>(n, item, alloc()));
  1212. }
  1213. //! Assign a copy of the range into the <code>circular_buffer</code>.
  1214. /*!
  1215. The content of the <code>circular_buffer</code> will be removed and replaced with copies of elements from the
  1216. specified range.
  1217. \pre Valid range <code>[first, last)</code>.<br>
  1218. <code>first</code> and <code>last</code> have to meet the requirements of
  1219. <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1220. \post <code>capacity() == std::distance(first, last) \&\& size() == std::distance(first, last) \&\&
  1221. (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... \&\& (*this)[std::distance(first, last) - 1]
  1222. == *(last - 1)</code>
  1223. \param first The beginning of the range to be copied.
  1224. \param last The end of the range to be copied.
  1225. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1226. used).
  1227. Whatever <code>T::T(const T&)</code> throws.
  1228. \par Exception Safety
  1229. Basic.
  1230. \par Iterator Invalidation
  1231. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1232. <code>end()</code>).
  1233. \par Complexity
  1234. Linear (in the <code>std::distance(first, last)</code>).
  1235. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1236. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1237. <code>\link assign(capacity_type, size_type, param_value_type)
  1238. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1239. <code>assign(capacity_type, InputIterator, InputIterator)</code>
  1240. */
  1241. template <class InputIterator>
  1242. void assign(InputIterator first, InputIterator last) {
  1243. assign(first, last, is_integral<InputIterator>());
  1244. }
  1245. //! Assign a copy of the range into the <code>circular_buffer</code> specifying the capacity.
  1246. /*!
  1247. The capacity of the <code>circular_buffer</code> will be set to the specified value and the content of the
  1248. <code>circular_buffer</code> will be removed and replaced with copies of elements from the specified range.
  1249. \pre Valid range <code>[first, last)</code>.<br>
  1250. <code>first</code> and <code>last</code> have to meet the requirements of
  1251. <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1252. \post <code>capacity() == buffer_capacity \&\& size() \<= std::distance(first, last) \&\&
  1253. (*this)[0]== *(last - buffer_capacity) \&\& (*this)[1] == *(last - buffer_capacity + 1) \&\& ... \&\&
  1254. (*this)[buffer_capacity - 1] == *(last - 1)</code><br><br>
  1255. If the number of items to be copied from the range <code>[first, last)</code> is greater than the
  1256. specified <code>buffer_capacity</code> then only elements from the range
  1257. <code>[last - buffer_capacity, last)</code> will be copied.
  1258. \param buffer_capacity The new capacity.
  1259. \param first The beginning of the range to be copied.
  1260. \param last The end of the range to be copied.
  1261. \throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
  1262. used).
  1263. Whatever <code>T::T(const T&)</code> throws.
  1264. \par Exception Safety
  1265. Basic.
  1266. \par Iterator Invalidation
  1267. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  1268. <code>end()</code>).
  1269. \par Complexity
  1270. Linear (in <code>std::distance(first, last)</code>; in
  1271. <code>min[capacity, std::distance(first, last)]</code> if the <code>InputIterator</code> is a
  1272. <a href="https://www.boost.org/sgi/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1273. \sa <code>\link operator=(const circular_buffer&) operator=\endlink</code>,
  1274. <code>\link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink</code>,
  1275. <code>\link assign(capacity_type, size_type, param_value_type)
  1276. assign(capacity_type, size_type, const_reference)\endlink</code>,
  1277. <code>assign(InputIterator, InputIterator)</code>
  1278. */
  1279. template <class InputIterator>
  1280. void assign(capacity_type buffer_capacity, InputIterator first, InputIterator last) {
  1281. assign(buffer_capacity, first, last, is_integral<InputIterator>());
  1282. }
  1283. //! Swap the contents of two <code>circular_buffer</code>s.
  1284. /*!
  1285. \post <code>this</code> contains elements of <code>cb</code> and vice versa; the capacity of <code>this</code>
  1286. equals to the capacity of <code>cb</code> and vice versa.
  1287. \param cb The <code>circular_buffer</code> whose content will be swapped.
  1288. \throws Nothing.
  1289. \par Exception Safety
  1290. No-throw.
  1291. \par Iterator Invalidation
  1292. Invalidates all iterators of both <code>circular_buffer</code>s. (On the other hand the iterators still
  1293. point to the same elements but within another container. If you want to rely on this feature you have to
  1294. turn the <a href="#debug">Debug Support</a> off otherwise an assertion will report an error if such
  1295. invalidated iterator is used.)
  1296. \par Complexity
  1297. Constant (in the size of the <code>circular_buffer</code>).
  1298. \sa <code>swap(circular_buffer<T, Alloc>&, circular_buffer<T, Alloc>&)</code>
  1299. */
  1300. void swap(circular_buffer<T, Alloc>& cb) BOOST_NOEXCEPT {
  1301. swap_allocator(cb, is_stateless<allocator_type>());
  1302. adl_move_swap(m_buff, cb.m_buff);
  1303. adl_move_swap(m_end, cb.m_end);
  1304. adl_move_swap(m_first, cb.m_first);
  1305. adl_move_swap(m_last, cb.m_last);
  1306. adl_move_swap(m_size, cb.m_size);
  1307. #if BOOST_CB_ENABLE_DEBUG
  1308. invalidate_all_iterators();
  1309. cb.invalidate_all_iterators();
  1310. #endif
  1311. }
  1312. // push and pop
  1313. private:
  1314. /*! INTERNAL ONLY */
  1315. template <class ValT>
  1316. void push_back_impl(ValT item) {
  1317. if (full()) {
  1318. if (empty())
  1319. return;
  1320. replace(m_last, static_cast<ValT>(item));
  1321. increment(m_last);
  1322. m_first = m_last;
  1323. } else {
  1324. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(m_last), static_cast<ValT>(item));
  1325. increment(m_last);
  1326. ++m_size;
  1327. }
  1328. }
  1329. /*! INTERNAL ONLY */
  1330. template <class ValT>
  1331. void push_front_impl(ValT item) {
  1332. BOOST_TRY {
  1333. if (full()) {
  1334. if (empty())
  1335. return;
  1336. decrement(m_first);
  1337. replace(m_first, static_cast<ValT>(item));
  1338. m_last = m_first;
  1339. } else {
  1340. decrement(m_first);
  1341. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(m_first), static_cast<ValT>(item));
  1342. ++m_size;
  1343. }
  1344. } BOOST_CATCH(...) {
  1345. increment(m_first);
  1346. BOOST_RETHROW
  1347. }
  1348. BOOST_CATCH_END
  1349. }
  1350. public:
  1351. //! Insert a new element at the end of the <code>circular_buffer</code>.
  1352. /*!
  1353. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1354. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1355. <code>0</code>, nothing will be inserted.
  1356. \param item The element to be inserted.
  1357. \throws Whatever <code>T::T(const T&)</code> throws.
  1358. Whatever <code>T::operator = (const T&)</code> throws.
  1359. \par Exception Safety
  1360. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1361. \par Iterator Invalidation
  1362. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1363. \par Complexity
  1364. Constant (in the size of the <code>circular_buffer</code>).
  1365. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1366. <code>pop_back()</code>, <code>pop_front()</code>
  1367. */
  1368. void push_back(param_value_type item) {
  1369. push_back_impl<param_value_type>(item);
  1370. }
  1371. //! Insert a new element at the end of the <code>circular_buffer</code> using rvalue references or rvalues references emulation.
  1372. /*!
  1373. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1374. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1375. <code>0</code>, nothing will be inserted.
  1376. \param item The element to be inserted.
  1377. \throws Whatever <code>T::T(T&&)</code> throws.
  1378. Whatever <code>T::operator = (T&&)</code> throws.
  1379. \par Exception Safety
  1380. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1381. \par Iterator Invalidation
  1382. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1383. \par Complexity
  1384. Constant (in the size of the <code>circular_buffer</code>).
  1385. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1386. <code>pop_back()</code>, <code>pop_front()</code>
  1387. */
  1388. void push_back(rvalue_type item) {
  1389. push_back_impl<rvalue_type>(boost::move(item));
  1390. }
  1391. //! Insert a new default-constructed element at the end of the <code>circular_buffer</code>.
  1392. /*!
  1393. \post if <code>capacity() > 0</code> then <code>back() == item</code><br>
  1394. If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
  1395. <code>0</code>, nothing will be inserted.
  1396. \throws Whatever <code>T::T()</code> throws.
  1397. Whatever <code>T::T(T&&)</code> throws.
  1398. Whatever <code>T::operator = (T&&)</code> throws.
  1399. \par Exception Safety
  1400. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1401. \par Iterator Invalidation
  1402. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1403. \par Complexity
  1404. Constant (in the size of the <code>circular_buffer</code>).
  1405. \sa <code>\link push_front() push_front(const_reference)\endlink</code>,
  1406. <code>pop_back()</code>, <code>pop_front()</code>
  1407. */
  1408. void push_back() {
  1409. value_type temp;
  1410. push_back(boost::move(temp));
  1411. }
  1412. //! Insert a new element at the beginning of the <code>circular_buffer</code>.
  1413. /*!
  1414. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1415. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1416. <code>0</code>, nothing will be inserted.
  1417. \param item The element to be inserted.
  1418. \throws Whatever <code>T::T(const T&)</code> throws.
  1419. Whatever <code>T::operator = (const T&)</code> throws.
  1420. \par Exception Safety
  1421. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1422. \par Iterator Invalidation
  1423. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1424. \par Complexity
  1425. Constant (in the size of the <code>circular_buffer</code>).
  1426. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1427. <code>pop_back()</code>, <code>pop_front()</code>
  1428. */
  1429. void push_front(param_value_type item) {
  1430. push_front_impl<param_value_type>(item);
  1431. }
  1432. //! Insert a new element at the beginning of the <code>circular_buffer</code> using rvalue references or rvalues references emulation.
  1433. /*!
  1434. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1435. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1436. <code>0</code>, nothing will be inserted.
  1437. \param item The element to be inserted.
  1438. \throws Whatever <code>T::T(T&&)</code> throws.
  1439. Whatever <code>T::operator = (T&&)</code> throws.
  1440. \par Exception Safety
  1441. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1442. \par Iterator Invalidation
  1443. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1444. \par Complexity
  1445. Constant (in the size of the <code>circular_buffer</code>).
  1446. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1447. <code>pop_back()</code>, <code>pop_front()</code>
  1448. */
  1449. void push_front(rvalue_type item) {
  1450. push_front_impl<rvalue_type>(boost::move(item));
  1451. }
  1452. //! Insert a new default-constructed element at the beginning of the <code>circular_buffer</code>.
  1453. /*!
  1454. \post if <code>capacity() > 0</code> then <code>front() == item</code><br>
  1455. If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
  1456. <code>0</code>, nothing will be inserted.
  1457. \throws Whatever <code>T::T()</code> throws.
  1458. Whatever <code>T::T(T&&)</code> throws.
  1459. Whatever <code>T::operator = (T&&)</code> throws.
  1460. \par Exception Safety
  1461. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1462. \par Iterator Invalidation
  1463. Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
  1464. \par Complexity
  1465. Constant (in the size of the <code>circular_buffer</code>).
  1466. \sa <code>\link push_back() push_back(const_reference)\endlink</code>,
  1467. <code>pop_back()</code>, <code>pop_front()</code>
  1468. */
  1469. void push_front() {
  1470. value_type temp;
  1471. push_front(boost::move(temp));
  1472. }
  1473. //! Remove the last element from the <code>circular_buffer</code>.
  1474. /*!
  1475. \pre <code>!empty()</code>
  1476. \post The last element is removed from the <code>circular_buffer</code>.
  1477. \throws Nothing.
  1478. \par Exception Safety
  1479. No-throw.
  1480. \par Iterator Invalidation
  1481. Invalidates only iterators pointing to the removed element.
  1482. \par Complexity
  1483. Constant (in the size of the <code>circular_buffer</code>).
  1484. \sa <code>pop_front()</code>, <code>\link push_back() push_back(const_reference)\endlink</code>,
  1485. <code>\link push_front() push_front(const_reference)\endlink</code>
  1486. */
  1487. void pop_back() {
  1488. BOOST_CB_ASSERT(!empty()); // check for empty buffer (back element not available)
  1489. decrement(m_last);
  1490. destroy_item(m_last);
  1491. --m_size;
  1492. }
  1493. //! Remove the first element from the <code>circular_buffer</code>.
  1494. /*!
  1495. \pre <code>!empty()</code>
  1496. \post The first element is removed from the <code>circular_buffer</code>.
  1497. \throws Nothing.
  1498. \par Exception Safety
  1499. No-throw.
  1500. \par Iterator Invalidation
  1501. Invalidates only iterators pointing to the removed element.
  1502. \par Complexity
  1503. Constant (in the size of the <code>circular_buffer</code>).
  1504. \sa <code>pop_back()</code>, <code>\link push_back() push_back(const_reference)\endlink</code>,
  1505. <code>\link push_front() push_front(const_reference)\endlink</code>
  1506. */
  1507. void pop_front() {
  1508. BOOST_CB_ASSERT(!empty()); // check for empty buffer (front element not available)
  1509. destroy_item(m_first);
  1510. increment(m_first);
  1511. --m_size;
  1512. }
  1513. private:
  1514. /*! INTERNAL ONLY */
  1515. template <class ValT>
  1516. iterator insert_impl(iterator pos, ValT item) {
  1517. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1518. iterator b = begin();
  1519. if (full() && pos == b)
  1520. return b;
  1521. return insert_item<ValT>(pos, static_cast<ValT>(item));
  1522. }
  1523. public:
  1524. // Insert
  1525. //! Insert an element at the specified position.
  1526. /*!
  1527. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1528. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1529. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1530. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1531. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1532. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1533. \param item The element to be inserted.
  1534. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1535. the <i>Effect</i>.)
  1536. \throws Whatever <code>T::T(const T&)</code> throws.
  1537. Whatever <code>T::operator = (const T&)</code> throws.
  1538. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1539. \par Exception Safety
  1540. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1541. \par Iterator Invalidation
  1542. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1543. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1544. also invalidates iterators pointing to the overwritten element.
  1545. \par Complexity
  1546. Linear (in <code>std::distance(pos, end())</code>).
  1547. \sa <code>\link insert(iterator, size_type, param_value_type)
  1548. insert(iterator, size_type, value_type)\endlink</code>,
  1549. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1550. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1551. <code>\link rinsert(iterator, size_type, param_value_type)
  1552. rinsert(iterator, size_type, value_type)\endlink</code>,
  1553. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1554. */
  1555. iterator insert(iterator pos, param_value_type item) {
  1556. return insert_impl<param_value_type>(pos, item);
  1557. }
  1558. //! Insert an element at the specified position.
  1559. /*!
  1560. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1561. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1562. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1563. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1564. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1565. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1566. \param item The element to be inserted.
  1567. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1568. the <i>Effect</i>.)
  1569. \throws Whatever <code>T::T(T&&)</code> throws.
  1570. Whatever <code>T::operator = (T&&)</code> throws.
  1571. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1572. \par Exception Safety
  1573. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1574. \par Iterator Invalidation
  1575. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1576. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1577. also invalidates iterators pointing to the overwritten element.
  1578. \par Complexity
  1579. Linear (in <code>std::distance(pos, end())</code>).
  1580. \sa <code>\link insert(iterator, size_type, param_value_type)
  1581. insert(iterator, size_type, value_type)\endlink</code>,
  1582. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1583. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1584. <code>\link rinsert(iterator, size_type, param_value_type)
  1585. rinsert(iterator, size_type, value_type)\endlink</code>,
  1586. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1587. */
  1588. iterator insert(iterator pos, rvalue_type item) {
  1589. return insert_impl<rvalue_type>(pos, boost::move(item));
  1590. }
  1591. //! Insert a default-constructed element at the specified position.
  1592. /*!
  1593. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1594. \post The <code>item</code> will be inserted at the position <code>pos</code>.<br>
  1595. If the <code>circular_buffer</code> is full, the first element will be overwritten. If the
  1596. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>begin()</code>, then the
  1597. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1598. \param pos An iterator specifying the position where the <code>item</code> will be inserted.
  1599. \return Iterator to the inserted element or <code>begin()</code> if the <code>item</code> is not inserted. (See
  1600. the <i>Effect</i>.)
  1601. \throws Whatever <code>T::T()</code> throws.
  1602. Whatever <code>T::T(T&&)</code> throws.
  1603. Whatever <code>T::operator = (T&&)</code> throws.
  1604. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1605. \par Exception Safety
  1606. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1607. \par Iterator Invalidation
  1608. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1609. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1610. also invalidates iterators pointing to the overwritten element.
  1611. \par Complexity
  1612. Linear (in <code>std::distance(pos, end())</code>).
  1613. \sa <code>\link insert(iterator, size_type, param_value_type)
  1614. insert(iterator, size_type, value_type)\endlink</code>,
  1615. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1616. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1617. <code>\link rinsert(iterator, size_type, param_value_type)
  1618. rinsert(iterator, size_type, value_type)\endlink</code>,
  1619. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1620. */
  1621. iterator insert(iterator pos) {
  1622. value_type temp;
  1623. return insert(pos, boost::move(temp));
  1624. }
  1625. //! Insert <code>n</code> copies of the <code>item</code> at the specified position.
  1626. /*!
  1627. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1628. \post The number of <code>min[n, (pos - begin()) + reserve()]</code> elements will be inserted at the position
  1629. <code>pos</code>.<br>The number of <code>min[pos - begin(), max[0, n - reserve()]]</code> elements will
  1630. be overwritten at the beginning of the <code>circular_buffer</code>.<br>(See <i>Example</i> for the
  1631. explanation.)
  1632. \param pos An iterator specifying the position where the <code>item</code>s will be inserted.
  1633. \param n The number of <code>item</code>s the to be inserted.
  1634. \param item The element whose copies will be inserted.
  1635. \throws Whatever <code>T::T(const T&)</code> throws.
  1636. Whatever <code>T::operator = (const T&)</code> throws.
  1637. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1638. \par Exception Safety
  1639. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1640. \par Iterator Invalidation
  1641. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1642. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1643. also invalidates iterators pointing to the overwritten elements.
  1644. \par Complexity
  1645. Linear (in <code>min[capacity(), std::distance(pos, end()) + n]</code>).
  1646. \par Example
  1647. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1648. look like the one below.<br><br>
  1649. <code>|1|2|3|4| | |</code><br>
  1650. <code>p ___^</code><br><br>After inserting 5 elements at the position <code>p</code>:<br><br>
  1651. <code>insert(p, (size_t)5, 0);</code><br><br>actually only 4 elements get inserted and elements
  1652. <code>1</code> and <code>2</code> are overwritten. This is due to the fact the insert operation preserves
  1653. the capacity. After insertion the internal buffer looks like this:<br><br><code>|0|0|0|0|3|4|</code><br>
  1654. <br>For comparison if the capacity would not be preserved the internal buffer would then result in
  1655. <code>|1|2|0|0|0|0|0|3|4|</code>.
  1656. \sa <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1657. <code>insert(iterator, InputIterator, InputIterator)</code>,
  1658. <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1659. <code>\link rinsert(iterator, size_type, param_value_type)
  1660. rinsert(iterator, size_type, value_type)\endlink</code>,
  1661. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1662. */
  1663. void insert(iterator pos, size_type n, param_value_type item) {
  1664. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1665. if (n == 0)
  1666. return;
  1667. size_type copy = capacity() - (end() - pos);
  1668. if (copy == 0)
  1669. return;
  1670. if (n > copy)
  1671. n = copy;
  1672. insert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
  1673. }
  1674. //! Insert the range <code>[first, last)</code> at the specified position.
  1675. /*!
  1676. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.<br>
  1677. Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
  1678. requirements of an <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1679. \post Elements from the range
  1680. <code>[first + max[0, distance(first, last) - (pos - begin()) - reserve()], last)</code> will be
  1681. inserted at the position <code>pos</code>.<br>The number of <code>min[pos - begin(), max[0,
  1682. distance(first, last) - reserve()]]</code> elements will be overwritten at the beginning of the
  1683. <code>circular_buffer</code>.<br>(See <i>Example</i> for the explanation.)
  1684. \param pos An iterator specifying the position where the range will be inserted.
  1685. \param first The beginning of the range to be inserted.
  1686. \param last The end of the range to be inserted.
  1687. \throws Whatever <code>T::T(const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1688. Whatever <code>T::operator = (const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1689. Whatever <code>T::T(T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1690. Whatever <code>T::operator = (T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1691. \par Exception Safety
  1692. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1693. \par Iterator Invalidation
  1694. Invalidates iterators pointing to the elements at the insertion point (including <code>pos</code>) and
  1695. iterators behind the insertion point (towards the end; except iterators equal to <code>end()</code>). It
  1696. also invalidates iterators pointing to the overwritten elements.
  1697. \par Complexity
  1698. Linear (in <code>[std::distance(pos, end()) + std::distance(first, last)]</code>; in
  1699. <code>min[capacity(), std::distance(pos, end()) + std::distance(first, last)]</code> if the
  1700. <code>InputIterator</code> is a
  1701. <a href="https://www.boost.org/sgi/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1702. \par Example
  1703. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1704. look like the one below.<br><br>
  1705. <code>|1|2|3|4| | |</code><br>
  1706. <code>p ___^</code><br><br>After inserting a range of elements at the position <code>p</code>:<br><br>
  1707. <code>int array[] = { 5, 6, 7, 8, 9 };</code><br><code>insert(p, array, array + 5);</code><br><br>
  1708. actually only elements <code>6</code>, <code>7</code>, <code>8</code> and <code>9</code> from the
  1709. specified range get inserted and elements <code>1</code> and <code>2</code> are overwritten. This is due
  1710. to the fact the insert operation preserves the capacity. After insertion the internal buffer looks like
  1711. this:<br><br><code>|6|7|8|9|3|4|</code><br><br>For comparison if the capacity would not be preserved the
  1712. internal buffer would then result in <code>|1|2|5|6|7|8|9|3|4|</code>.
  1713. \sa <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1714. <code>\link insert(iterator, size_type, param_value_type)
  1715. insert(iterator, size_type, value_type)\endlink</code>, <code>\link rinsert(iterator, param_value_type)
  1716. rinsert(iterator, value_type)\endlink</code>, <code>\link rinsert(iterator, size_type, param_value_type)
  1717. rinsert(iterator, size_type, value_type)\endlink</code>,
  1718. <code>rinsert(iterator, InputIterator, InputIterator)</code>
  1719. */
  1720. template <class InputIterator>
  1721. void insert(iterator pos, InputIterator first, InputIterator last) {
  1722. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1723. insert(pos, first, last, is_integral<InputIterator>());
  1724. }
  1725. private:
  1726. /*! INTERNAL ONLY */
  1727. template <class ValT>
  1728. iterator rinsert_impl(iterator pos, ValT item) {
  1729. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1730. if (full() && pos.m_it == 0)
  1731. return end();
  1732. if (pos == begin()) {
  1733. BOOST_TRY {
  1734. decrement(m_first);
  1735. construct_or_replace(!full(), m_first, static_cast<ValT>(item));
  1736. } BOOST_CATCH(...) {
  1737. increment(m_first);
  1738. BOOST_RETHROW
  1739. }
  1740. BOOST_CATCH_END
  1741. pos.m_it = m_first;
  1742. } else {
  1743. pointer src = m_first;
  1744. pointer dest = m_first;
  1745. decrement(dest);
  1746. pos.m_it = map_pointer(pos.m_it);
  1747. bool construct = !full();
  1748. BOOST_TRY {
  1749. while (src != pos.m_it) {
  1750. construct_or_replace(construct, dest, boost::move_if_noexcept(*src));
  1751. increment(src);
  1752. increment(dest);
  1753. construct = false;
  1754. }
  1755. decrement(pos.m_it);
  1756. replace(pos.m_it, static_cast<ValT>(item));
  1757. } BOOST_CATCH(...) {
  1758. if (!construct && !full()) {
  1759. decrement(m_first);
  1760. ++m_size;
  1761. }
  1762. BOOST_RETHROW
  1763. }
  1764. BOOST_CATCH_END
  1765. decrement(m_first);
  1766. }
  1767. if (full())
  1768. m_last = m_first;
  1769. else
  1770. ++m_size;
  1771. return iterator(this, pos.m_it);
  1772. }
  1773. public:
  1774. //! Insert an element before the specified position.
  1775. /*!
  1776. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1777. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1778. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1779. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1780. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1781. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1782. \param item The element to be inserted.
  1783. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1784. the <i>Effect</i>.)
  1785. \throws Whatever <code>T::T(const T&)</code> throws.
  1786. Whatever <code>T::operator = (const T&)</code> throws.
  1787. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1788. \par Exception Safety
  1789. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1790. \par Iterator Invalidation
  1791. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1792. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1793. \par Complexity
  1794. Linear (in <code>std::distance(begin(), pos)</code>).
  1795. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1796. rinsert(iterator, size_type, value_type)\endlink</code>,
  1797. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1798. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1799. <code>\link insert(iterator, size_type, param_value_type)
  1800. insert(iterator, size_type, value_type)\endlink</code>,
  1801. <code>insert(iterator, InputIterator, InputIterator)</code>
  1802. */
  1803. iterator rinsert(iterator pos, param_value_type item) {
  1804. return rinsert_impl<param_value_type>(pos, item);
  1805. }
  1806. //! Insert an element before the specified position.
  1807. /*!
  1808. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1809. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1810. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1811. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1812. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1813. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1814. \param item The element to be inserted.
  1815. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1816. the <i>Effect</i>.)
  1817. \throws Whatever <code>T::T(T&&)</code> throws.
  1818. Whatever <code>T::operator = (T&&)</code> throws.
  1819. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1820. \par Exception Safety
  1821. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1822. \par Iterator Invalidation
  1823. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1824. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1825. \par Complexity
  1826. Linear (in <code>std::distance(begin(), pos)</code>).
  1827. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1828. rinsert(iterator, size_type, value_type)\endlink</code>,
  1829. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1830. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1831. <code>\link insert(iterator, size_type, param_value_type)
  1832. insert(iterator, size_type, value_type)\endlink</code>,
  1833. <code>insert(iterator, InputIterator, InputIterator)</code>
  1834. */
  1835. iterator rinsert(iterator pos, rvalue_type item) {
  1836. return rinsert_impl<rvalue_type>(pos, boost::move(item));
  1837. }
  1838. //! Insert an element before the specified position.
  1839. /*!
  1840. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1841. \post The <code>item</code> will be inserted before the position <code>pos</code>.<br>
  1842. If the <code>circular_buffer</code> is full, the last element will be overwritten. If the
  1843. <code>circular_buffer</code> is full and the <code>pos</code> points to <code>end()</code>, then the
  1844. <code>item</code> will not be inserted. If the capacity is <code>0</code>, nothing will be inserted.
  1845. \param pos An iterator specifying the position before which the <code>item</code> will be inserted.
  1846. \return Iterator to the inserted element or <code>end()</code> if the <code>item</code> is not inserted. (See
  1847. the <i>Effect</i>.)
  1848. \throws Whatever <code>T::T()</code> throws.
  1849. Whatever <code>T::T(T&&)</code> throws.
  1850. Whatever <code>T::operator = (T&&)</code> throws.
  1851. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1852. \par Exception Safety
  1853. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1854. \par Iterator Invalidation
  1855. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1856. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten element.
  1857. \par Complexity
  1858. Linear (in <code>std::distance(begin(), pos)</code>).
  1859. \sa <code>\link rinsert(iterator, size_type, param_value_type)
  1860. rinsert(iterator, size_type, value_type)\endlink</code>,
  1861. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1862. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1863. <code>\link insert(iterator, size_type, param_value_type)
  1864. insert(iterator, size_type, value_type)\endlink</code>,
  1865. <code>insert(iterator, InputIterator, InputIterator)</code>
  1866. */
  1867. iterator rinsert(iterator pos) {
  1868. value_type temp;
  1869. return rinsert(pos, boost::move(temp));
  1870. }
  1871. //! Insert <code>n</code> copies of the <code>item</code> before the specified position.
  1872. /*!
  1873. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
  1874. \post The number of <code>min[n, (end() - pos) + reserve()]</code> elements will be inserted before the
  1875. position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0, n - reserve()]]</code> elements
  1876. will be overwritten at the end of the <code>circular_buffer</code>.<br>(See <i>Example</i> for the
  1877. explanation.)
  1878. \param pos An iterator specifying the position where the <code>item</code>s will be inserted.
  1879. \param n The number of <code>item</code>s the to be inserted.
  1880. \param item The element whose copies will be inserted.
  1881. \throws Whatever <code>T::T(const T&)</code> throws.
  1882. Whatever <code>T::operator = (const T&)</code> throws.
  1883. <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1884. \par Exception Safety
  1885. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1886. \par Iterator Invalidation
  1887. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1888. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten elements.
  1889. \par Complexity
  1890. Linear (in <code>min[capacity(), std::distance(begin(), pos) + n]</code>).
  1891. \par Example
  1892. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1893. look like the one below.<br><br>
  1894. <code>|1|2|3|4| | |</code><br>
  1895. <code>p ___^</code><br><br>After inserting 5 elements before the position <code>p</code>:<br><br>
  1896. <code>rinsert(p, (size_t)5, 0);</code><br><br>actually only 4 elements get inserted and elements
  1897. <code>3</code> and <code>4</code> are overwritten. This is due to the fact the rinsert operation preserves
  1898. the capacity. After insertion the internal buffer looks like this:<br><br><code>|1|2|0|0|0|0|</code><br>
  1899. <br>For comparison if the capacity would not be preserved the internal buffer would then result in
  1900. <code>|1|2|0|0|0|0|0|3|4|</code>.
  1901. \sa <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1902. <code>rinsert(iterator, InputIterator, InputIterator)</code>,
  1903. <code>\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink</code>,
  1904. <code>\link insert(iterator, size_type, param_value_type)
  1905. insert(iterator, size_type, value_type)\endlink</code>,
  1906. <code>insert(iterator, InputIterator, InputIterator)</code>
  1907. */
  1908. void rinsert(iterator pos, size_type n, param_value_type item) {
  1909. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1910. rinsert_n(pos, n, cb_details::item_wrapper<const_pointer, param_value_type>(item));
  1911. }
  1912. //! Insert the range <code>[first, last)</code> before the specified position.
  1913. /*!
  1914. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.<br>
  1915. Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
  1916. requirements of an <a href="https://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a>.
  1917. \post Elements from the range
  1918. <code>[first, last - max[0, distance(first, last) - (end() - pos) - reserve()])</code> will be inserted
  1919. before the position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0,
  1920. distance(first, last) - reserve()]]</code> elements will be overwritten at the end of the
  1921. <code>circular_buffer</code>.<br>(See <i>Example</i> for the explanation.)
  1922. \param pos An iterator specifying the position where the range will be inserted.
  1923. \param first The beginning of the range to be inserted.
  1924. \param last The end of the range to be inserted.
  1925. \throws Whatever <code>T::T(const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1926. Whatever <code>T::operator = (const T&)</code> throws if the <code>InputIterator</code> is not a move iterator.
  1927. Whatever <code>T::T(T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1928. Whatever <code>T::operator = (T&&)</code> throws if the <code>InputIterator</code> is a move iterator.
  1929. \par Exception Safety
  1930. Basic; no-throw if the operations in the <i>Throws</i> section do not throw anything.
  1931. \par Iterator Invalidation
  1932. Invalidates iterators pointing to the elements before the insertion point (towards the beginning and
  1933. excluding <code>pos</code>). It also invalidates iterators pointing to the overwritten elements.
  1934. \par Complexity
  1935. Linear (in <code>[std::distance(begin(), pos) + std::distance(first, last)]</code>; in
  1936. <code>min[capacity(), std::distance(begin(), pos) + std::distance(first, last)]</code> if the
  1937. <code>InputIterator</code> is a
  1938. <a href="https://www.boost.org/sgi/stl/RandomAccessIterator.html">RandomAccessIterator</a>).
  1939. \par Example
  1940. Consider a <code>circular_buffer</code> with the capacity of 6 and the size of 4. Its internal buffer may
  1941. look like the one below.<br><br>
  1942. <code>|1|2|3|4| | |</code><br>
  1943. <code>p ___^</code><br><br>After inserting a range of elements before the position <code>p</code>:<br><br>
  1944. <code>int array[] = { 5, 6, 7, 8, 9 };</code><br><code>insert(p, array, array + 5);</code><br><br>
  1945. actually only elements <code>5</code>, <code>6</code>, <code>7</code> and <code>8</code> from the
  1946. specified range get inserted and elements <code>3</code> and <code>4</code> are overwritten. This is due
  1947. to the fact the rinsert operation preserves the capacity. After insertion the internal buffer looks like
  1948. this:<br><br><code>|1|2|5|6|7|8|</code><br><br>For comparison if the capacity would not be preserved the
  1949. internal buffer would then result in <code>|1|2|5|6|7|8|9|3|4|</code>.
  1950. \sa <code>\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink</code>,
  1951. <code>\link rinsert(iterator, size_type, param_value_type)
  1952. rinsert(iterator, size_type, value_type)\endlink</code>, <code>\link insert(iterator, param_value_type)
  1953. insert(iterator, value_type)\endlink</code>, <code>\link insert(iterator, size_type, param_value_type)
  1954. insert(iterator, size_type, value_type)\endlink</code>,
  1955. <code>insert(iterator, InputIterator, InputIterator)</code>
  1956. */
  1957. template <class InputIterator>
  1958. void rinsert(iterator pos, InputIterator first, InputIterator last) {
  1959. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1960. rinsert(pos, first, last, is_integral<InputIterator>());
  1961. }
  1962. // Erase
  1963. //! Remove an element at the specified position.
  1964. /*!
  1965. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> (but not an
  1966. <code>end()</code>).
  1967. \post The element at the position <code>pos</code> is removed.
  1968. \param pos An iterator pointing at the element to be removed.
  1969. \return Iterator to the first element remaining beyond the removed element or <code>end()</code> if no such
  1970. element exists.
  1971. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  1972. \par Exception Safety
  1973. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  1974. \par Iterator Invalidation
  1975. Invalidates iterators pointing to the erased element and iterators pointing to the elements behind
  1976. the erased element (towards the end; except iterators equal to <code>end()</code>).
  1977. \par Complexity
  1978. Linear (in <code>std::distance(pos, end())</code>).
  1979. \sa <code>erase(iterator, iterator)</code>, <code>rerase(iterator)</code>,
  1980. <code>rerase(iterator, iterator)</code>, <code>erase_begin(size_type)</code>,
  1981. <code>erase_end(size_type)</code>, <code>clear()</code>
  1982. */
  1983. iterator erase(iterator pos) {
  1984. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  1985. BOOST_CB_ASSERT(pos.m_it != 0); // check for iterator pointing to end()
  1986. pointer next = pos.m_it;
  1987. increment(next);
  1988. for (pointer p = pos.m_it; next != m_last; p = next, increment(next))
  1989. replace(p, boost::move_if_noexcept(*next));
  1990. decrement(m_last);
  1991. destroy_item(m_last);
  1992. --m_size;
  1993. #if BOOST_CB_ENABLE_DEBUG
  1994. return m_last == pos.m_it ? end() : iterator(this, pos.m_it);
  1995. #else
  1996. return m_last == pos.m_it ? end() : pos;
  1997. #endif
  1998. }
  1999. //! Erase the range <code>[first, last)</code>.
  2000. /*!
  2001. \pre Valid range <code>[first, last)</code>.
  2002. \post The elements from the range <code>[first, last)</code> are removed. (If <code>first == last</code>
  2003. nothing is removed.)
  2004. \param first The beginning of the range to be removed.
  2005. \param last The end of the range to be removed.
  2006. \return Iterator to the first element remaining beyond the removed elements or <code>end()</code> if no such
  2007. element exists.
  2008. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2009. \par Exception Safety
  2010. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2011. \par Iterator Invalidation
  2012. Invalidates iterators pointing to the erased elements and iterators pointing to the elements behind
  2013. the erased range (towards the end; except iterators equal to <code>end()</code>).
  2014. \par Complexity
  2015. Linear (in <code>std::distance(first, end())</code>).
  2016. \sa <code>erase(iterator)</code>, <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2017. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>, <code>clear()</code>
  2018. */
  2019. iterator erase(iterator first, iterator last) {
  2020. BOOST_CB_ASSERT(first.is_valid(this)); // check for uninitialized or invalidated iterator
  2021. BOOST_CB_ASSERT(last.is_valid(this)); // check for uninitialized or invalidated iterator
  2022. BOOST_CB_ASSERT(first <= last); // check for wrong range
  2023. if (first == last)
  2024. return first;
  2025. pointer p = first.m_it;
  2026. while (last.m_it != 0)
  2027. replace((first++).m_it, boost::move_if_noexcept(*last++));
  2028. do {
  2029. decrement(m_last);
  2030. destroy_item(m_last);
  2031. --m_size;
  2032. } while(m_last != first.m_it);
  2033. return m_last == p ? end() : iterator(this, p);
  2034. }
  2035. //! Remove an element at the specified position.
  2036. /*!
  2037. \pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> (but not an
  2038. <code>end()</code>).
  2039. \post The element at the position <code>pos</code> is removed.
  2040. \param pos An iterator pointing at the element to be removed.
  2041. \return Iterator to the first element remaining in front of the removed element or <code>begin()</code> if no
  2042. such element exists.
  2043. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2044. \par Exception Safety
  2045. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2046. \par Iterator Invalidation
  2047. Invalidates iterators pointing to the erased element and iterators pointing to the elements in front of
  2048. the erased element (towards the beginning).
  2049. \par Complexity
  2050. Linear (in <code>std::distance(begin(), pos)</code>).
  2051. \note This method is symmetric to the <code>erase(iterator)</code> method and is more effective than
  2052. <code>erase(iterator)</code> if the iterator <code>pos</code> is close to the beginning of the
  2053. <code>circular_buffer</code>. (See the <i>Complexity</i>.)
  2054. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2055. <code>rerase(iterator, iterator)</code>, <code>erase_begin(size_type)</code>,
  2056. <code>erase_end(size_type)</code>, <code>clear()</code>
  2057. */
  2058. iterator rerase(iterator pos) {
  2059. BOOST_CB_ASSERT(pos.is_valid(this)); // check for uninitialized or invalidated iterator
  2060. BOOST_CB_ASSERT(pos.m_it != 0); // check for iterator pointing to end()
  2061. pointer prev = pos.m_it;
  2062. pointer p = prev;
  2063. for (decrement(prev); p != m_first; p = prev, decrement(prev))
  2064. replace(p, boost::move_if_noexcept(*prev));
  2065. destroy_item(m_first);
  2066. increment(m_first);
  2067. --m_size;
  2068. #if BOOST_CB_ENABLE_DEBUG
  2069. return p == pos.m_it ? begin() : iterator(this, pos.m_it);
  2070. #else
  2071. return p == pos.m_it ? begin() : pos;
  2072. #endif
  2073. }
  2074. //! Erase the range <code>[first, last)</code>.
  2075. /*!
  2076. \pre Valid range <code>[first, last)</code>.
  2077. \post The elements from the range <code>[first, last)</code> are removed. (If <code>first == last</code>
  2078. nothing is removed.)
  2079. \param first The beginning of the range to be removed.
  2080. \param last The end of the range to be removed.
  2081. \return Iterator to the first element remaining in front of the removed elements or <code>begin()</code> if no
  2082. such element exists.
  2083. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2084. \par Exception Safety
  2085. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
  2086. \par Iterator Invalidation
  2087. Invalidates iterators pointing to the erased elements and iterators pointing to the elements in front of
  2088. the erased range (towards the beginning).
  2089. \par Complexity
  2090. Linear (in <code>std::distance(begin(), last)</code>).
  2091. \note This method is symmetric to the <code>erase(iterator, iterator)</code> method and is more effective than
  2092. <code>erase(iterator, iterator)</code> if <code>std::distance(begin(), first)</code> is lower that
  2093. <code>std::distance(last, end())</code>.
  2094. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>, <code>rerase(iterator)</code>,
  2095. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>, <code>clear()</code>
  2096. */
  2097. iterator rerase(iterator first, iterator last) {
  2098. BOOST_CB_ASSERT(first.is_valid(this)); // check for uninitialized or invalidated iterator
  2099. BOOST_CB_ASSERT(last.is_valid(this)); // check for uninitialized or invalidated iterator
  2100. BOOST_CB_ASSERT(first <= last); // check for wrong range
  2101. if (first == last)
  2102. return first;
  2103. pointer p = map_pointer(last.m_it);
  2104. last.m_it = p;
  2105. while (first.m_it != m_first) {
  2106. decrement(first.m_it);
  2107. decrement(p);
  2108. replace(p, boost::move_if_noexcept(*first.m_it));
  2109. }
  2110. do {
  2111. destroy_item(m_first);
  2112. increment(m_first);
  2113. --m_size;
  2114. } while(m_first != p);
  2115. if (m_first == last.m_it)
  2116. return begin();
  2117. decrement(last.m_it);
  2118. return iterator(this, last.m_it);
  2119. }
  2120. //! Remove first <code>n</code> elements (with constant complexity for scalar types).
  2121. /*!
  2122. \pre <code>n \<= size()</code>
  2123. \post The <code>n</code> elements at the beginning of the <code>circular_buffer</code> will be removed.
  2124. \param n The number of elements to be removed.
  2125. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2126. \par Exception Safety
  2127. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
  2128. case of scalars.)
  2129. \par Iterator Invalidation
  2130. Invalidates iterators pointing to the first <code>n</code> erased elements.
  2131. \par Complexity
  2132. Constant (in <code>n</code>) for scalar types; linear for other types.
  2133. \note This method has been specially designed for types which do not require an explicit destructruction (e.g.
  2134. integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
  2135. it possible to implement the "erase from beginning" operation with a constant complexity. For non-sacalar
  2136. types the complexity is linear (hence the explicit destruction is needed) and the implementation is
  2137. actually equivalent to
  2138. <code>\link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink</code>.
  2139. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2140. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2141. <code>erase_end(size_type)</code>, <code>clear()</code>
  2142. */
  2143. void erase_begin(size_type n) {
  2144. BOOST_CB_ASSERT(n <= size()); // check for n greater than size
  2145. #if BOOST_CB_ENABLE_DEBUG
  2146. erase_begin(n, false_type());
  2147. #else
  2148. erase_begin(n, is_scalar<value_type>());
  2149. #endif
  2150. }
  2151. //! Remove last <code>n</code> elements (with constant complexity for scalar types).
  2152. /*!
  2153. \pre <code>n \<= size()</code>
  2154. \post The <code>n</code> elements at the end of the <code>circular_buffer</code> will be removed.
  2155. \param n The number of elements to be removed.
  2156. \throws <a href="circular_buffer/implementation.html#circular_buffer.implementation.exceptions_of_move_if_noexcept_t">Exceptions of move_if_noexcept(T&)</a>.
  2157. \par Exception Safety
  2158. Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything. (I.e. no throw in
  2159. case of scalars.)
  2160. \par Iterator Invalidation
  2161. Invalidates iterators pointing to the last <code>n</code> erased elements.
  2162. \par Complexity
  2163. Constant (in <code>n</code>) for scalar types; linear for other types.
  2164. \note This method has been specially designed for types which do not require an explicit destructruction (e.g.
  2165. integer, float or a pointer). For these scalar types a call to a destructor is not required which makes
  2166. it possible to implement the "erase from end" operation with a constant complexity. For non-sacalar
  2167. types the complexity is linear (hence the explicit destruction is needed) and the implementation is
  2168. actually equivalent to
  2169. <code>\link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink</code>.
  2170. \sa <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2171. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2172. <code>erase_begin(size_type)</code>, <code>clear()</code>
  2173. */
  2174. void erase_end(size_type n) {
  2175. BOOST_CB_ASSERT(n <= size()); // check for n greater than size
  2176. #if BOOST_CB_ENABLE_DEBUG
  2177. erase_end(n, false_type());
  2178. #else
  2179. erase_end(n, is_scalar<value_type>());
  2180. #endif
  2181. }
  2182. //! Remove all stored elements from the <code>circular_buffer</code>.
  2183. /*!
  2184. \post <code>size() == 0</code>
  2185. \throws Nothing.
  2186. \par Exception Safety
  2187. No-throw.
  2188. \par Iterator Invalidation
  2189. Invalidates all iterators pointing to the <code>circular_buffer</code> (except iterators equal to
  2190. <code>end()</code>).
  2191. \par Complexity
  2192. Constant (in the size of the <code>circular_buffer</code>) for scalar types; linear for other types.
  2193. \sa <code>~circular_buffer()</code>, <code>erase(iterator)</code>, <code>erase(iterator, iterator)</code>,
  2194. <code>rerase(iterator)</code>, <code>rerase(iterator, iterator)</code>,
  2195. <code>erase_begin(size_type)</code>, <code>erase_end(size_type)</code>
  2196. */
  2197. void clear() BOOST_NOEXCEPT {
  2198. destroy_content();
  2199. m_size = 0;
  2200. }
  2201. private:
  2202. // Helper methods
  2203. /*! INTERNAL ONLY */
  2204. void check_position(size_type index) const {
  2205. if (index >= size())
  2206. throw_exception(std::out_of_range("circular_buffer"));
  2207. }
  2208. /*! INTERNAL ONLY */
  2209. template <class Pointer>
  2210. void increment(Pointer& p) const {
  2211. if (++p == m_end)
  2212. p = m_buff;
  2213. }
  2214. /*! INTERNAL ONLY */
  2215. template <class Pointer>
  2216. void decrement(Pointer& p) const {
  2217. if (p == m_buff)
  2218. p = m_end;
  2219. --p;
  2220. }
  2221. /*! INTERNAL ONLY */
  2222. template <class Pointer>
  2223. Pointer add(Pointer p, difference_type n) const {
  2224. return p + (n < (m_end - p) ? n : n - (m_end - m_buff));
  2225. }
  2226. /*! INTERNAL ONLY */
  2227. template <class Pointer>
  2228. Pointer sub(Pointer p, difference_type n) const {
  2229. return p - (n > (p - m_buff) ? n - (m_end - m_buff) : n);
  2230. }
  2231. /*! INTERNAL ONLY */
  2232. pointer map_pointer(pointer p) const { return p == 0 ? m_last : p; }
  2233. /*! INTERNAL ONLY */
  2234. const Alloc& alloc() const {
  2235. return base::get();
  2236. }
  2237. /*! INTERNAL ONLY */
  2238. Alloc& alloc() {
  2239. return base::get();
  2240. }
  2241. /*! INTERNAL ONLY */
  2242. pointer allocate(size_type n) {
  2243. if (n > max_size())
  2244. throw_exception(std::length_error("circular_buffer"));
  2245. #if BOOST_CB_ENABLE_DEBUG
  2246. pointer p = (n == 0) ? 0 : alloc().allocate(n);
  2247. cb_details::do_fill_uninitialized_memory(p, sizeof(value_type) * n);
  2248. return p;
  2249. #else
  2250. return (n == 0) ? 0 : alloc().allocate(n);
  2251. #endif
  2252. }
  2253. /*! INTERNAL ONLY */
  2254. void deallocate(pointer p, size_type n) {
  2255. if (p != 0)
  2256. alloc().deallocate(p, n);
  2257. }
  2258. /*! INTERNAL ONLY */
  2259. bool is_uninitialized(const_pointer p) const BOOST_NOEXCEPT {
  2260. return (m_first < m_last)
  2261. ? (p >= m_last || p < m_first)
  2262. : (p >= m_last && p < m_first);
  2263. }
  2264. /*! INTERNAL ONLY */
  2265. void replace(pointer pos, param_value_type item) {
  2266. *pos = item;
  2267. #if BOOST_CB_ENABLE_DEBUG
  2268. invalidate_iterators(iterator(this, pos));
  2269. #endif
  2270. }
  2271. /*! INTERNAL ONLY */
  2272. void replace(pointer pos, rvalue_type item) {
  2273. *pos = boost::move(item);
  2274. #if BOOST_CB_ENABLE_DEBUG
  2275. invalidate_iterators(iterator(this, pos));
  2276. #endif
  2277. }
  2278. /*! INTERNAL ONLY */
  2279. void construct_or_replace(bool construct, pointer pos, param_value_type item) {
  2280. if (construct)
  2281. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(pos), item);
  2282. else
  2283. replace(pos, item);
  2284. }
  2285. /*! INTERNAL ONLY */
  2286. void construct_or_replace(bool construct, pointer pos, rvalue_type item) {
  2287. if (construct)
  2288. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(pos), boost::move(item));
  2289. else
  2290. replace(pos, boost::move(item));
  2291. }
  2292. /*! INTERNAL ONLY */
  2293. void destroy_item(pointer p) {
  2294. cb_details::allocator_traits<Alloc>::destroy(alloc(), boost::to_address(p));
  2295. #if BOOST_CB_ENABLE_DEBUG
  2296. invalidate_iterators(iterator(this, p));
  2297. cb_details::do_fill_uninitialized_memory(p, sizeof(value_type));
  2298. #endif
  2299. }
  2300. /*! INTERNAL ONLY */
  2301. void destroy_if_constructed(pointer pos) {
  2302. if (is_uninitialized(pos))
  2303. destroy_item(pos);
  2304. }
  2305. /*! INTERNAL ONLY */
  2306. void destroy_content() {
  2307. #if BOOST_CB_ENABLE_DEBUG
  2308. destroy_content(false_type());
  2309. #else
  2310. destroy_content(is_scalar<value_type>());
  2311. #endif
  2312. }
  2313. /*! INTERNAL ONLY */
  2314. void destroy_content(const true_type&) {
  2315. m_first = add(m_first, size());
  2316. }
  2317. /*! INTERNAL ONLY */
  2318. void destroy_content(const false_type&) {
  2319. for (size_type ii = 0; ii < size(); ++ii, increment(m_first))
  2320. destroy_item(m_first);
  2321. }
  2322. /*! INTERNAL ONLY */
  2323. void destroy() BOOST_NOEXCEPT {
  2324. destroy_content();
  2325. deallocate(m_buff, capacity());
  2326. #if BOOST_CB_ENABLE_DEBUG
  2327. m_buff = 0;
  2328. m_first = 0;
  2329. m_last = 0;
  2330. m_end = 0;
  2331. #endif
  2332. }
  2333. /*! INTERNAL ONLY */
  2334. void initialize_buffer(capacity_type buffer_capacity) {
  2335. m_buff = allocate(buffer_capacity);
  2336. m_end = m_buff + buffer_capacity;
  2337. }
  2338. /*! INTERNAL ONLY */
  2339. void initialize_buffer(capacity_type buffer_capacity, param_value_type item) {
  2340. initialize_buffer(buffer_capacity);
  2341. BOOST_TRY {
  2342. cb_details::uninitialized_fill_n_with_alloc(m_buff, size(), item, alloc());
  2343. } BOOST_CATCH(...) {
  2344. deallocate(m_buff, size());
  2345. BOOST_RETHROW
  2346. }
  2347. BOOST_CATCH_END
  2348. }
  2349. /*! INTERNAL ONLY */
  2350. template <class IntegralType>
  2351. void initialize(IntegralType n, IntegralType item, const true_type&) {
  2352. m_size = static_cast<size_type>(n);
  2353. initialize_buffer(size(), item);
  2354. m_first = m_last = m_buff;
  2355. }
  2356. /*! INTERNAL ONLY */
  2357. template <class Iterator>
  2358. void initialize(Iterator first, Iterator last, const false_type&) {
  2359. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2360. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2361. initialize(first, last, std::iterator_traits<Iterator>::iterator_category());
  2362. #else
  2363. initialize(first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2364. #endif
  2365. }
  2366. /*! INTERNAL ONLY */
  2367. template <class InputIterator>
  2368. void initialize(InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2369. BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS // check if the STL provides templated iterator constructors
  2370. // for containers
  2371. std::deque<value_type, allocator_type> tmp(first, last, alloc());
  2372. size_type distance = tmp.size();
  2373. initialize(distance, boost::make_move_iterator(tmp.begin()), boost::make_move_iterator(tmp.end()), distance);
  2374. }
  2375. /*! INTERNAL ONLY */
  2376. template <class ForwardIterator>
  2377. void initialize(ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2378. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2379. size_type distance = std::distance(first, last);
  2380. initialize(distance, first, last, distance);
  2381. }
  2382. /*! INTERNAL ONLY */
  2383. template <class IntegralType>
  2384. void initialize(capacity_type buffer_capacity, IntegralType n, IntegralType item, const true_type&) {
  2385. BOOST_CB_ASSERT(buffer_capacity >= static_cast<size_type>(n)); // check for capacity lower than n
  2386. m_size = static_cast<size_type>(n);
  2387. initialize_buffer(buffer_capacity, item);
  2388. m_first = m_buff;
  2389. m_last = buffer_capacity == size() ? m_buff : m_buff + size();
  2390. }
  2391. /*! INTERNAL ONLY */
  2392. template <class Iterator>
  2393. void initialize(capacity_type buffer_capacity, Iterator first, Iterator last, const false_type&) {
  2394. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2395. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2396. initialize(buffer_capacity, first, last, std::iterator_traits<Iterator>::iterator_category());
  2397. #else
  2398. initialize(buffer_capacity, first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2399. #endif
  2400. }
  2401. /*! INTERNAL ONLY */
  2402. template <class InputIterator>
  2403. void initialize(capacity_type buffer_capacity,
  2404. InputIterator first,
  2405. InputIterator last,
  2406. const std::input_iterator_tag&) {
  2407. initialize_buffer(buffer_capacity);
  2408. m_first = m_last = m_buff;
  2409. m_size = 0;
  2410. if (buffer_capacity == 0)
  2411. return;
  2412. while (first != last && !full()) {
  2413. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(m_last), *first++);
  2414. increment(m_last);
  2415. ++m_size;
  2416. }
  2417. while (first != last) {
  2418. replace(m_last, *first++);
  2419. increment(m_last);
  2420. m_first = m_last;
  2421. }
  2422. }
  2423. /*! INTERNAL ONLY */
  2424. template <class ForwardIterator>
  2425. void initialize(capacity_type buffer_capacity,
  2426. ForwardIterator first,
  2427. ForwardIterator last,
  2428. const std::forward_iterator_tag&) {
  2429. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2430. initialize(buffer_capacity, first, last, std::distance(first, last));
  2431. }
  2432. /*! INTERNAL ONLY */
  2433. template <class ForwardIterator>
  2434. void initialize(capacity_type buffer_capacity,
  2435. ForwardIterator first,
  2436. ForwardIterator last,
  2437. size_type distance) {
  2438. initialize_buffer(buffer_capacity);
  2439. m_first = m_buff;
  2440. if (distance > buffer_capacity) {
  2441. std::advance(first, distance - buffer_capacity);
  2442. m_size = buffer_capacity;
  2443. } else {
  2444. m_size = distance;
  2445. }
  2446. BOOST_TRY {
  2447. m_last = cb_details::uninitialized_copy(first, last, m_buff, alloc());
  2448. } BOOST_CATCH(...) {
  2449. deallocate(m_buff, buffer_capacity);
  2450. BOOST_RETHROW
  2451. }
  2452. BOOST_CATCH_END
  2453. if (m_last == m_end)
  2454. m_last = m_buff;
  2455. }
  2456. /*! INTERNAL ONLY */
  2457. void reset(pointer buff, pointer last, capacity_type new_capacity) {
  2458. destroy();
  2459. m_size = last - buff;
  2460. m_first = m_buff = buff;
  2461. m_end = m_buff + new_capacity;
  2462. m_last = last == m_end ? m_buff : last;
  2463. }
  2464. /*! INTERNAL ONLY */
  2465. void swap_allocator(circular_buffer<T, Alloc>&, const true_type&) {
  2466. // Swap is not needed because allocators have no state.
  2467. }
  2468. /*! INTERNAL ONLY */
  2469. void swap_allocator(circular_buffer<T, Alloc>& cb, const false_type&) {
  2470. adl_move_swap(alloc(), cb.alloc());
  2471. }
  2472. /*! INTERNAL ONLY */
  2473. template <class IntegralType>
  2474. void assign(IntegralType n, IntegralType item, const true_type&) {
  2475. assign(static_cast<size_type>(n), static_cast<value_type>(item));
  2476. }
  2477. /*! INTERNAL ONLY */
  2478. template <class Iterator>
  2479. void assign(Iterator first, Iterator last, const false_type&) {
  2480. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2481. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2482. assign(first, last, std::iterator_traits<Iterator>::iterator_category());
  2483. #else
  2484. assign(first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2485. #endif
  2486. }
  2487. /*! INTERNAL ONLY */
  2488. template <class InputIterator>
  2489. void assign(InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2490. BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS // check if the STL provides templated iterator constructors
  2491. // for containers
  2492. std::deque<value_type, allocator_type> tmp(first, last, alloc());
  2493. size_type distance = tmp.size();
  2494. assign_n(distance, distance,
  2495. cb_details::make_assign_range
  2496. (boost::make_move_iterator(tmp.begin()), boost::make_move_iterator(tmp.end()), alloc()));
  2497. }
  2498. /*! INTERNAL ONLY */
  2499. template <class ForwardIterator>
  2500. void assign(ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2501. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2502. size_type distance = std::distance(first, last);
  2503. assign_n(distance, distance, cb_details::make_assign_range(first, last, alloc()));
  2504. }
  2505. /*! INTERNAL ONLY */
  2506. template <class IntegralType>
  2507. void assign(capacity_type new_capacity, IntegralType n, IntegralType item, const true_type&) {
  2508. assign(new_capacity, static_cast<size_type>(n), static_cast<value_type>(item));
  2509. }
  2510. /*! INTERNAL ONLY */
  2511. template <class Iterator>
  2512. void assign(capacity_type new_capacity, Iterator first, Iterator last, const false_type&) {
  2513. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2514. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2515. assign(new_capacity, first, last, std::iterator_traits<Iterator>::iterator_category());
  2516. #else
  2517. assign(new_capacity, first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2518. #endif
  2519. }
  2520. /*! INTERNAL ONLY */
  2521. template <class InputIterator>
  2522. void assign(capacity_type new_capacity, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2523. if (new_capacity == capacity()) {
  2524. clear();
  2525. insert(begin(), first, last);
  2526. } else {
  2527. circular_buffer<value_type, allocator_type> tmp(new_capacity, first, last, alloc());
  2528. tmp.swap(*this);
  2529. }
  2530. }
  2531. /*! INTERNAL ONLY */
  2532. template <class ForwardIterator>
  2533. void assign(capacity_type new_capacity, ForwardIterator first, ForwardIterator last,
  2534. const std::forward_iterator_tag&) {
  2535. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2536. size_type distance = std::distance(first, last);
  2537. if (distance > new_capacity) {
  2538. std::advance(first, distance - new_capacity);
  2539. distance = new_capacity;
  2540. }
  2541. assign_n(new_capacity, distance,
  2542. cb_details::make_assign_range(first, last, alloc()));
  2543. }
  2544. /*! INTERNAL ONLY */
  2545. template <class Functor>
  2546. void assign_n(capacity_type new_capacity, size_type n, const Functor& fnc) {
  2547. if (new_capacity == capacity()) {
  2548. destroy_content();
  2549. BOOST_TRY {
  2550. fnc(m_buff);
  2551. } BOOST_CATCH(...) {
  2552. m_size = 0;
  2553. BOOST_RETHROW
  2554. }
  2555. BOOST_CATCH_END
  2556. } else {
  2557. pointer buff = allocate(new_capacity);
  2558. BOOST_TRY {
  2559. fnc(buff);
  2560. } BOOST_CATCH(...) {
  2561. deallocate(buff, new_capacity);
  2562. BOOST_RETHROW
  2563. }
  2564. BOOST_CATCH_END
  2565. destroy();
  2566. m_buff = buff;
  2567. m_end = m_buff + new_capacity;
  2568. }
  2569. m_size = n;
  2570. m_first = m_buff;
  2571. m_last = add(m_buff, size());
  2572. }
  2573. /*! INTERNAL ONLY */
  2574. template <class ValT>
  2575. iterator insert_item(const iterator& pos, ValT item) {
  2576. pointer p = pos.m_it;
  2577. if (p == 0) {
  2578. construct_or_replace(!full(), m_last, static_cast<ValT>(item));
  2579. p = m_last;
  2580. } else {
  2581. pointer src = m_last;
  2582. pointer dest = m_last;
  2583. bool construct = !full();
  2584. BOOST_TRY {
  2585. while (src != p) {
  2586. decrement(src);
  2587. construct_or_replace(construct, dest, boost::move_if_noexcept(*src));
  2588. decrement(dest);
  2589. construct = false;
  2590. }
  2591. replace(p, static_cast<ValT>(item));
  2592. } BOOST_CATCH(...) {
  2593. if (!construct && !full()) {
  2594. increment(m_last);
  2595. ++m_size;
  2596. }
  2597. BOOST_RETHROW
  2598. }
  2599. BOOST_CATCH_END
  2600. }
  2601. increment(m_last);
  2602. if (full())
  2603. m_first = m_last;
  2604. else
  2605. ++m_size;
  2606. return iterator(this, p);
  2607. }
  2608. /*! INTERNAL ONLY */
  2609. template <class IntegralType>
  2610. void insert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) {
  2611. insert(pos, static_cast<size_type>(n), static_cast<value_type>(item));
  2612. }
  2613. /*! INTERNAL ONLY */
  2614. template <class Iterator>
  2615. void insert(const iterator& pos, Iterator first, Iterator last, const false_type&) {
  2616. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2617. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2618. insert(pos, first, last, std::iterator_traits<Iterator>::iterator_category());
  2619. #else
  2620. insert(pos, first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2621. #endif
  2622. }
  2623. /*! INTERNAL ONLY */
  2624. template <class InputIterator>
  2625. void insert(iterator pos, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2626. if (!full() || pos != begin()) {
  2627. for (;first != last; ++pos)
  2628. pos = insert(pos, *first++);
  2629. }
  2630. }
  2631. /*! INTERNAL ONLY */
  2632. template <class ForwardIterator>
  2633. void insert(const iterator& pos, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2634. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2635. size_type n = std::distance(first, last);
  2636. if (n == 0)
  2637. return;
  2638. size_type copy = capacity() - (end() - pos);
  2639. if (copy == 0)
  2640. return;
  2641. if (n > copy) {
  2642. std::advance(first, n - copy);
  2643. n = copy;
  2644. }
  2645. insert_n(pos, n, cb_details::iterator_wrapper<ForwardIterator>(first));
  2646. }
  2647. /*! INTERNAL ONLY */
  2648. template <class Wrapper>
  2649. void insert_n(const iterator& pos, size_type n, const Wrapper& wrapper) {
  2650. size_type construct = reserve();
  2651. if (construct > n)
  2652. construct = n;
  2653. if (pos.m_it == 0) {
  2654. size_type ii = 0;
  2655. pointer p = m_last;
  2656. BOOST_TRY {
  2657. for (; ii < construct; ++ii, increment(p))
  2658. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(p), *wrapper());
  2659. for (;ii < n; ++ii, increment(p))
  2660. replace(p, *wrapper());
  2661. } BOOST_CATCH(...) {
  2662. size_type constructed = (std::min)(ii, construct);
  2663. m_last = add(m_last, constructed);
  2664. m_size += constructed;
  2665. BOOST_RETHROW
  2666. }
  2667. BOOST_CATCH_END
  2668. } else {
  2669. pointer src = m_last;
  2670. pointer dest = add(m_last, n - 1);
  2671. pointer p = pos.m_it;
  2672. size_type ii = 0;
  2673. BOOST_TRY {
  2674. while (src != pos.m_it) {
  2675. decrement(src);
  2676. construct_or_replace(is_uninitialized(dest), dest, *src);
  2677. decrement(dest);
  2678. }
  2679. for (; ii < n; ++ii, increment(p))
  2680. construct_or_replace(is_uninitialized(p), p, *wrapper());
  2681. } BOOST_CATCH(...) {
  2682. for (p = add(m_last, n - 1); p != dest; decrement(p))
  2683. destroy_if_constructed(p);
  2684. for (n = 0, p = pos.m_it; n < ii; ++n, increment(p))
  2685. destroy_if_constructed(p);
  2686. BOOST_RETHROW
  2687. }
  2688. BOOST_CATCH_END
  2689. }
  2690. m_last = add(m_last, n);
  2691. m_first = add(m_first, n - construct);
  2692. m_size += construct;
  2693. }
  2694. /*! INTERNAL ONLY */
  2695. template <class IntegralType>
  2696. void rinsert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) {
  2697. rinsert(pos, static_cast<size_type>(n), static_cast<value_type>(item));
  2698. }
  2699. /*! INTERNAL ONLY */
  2700. template <class Iterator>
  2701. void rinsert(const iterator& pos, Iterator first, Iterator last, const false_type&) {
  2702. BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type
  2703. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  2704. rinsert(pos, first, last, std::iterator_traits<Iterator>::iterator_category());
  2705. #else
  2706. rinsert(pos, first, last, BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::iterator_category());
  2707. #endif
  2708. }
  2709. /*! INTERNAL ONLY */
  2710. template <class InputIterator>
  2711. void rinsert(iterator pos, InputIterator first, InputIterator last, const std::input_iterator_tag&) {
  2712. if (!full() || pos.m_it != 0) {
  2713. for (;first != last; ++pos) {
  2714. pos = rinsert(pos, *first++);
  2715. if (pos.m_it == 0)
  2716. break;
  2717. }
  2718. }
  2719. }
  2720. /*! INTERNAL ONLY */
  2721. template <class ForwardIterator>
  2722. void rinsert(const iterator& pos, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
  2723. BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
  2724. rinsert_n(pos, std::distance(first, last), cb_details::iterator_wrapper<ForwardIterator>(first));
  2725. }
  2726. /*! INTERNAL ONLY */
  2727. template <class Wrapper>
  2728. void rinsert_n(const iterator& pos, size_type n, const Wrapper& wrapper) {
  2729. if (n == 0)
  2730. return;
  2731. iterator b = begin();
  2732. size_type copy = capacity() - (pos - b);
  2733. if (copy == 0)
  2734. return;
  2735. if (n > copy)
  2736. n = copy;
  2737. size_type construct = reserve();
  2738. if (construct > n)
  2739. construct = n;
  2740. if (pos == b) {
  2741. pointer p = sub(m_first, n);
  2742. size_type ii = n;
  2743. BOOST_TRY {
  2744. for (;ii > construct; --ii, increment(p))
  2745. replace(p, *wrapper());
  2746. for (; ii > 0; --ii, increment(p))
  2747. cb_details::allocator_traits<Alloc>::construct(alloc(), boost::to_address(p), *wrapper());
  2748. } BOOST_CATCH(...) {
  2749. size_type constructed = ii < construct ? construct - ii : 0;
  2750. m_last = add(m_last, constructed);
  2751. m_size += constructed;
  2752. BOOST_RETHROW
  2753. }
  2754. BOOST_CATCH_END
  2755. } else {
  2756. pointer src = m_first;
  2757. pointer dest = sub(m_first, n);
  2758. pointer p = map_pointer(pos.m_it);
  2759. BOOST_TRY {
  2760. while (src != p) {
  2761. construct_or_replace(is_uninitialized(dest), dest, *src);
  2762. increment(src);
  2763. increment(dest);
  2764. }
  2765. for (size_type ii = 0; ii < n; ++ii, increment(dest))
  2766. construct_or_replace(is_uninitialized(dest), dest, *wrapper());
  2767. } BOOST_CATCH(...) {
  2768. for (src = sub(m_first, n); src != dest; increment(src))
  2769. destroy_if_constructed(src);
  2770. BOOST_RETHROW
  2771. }
  2772. BOOST_CATCH_END
  2773. }
  2774. m_first = sub(m_first, n);
  2775. m_last = sub(m_last, n - construct);
  2776. m_size += construct;
  2777. }
  2778. /*! INTERNAL ONLY */
  2779. void erase_begin(size_type n, const true_type&) {
  2780. m_first = add(m_first, n);
  2781. m_size -= n;
  2782. }
  2783. /*! INTERNAL ONLY */
  2784. void erase_begin(size_type n, const false_type&) {
  2785. iterator b = begin();
  2786. rerase(b, b + n);
  2787. }
  2788. /*! INTERNAL ONLY */
  2789. void erase_end(size_type n, const true_type&) {
  2790. m_last = sub(m_last, n);
  2791. m_size -= n;
  2792. }
  2793. /*! INTERNAL ONLY */
  2794. void erase_end(size_type n, const false_type&) {
  2795. iterator e = end();
  2796. erase(e - n, e);
  2797. }
  2798. };
  2799. // Non-member functions
  2800. //! Compare two <code>circular_buffer</code>s element-by-element to determine if they are equal.
  2801. /*!
  2802. \param lhs The <code>circular_buffer</code> to compare.
  2803. \param rhs The <code>circular_buffer</code> to compare.
  2804. \return <code>lhs.\link circular_buffer::size() size()\endlink == rhs.\link circular_buffer::size() size()\endlink
  2805. && <a href="https://www.boost.org/sgi/stl/equal.html">std::equal</a>(lhs.\link circular_buffer::begin()
  2806. begin()\endlink, lhs.\link circular_buffer::end() end()\endlink,
  2807. rhs.\link circular_buffer::begin() begin()\endlink)</code>
  2808. \throws Nothing.
  2809. \par Complexity
  2810. Linear (in the size of the <code>circular_buffer</code>s).
  2811. \par Iterator Invalidation
  2812. Does not invalidate any iterators.
  2813. */
  2814. template <class T, class Alloc>
  2815. inline bool operator == (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2816. return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
  2817. }
  2818. /*!
  2819. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is lesser than the
  2820. right one.
  2821. \param lhs The <code>circular_buffer</code> to compare.
  2822. \param rhs The <code>circular_buffer</code> to compare.
  2823. \return <code><a href="https://www.boost.org/sgi/stl/lexicographical_compare.html">
  2824. std::lexicographical_compare</a>(lhs.\link circular_buffer::begin() begin()\endlink,
  2825. lhs.\link circular_buffer::end() end()\endlink, rhs.\link circular_buffer::begin() begin()\endlink,
  2826. rhs.\link circular_buffer::end() end()\endlink)</code>
  2827. \throws Nothing.
  2828. \par Complexity
  2829. Linear (in the size of the <code>circular_buffer</code>s).
  2830. \par Iterator Invalidation
  2831. Does not invalidate any iterators.
  2832. */
  2833. template <class T, class Alloc>
  2834. inline bool operator < (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2835. return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  2836. }
  2837. #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
  2838. //! Compare two <code>circular_buffer</code>s element-by-element to determine if they are non-equal.
  2839. /*!
  2840. \param lhs The <code>circular_buffer</code> to compare.
  2841. \param rhs The <code>circular_buffer</code> to compare.
  2842. \return <code>!(lhs == rhs)</code>
  2843. \throws Nothing.
  2844. \par Complexity
  2845. Linear (in the size of the <code>circular_buffer</code>s).
  2846. \par Iterator Invalidation
  2847. Does not invalidate any iterators.
  2848. \sa <code>operator==(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2849. */
  2850. template <class T, class Alloc>
  2851. inline bool operator != (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2852. return !(lhs == rhs);
  2853. }
  2854. /*!
  2855. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is greater than
  2856. the right one.
  2857. \param lhs The <code>circular_buffer</code> to compare.
  2858. \param rhs The <code>circular_buffer</code> to compare.
  2859. \return <code>rhs \< lhs</code>
  2860. \throws Nothing.
  2861. \par Complexity
  2862. Linear (in the size of the <code>circular_buffer</code>s).
  2863. \par Iterator Invalidation
  2864. Does not invalidate any iterators.
  2865. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2866. */
  2867. template <class T, class Alloc>
  2868. inline bool operator > (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2869. return rhs < lhs;
  2870. }
  2871. /*!
  2872. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is lesser or equal
  2873. to the right one.
  2874. \param lhs The <code>circular_buffer</code> to compare.
  2875. \param rhs The <code>circular_buffer</code> to compare.
  2876. \return <code>!(rhs \< lhs)</code>
  2877. \throws Nothing.
  2878. \par Complexity
  2879. Linear (in the size of the <code>circular_buffer</code>s).
  2880. \par Iterator Invalidation
  2881. Does not invalidate any iterators.
  2882. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2883. */
  2884. template <class T, class Alloc>
  2885. inline bool operator <= (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2886. return !(rhs < lhs);
  2887. }
  2888. /*!
  2889. \brief Compare two <code>circular_buffer</code>s element-by-element to determine if the left one is greater or
  2890. equal to the right one.
  2891. \param lhs The <code>circular_buffer</code> to compare.
  2892. \param rhs The <code>circular_buffer</code> to compare.
  2893. \return <code>!(lhs < rhs)</code>
  2894. \throws Nothing.
  2895. \par Complexity
  2896. Linear (in the size of the <code>circular_buffer</code>s).
  2897. \par Iterator Invalidation
  2898. Does not invalidate any iterators.
  2899. \sa <code>operator<(const circular_buffer<T,Alloc>&, const circular_buffer<T,Alloc>&)</code>
  2900. */
  2901. template <class T, class Alloc>
  2902. inline bool operator >= (const circular_buffer<T, Alloc>& lhs, const circular_buffer<T, Alloc>& rhs) {
  2903. return !(lhs < rhs);
  2904. }
  2905. //! Swap the contents of two <code>circular_buffer</code>s.
  2906. /*!
  2907. \post <code>lhs</code> contains elements of <code>rhs</code> and vice versa.
  2908. \param lhs The <code>circular_buffer</code> whose content will be swapped with <code>rhs</code>.
  2909. \param rhs The <code>circular_buffer</code> whose content will be swapped with <code>lhs</code>.
  2910. \throws Nothing.
  2911. \par Complexity
  2912. Constant (in the size of the <code>circular_buffer</code>s).
  2913. \par Iterator Invalidation
  2914. Invalidates all iterators of both <code>circular_buffer</code>s. (On the other hand the iterators still
  2915. point to the same elements but within another container. If you want to rely on this feature you have to
  2916. turn the <a href="#debug">Debug Support</a> off otherwise an assertion will report an error if such
  2917. invalidated iterator is used.)
  2918. \sa <code>\link circular_buffer::swap(circular_buffer<T, Alloc>&) swap(circular_buffer<T, Alloc>&)\endlink</code>
  2919. */
  2920. template <class T, class Alloc>
  2921. inline void swap(circular_buffer<T, Alloc>& lhs, circular_buffer<T, Alloc>& rhs) BOOST_NOEXCEPT {
  2922. lhs.swap(rhs);
  2923. }
  2924. #endif // #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
  2925. } // namespace boost
  2926. #endif // #if !defined(BOOST_CIRCULAR_BUFFER_BASE_HPP)