base.hpp 151 KB

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