base.hpp 150 KB

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