rope 86 KB

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