rope 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  1. // SGI's rope class -*- C++ -*-
  2. // Copyright (C) 2001-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file ext/rope
  33. * This file is a GNU extension to the Standard C++ Library (possibly
  34. * containing extensions from the HP/SGI STL subset).
  35. */
  36. #ifndef _ROPE
  37. #define _ROPE 1
  38. #pragma GCC system_header
  39. #include <algorithm>
  40. #include <iosfwd>
  41. #include <bits/stl_construct.h>
  42. #include <bits/stl_uninitialized.h>
  43. #include <bits/stl_function.h>
  44. #include <bits/stl_numeric.h>
  45. #include <bits/allocator.h>
  46. #include <bits/gthr.h>
  47. #include <ext/alloc_traits.h>
  48. #include <tr1/functional>
  49. # ifdef __GC
  50. # define __GC_CONST const
  51. # else
  52. # define __GC_CONST // constant except for deallocation
  53. # endif
  54. #include <ext/memory> // For uninitialized_copy_n
  55. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. namespace __detail
  59. {
  60. enum { _S_max_rope_depth = 45 };
  61. enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
  62. } // namespace __detail
  63. // See libstdc++/36832.
  64. template<typename _ForwardIterator, typename _Allocator>
  65. void
  66. _Destroy_const(_ForwardIterator __first,
  67. _ForwardIterator __last, _Allocator __alloc)
  68. {
  69. for (; __first != __last; ++__first)
  70. __alloc.destroy(&*__first);
  71. }
  72. template<typename _ForwardIterator, typename _Tp>
  73. inline void
  74. _Destroy_const(_ForwardIterator __first,
  75. _ForwardIterator __last, std::allocator<_Tp>)
  76. { std::_Destroy(__first, __last); }
  77. // The _S_eos function is used for those functions that
  78. // convert to/from C-like strings to detect the end of the string.
  79. // The end-of-C-string character.
  80. // This is what the draft standard says it should be.
  81. template <class _CharT>
  82. inline _CharT
  83. _S_eos(_CharT*)
  84. { return _CharT(); }
  85. // Test for basic character types.
  86. // For basic character types leaves having a trailing eos.
  87. template <class _CharT>
  88. inline bool
  89. _S_is_basic_char_type(_CharT*)
  90. { return false; }
  91. template <class _CharT>
  92. inline bool
  93. _S_is_one_byte_char_type(_CharT*)
  94. { return false; }
  95. inline bool
  96. _S_is_basic_char_type(char*)
  97. { return true; }
  98. inline bool
  99. _S_is_one_byte_char_type(char*)
  100. { return true; }
  101. inline bool
  102. _S_is_basic_char_type(wchar_t*)
  103. { return true; }
  104. // Store an eos iff _CharT is a basic character type.
  105. // Do not reference _S_eos if it isn't.
  106. template <class _CharT>
  107. inline void
  108. _S_cond_store_eos(_CharT&) { }
  109. inline void
  110. _S_cond_store_eos(char& __c)
  111. { __c = 0; }
  112. inline void
  113. _S_cond_store_eos(wchar_t& __c)
  114. { __c = 0; }
  115. // char_producers are logically functions that generate a section of
  116. // a string. These can be converted to ropes. The resulting rope
  117. // invokes the char_producer on demand. This allows, for example,
  118. // files to be viewed as ropes without reading the entire file.
  119. template <class _CharT>
  120. class char_producer
  121. {
  122. public:
  123. virtual ~char_producer() { }
  124. virtual void
  125. operator()(std::size_t __start_pos, std::size_t __len,
  126. _CharT* __buffer) = 0;
  127. // Buffer should really be an arbitrary output iterator.
  128. // That way we could flatten directly into an ostream, etc.
  129. // This is thoroughly impossible, since iterator types don't
  130. // have runtime descriptions.
  131. };
  132. // Sequence buffers:
  133. //
  134. // Sequence must provide an append operation that appends an
  135. // array to the sequence. Sequence buffers are useful only if
  136. // appending an entire array is cheaper than appending element by element.
  137. // This is true for many string representations.
  138. // This should perhaps inherit from ostream<sequence::value_type>
  139. // and be implemented correspondingly, so that they can be used
  140. // for formatted. For the sake of portability, we don't do this yet.
  141. //
  142. // For now, sequence buffers behave as output iterators. But they also
  143. // behave a little like basic_ostringstream<sequence::value_type> and a
  144. // little like containers.
  145. template<class _Sequence, std::size_t _Buf_sz = 100>
  146. class sequence_buffer
  147. : public std::iterator<std::output_iterator_tag, void, void, void, void>
  148. {
  149. public:
  150. typedef typename _Sequence::value_type value_type;
  151. protected:
  152. _Sequence* _M_prefix;
  153. value_type _M_buffer[_Buf_sz];
  154. std::size_t _M_buf_count;
  155. public:
  156. void
  157. flush()
  158. {
  159. _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
  160. _M_buf_count = 0;
  161. }
  162. ~sequence_buffer()
  163. { flush(); }
  164. sequence_buffer()
  165. : _M_prefix(0), _M_buf_count(0) { }
  166. sequence_buffer(const sequence_buffer& __x)
  167. {
  168. _M_prefix = __x._M_prefix;
  169. _M_buf_count = __x._M_buf_count;
  170. std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
  171. }
  172. sequence_buffer(sequence_buffer& __x)
  173. {
  174. __x.flush();
  175. _M_prefix = __x._M_prefix;
  176. _M_buf_count = 0;
  177. }
  178. sequence_buffer(_Sequence& __s)
  179. : _M_prefix(&__s), _M_buf_count(0) { }
  180. sequence_buffer&
  181. operator=(sequence_buffer& __x)
  182. {
  183. __x.flush();
  184. _M_prefix = __x._M_prefix;
  185. _M_buf_count = 0;
  186. return *this;
  187. }
  188. sequence_buffer&
  189. operator=(const sequence_buffer& __x)
  190. {
  191. _M_prefix = __x._M_prefix;
  192. _M_buf_count = __x._M_buf_count;
  193. std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
  194. return *this;
  195. }
  196. void
  197. push_back(value_type __x)
  198. {
  199. if (_M_buf_count < _Buf_sz)
  200. {
  201. _M_buffer[_M_buf_count] = __x;
  202. ++_M_buf_count;
  203. }
  204. else
  205. {
  206. flush();
  207. _M_buffer[0] = __x;
  208. _M_buf_count = 1;
  209. }
  210. }
  211. void
  212. append(value_type* __s, std::size_t __len)
  213. {
  214. if (__len + _M_buf_count <= _Buf_sz)
  215. {
  216. std::size_t __i = _M_buf_count;
  217. for (std::size_t __j = 0; __j < __len; __i++, __j++)
  218. _M_buffer[__i] = __s[__j];
  219. _M_buf_count += __len;
  220. }
  221. else if (0 == _M_buf_count)
  222. _M_prefix->append(__s, __s + __len);
  223. else
  224. {
  225. flush();
  226. append(__s, __len);
  227. }
  228. }
  229. sequence_buffer&
  230. write(value_type* __s, std::size_t __len)
  231. {
  232. append(__s, __len);
  233. return *this;
  234. }
  235. sequence_buffer&
  236. put(value_type __x)
  237. {
  238. push_back(__x);
  239. return *this;
  240. }
  241. sequence_buffer&
  242. operator=(const value_type& __rhs)
  243. {
  244. push_back(__rhs);
  245. return *this;
  246. }
  247. sequence_buffer&
  248. operator*()
  249. { return *this; }
  250. sequence_buffer&
  251. operator++()
  252. { return *this; }
  253. sequence_buffer
  254. operator++(int)
  255. { return *this; }
  256. };
  257. // The following should be treated as private, at least for now.
  258. template<class _CharT>
  259. class _Rope_char_consumer
  260. {
  261. public:
  262. // If we had member templates, these should not be virtual.
  263. // For now we need to use run-time parametrization where
  264. // compile-time would do. Hence this should all be private
  265. // for now.
  266. // The symmetry with char_producer is accidental and temporary.
  267. virtual ~_Rope_char_consumer() { }
  268. virtual bool
  269. operator()(const _CharT* __buffer, std::size_t __len) = 0;
  270. };
  271. // First a lot of forward declarations. The standard seems to require
  272. // much stricter "declaration before use" than many of the implementations
  273. // that preceded it.
  274. template<class _CharT, class _Alloc = std::allocator<_CharT> >
  275. class rope;
  276. template<class _CharT, class _Alloc>
  277. struct _Rope_RopeConcatenation;
  278. template<class _CharT, class _Alloc>
  279. struct _Rope_RopeLeaf;
  280. template<class _CharT, class _Alloc>
  281. struct _Rope_RopeFunction;
  282. template<class _CharT, class _Alloc>
  283. struct _Rope_RopeSubstring;
  284. template<class _CharT, class _Alloc>
  285. class _Rope_iterator;
  286. template<class _CharT, class _Alloc>
  287. class _Rope_const_iterator;
  288. template<class _CharT, class _Alloc>
  289. class _Rope_char_ref_proxy;
  290. template<class _CharT, class _Alloc>
  291. class _Rope_char_ptr_proxy;
  292. template<class _CharT, class _Alloc>
  293. bool
  294. operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
  295. const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
  296. template<class _CharT, class _Alloc>
  297. _Rope_const_iterator<_CharT, _Alloc>
  298. operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  299. std::ptrdiff_t __n);
  300. template<class _CharT, class _Alloc>
  301. _Rope_const_iterator<_CharT, _Alloc>
  302. operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  303. std::ptrdiff_t __n);
  304. template<class _CharT, class _Alloc>
  305. _Rope_const_iterator<_CharT, _Alloc>
  306. operator+(std::ptrdiff_t __n,
  307. const _Rope_const_iterator<_CharT, _Alloc>& __x);
  308. template<class _CharT, class _Alloc>
  309. bool
  310. operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  311. const _Rope_const_iterator<_CharT, _Alloc>& __y);
  312. template<class _CharT, class _Alloc>
  313. bool
  314. operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  315. const _Rope_const_iterator<_CharT, _Alloc>& __y);
  316. template<class _CharT, class _Alloc>
  317. std::ptrdiff_t
  318. operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  319. const _Rope_const_iterator<_CharT, _Alloc>& __y);
  320. template<class _CharT, class _Alloc>
  321. _Rope_iterator<_CharT, _Alloc>
  322. operator-(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n);
  323. template<class _CharT, class _Alloc>
  324. _Rope_iterator<_CharT, _Alloc>
  325. operator+(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n);
  326. template<class _CharT, class _Alloc>
  327. _Rope_iterator<_CharT, _Alloc>
  328. operator+(std::ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
  329. template<class _CharT, class _Alloc>
  330. bool
  331. operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
  332. const _Rope_iterator<_CharT, _Alloc>& __y);
  333. template<class _CharT, class _Alloc>
  334. bool
  335. operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
  336. const _Rope_iterator<_CharT, _Alloc>& __y);
  337. template<class _CharT, class _Alloc>
  338. std::ptrdiff_t
  339. operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
  340. const _Rope_iterator<_CharT, _Alloc>& __y);
  341. template<class _CharT, class _Alloc>
  342. rope<_CharT, _Alloc>
  343. operator+(const rope<_CharT, _Alloc>& __left,
  344. const rope<_CharT, _Alloc>& __right);
  345. template<class _CharT, class _Alloc>
  346. rope<_CharT, _Alloc>
  347. operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
  348. template<class _CharT, class _Alloc>
  349. rope<_CharT, _Alloc>
  350. operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
  351. // Some helpers, so we can use power on ropes.
  352. // See below for why this isn't local to the implementation.
  353. // This uses a nonstandard refcount convention.
  354. // The result has refcount 0.
  355. template<class _CharT, class _Alloc>
  356. struct _Rope_Concat_fn
  357. : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
  358. rope<_CharT, _Alloc> >
  359. {
  360. rope<_CharT, _Alloc>
  361. operator()(const rope<_CharT, _Alloc>& __x,
  362. const rope<_CharT, _Alloc>& __y)
  363. { return __x + __y; }
  364. };
  365. template <class _CharT, class _Alloc>
  366. inline rope<_CharT, _Alloc>
  367. identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
  368. { return rope<_CharT, _Alloc>(); }
  369. // Class _Refcount_Base provides a type, _RC_t, a data member,
  370. // _M_ref_count, and member functions _M_incr and _M_decr, which perform
  371. // atomic preincrement/predecrement. The constructor initializes
  372. // _M_ref_count.
  373. struct _Refcount_Base
  374. {
  375. // The type _RC_t
  376. typedef std::size_t _RC_t;
  377. // The data member _M_ref_count
  378. _RC_t _M_ref_count;
  379. // Constructor
  380. #ifdef __GTHREAD_MUTEX_INIT
  381. __gthread_mutex_t _M_ref_count_lock = __GTHREAD_MUTEX_INIT;
  382. #else
  383. __gthread_mutex_t _M_ref_count_lock;
  384. #endif
  385. _Refcount_Base(_RC_t __n) : _M_ref_count(__n)
  386. {
  387. #ifndef __GTHREAD_MUTEX_INIT
  388. #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
  389. __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
  390. #else
  391. #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
  392. #endif
  393. #endif
  394. }
  395. #ifndef __GTHREAD_MUTEX_INIT
  396. ~_Refcount_Base()
  397. { __gthread_mutex_destroy(&_M_ref_count_lock); }
  398. #endif
  399. void
  400. _M_incr()
  401. {
  402. __gthread_mutex_lock(&_M_ref_count_lock);
  403. ++_M_ref_count;
  404. __gthread_mutex_unlock(&_M_ref_count_lock);
  405. }
  406. _RC_t
  407. _M_decr()
  408. {
  409. __gthread_mutex_lock(&_M_ref_count_lock);
  410. _RC_t __tmp = --_M_ref_count;
  411. __gthread_mutex_unlock(&_M_ref_count_lock);
  412. return __tmp;
  413. }
  414. };
  415. //
  416. // What follows should really be local to rope. Unfortunately,
  417. // that doesn't work, since it makes it impossible to define generic
  418. // equality on rope iterators. According to the draft standard, the
  419. // template parameters for such an equality operator cannot be inferred
  420. // from the occurrence of a member class as a parameter.
  421. // (SGI compilers in fact allow this, but the __result wouldn't be
  422. // portable.)
  423. // Similarly, some of the static member functions are member functions
  424. // only to avoid polluting the global namespace, and to circumvent
  425. // restrictions on type inference for template functions.
  426. //
  427. //
  428. // The internal data structure for representing a rope. This is
  429. // private to the implementation. A rope is really just a pointer
  430. // to one of these.
  431. //
  432. // A few basic functions for manipulating this data structure
  433. // are members of _RopeRep. Most of the more complex algorithms
  434. // are implemented as rope members.
  435. //
  436. // Some of the static member functions of _RopeRep have identically
  437. // named functions in rope that simply invoke the _RopeRep versions.
  438. #define __ROPE_DEFINE_ALLOCS(__a) \
  439. __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
  440. typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
  441. __ROPE_DEFINE_ALLOC(__C,_C) \
  442. typedef _Rope_RopeLeaf<_CharT,__a> __L; \
  443. __ROPE_DEFINE_ALLOC(__L,_L) \
  444. typedef _Rope_RopeFunction<_CharT,__a> __F; \
  445. __ROPE_DEFINE_ALLOC(__F,_F) \
  446. typedef _Rope_RopeSubstring<_CharT,__a> __S; \
  447. __ROPE_DEFINE_ALLOC(__S,_S)
  448. // Internal rope nodes potentially store a copy of the allocator
  449. // instance used to allocate them. This is mostly redundant.
  450. // But the alternative would be to pass allocator instances around
  451. // in some form to nearly all internal functions, since any pointer
  452. // assignment may result in a zero reference count and thus require
  453. // deallocation.
  454. #define __STATIC_IF_SGI_ALLOC /* not static */
  455. template <class _CharT, class _Alloc>
  456. struct _Rope_rep_base
  457. : public _Alloc
  458. {
  459. typedef std::size_t size_type;
  460. typedef _Alloc allocator_type;
  461. allocator_type
  462. get_allocator() const
  463. { return *static_cast<const _Alloc*>(this); }
  464. allocator_type&
  465. _M_get_allocator()
  466. { return *static_cast<_Alloc*>(this); }
  467. const allocator_type&
  468. _M_get_allocator() const
  469. { return *static_cast<const _Alloc*>(this); }
  470. _Rope_rep_base(size_type __size, const allocator_type&)
  471. : _M_size(__size) { }
  472. size_type _M_size;
  473. # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
  474. typedef typename \
  475. __alloc_traits<_Alloc>::template rebind<_Tp>::other __name##Alloc; \
  476. static _Tp* __name##_allocate(size_type __n) \
  477. { return __name##Alloc().allocate(__n); } \
  478. static void __name##_deallocate(_Tp *__p, size_type __n) \
  479. { __name##Alloc().deallocate(__p, __n); }
  480. __ROPE_DEFINE_ALLOCS(_Alloc)
  481. # undef __ROPE_DEFINE_ALLOC
  482. };
  483. template<class _CharT, class _Alloc>
  484. struct _Rope_RopeRep
  485. : public _Rope_rep_base<_CharT, _Alloc>
  486. # ifndef __GC
  487. , _Refcount_Base
  488. # endif
  489. {
  490. public:
  491. __detail::_Tag _M_tag:8;
  492. bool _M_is_balanced:8;
  493. unsigned char _M_depth;
  494. __GC_CONST _CharT* _M_c_string;
  495. #ifdef __GTHREAD_MUTEX_INIT
  496. __gthread_mutex_t _M_c_string_lock = __GTHREAD_MUTEX_INIT;
  497. #else
  498. __gthread_mutex_t _M_c_string_lock;
  499. #endif
  500. /* Flattened version of string, if needed. */
  501. /* typically 0. */
  502. /* If it's not 0, then the memory is owned */
  503. /* by this node. */
  504. /* In the case of a leaf, this may point to */
  505. /* the same memory as the data field. */
  506. typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
  507. allocator_type;
  508. typedef std::size_t size_type;
  509. using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
  510. using _Rope_rep_base<_CharT, _Alloc>::_M_get_allocator;
  511. _Rope_RopeRep(__detail::_Tag __t, int __d, bool __b, size_type __size,
  512. const allocator_type& __a)
  513. : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
  514. #ifndef __GC
  515. _Refcount_Base(1),
  516. #endif
  517. _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
  518. #ifdef __GTHREAD_MUTEX_INIT
  519. { }
  520. #else
  521. { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
  522. ~_Rope_RopeRep()
  523. { __gthread_mutex_destroy (&_M_c_string_lock); }
  524. #endif
  525. #ifdef __GC
  526. void
  527. _M_incr () { }
  528. #endif
  529. static void
  530. _S_free_string(__GC_CONST _CharT*, size_type __len,
  531. allocator_type& __a);
  532. #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
  533. // Deallocate data section of a leaf.
  534. // This shouldn't be a member function.
  535. // But its hard to do anything else at the
  536. // moment, because it's templatized w.r.t.
  537. // an allocator.
  538. // Does nothing if __GC is defined.
  539. #ifndef __GC
  540. void _M_free_c_string();
  541. void _M_free_tree();
  542. // Deallocate t. Assumes t is not 0.
  543. void
  544. _M_unref_nonnil()
  545. {
  546. if (0 == _M_decr())
  547. _M_free_tree();
  548. }
  549. void
  550. _M_ref_nonnil()
  551. { _M_incr(); }
  552. static void
  553. _S_unref(_Rope_RopeRep* __t)
  554. {
  555. if (0 != __t)
  556. __t->_M_unref_nonnil();
  557. }
  558. static void
  559. _S_ref(_Rope_RopeRep* __t)
  560. {
  561. if (0 != __t)
  562. __t->_M_incr();
  563. }
  564. static void
  565. _S_free_if_unref(_Rope_RopeRep* __t)
  566. {
  567. if (0 != __t && 0 == __t->_M_ref_count)
  568. __t->_M_free_tree();
  569. }
  570. # else /* __GC */
  571. void _M_unref_nonnil() { }
  572. void _M_ref_nonnil() { }
  573. static void _S_unref(_Rope_RopeRep*) { }
  574. static void _S_ref(_Rope_RopeRep*) { }
  575. static void _S_free_if_unref(_Rope_RopeRep*) { }
  576. # endif
  577. protected:
  578. _Rope_RopeRep&
  579. operator=(const _Rope_RopeRep&);
  580. _Rope_RopeRep(const _Rope_RopeRep&);
  581. };
  582. template<class _CharT, class _Alloc>
  583. struct _Rope_RopeLeaf
  584. : public _Rope_RopeRep<_CharT, _Alloc>
  585. {
  586. typedef std::size_t size_type;
  587. public:
  588. // Apparently needed by VC++
  589. // The data fields of leaves are allocated with some
  590. // extra space, to accommodate future growth and for basic
  591. // character types, to hold a trailing eos character.
  592. enum { _S_alloc_granularity = 8 };
  593. static size_type
  594. _S_rounded_up_size(size_type __n)
  595. {
  596. size_type __size_with_eos;
  597. if (_S_is_basic_char_type((_CharT*)0))
  598. __size_with_eos = __n + 1;
  599. else
  600. __size_with_eos = __n;
  601. #ifdef __GC
  602. return __size_with_eos;
  603. #else
  604. // Allow slop for in-place expansion.
  605. return ((__size_with_eos + size_type(_S_alloc_granularity) - 1)
  606. &~ (size_type(_S_alloc_granularity) - 1));
  607. #endif
  608. }
  609. __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
  610. /* The allocated size is */
  611. /* _S_rounded_up_size(size), except */
  612. /* in the GC case, in which it */
  613. /* doesn't matter. */
  614. typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
  615. allocator_type;
  616. _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_type __size,
  617. const allocator_type& __a)
  618. : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_leaf, 0, true,
  619. __size, __a), _M_data(__d)
  620. {
  621. if (_S_is_basic_char_type((_CharT *)0))
  622. {
  623. // already eos terminated.
  624. this->_M_c_string = __d;
  625. }
  626. }
  627. // The constructor assumes that d has been allocated with
  628. // the proper allocator and the properly padded size.
  629. // In contrast, the destructor deallocates the data:
  630. #ifndef __GC
  631. ~_Rope_RopeLeaf() throw()
  632. {
  633. if (_M_data != this->_M_c_string)
  634. this->_M_free_c_string();
  635. this->__STL_FREE_STRING(_M_data, this->_M_size, this->_M_get_allocator());
  636. }
  637. #endif
  638. protected:
  639. _Rope_RopeLeaf&
  640. operator=(const _Rope_RopeLeaf&);
  641. _Rope_RopeLeaf(const _Rope_RopeLeaf&);
  642. };
  643. template<class _CharT, class _Alloc>
  644. struct _Rope_RopeConcatenation
  645. : public _Rope_RopeRep<_CharT, _Alloc>
  646. {
  647. public:
  648. _Rope_RopeRep<_CharT, _Alloc>* _M_left;
  649. _Rope_RopeRep<_CharT, _Alloc>* _M_right;
  650. typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
  651. allocator_type;
  652. _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
  653. _Rope_RopeRep<_CharT, _Alloc>* __r,
  654. const allocator_type& __a)
  655. : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_concat,
  656. std::max(__l->_M_depth,
  657. __r->_M_depth) + 1,
  658. false,
  659. __l->_M_size + __r->_M_size, __a),
  660. _M_left(__l), _M_right(__r)
  661. { }
  662. #ifndef __GC
  663. ~_Rope_RopeConcatenation() throw()
  664. {
  665. this->_M_free_c_string();
  666. _M_left->_M_unref_nonnil();
  667. _M_right->_M_unref_nonnil();
  668. }
  669. #endif
  670. protected:
  671. _Rope_RopeConcatenation&
  672. operator=(const _Rope_RopeConcatenation&);
  673. _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
  674. };
  675. template<class _CharT, class _Alloc>
  676. struct _Rope_RopeFunction
  677. : public _Rope_RopeRep<_CharT, _Alloc>
  678. {
  679. public:
  680. char_producer<_CharT>* _M_fn;
  681. #ifndef __GC
  682. bool _M_delete_when_done; // Char_producer is owned by the
  683. // rope and should be explicitly
  684. // deleted when the rope becomes
  685. // inaccessible.
  686. #else
  687. // In the GC case, we either register the rope for
  688. // finalization, or not. Thus the field is unnecessary;
  689. // the information is stored in the collector data structures.
  690. // We do need a finalization procedure to be invoked by the
  691. // collector.
  692. static void
  693. _S_fn_finalization_proc(void * __tree, void *)
  694. { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
  695. #endif
  696. typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
  697. allocator_type;
  698. _Rope_RopeFunction(char_producer<_CharT>* __f, std::size_t __size,
  699. bool __d, const allocator_type& __a)
  700. : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_function, 0, true, __size, __a)
  701. , _M_fn(__f)
  702. #ifndef __GC
  703. , _M_delete_when_done(__d)
  704. #endif
  705. {
  706. #ifdef __GC
  707. if (__d)
  708. {
  709. GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
  710. _S_fn_finalization_proc, 0, 0, 0);
  711. }
  712. #endif
  713. }
  714. #ifndef __GC
  715. ~_Rope_RopeFunction() throw()
  716. {
  717. this->_M_free_c_string();
  718. if (_M_delete_when_done)
  719. delete _M_fn;
  720. }
  721. # endif
  722. protected:
  723. _Rope_RopeFunction&
  724. operator=(const _Rope_RopeFunction&);
  725. _Rope_RopeFunction(const _Rope_RopeFunction&);
  726. };
  727. // Substring results are usually represented using just
  728. // concatenation nodes. But in the case of very long flat ropes
  729. // or ropes with a functional representation that isn't practical.
  730. // In that case, we represent the __result as a special case of
  731. // RopeFunction, whose char_producer points back to the rope itself.
  732. // In all cases except repeated substring operations and
  733. // deallocation, we treat the __result as a RopeFunction.
  734. template<class _CharT, class _Alloc>
  735. struct _Rope_RopeSubstring
  736. : public _Rope_RopeFunction<_CharT, _Alloc>,
  737. public char_producer<_CharT>
  738. {
  739. typedef std::size_t size_type;
  740. public:
  741. // XXX this whole class should be rewritten.
  742. _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
  743. size_type _M_start;
  744. virtual void
  745. operator()(size_type __start_pos, size_type __req_len,
  746. _CharT* __buffer)
  747. {
  748. switch(_M_base->_M_tag)
  749. {
  750. case __detail::_S_function:
  751. case __detail::_S_substringfn:
  752. {
  753. char_producer<_CharT>* __fn =
  754. ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
  755. (*__fn)(__start_pos + _M_start, __req_len, __buffer);
  756. }
  757. break;
  758. case __detail::_S_leaf:
  759. {
  760. __GC_CONST _CharT* __s =
  761. ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
  762. uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
  763. __buffer);
  764. }
  765. break;
  766. default:
  767. break;
  768. }
  769. }
  770. typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
  771. allocator_type;
  772. _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_type __s,
  773. size_type __l, const allocator_type& __a)
  774. : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
  775. char_producer<_CharT>(), _M_base(__b), _M_start(__s)
  776. {
  777. #ifndef __GC
  778. _M_base->_M_ref_nonnil();
  779. #endif
  780. this->_M_tag = __detail::_S_substringfn;
  781. }
  782. virtual ~_Rope_RopeSubstring() throw()
  783. {
  784. #ifndef __GC
  785. _M_base->_M_unref_nonnil();
  786. // _M_free_c_string(); -- done by parent class
  787. #endif
  788. }
  789. };
  790. // Self-destructing pointers to Rope_rep.
  791. // These are not conventional smart pointers. Their
  792. // only purpose in life is to ensure that unref is called
  793. // on the pointer either at normal exit or if an exception
  794. // is raised. It is the caller's responsibility to
  795. // adjust reference counts when these pointers are initialized
  796. // or assigned to. (This convention significantly reduces
  797. // the number of potentially expensive reference count
  798. // updates.)
  799. #ifndef __GC
  800. template<class _CharT, class _Alloc>
  801. struct _Rope_self_destruct_ptr
  802. {
  803. _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
  804. ~_Rope_self_destruct_ptr()
  805. { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
  806. #if __cpp_exceptions
  807. _Rope_self_destruct_ptr() : _M_ptr(0) { }
  808. #else
  809. _Rope_self_destruct_ptr() { }
  810. #endif
  811. _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
  812. : _M_ptr(__p) { }
  813. _Rope_RopeRep<_CharT, _Alloc>&
  814. operator*()
  815. { return *_M_ptr; }
  816. _Rope_RopeRep<_CharT, _Alloc>*
  817. operator->()
  818. { return _M_ptr; }
  819. operator _Rope_RopeRep<_CharT, _Alloc>*()
  820. { return _M_ptr; }
  821. _Rope_self_destruct_ptr&
  822. operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
  823. { _M_ptr = __x; return *this; }
  824. };
  825. #endif
  826. // Dereferencing a nonconst iterator has to return something
  827. // that behaves almost like a reference. It's not possible to
  828. // return an actual reference since assignment requires extra
  829. // work. And we would get into the same problems as with the
  830. // CD2 version of basic_string.
  831. template<class _CharT, class _Alloc>
  832. class _Rope_char_ref_proxy
  833. {
  834. friend class rope<_CharT, _Alloc>;
  835. friend class _Rope_iterator<_CharT, _Alloc>;
  836. friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
  837. #ifdef __GC
  838. typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
  839. #else
  840. typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
  841. #endif
  842. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  843. typedef rope<_CharT, _Alloc> _My_rope;
  844. std::size_t _M_pos;
  845. _CharT _M_current;
  846. bool _M_current_valid;
  847. _My_rope* _M_root; // The whole rope.
  848. public:
  849. _Rope_char_ref_proxy(_My_rope* __r, std::size_t __p)
  850. : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) { }
  851. _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
  852. : _M_pos(__x._M_pos), _M_current(__x._M_current),
  853. _M_current_valid(false), _M_root(__x._M_root) { }
  854. // Don't preserve cache if the reference can outlive the
  855. // expression. We claim that's not possible without calling
  856. // a copy constructor or generating reference to a proxy
  857. // reference. We declare the latter to have undefined semantics.
  858. _Rope_char_ref_proxy(_My_rope* __r, std::size_t __p, _CharT __c)
  859. : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) { }
  860. inline operator _CharT () const;
  861. _Rope_char_ref_proxy&
  862. operator=(_CharT __c);
  863. _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
  864. _Rope_char_ref_proxy&
  865. operator=(const _Rope_char_ref_proxy& __c)
  866. { return operator=((_CharT)__c); }
  867. };
  868. template<class _CharT, class __Alloc>
  869. inline void
  870. swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
  871. _Rope_char_ref_proxy <_CharT, __Alloc > __b)
  872. {
  873. _CharT __tmp = __a;
  874. __a = __b;
  875. __b = __tmp;
  876. }
  877. template<class _CharT, class _Alloc>
  878. class _Rope_char_ptr_proxy
  879. {
  880. // XXX this class should be rewritten.
  881. friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
  882. std::size_t _M_pos;
  883. rope<_CharT,_Alloc>* _M_root; // The whole rope.
  884. public:
  885. _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
  886. : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
  887. _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
  888. : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
  889. _Rope_char_ptr_proxy() { }
  890. _Rope_char_ptr_proxy(_CharT* __x)
  891. : _M_root(0), _M_pos(0) { }
  892. _Rope_char_ptr_proxy&
  893. operator=(const _Rope_char_ptr_proxy& __x)
  894. {
  895. _M_pos = __x._M_pos;
  896. _M_root = __x._M_root;
  897. return *this;
  898. }
  899. template<class _CharT2, class _Alloc2>
  900. friend bool
  901. operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
  902. const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
  903. _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
  904. { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
  905. };
  906. // Rope iterators:
  907. // Unlike in the C version, we cache only part of the stack
  908. // for rope iterators, since they must be efficiently copyable.
  909. // When we run out of cache, we have to reconstruct the iterator
  910. // value.
  911. // Pointers from iterators are not included in reference counts.
  912. // Iterators are assumed to be thread private. Ropes can
  913. // be shared.
  914. template<class _CharT, class _Alloc>
  915. class _Rope_iterator_base
  916. : public std::iterator<std::random_access_iterator_tag, _CharT>
  917. {
  918. friend class rope<_CharT, _Alloc>;
  919. public:
  920. typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
  921. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  922. // Borland doesn't want this to be protected.
  923. protected:
  924. enum { _S_path_cache_len = 4 }; // Must be <= 9.
  925. enum { _S_iterator_buf_len = 15 };
  926. std::size_t _M_current_pos;
  927. _RopeRep* _M_root; // The whole rope.
  928. std::size_t _M_leaf_pos; // Starting position for current leaf
  929. __GC_CONST _CharT* _M_buf_start;
  930. // Buffer possibly
  931. // containing current char.
  932. __GC_CONST _CharT* _M_buf_ptr;
  933. // Pointer to current char in buffer.
  934. // != 0 ==> buffer valid.
  935. __GC_CONST _CharT* _M_buf_end;
  936. // One past __last valid char in buffer.
  937. // What follows is the path cache. We go out of our
  938. // way to make this compact.
  939. // Path_end contains the bottom section of the path from
  940. // the root to the current leaf.
  941. const _RopeRep* _M_path_end[_S_path_cache_len];
  942. int _M_leaf_index; // Last valid __pos in path_end;
  943. // _M_path_end[0] ... _M_path_end[leaf_index-1]
  944. // point to concatenation nodes.
  945. unsigned char _M_path_directions;
  946. // (path_directions >> __i) & 1 is 1
  947. // iff we got from _M_path_end[leaf_index - __i - 1]
  948. // to _M_path_end[leaf_index - __i] by going to the
  949. // __right. Assumes path_cache_len <= 9.
  950. _CharT _M_tmp_buf[_S_iterator_buf_len];
  951. // Short buffer for surrounding chars.
  952. // This is useful primarily for
  953. // RopeFunctions. We put the buffer
  954. // here to avoid locking in the
  955. // multithreaded case.
  956. // The cached path is generally assumed to be valid
  957. // only if the buffer is valid.
  958. static void _S_setbuf(_Rope_iterator_base& __x);
  959. // Set buffer contents given
  960. // path cache.
  961. static void _S_setcache(_Rope_iterator_base& __x);
  962. // Set buffer contents and
  963. // path cache.
  964. static void _S_setcache_for_incr(_Rope_iterator_base& __x);
  965. // As above, but assumes path
  966. // cache is valid for previous posn.
  967. _Rope_iterator_base() { }
  968. _Rope_iterator_base(_RopeRep* __root, std::size_t __pos)
  969. : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) { }
  970. void _M_incr(std::size_t __n);
  971. void _M_decr(std::size_t __n);
  972. public:
  973. std::size_t
  974. index() const
  975. { return _M_current_pos; }
  976. _Rope_iterator_base(const _Rope_iterator_base& __x)
  977. {
  978. if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
  979. *this = __x;
  980. else
  981. {
  982. _M_current_pos = __x._M_current_pos;
  983. _M_root = __x._M_root;
  984. _M_buf_ptr = 0;
  985. }
  986. }
  987. };
  988. template<class _CharT, class _Alloc>
  989. class _Rope_iterator;
  990. template<class _CharT, class _Alloc>
  991. class _Rope_const_iterator
  992. : public _Rope_iterator_base<_CharT, _Alloc>
  993. {
  994. friend class rope<_CharT, _Alloc>;
  995. protected:
  996. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  997. // The one from the base class may not be directly visible.
  998. _Rope_const_iterator(const _RopeRep* __root, std::size_t __pos)
  999. : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
  1000. __pos)
  1001. // Only nonconst iterators modify root ref count
  1002. { }
  1003. public:
  1004. typedef _CharT reference; // Really a value. Returning a reference
  1005. // Would be a mess, since it would have
  1006. // to be included in refcount.
  1007. typedef const _CharT* pointer;
  1008. public:
  1009. _Rope_const_iterator() { }
  1010. _Rope_const_iterator(const _Rope_const_iterator& __x)
  1011. : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
  1012. _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
  1013. _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, std::size_t __pos)
  1014. : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
  1015. _Rope_const_iterator&
  1016. operator=(const _Rope_const_iterator& __x)
  1017. {
  1018. if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
  1019. *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
  1020. else
  1021. {
  1022. this->_M_current_pos = __x._M_current_pos;
  1023. this->_M_root = __x._M_root;
  1024. this->_M_buf_ptr = 0;
  1025. }
  1026. return(*this);
  1027. }
  1028. reference
  1029. operator*()
  1030. {
  1031. if (0 == this->_M_buf_ptr)
  1032. this->_S_setcache(*this);
  1033. return *this->_M_buf_ptr;
  1034. }
  1035. // Without this const version, Rope iterators do not meet the
  1036. // requirements of an Input Iterator.
  1037. reference
  1038. operator*() const
  1039. {
  1040. return *const_cast<_Rope_const_iterator&>(*this);
  1041. }
  1042. _Rope_const_iterator&
  1043. operator++()
  1044. {
  1045. __GC_CONST _CharT* __next;
  1046. if (0 != this->_M_buf_ptr
  1047. && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
  1048. {
  1049. this->_M_buf_ptr = __next;
  1050. ++this->_M_current_pos;
  1051. }
  1052. else
  1053. this->_M_incr(1);
  1054. return *this;
  1055. }
  1056. _Rope_const_iterator&
  1057. operator+=(std::ptrdiff_t __n)
  1058. {
  1059. if (__n >= 0)
  1060. this->_M_incr(__n);
  1061. else
  1062. this->_M_decr(-__n);
  1063. return *this;
  1064. }
  1065. _Rope_const_iterator&
  1066. operator--()
  1067. {
  1068. this->_M_decr(1);
  1069. return *this;
  1070. }
  1071. _Rope_const_iterator&
  1072. operator-=(std::ptrdiff_t __n)
  1073. {
  1074. if (__n >= 0)
  1075. this->_M_decr(__n);
  1076. else
  1077. this->_M_incr(-__n);
  1078. return *this;
  1079. }
  1080. _Rope_const_iterator
  1081. operator++(int)
  1082. {
  1083. std::size_t __old_pos = this->_M_current_pos;
  1084. this->_M_incr(1);
  1085. return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
  1086. // This makes a subsequent dereference expensive.
  1087. // Perhaps we should instead copy the iterator
  1088. // if it has a valid cache?
  1089. }
  1090. _Rope_const_iterator
  1091. operator--(int)
  1092. {
  1093. std::size_t __old_pos = this->_M_current_pos;
  1094. this->_M_decr(1);
  1095. return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
  1096. }
  1097. template<class _CharT2, class _Alloc2>
  1098. friend _Rope_const_iterator<_CharT2, _Alloc2>
  1099. operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
  1100. std::ptrdiff_t __n);
  1101. template<class _CharT2, class _Alloc2>
  1102. friend _Rope_const_iterator<_CharT2, _Alloc2>
  1103. operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
  1104. std::ptrdiff_t __n);
  1105. template<class _CharT2, class _Alloc2>
  1106. friend _Rope_const_iterator<_CharT2, _Alloc2>
  1107. operator+(std::ptrdiff_t __n,
  1108. const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
  1109. reference
  1110. operator[](std::size_t __n)
  1111. { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
  1112. this->_M_current_pos + __n); }
  1113. template<class _CharT2, class _Alloc2>
  1114. friend bool
  1115. operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
  1116. const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
  1117. template<class _CharT2, class _Alloc2>
  1118. friend bool
  1119. operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
  1120. const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
  1121. template<class _CharT2, class _Alloc2>
  1122. friend std::ptrdiff_t
  1123. operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
  1124. const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
  1125. };
  1126. template<class _CharT, class _Alloc>
  1127. class _Rope_iterator
  1128. : public _Rope_iterator_base<_CharT, _Alloc>
  1129. {
  1130. friend class rope<_CharT, _Alloc>;
  1131. protected:
  1132. typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
  1133. rope<_CharT, _Alloc>* _M_root_rope;
  1134. // root is treated as a cached version of this, and is used to
  1135. // detect changes to the underlying rope.
  1136. // Root is included in the reference count. This is necessary
  1137. // so that we can detect changes reliably. Unfortunately, it
  1138. // requires careful bookkeeping for the nonGC case.
  1139. _Rope_iterator(rope<_CharT, _Alloc>* __r, std::size_t __pos)
  1140. : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
  1141. _M_root_rope(__r)
  1142. { _RopeRep::_S_ref(this->_M_root);
  1143. if (!(__r -> empty()))
  1144. this->_S_setcache(*this);
  1145. }
  1146. void _M_check();
  1147. public:
  1148. typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
  1149. typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
  1150. rope<_CharT, _Alloc>&
  1151. container()
  1152. { return *_M_root_rope; }
  1153. _Rope_iterator()
  1154. {
  1155. this->_M_root = 0; // Needed for reference counting.
  1156. }
  1157. _Rope_iterator(const _Rope_iterator& __x)
  1158. : _Rope_iterator_base<_CharT, _Alloc>(__x)
  1159. {
  1160. _M_root_rope = __x._M_root_rope;
  1161. _RopeRep::_S_ref(this->_M_root);
  1162. }
  1163. _Rope_iterator(rope<_CharT, _Alloc>& __r, std::size_t __pos);
  1164. ~_Rope_iterator()
  1165. { _RopeRep::_S_unref(this->_M_root); }
  1166. _Rope_iterator&
  1167. operator=(const _Rope_iterator& __x)
  1168. {
  1169. _RopeRep* __old = this->_M_root;
  1170. _RopeRep::_S_ref(__x._M_root);
  1171. if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
  1172. {
  1173. _M_root_rope = __x._M_root_rope;
  1174. *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
  1175. }
  1176. else
  1177. {
  1178. this->_M_current_pos = __x._M_current_pos;
  1179. this->_M_root = __x._M_root;
  1180. _M_root_rope = __x._M_root_rope;
  1181. this->_M_buf_ptr = 0;
  1182. }
  1183. _RopeRep::_S_unref(__old);
  1184. return(*this);
  1185. }
  1186. reference
  1187. operator*()
  1188. {
  1189. _M_check();
  1190. if (0 == this->_M_buf_ptr)
  1191. return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
  1192. this->_M_current_pos);
  1193. else
  1194. return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
  1195. this->_M_current_pos,
  1196. *this->_M_buf_ptr);
  1197. }
  1198. // See above comment.
  1199. reference
  1200. operator*() const
  1201. {
  1202. return *const_cast<_Rope_iterator&>(*this);
  1203. }
  1204. _Rope_iterator&
  1205. operator++()
  1206. {
  1207. this->_M_incr(1);
  1208. return *this;
  1209. }
  1210. _Rope_iterator&
  1211. operator+=(std::ptrdiff_t __n)
  1212. {
  1213. if (__n >= 0)
  1214. this->_M_incr(__n);
  1215. else
  1216. this->_M_decr(-__n);
  1217. return *this;
  1218. }
  1219. _Rope_iterator&
  1220. operator--()
  1221. {
  1222. this->_M_decr(1);
  1223. return *this;
  1224. }
  1225. _Rope_iterator&
  1226. operator-=(std::ptrdiff_t __n)
  1227. {
  1228. if (__n >= 0)
  1229. this->_M_decr(__n);
  1230. else
  1231. this->_M_incr(-__n);
  1232. return *this;
  1233. }
  1234. _Rope_iterator
  1235. operator++(int)
  1236. {
  1237. std::size_t __old_pos = this->_M_current_pos;
  1238. this->_M_incr(1);
  1239. return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
  1240. }
  1241. _Rope_iterator
  1242. operator--(int)
  1243. {
  1244. std::size_t __old_pos = this->_M_current_pos;
  1245. this->_M_decr(1);
  1246. return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
  1247. }
  1248. reference
  1249. operator[](std::ptrdiff_t __n)
  1250. { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
  1251. this->_M_current_pos
  1252. + __n); }
  1253. template<class _CharT2, class _Alloc2>
  1254. friend bool
  1255. operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
  1256. const _Rope_iterator<_CharT2, _Alloc2>& __y);
  1257. template<class _CharT2, class _Alloc2>
  1258. friend bool
  1259. operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
  1260. const _Rope_iterator<_CharT2, _Alloc2>& __y);
  1261. template<class _CharT2, class _Alloc2>
  1262. friend std::ptrdiff_t
  1263. operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
  1264. const _Rope_iterator<_CharT2, _Alloc2>& __y);
  1265. template<class _CharT2, class _Alloc2>
  1266. friend _Rope_iterator<_CharT2, _Alloc2>
  1267. operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
  1268. std::ptrdiff_t __n);
  1269. template<class _CharT2, class _Alloc2>
  1270. friend _Rope_iterator<_CharT2, _Alloc2>
  1271. operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x,
  1272. std::ptrdiff_t __n);
  1273. template<class _CharT2, class _Alloc2>
  1274. friend _Rope_iterator<_CharT2, _Alloc2>
  1275. operator+(std::ptrdiff_t __n,
  1276. const _Rope_iterator<_CharT2, _Alloc2>& __x);
  1277. };
  1278. template <class _CharT, class _Alloc>
  1279. struct _Rope_base
  1280. : public _Alloc
  1281. {
  1282. typedef _Alloc allocator_type;
  1283. allocator_type
  1284. get_allocator() const
  1285. { return *static_cast<const _Alloc*>(this); }
  1286. allocator_type&
  1287. _M_get_allocator()
  1288. { return *static_cast<_Alloc*>(this); }
  1289. const allocator_type&
  1290. _M_get_allocator() const
  1291. { return *static_cast<const _Alloc*>(this); }
  1292. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  1293. // The one in _Base may not be visible due to template rules.
  1294. _Rope_base(_RopeRep* __t, const allocator_type&)
  1295. : _M_tree_ptr(__t) { }
  1296. _Rope_base(const allocator_type&) { }
  1297. // The only data member of a rope:
  1298. _RopeRep *_M_tree_ptr;
  1299. #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
  1300. typedef typename \
  1301. __alloc_traits<_Alloc>::template rebind<_Tp>::other __name##Alloc; \
  1302. static _Tp* __name##_allocate(std::size_t __n) \
  1303. { return __name##Alloc().allocate(__n); } \
  1304. static void __name##_deallocate(_Tp *__p, std::size_t __n) \
  1305. { __name##Alloc().deallocate(__p, __n); }
  1306. __ROPE_DEFINE_ALLOCS(_Alloc)
  1307. #undef __ROPE_DEFINE_ALLOC
  1308. protected:
  1309. _Rope_base&
  1310. operator=(const _Rope_base&);
  1311. _Rope_base(const _Rope_base&);
  1312. };
  1313. /**
  1314. * This is an SGI extension.
  1315. * @ingroup SGIextensions
  1316. * @doctodo
  1317. */
  1318. template <class _CharT, class _Alloc>
  1319. class rope : public _Rope_base<_CharT, _Alloc>
  1320. {
  1321. public:
  1322. typedef _CharT value_type;
  1323. typedef std::ptrdiff_t difference_type;
  1324. typedef std::size_t size_type;
  1325. typedef _CharT const_reference;
  1326. typedef const _CharT* const_pointer;
  1327. typedef _Rope_iterator<_CharT, _Alloc> iterator;
  1328. typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
  1329. typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
  1330. typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
  1331. friend class _Rope_iterator<_CharT, _Alloc>;
  1332. friend class _Rope_const_iterator<_CharT, _Alloc>;
  1333. friend struct _Rope_RopeRep<_CharT, _Alloc>;
  1334. friend class _Rope_iterator_base<_CharT, _Alloc>;
  1335. friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
  1336. friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
  1337. friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
  1338. protected:
  1339. typedef _Rope_base<_CharT, _Alloc> _Base;
  1340. typedef typename _Base::allocator_type allocator_type;
  1341. using _Base::_M_tree_ptr;
  1342. using _Base::get_allocator;
  1343. using _Base::_M_get_allocator;
  1344. typedef __GC_CONST _CharT* _Cstrptr;
  1345. static _CharT _S_empty_c_str[1];
  1346. static bool
  1347. _S_is0(_CharT __c)
  1348. { return __c == _S_eos((_CharT*)0); }
  1349. enum { _S_copy_max = 23 };
  1350. // For strings shorter than _S_copy_max, we copy to
  1351. // concatenate.
  1352. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  1353. typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
  1354. typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
  1355. typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
  1356. typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
  1357. // Retrieve a character at the indicated position.
  1358. static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
  1359. #ifndef __GC
  1360. // Obtain a pointer to the character at the indicated position.
  1361. // The pointer can be used to change the character.
  1362. // If such a pointer cannot be produced, as is frequently the
  1363. // case, 0 is returned instead.
  1364. // (Returns nonzero only if all nodes in the path have a refcount
  1365. // of 1.)
  1366. static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
  1367. #endif
  1368. static bool
  1369. _S_apply_to_pieces(// should be template parameter
  1370. _Rope_char_consumer<_CharT>& __c,
  1371. const _RopeRep* __r,
  1372. size_type __begin, size_type __end);
  1373. // begin and end are assumed to be in range.
  1374. #ifndef __GC
  1375. static void
  1376. _S_unref(_RopeRep* __t)
  1377. { _RopeRep::_S_unref(__t); }
  1378. static void
  1379. _S_ref(_RopeRep* __t)
  1380. { _RopeRep::_S_ref(__t); }
  1381. #else /* __GC */
  1382. static void _S_unref(_RopeRep*) { }
  1383. static void _S_ref(_RopeRep*) { }
  1384. #endif
  1385. #ifdef __GC
  1386. typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
  1387. #else
  1388. typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
  1389. #endif
  1390. // _Result is counted in refcount.
  1391. static _RopeRep* _S_substring(_RopeRep* __base,
  1392. size_type __start, size_type __endp1);
  1393. static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
  1394. const _CharT* __iter,
  1395. size_type __slen,
  1396. allocator_type& __a);
  1397. // Concatenate rope and char ptr, copying __iter.
  1398. // Should really take an arbitrary iterator.
  1399. // Result is counted in refcount.
  1400. static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
  1401. const _CharT* __iter,
  1402. size_type __slen,
  1403. allocator_type& __a)
  1404. // As above, but one reference to __r is about to be
  1405. // destroyed. Thus the pieces may be recycled if all
  1406. // relevant reference counts are 1.
  1407. #ifdef __GC
  1408. // We can't really do anything since refcounts are unavailable.
  1409. { return _S_concat_char_iter(__r, __iter, __slen, __a); }
  1410. #else
  1411. ;
  1412. #endif
  1413. static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
  1414. // General concatenation on _RopeRep. _Result
  1415. // has refcount of 1. Adjusts argument refcounts.
  1416. public:
  1417. void
  1418. apply_to_pieces(size_type __begin, size_type __end,
  1419. _Rope_char_consumer<_CharT>& __c) const
  1420. { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
  1421. protected:
  1422. static size_type
  1423. _S_rounded_up_size(size_type __n)
  1424. { return _RopeLeaf::_S_rounded_up_size(__n); }
  1425. static size_type
  1426. _S_allocated_capacity(size_type __n)
  1427. {
  1428. if (_S_is_basic_char_type((_CharT*)0))
  1429. return _S_rounded_up_size(__n) - 1;
  1430. else
  1431. return _S_rounded_up_size(__n);
  1432. }
  1433. // Allocate and construct a RopeLeaf using the supplied allocator
  1434. // Takes ownership of s instead of copying.
  1435. static _RopeLeaf*
  1436. _S_new_RopeLeaf(__GC_CONST _CharT *__s,
  1437. size_type __size, allocator_type& __a)
  1438. {
  1439. _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
  1440. return new(__space) _RopeLeaf(__s, __size, __a);
  1441. }
  1442. static _RopeConcatenation*
  1443. _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
  1444. allocator_type& __a)
  1445. {
  1446. _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
  1447. return new(__space) _RopeConcatenation(__left, __right, __a);
  1448. }
  1449. static _RopeFunction*
  1450. _S_new_RopeFunction(char_producer<_CharT>* __f,
  1451. size_type __size, bool __d, allocator_type& __a)
  1452. {
  1453. _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
  1454. return new(__space) _RopeFunction(__f, __size, __d, __a);
  1455. }
  1456. static _RopeSubstring*
  1457. _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_type __s,
  1458. size_type __l, allocator_type& __a)
  1459. {
  1460. _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
  1461. return new(__space) _RopeSubstring(__b, __s, __l, __a);
  1462. }
  1463. static _RopeLeaf*
  1464. _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
  1465. size_type __size, allocator_type& __a)
  1466. #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
  1467. _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
  1468. {
  1469. if (0 == __size)
  1470. return 0;
  1471. _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
  1472. __uninitialized_copy_n_a(__s, __size, __buf, __a);
  1473. _S_cond_store_eos(__buf[__size]);
  1474. __try
  1475. { return _S_new_RopeLeaf(__buf, __size, __a); }
  1476. __catch(...)
  1477. {
  1478. _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
  1479. __throw_exception_again;
  1480. }
  1481. }
  1482. // Concatenation of nonempty strings.
  1483. // Always builds a concatenation node.
  1484. // Rebalances if the result is too deep.
  1485. // Result has refcount 1.
  1486. // Does not increment left and right ref counts even though
  1487. // they are referenced.
  1488. static _RopeRep*
  1489. _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
  1490. // Concatenation helper functions
  1491. static _RopeLeaf*
  1492. _S_leaf_concat_char_iter(_RopeLeaf* __r,
  1493. const _CharT* __iter, size_type __slen);
  1494. // Concatenate by copying leaf.
  1495. // should take an arbitrary iterator
  1496. // result has refcount 1.
  1497. #ifndef __GC
  1498. static _RopeLeaf*
  1499. _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
  1500. const _CharT* __iter, size_type __slen);
  1501. // A version that potentially clobbers __r if __r->_M_ref_count == 1.
  1502. #endif
  1503. private:
  1504. static size_type _S_char_ptr_len(const _CharT* __s);
  1505. // slightly generalized strlen
  1506. rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
  1507. : _Base(__t, __a) { }
  1508. // Copy __r to the _CharT buffer.
  1509. // Returns __buffer + __r->_M_size.
  1510. // Assumes that buffer is uninitialized.
  1511. static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
  1512. // Again, with explicit starting position and length.
  1513. // Assumes that buffer is uninitialized.
  1514. static _CharT* _S_flatten(_RopeRep* __r,
  1515. size_type __start, size_type __len,
  1516. _CharT* __buffer);
  1517. static const unsigned long
  1518. _S_min_len[__detail::_S_max_rope_depth + 1];
  1519. static bool
  1520. _S_is_balanced(_RopeRep* __r)
  1521. { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
  1522. static bool
  1523. _S_is_almost_balanced(_RopeRep* __r)
  1524. { return (__r->_M_depth == 0
  1525. || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
  1526. static bool
  1527. _S_is_roughly_balanced(_RopeRep* __r)
  1528. { return (__r->_M_depth <= 1
  1529. || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
  1530. // Assumes the result is not empty.
  1531. static _RopeRep*
  1532. _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
  1533. {
  1534. _RopeRep* __result = _S_concat(__left, __right);
  1535. if (_S_is_balanced(__result))
  1536. __result->_M_is_balanced = true;
  1537. return __result;
  1538. }
  1539. // The basic rebalancing operation. Logically copies the
  1540. // rope. The result has refcount of 1. The client will
  1541. // usually decrement the reference count of __r.
  1542. // The result is within height 2 of balanced by the above
  1543. // definition.
  1544. static _RopeRep* _S_balance(_RopeRep* __r);
  1545. // Add all unbalanced subtrees to the forest of balanced trees.
  1546. // Used only by balance.
  1547. static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
  1548. // Add __r to forest, assuming __r is already balanced.
  1549. static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
  1550. // Print to stdout, exposing structure
  1551. static void _S_dump(_RopeRep* __r, int __indent = 0);
  1552. // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
  1553. static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
  1554. public:
  1555. _GLIBCXX_NODISCARD bool
  1556. empty() const
  1557. { return 0 == this->_M_tree_ptr; }
  1558. // Comparison member function. This is public only for those
  1559. // clients that need a ternary comparison. Others
  1560. // should use the comparison operators below.
  1561. int
  1562. compare(const rope& __y) const
  1563. { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
  1564. rope(const _CharT* __s, const allocator_type& __a = allocator_type())
  1565. : _Base(__a)
  1566. {
  1567. this->_M_tree_ptr =
  1568. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
  1569. _M_get_allocator());
  1570. }
  1571. rope(const _CharT* __s, size_type __len,
  1572. const allocator_type& __a = allocator_type())
  1573. : _Base(__a)
  1574. {
  1575. this->_M_tree_ptr =
  1576. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, _M_get_allocator());
  1577. }
  1578. // Should perhaps be templatized with respect to the iterator type
  1579. // and use Sequence_buffer. (It should perhaps use sequence_buffer
  1580. // even now.)
  1581. rope(const _CharT* __s, const _CharT* __e,
  1582. const allocator_type& __a = allocator_type())
  1583. : _Base(__a)
  1584. {
  1585. this->_M_tree_ptr =
  1586. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, _M_get_allocator());
  1587. }
  1588. rope(const const_iterator& __s, const const_iterator& __e,
  1589. const allocator_type& __a = allocator_type())
  1590. : _Base(_S_substring(__s._M_root, __s._M_current_pos,
  1591. __e._M_current_pos), __a)
  1592. { }
  1593. rope(const iterator& __s, const iterator& __e,
  1594. const allocator_type& __a = allocator_type())
  1595. : _Base(_S_substring(__s._M_root, __s._M_current_pos,
  1596. __e._M_current_pos), __a)
  1597. { }
  1598. rope(_CharT __c, const allocator_type& __a = allocator_type())
  1599. : _Base(__a)
  1600. {
  1601. _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
  1602. __alloc_traits<allocator_type>::construct(_M_get_allocator(),
  1603. __buf, __c);
  1604. __try
  1605. {
  1606. this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1,
  1607. _M_get_allocator());
  1608. }
  1609. __catch(...)
  1610. {
  1611. _RopeRep::__STL_FREE_STRING(__buf, 1, _M_get_allocator());
  1612. __throw_exception_again;
  1613. }
  1614. }
  1615. rope(size_type __n, _CharT __c,
  1616. const allocator_type& __a = allocator_type());
  1617. rope(const allocator_type& __a = allocator_type())
  1618. : _Base(0, __a) { }
  1619. // Construct a rope from a function that can compute its members
  1620. rope(char_producer<_CharT> *__fn, size_type __len, bool __delete_fn,
  1621. const allocator_type& __a = allocator_type())
  1622. : _Base(__a)
  1623. {
  1624. this->_M_tree_ptr = (0 == __len)
  1625. ? 0
  1626. : _S_new_RopeFunction(__fn, __len, __delete_fn, _M_get_allocator());
  1627. }
  1628. rope(const rope& __x, const allocator_type& __a = allocator_type())
  1629. : _Base(__x._M_tree_ptr, __a)
  1630. { _S_ref(this->_M_tree_ptr); }
  1631. ~rope() throw()
  1632. { _S_unref(this->_M_tree_ptr); }
  1633. rope&
  1634. operator=(const rope& __x)
  1635. {
  1636. _RopeRep* __old = this->_M_tree_ptr;
  1637. this->_M_tree_ptr = __x._M_tree_ptr;
  1638. _S_ref(this->_M_tree_ptr);
  1639. _S_unref(__old);
  1640. return *this;
  1641. }
  1642. void
  1643. clear()
  1644. {
  1645. _S_unref(this->_M_tree_ptr);
  1646. this->_M_tree_ptr = 0;
  1647. }
  1648. void
  1649. push_back(_CharT __x)
  1650. {
  1651. allocator_type __a = _M_get_allocator();
  1652. _RopeRep* __old = this->_M_tree_ptr;
  1653. this->_M_tree_ptr
  1654. = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1, __a);
  1655. _S_unref(__old);
  1656. }
  1657. void
  1658. pop_back()
  1659. {
  1660. _RopeRep* __old = this->_M_tree_ptr;
  1661. this->_M_tree_ptr = _S_substring(this->_M_tree_ptr,
  1662. 0, this->_M_tree_ptr->_M_size - 1);
  1663. _S_unref(__old);
  1664. }
  1665. _CharT
  1666. back() const
  1667. { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
  1668. void
  1669. push_front(_CharT __x)
  1670. {
  1671. _RopeRep* __old = this->_M_tree_ptr;
  1672. _RopeRep* __left =
  1673. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, _M_get_allocator());
  1674. __try
  1675. {
  1676. this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
  1677. _S_unref(__old);
  1678. _S_unref(__left);
  1679. }
  1680. __catch(...)
  1681. {
  1682. _S_unref(__left);
  1683. __throw_exception_again;
  1684. }
  1685. }
  1686. void
  1687. pop_front()
  1688. {
  1689. _RopeRep* __old = this->_M_tree_ptr;
  1690. this->_M_tree_ptr
  1691. = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
  1692. _S_unref(__old);
  1693. }
  1694. _CharT
  1695. front() const
  1696. { return _S_fetch(this->_M_tree_ptr, 0); }
  1697. void
  1698. balance()
  1699. {
  1700. _RopeRep* __old = this->_M_tree_ptr;
  1701. this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
  1702. _S_unref(__old);
  1703. }
  1704. void
  1705. copy(_CharT* __buffer) const
  1706. {
  1707. _Destroy_const(__buffer, __buffer + size(), _M_get_allocator());
  1708. _S_flatten(this->_M_tree_ptr, __buffer);
  1709. }
  1710. // This is the copy function from the standard, but
  1711. // with the arguments reordered to make it consistent with the
  1712. // rest of the interface.
  1713. // Note that this guaranteed not to compile if the draft standard
  1714. // order is assumed.
  1715. size_type
  1716. copy(size_type __pos, size_type __n, _CharT* __buffer) const
  1717. {
  1718. size_type __size = size();
  1719. size_type __len = (__pos + __n > __size? __size - __pos : __n);
  1720. _Destroy_const(__buffer, __buffer + __len, _M_get_allocator());
  1721. _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
  1722. return __len;
  1723. }
  1724. // Print to stdout, exposing structure. May be useful for
  1725. // performance debugging.
  1726. void
  1727. dump()
  1728. { _S_dump(this->_M_tree_ptr); }
  1729. // Convert to 0 terminated string in new allocated memory.
  1730. // Embedded 0s in the input do not terminate the copy.
  1731. const _CharT* c_str() const;
  1732. // As above, but also use the flattened representation as
  1733. // the new rope representation.
  1734. const _CharT* replace_with_c_str();
  1735. // Reclaim memory for the c_str generated flattened string.
  1736. // Intentionally undocumented, since it's hard to say when this
  1737. // is safe for multiple threads.
  1738. void
  1739. delete_c_str ()
  1740. {
  1741. if (0 == this->_M_tree_ptr)
  1742. return;
  1743. if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag &&
  1744. ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
  1745. this->_M_tree_ptr->_M_c_string)
  1746. {
  1747. // Representation shared
  1748. return;
  1749. }
  1750. #ifndef __GC
  1751. this->_M_tree_ptr->_M_free_c_string();
  1752. #endif
  1753. this->_M_tree_ptr->_M_c_string = 0;
  1754. }
  1755. _CharT
  1756. operator[] (size_type __pos) const
  1757. { return _S_fetch(this->_M_tree_ptr, __pos); }
  1758. _CharT
  1759. at(size_type __pos) const
  1760. {
  1761. // if (__pos >= size()) throw out_of_range; // XXX
  1762. return (*this)[__pos];
  1763. }
  1764. const_iterator
  1765. begin() const
  1766. { return(const_iterator(this->_M_tree_ptr, 0)); }
  1767. // An easy way to get a const iterator from a non-const container.
  1768. const_iterator
  1769. const_begin() const
  1770. { return(const_iterator(this->_M_tree_ptr, 0)); }
  1771. const_iterator
  1772. end() const
  1773. { return(const_iterator(this->_M_tree_ptr, size())); }
  1774. const_iterator
  1775. const_end() const
  1776. { return(const_iterator(this->_M_tree_ptr, size())); }
  1777. size_type
  1778. size() const
  1779. { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
  1780. size_type
  1781. length() const
  1782. { return size(); }
  1783. size_type
  1784. max_size() const
  1785. {
  1786. return _S_min_len[int(__detail::_S_max_rope_depth) - 1] - 1;
  1787. // Guarantees that the result can be sufficiently
  1788. // balanced. Longer ropes will probably still work,
  1789. // but it's harder to make guarantees.
  1790. }
  1791. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  1792. const_reverse_iterator
  1793. rbegin() const
  1794. { return const_reverse_iterator(end()); }
  1795. const_reverse_iterator
  1796. const_rbegin() const
  1797. { return const_reverse_iterator(end()); }
  1798. const_reverse_iterator
  1799. rend() const
  1800. { return const_reverse_iterator(begin()); }
  1801. const_reverse_iterator
  1802. const_rend() const
  1803. { return const_reverse_iterator(begin()); }
  1804. template<class _CharT2, class _Alloc2>
  1805. friend rope<_CharT2, _Alloc2>
  1806. operator+(const rope<_CharT2, _Alloc2>& __left,
  1807. const rope<_CharT2, _Alloc2>& __right);
  1808. template<class _CharT2, class _Alloc2>
  1809. friend rope<_CharT2, _Alloc2>
  1810. operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
  1811. template<class _CharT2, class _Alloc2>
  1812. friend rope<_CharT2, _Alloc2>
  1813. operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
  1814. // The symmetric cases are intentionally omitted, since they're
  1815. // presumed to be less common, and we don't handle them as well.
  1816. // The following should really be templatized. The first
  1817. // argument should be an input iterator or forward iterator with
  1818. // value_type _CharT.
  1819. rope&
  1820. append(const _CharT* __iter, size_type __n)
  1821. {
  1822. allocator_type __a = _M_get_allocator();
  1823. _RopeRep* __result =
  1824. _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n, __a);
  1825. _S_unref(this->_M_tree_ptr);
  1826. this->_M_tree_ptr = __result;
  1827. return *this;
  1828. }
  1829. rope&
  1830. append(const _CharT* __c_string)
  1831. {
  1832. size_type __len = _S_char_ptr_len(__c_string);
  1833. append(__c_string, __len);
  1834. return(*this);
  1835. }
  1836. rope&
  1837. append(const _CharT* __s, const _CharT* __e)
  1838. {
  1839. allocator_type __a = _M_get_allocator();
  1840. _RopeRep* __result =
  1841. _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s, __a);
  1842. _S_unref(this->_M_tree_ptr);
  1843. this->_M_tree_ptr = __result;
  1844. return *this;
  1845. }
  1846. rope&
  1847. append(const_iterator __s, const_iterator __e)
  1848. {
  1849. _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
  1850. __s._M_current_pos,
  1851. __e._M_current_pos));
  1852. _RopeRep* __result = _S_concat(this->_M_tree_ptr,
  1853. (_RopeRep*)__appendee);
  1854. _S_unref(this->_M_tree_ptr);
  1855. this->_M_tree_ptr = __result;
  1856. return *this;
  1857. }
  1858. rope&
  1859. append(_CharT __c)
  1860. {
  1861. allocator_type __a = _M_get_allocator();
  1862. _RopeRep* __result =
  1863. _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1, __a);
  1864. _S_unref(this->_M_tree_ptr);
  1865. this->_M_tree_ptr = __result;
  1866. return *this;
  1867. }
  1868. rope&
  1869. append()
  1870. { return append(_CharT()); } // XXX why?
  1871. rope&
  1872. append(const rope& __y)
  1873. {
  1874. _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
  1875. _S_unref(this->_M_tree_ptr);
  1876. this->_M_tree_ptr = __result;
  1877. return *this;
  1878. }
  1879. rope&
  1880. append(size_type __n, _CharT __c)
  1881. {
  1882. rope<_CharT,_Alloc> __last(__n, __c);
  1883. return append(__last);
  1884. }
  1885. void
  1886. swap(rope& __b)
  1887. {
  1888. _RopeRep* __tmp = this->_M_tree_ptr;
  1889. this->_M_tree_ptr = __b._M_tree_ptr;
  1890. __b._M_tree_ptr = __tmp;
  1891. }
  1892. protected:
  1893. // Result is included in refcount.
  1894. static _RopeRep*
  1895. replace(_RopeRep* __old, size_type __pos1,
  1896. size_type __pos2, _RopeRep* __r)
  1897. {
  1898. if (0 == __old)
  1899. {
  1900. _S_ref(__r);
  1901. return __r;
  1902. }
  1903. _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
  1904. _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
  1905. _RopeRep* __result;
  1906. if (0 == __r)
  1907. __result = _S_concat(__left, __right);
  1908. else
  1909. {
  1910. _Self_destruct_ptr __left_result(_S_concat(__left, __r));
  1911. __result = _S_concat(__left_result, __right);
  1912. }
  1913. return __result;
  1914. }
  1915. public:
  1916. void
  1917. insert(size_type __p, const rope& __r)
  1918. {
  1919. _RopeRep* __result =
  1920. replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
  1921. _S_unref(this->_M_tree_ptr);
  1922. this->_M_tree_ptr = __result;
  1923. }
  1924. void
  1925. insert(size_type __p, size_type __n, _CharT __c)
  1926. {
  1927. rope<_CharT,_Alloc> __r(__n,__c);
  1928. insert(__p, __r);
  1929. }
  1930. void
  1931. insert(size_type __p, const _CharT* __i, size_type __n)
  1932. {
  1933. _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
  1934. _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
  1935. __p, size()));
  1936. _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n,
  1937. _M_get_allocator()));
  1938. // _S_ destr_concat_char_iter should be safe here.
  1939. // But as it stands it's probably not a win, since __left
  1940. // is likely to have additional references.
  1941. _RopeRep* __result = _S_concat(__left_result, __right);
  1942. _S_unref(this->_M_tree_ptr);
  1943. this->_M_tree_ptr = __result;
  1944. }
  1945. void
  1946. insert(size_type __p, const _CharT* __c_string)
  1947. { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
  1948. void
  1949. insert(size_type __p, _CharT __c)
  1950. { insert(__p, &__c, 1); }
  1951. void
  1952. insert(size_type __p)
  1953. {
  1954. _CharT __c = _CharT();
  1955. insert(__p, &__c, 1);
  1956. }
  1957. void
  1958. insert(size_type __p, const _CharT* __i, const _CharT* __j)
  1959. {
  1960. rope __r(__i, __j);
  1961. insert(__p, __r);
  1962. }
  1963. void
  1964. insert(size_type __p, const const_iterator& __i,
  1965. const const_iterator& __j)
  1966. {
  1967. rope __r(__i, __j);
  1968. insert(__p, __r);
  1969. }
  1970. void
  1971. insert(size_type __p, const iterator& __i,
  1972. const iterator& __j)
  1973. {
  1974. rope __r(__i, __j);
  1975. insert(__p, __r);
  1976. }
  1977. // (position, length) versions of replace operations:
  1978. void
  1979. replace(size_type __p, size_type __n, const rope& __r)
  1980. {
  1981. _RopeRep* __result =
  1982. replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
  1983. _S_unref(this->_M_tree_ptr);
  1984. this->_M_tree_ptr = __result;
  1985. }
  1986. void
  1987. replace(size_type __p, size_type __n,
  1988. const _CharT* __i, size_type __i_len)
  1989. {
  1990. rope __r(__i, __i_len);
  1991. replace(__p, __n, __r);
  1992. }
  1993. void
  1994. replace(size_type __p, size_type __n, _CharT __c)
  1995. {
  1996. rope __r(__c);
  1997. replace(__p, __n, __r);
  1998. }
  1999. void
  2000. replace(size_type __p, size_type __n, const _CharT* __c_string)
  2001. {
  2002. rope __r(__c_string);
  2003. replace(__p, __n, __r);
  2004. }
  2005. void
  2006. replace(size_type __p, size_type __n,
  2007. const _CharT* __i, const _CharT* __j)
  2008. {
  2009. rope __r(__i, __j);
  2010. replace(__p, __n, __r);
  2011. }
  2012. void
  2013. replace(size_type __p, size_type __n,
  2014. const const_iterator& __i, const const_iterator& __j)
  2015. {
  2016. rope __r(__i, __j);
  2017. replace(__p, __n, __r);
  2018. }
  2019. void
  2020. replace(size_type __p, size_type __n,
  2021. const iterator& __i, const iterator& __j)
  2022. {
  2023. rope __r(__i, __j);
  2024. replace(__p, __n, __r);
  2025. }
  2026. // Single character variants:
  2027. void
  2028. replace(size_type __p, _CharT __c)
  2029. {
  2030. iterator __i(this, __p);
  2031. *__i = __c;
  2032. }
  2033. void
  2034. replace(size_type __p, const rope& __r)
  2035. { replace(__p, 1, __r); }
  2036. void
  2037. replace(size_type __p, const _CharT* __i, size_type __i_len)
  2038. { replace(__p, 1, __i, __i_len); }
  2039. void
  2040. replace(size_type __p, const _CharT* __c_string)
  2041. { replace(__p, 1, __c_string); }
  2042. void
  2043. replace(size_type __p, const _CharT* __i, const _CharT* __j)
  2044. { replace(__p, 1, __i, __j); }
  2045. void
  2046. replace(size_type __p, const const_iterator& __i,
  2047. const const_iterator& __j)
  2048. { replace(__p, 1, __i, __j); }
  2049. void
  2050. replace(size_type __p, const iterator& __i,
  2051. const iterator& __j)
  2052. { replace(__p, 1, __i, __j); }
  2053. // Erase, (position, size) variant.
  2054. void
  2055. erase(size_type __p, size_type __n)
  2056. {
  2057. _RopeRep* __result = replace(this->_M_tree_ptr, __p,
  2058. __p + __n, 0);
  2059. _S_unref(this->_M_tree_ptr);
  2060. this->_M_tree_ptr = __result;
  2061. }
  2062. // Erase, single character
  2063. void
  2064. erase(size_type __p)
  2065. { erase(__p, __p + 1); }
  2066. // Insert, iterator variants.
  2067. iterator
  2068. insert(const iterator& __p, const rope& __r)
  2069. {
  2070. insert(__p.index(), __r);
  2071. return __p;
  2072. }
  2073. iterator
  2074. insert(const iterator& __p, size_type __n, _CharT __c)
  2075. {
  2076. insert(__p.index(), __n, __c);
  2077. return __p;
  2078. }
  2079. iterator insert(const iterator& __p, _CharT __c)
  2080. {
  2081. insert(__p.index(), __c);
  2082. return __p;
  2083. }
  2084. iterator
  2085. insert(const iterator& __p )
  2086. {
  2087. insert(__p.index());
  2088. return __p;
  2089. }
  2090. iterator
  2091. insert(const iterator& __p, const _CharT* c_string)
  2092. {
  2093. insert(__p.index(), c_string);
  2094. return __p;
  2095. }
  2096. iterator
  2097. insert(const iterator& __p, const _CharT* __i, size_type __n)
  2098. {
  2099. insert(__p.index(), __i, __n);
  2100. return __p;
  2101. }
  2102. iterator
  2103. insert(const iterator& __p, const _CharT* __i,
  2104. const _CharT* __j)
  2105. {
  2106. insert(__p.index(), __i, __j);
  2107. return __p;
  2108. }
  2109. iterator
  2110. insert(const iterator& __p,
  2111. const const_iterator& __i, const const_iterator& __j)
  2112. {
  2113. insert(__p.index(), __i, __j);
  2114. return __p;
  2115. }
  2116. iterator
  2117. insert(const iterator& __p,
  2118. const iterator& __i, const iterator& __j)
  2119. {
  2120. insert(__p.index(), __i, __j);
  2121. return __p;
  2122. }
  2123. // Replace, range variants.
  2124. void
  2125. replace(const iterator& __p, const iterator& __q, const rope& __r)
  2126. { replace(__p.index(), __q.index() - __p.index(), __r); }
  2127. void
  2128. replace(const iterator& __p, const iterator& __q, _CharT __c)
  2129. { replace(__p.index(), __q.index() - __p.index(), __c); }
  2130. void
  2131. replace(const iterator& __p, const iterator& __q,
  2132. const _CharT* __c_string)
  2133. { replace(__p.index(), __q.index() - __p.index(), __c_string); }
  2134. void
  2135. replace(const iterator& __p, const iterator& __q,
  2136. const _CharT* __i, size_type __n)
  2137. { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
  2138. void
  2139. replace(const iterator& __p, const iterator& __q,
  2140. const _CharT* __i, const _CharT* __j)
  2141. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  2142. void
  2143. replace(const iterator& __p, const iterator& __q,
  2144. const const_iterator& __i, const const_iterator& __j)
  2145. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  2146. void
  2147. replace(const iterator& __p, const iterator& __q,
  2148. const iterator& __i, const iterator& __j)
  2149. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  2150. // Replace, iterator variants.
  2151. void
  2152. replace(const iterator& __p, const rope& __r)
  2153. { replace(__p.index(), __r); }
  2154. void
  2155. replace(const iterator& __p, _CharT __c)
  2156. { replace(__p.index(), __c); }
  2157. void
  2158. replace(const iterator& __p, const _CharT* __c_string)
  2159. { replace(__p.index(), __c_string); }
  2160. void
  2161. replace(const iterator& __p, const _CharT* __i, size_type __n)
  2162. { replace(__p.index(), __i, __n); }
  2163. void
  2164. replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
  2165. { replace(__p.index(), __i, __j); }
  2166. void
  2167. replace(const iterator& __p, const_iterator __i, const_iterator __j)
  2168. { replace(__p.index(), __i, __j); }
  2169. void
  2170. replace(const iterator& __p, iterator __i, iterator __j)
  2171. { replace(__p.index(), __i, __j); }
  2172. // Iterator and range variants of erase
  2173. iterator
  2174. erase(const iterator& __p, const iterator& __q)
  2175. {
  2176. size_type __p_index = __p.index();
  2177. erase(__p_index, __q.index() - __p_index);
  2178. return iterator(this, __p_index);
  2179. }
  2180. iterator
  2181. erase(const iterator& __p)
  2182. {
  2183. size_type __p_index = __p.index();
  2184. erase(__p_index, 1);
  2185. return iterator(this, __p_index);
  2186. }
  2187. rope
  2188. substr(size_type __start, size_type __len = 1) const
  2189. {
  2190. return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
  2191. __start,
  2192. __start + __len));
  2193. }
  2194. rope
  2195. substr(iterator __start, iterator __end) const
  2196. {
  2197. return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
  2198. __start.index(),
  2199. __end.index()));
  2200. }
  2201. rope
  2202. substr(iterator __start) const
  2203. {
  2204. size_type __pos = __start.index();
  2205. return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
  2206. __pos, __pos + 1));
  2207. }
  2208. rope
  2209. substr(const_iterator __start, const_iterator __end) const
  2210. {
  2211. // This might eventually take advantage of the cache in the
  2212. // iterator.
  2213. return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
  2214. __start.index(),
  2215. __end.index()));
  2216. }
  2217. rope<_CharT, _Alloc>
  2218. substr(const_iterator __start)
  2219. {
  2220. size_type __pos = __start.index();
  2221. return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
  2222. __pos, __pos + 1));
  2223. }
  2224. static const size_type npos;
  2225. size_type find(_CharT __c, size_type __pos = 0) const;
  2226. size_type
  2227. find(const _CharT* __s, size_type __pos = 0) const
  2228. {
  2229. size_type __result_pos;
  2230. const_iterator __result =
  2231. std::search(const_begin() + __pos, const_end(),
  2232. __s, __s + _S_char_ptr_len(__s));
  2233. __result_pos = __result.index();
  2234. #ifndef __STL_OLD_ROPE_SEMANTICS
  2235. if (__result_pos == size())
  2236. __result_pos = npos;
  2237. #endif
  2238. return __result_pos;
  2239. }
  2240. iterator
  2241. mutable_begin()
  2242. { return(iterator(this, 0)); }
  2243. iterator
  2244. mutable_end()
  2245. { return(iterator(this, size())); }
  2246. typedef std::reverse_iterator<iterator> reverse_iterator;
  2247. reverse_iterator
  2248. mutable_rbegin()
  2249. { return reverse_iterator(mutable_end()); }
  2250. reverse_iterator
  2251. mutable_rend()
  2252. { return reverse_iterator(mutable_begin()); }
  2253. reference
  2254. mutable_reference_at(size_type __pos)
  2255. { return reference(this, __pos); }
  2256. #ifdef __STD_STUFF
  2257. reference
  2258. operator[] (size_type __pos)
  2259. { return _char_ref_proxy(this, __pos); }
  2260. reference
  2261. at(size_type __pos)
  2262. {
  2263. // if (__pos >= size()) throw out_of_range; // XXX
  2264. return (*this)[__pos];
  2265. }
  2266. void resize(size_type __n, _CharT __c) { }
  2267. void resize(size_type __n) { }
  2268. void reserve(size_type __res_arg = 0) { }
  2269. size_type
  2270. capacity() const
  2271. { return max_size(); }
  2272. // Stuff below this line is dangerous because it's error prone.
  2273. // I would really like to get rid of it.
  2274. // copy function with funny arg ordering.
  2275. size_type
  2276. copy(_CharT* __buffer, size_type __n,
  2277. size_type __pos = 0) const
  2278. { return copy(__pos, __n, __buffer); }
  2279. iterator
  2280. end()
  2281. { return mutable_end(); }
  2282. iterator
  2283. begin()
  2284. { return mutable_begin(); }
  2285. reverse_iterator
  2286. rend()
  2287. { return mutable_rend(); }
  2288. reverse_iterator
  2289. rbegin()
  2290. { return mutable_rbegin(); }
  2291. #else
  2292. const_iterator
  2293. end()
  2294. { return const_end(); }
  2295. const_iterator
  2296. begin()
  2297. { return const_begin(); }
  2298. const_reverse_iterator
  2299. rend()
  2300. { return const_rend(); }
  2301. const_reverse_iterator
  2302. rbegin()
  2303. { return const_rbegin(); }
  2304. #endif
  2305. };
  2306. template <class _CharT, class _Alloc>
  2307. const typename rope<_CharT, _Alloc>::size_type
  2308. rope<_CharT, _Alloc>::npos = (size_type)(-1);
  2309. template <class _CharT, class _Alloc>
  2310. inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2311. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2312. { return (__x._M_current_pos == __y._M_current_pos
  2313. && __x._M_root == __y._M_root); }
  2314. template <class _CharT, class _Alloc>
  2315. inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2316. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2317. { return (__x._M_current_pos < __y._M_current_pos); }
  2318. template <class _CharT, class _Alloc>
  2319. inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2320. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2321. { return !(__x == __y); }
  2322. template <class _CharT, class _Alloc>
  2323. inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2324. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2325. { return __y < __x; }
  2326. template <class _CharT, class _Alloc>
  2327. inline bool
  2328. operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2329. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2330. { return !(__y < __x); }
  2331. template <class _CharT, class _Alloc>
  2332. inline bool
  2333. operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2334. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2335. { return !(__x < __y); }
  2336. template <class _CharT, class _Alloc>
  2337. inline std::ptrdiff_t
  2338. operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2339. const _Rope_const_iterator<_CharT, _Alloc>& __y)
  2340. {
  2341. return (std::ptrdiff_t)__x._M_current_pos
  2342. - (std::ptrdiff_t)__y._M_current_pos;
  2343. }
  2344. template <class _CharT, class _Alloc>
  2345. inline _Rope_const_iterator<_CharT, _Alloc>
  2346. operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2347. std::ptrdiff_t __n)
  2348. { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
  2349. __x._M_current_pos - __n); }
  2350. template <class _CharT, class _Alloc>
  2351. inline _Rope_const_iterator<_CharT, _Alloc>
  2352. operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
  2353. std::ptrdiff_t __n)
  2354. { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
  2355. __x._M_current_pos + __n); }
  2356. template <class _CharT, class _Alloc>
  2357. inline _Rope_const_iterator<_CharT, _Alloc>
  2358. operator+(std::ptrdiff_t __n,
  2359. const _Rope_const_iterator<_CharT, _Alloc>& __x)
  2360. { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
  2361. __x._M_current_pos + __n); }
  2362. template <class _CharT, class _Alloc>
  2363. inline bool
  2364. operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
  2365. const _Rope_iterator<_CharT, _Alloc>& __y)
  2366. {return (__x._M_current_pos == __y._M_current_pos
  2367. && __x._M_root_rope == __y._M_root_rope); }
  2368. template <class _CharT, class _Alloc>
  2369. inline bool
  2370. operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
  2371. const _Rope_iterator<_CharT, _Alloc>& __y)
  2372. { return (__x._M_current_pos < __y._M_current_pos); }
  2373. template <class _CharT, class _Alloc>
  2374. inline bool
  2375. operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
  2376. const _Rope_iterator<_CharT, _Alloc>& __y)
  2377. { return !(__x == __y); }
  2378. template <class _CharT, class _Alloc>
  2379. inline bool
  2380. operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
  2381. const _Rope_iterator<_CharT, _Alloc>& __y)
  2382. { return __y < __x; }
  2383. template <class _CharT, class _Alloc>
  2384. inline bool
  2385. operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
  2386. const _Rope_iterator<_CharT, _Alloc>& __y)
  2387. { return !(__y < __x); }
  2388. template <class _CharT, class _Alloc>
  2389. inline bool
  2390. operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
  2391. const _Rope_iterator<_CharT, _Alloc>& __y)
  2392. { return !(__x < __y); }
  2393. template <class _CharT, class _Alloc>
  2394. inline std::ptrdiff_t
  2395. operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
  2396. const _Rope_iterator<_CharT, _Alloc>& __y)
  2397. { return ((std::ptrdiff_t)__x._M_current_pos
  2398. - (std::ptrdiff_t)__y._M_current_pos); }
  2399. template <class _CharT, class _Alloc>
  2400. inline _Rope_iterator<_CharT, _Alloc>
  2401. operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
  2402. std::ptrdiff_t __n)
  2403. { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
  2404. __x._M_current_pos - __n); }
  2405. template <class _CharT, class _Alloc>
  2406. inline _Rope_iterator<_CharT, _Alloc>
  2407. operator+(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n)
  2408. { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
  2409. __x._M_current_pos + __n); }
  2410. template <class _CharT, class _Alloc>
  2411. inline _Rope_iterator<_CharT, _Alloc>
  2412. operator+(std::ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
  2413. { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
  2414. __x._M_current_pos + __n); }
  2415. template <class _CharT, class _Alloc>
  2416. inline rope<_CharT, _Alloc>
  2417. operator+(const rope<_CharT, _Alloc>& __left,
  2418. const rope<_CharT, _Alloc>& __right)
  2419. {
  2420. // Inlining this should make it possible to keep __left and
  2421. // __right in registers.
  2422. typedef rope<_CharT, _Alloc> rope_type;
  2423. return rope_type(rope_type::_S_concat(__left._M_tree_ptr,
  2424. __right._M_tree_ptr));
  2425. }
  2426. template <class _CharT, class _Alloc>
  2427. inline rope<_CharT, _Alloc>&
  2428. operator+=(rope<_CharT, _Alloc>& __left,
  2429. const rope<_CharT, _Alloc>& __right)
  2430. {
  2431. __left.append(__right);
  2432. return __left;
  2433. }
  2434. template <class _CharT, class _Alloc>
  2435. inline rope<_CharT, _Alloc>
  2436. operator+(const rope<_CharT, _Alloc>& __left,
  2437. const _CharT* __right)
  2438. {
  2439. typedef rope<_CharT, _Alloc> rope_type;
  2440. std::size_t __rlen = rope_type::_S_char_ptr_len(__right);
  2441. _Alloc __a = __left.get_allocator();
  2442. return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
  2443. __right, __rlen, __a));
  2444. }
  2445. template <class _CharT, class _Alloc>
  2446. inline rope<_CharT, _Alloc>&
  2447. operator+=(rope<_CharT, _Alloc>& __left,
  2448. const _CharT* __right)
  2449. {
  2450. __left.append(__right);
  2451. return __left;
  2452. }
  2453. template <class _CharT, class _Alloc>
  2454. inline rope<_CharT, _Alloc>
  2455. operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
  2456. {
  2457. typedef rope<_CharT, _Alloc> rope_type;
  2458. _Alloc __a = __left.get_allocator();
  2459. return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
  2460. &__right, 1, __a));
  2461. }
  2462. template <class _CharT, class _Alloc>
  2463. inline rope<_CharT, _Alloc>&
  2464. operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
  2465. {
  2466. __left.append(__right);
  2467. return __left;
  2468. }
  2469. template <class _CharT, class _Alloc>
  2470. bool
  2471. operator<(const rope<_CharT, _Alloc>& __left,
  2472. const rope<_CharT, _Alloc>& __right)
  2473. { return __left.compare(__right) < 0; }
  2474. template <class _CharT, class _Alloc>
  2475. bool
  2476. operator==(const rope<_CharT, _Alloc>& __left,
  2477. const rope<_CharT, _Alloc>& __right)
  2478. { return __left.compare(__right) == 0; }
  2479. template <class _CharT, class _Alloc>
  2480. inline bool
  2481. operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
  2482. const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
  2483. { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
  2484. template <class _CharT, class _Alloc>
  2485. inline bool
  2486. operator!=(const rope<_CharT, _Alloc>& __x,
  2487. const rope<_CharT, _Alloc>& __y)
  2488. { return !(__x == __y); }
  2489. template <class _CharT, class _Alloc>
  2490. inline bool
  2491. operator>(const rope<_CharT, _Alloc>& __x,
  2492. const rope<_CharT, _Alloc>& __y)
  2493. { return __y < __x; }
  2494. template <class _CharT, class _Alloc>
  2495. inline bool
  2496. operator<=(const rope<_CharT, _Alloc>& __x,
  2497. const rope<_CharT, _Alloc>& __y)
  2498. { return !(__y < __x); }
  2499. template <class _CharT, class _Alloc>
  2500. inline bool
  2501. operator>=(const rope<_CharT, _Alloc>& __x,
  2502. const rope<_CharT, _Alloc>& __y)
  2503. { return !(__x < __y); }
  2504. template <class _CharT, class _Alloc>
  2505. inline bool
  2506. operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
  2507. const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
  2508. { return !(__x == __y); }
  2509. template<class _CharT, class _Traits, class _Alloc>
  2510. std::basic_ostream<_CharT, _Traits>&
  2511. operator<<(std::basic_ostream<_CharT, _Traits>& __o,
  2512. const rope<_CharT, _Alloc>& __r);
  2513. typedef rope<char> crope;
  2514. typedef rope<wchar_t> wrope;
  2515. inline crope::reference
  2516. __mutable_reference_at(crope& __c, std::size_t __i)
  2517. { return __c.mutable_reference_at(__i); }
  2518. inline wrope::reference
  2519. __mutable_reference_at(wrope& __c, std::size_t __i)
  2520. { return __c.mutable_reference_at(__i); }
  2521. template <class _CharT, class _Alloc>
  2522. inline void
  2523. swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
  2524. { __x.swap(__y); }
  2525. _GLIBCXX_END_NAMESPACE_VERSION
  2526. } // namespace
  2527. namespace std _GLIBCXX_VISIBILITY(default)
  2528. {
  2529. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2530. namespace tr1
  2531. {
  2532. template<>
  2533. struct hash<__gnu_cxx::crope>
  2534. {
  2535. size_t
  2536. operator()(const __gnu_cxx::crope& __str) const
  2537. {
  2538. size_t __size = __str.size();
  2539. if (0 == __size)
  2540. return 0;
  2541. return 13 * __str[0] + 5 * __str[__size - 1] + __size;
  2542. }
  2543. };
  2544. template<>
  2545. struct hash<__gnu_cxx::wrope>
  2546. {
  2547. size_t
  2548. operator()(const __gnu_cxx::wrope& __str) const
  2549. {
  2550. size_t __size = __str.size();
  2551. if (0 == __size)
  2552. return 0;
  2553. return 13 * __str[0] + 5 * __str[__size - 1] + __size;
  2554. }
  2555. };
  2556. } // namespace tr1
  2557. _GLIBCXX_END_NAMESPACE_VERSION
  2558. } // namespace std
  2559. # include <ext/ropeimpl.h>
  2560. #endif