rope 85 KB

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