base.hpp 150 KB

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