_rope.h 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. /*
  2. *
  3. * Copyright (c) 1996,1997
  4. * Silicon Graphics Computer Systems, Inc.
  5. *
  6. * Copyright (c) 1997
  7. * Moscow Center for SPARC Technology
  8. *
  9. * Copyright (c) 1999
  10. * Boris Fomitchev
  11. *
  12. * This material is provided "as is", with absolutely no warranty expressed
  13. * or implied. Any use is at your own risk.
  14. *
  15. * Permission to use or copy this software for any purpose is hereby granted
  16. * without fee, provided the above notices are retained on all copies.
  17. * Permission to modify the code and to distribute modified code is granted,
  18. * provided the above notices are retained, and a notice that the code was
  19. * modified is included with the above copyright notice.
  20. *
  21. */
  22. /* NOTE: This is an internal header file, included by other STL headers.
  23. * You should not attempt to use it directly.
  24. */
  25. // rope<_CharT,_Alloc> is a sequence of _CharT.
  26. // Ropes appear to be mutable, but update operations
  27. // really copy enough of the data structure to leave the original
  28. // valid. Thus ropes can be logically copied by just copying
  29. // a pointer value.
  30. #ifndef _STLP_INTERNAL_ROPE_H
  31. #define _STLP_INTERNAL_ROPE_H
  32. #ifndef _STLP_INTERNAL_ALGOBASE_H
  33. # include <stl/_algobase.h>
  34. #endif
  35. #if !defined (_STLP_USE_NO_IOSTREAMS) && !defined (_STLP_INTERNAL_IOSFWD)
  36. # include <stl/_iosfwd.h>
  37. #endif
  38. #ifndef _STLP_INTERNAL_ALLOC_H
  39. # include <stl/_alloc.h>
  40. #endif
  41. #ifndef _STLP_INTERNAL_ITERATOR_H
  42. # include <stl/_iterator.h>
  43. #endif
  44. #ifndef _STLP_INTERNAL_ALGO_H
  45. # include <stl/_algo.h>
  46. #endif
  47. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  48. # include <stl/_function_base.h>
  49. #endif
  50. #ifndef _STLP_INTERNAL_NUMERIC_H
  51. # include <stl/_numeric.h>
  52. #endif
  53. #ifndef _STLP_INTERNAL_HASH_FUN_H
  54. # include <stl/_hash_fun.h>
  55. #endif
  56. #ifndef _STLP_CHAR_TRAITS_H
  57. # include <stl/char_traits.h>
  58. #endif
  59. #ifndef _STLP_INTERNAL_THREADS_H
  60. # include <stl/_threads.h>
  61. #endif
  62. #ifdef _STLP_SGI_THREADS
  63. # include <mutex.h>
  64. #endif
  65. #ifndef _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE
  66. # define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) (_Alloc_traits<_Tp,__atype>::create_allocator(__a))
  67. #else
  68. # define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
  69. #endif
  70. _STLP_BEGIN_NAMESPACE
  71. // First a lot of forward declarations. The standard seems to require
  72. // much stricter "declaration before use" than many of the implementations
  73. // that preceded it.
  74. template<class _CharT, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_CharT>) > class rope;
  75. template<class _CharT, class _Alloc> struct _Rope_RopeConcatenation;
  76. template<class _CharT, class _Alloc> struct _Rope_RopeRep;
  77. template<class _CharT, class _Alloc> struct _Rope_RopeLeaf;
  78. template<class _CharT, class _Alloc> struct _Rope_RopeFunction;
  79. template<class _CharT, class _Alloc> struct _Rope_RopeSubstring;
  80. template<class _CharT, class _Alloc> class _Rope_iterator;
  81. template<class _CharT, class _Alloc> class _Rope_const_iterator;
  82. template<class _CharT, class _Alloc> class _Rope_char_ref_proxy;
  83. template<class _CharT, class _Alloc> class _Rope_char_ptr_proxy;
  84. _STLP_MOVE_TO_PRIV_NAMESPACE
  85. template <class _CharT>
  86. struct _BasicCharType { typedef __false_type _Ret; };
  87. _STLP_TEMPLATE_NULL
  88. struct _BasicCharType<char> { typedef __true_type _Ret; };
  89. #ifdef _STLP_HAS_WCHAR_T
  90. _STLP_TEMPLATE_NULL
  91. struct _BasicCharType<wchar_t> { typedef __true_type _Ret; };
  92. #endif
  93. // Some helpers, so we can use the power algorithm on ropes.
  94. // See below for why this isn't local to the implementation.
  95. // This uses a nonstandard refcount convention.
  96. // The result has refcount 0.
  97. template<class _CharT, class _Alloc>
  98. struct _Rope_Concat_fn
  99. : public binary_function<rope<_CharT,_Alloc>, rope<_CharT,_Alloc>,
  100. rope<_CharT,_Alloc> > {
  101. rope<_CharT,_Alloc> operator() (const rope<_CharT,_Alloc>& __x,
  102. const rope<_CharT,_Alloc>& __y) {
  103. return __x + __y;
  104. }
  105. };
  106. template <class _CharT, class _Alloc>
  107. inline
  108. rope<_CharT,_Alloc>
  109. __identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
  110. { return rope<_CharT,_Alloc>(); }
  111. _STLP_MOVE_TO_STD_NAMESPACE
  112. // Store an eos
  113. template <class _CharT>
  114. inline void _S_construct_null_aux(_CharT *__p, const __true_type&)
  115. { *__p = 0; }
  116. template <class _CharT>
  117. inline void _S_construct_null_aux(_CharT *__p, const __false_type&)
  118. { _STLP_STD::_Construct(__p); }
  119. template <class _CharT>
  120. inline void _S_construct_null(_CharT *__p) {
  121. typedef typename _IsIntegral<_CharT>::_Ret _Char_Is_Integral;
  122. _S_construct_null_aux(__p, _Char_Is_Integral());
  123. }
  124. // char_producers are logically functions that generate a section of
  125. // a string. These can be converted to ropes. The resulting rope
  126. // invokes the char_producer on demand. This allows, for example,
  127. // files to be viewed as ropes without reading the entire file.
  128. template <class _CharT>
  129. class char_producer {
  130. public:
  131. virtual ~char_producer() {}
  132. virtual void operator()(size_t __start_pos, size_t __len,
  133. _CharT* __buffer) = 0;
  134. // Buffer should really be an arbitrary output iterator.
  135. // That way we could flatten directly into an ostream, etc.
  136. // This is thoroughly impossible, since iterator types don't
  137. // have runtime descriptions.
  138. };
  139. // Sequence buffers:
  140. //
  141. // Sequence must provide an append operation that appends an
  142. // array to the sequence. Sequence buffers are useful only if
  143. // appending an entire array is cheaper than appending element by element.
  144. // This is true for many string representations.
  145. // This should perhaps inherit from ostream<sequence::value_type>
  146. // and be implemented correspondingly, so that they can be used
  147. // for formatted. For the sake of portability, we don't do this yet.
  148. //
  149. // For now, sequence buffers behave as output iterators. But they also
  150. // behave a little like basic_ostringstream<sequence::value_type> and a
  151. // little like containers.
  152. template<class _Sequence
  153. # if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
  154. defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
  155. , size_t _Buf_sz = 100
  156. # if defined(__sgi) && !defined(__GNUC__)
  157. # define __TYPEDEF_WORKAROUND
  158. ,class _V = typename _Sequence::value_type
  159. # endif /* __sgi */
  160. # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  161. >
  162. // The 3rd parameter works around a common compiler bug.
  163. class sequence_buffer : public iterator <output_iterator_tag, void, void, void, void> {
  164. public:
  165. # ifndef __TYPEDEF_WORKAROUND
  166. typedef typename _Sequence::value_type value_type;
  167. typedef sequence_buffer<_Sequence
  168. # if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
  169. defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
  170. , _Buf_sz
  171. > _Self;
  172. # else /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  173. > _Self;
  174. enum { _Buf_sz = 100};
  175. # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  176. // # endif
  177. # else /* __TYPEDEF_WORKAROUND */
  178. typedef _V value_type;
  179. typedef sequence_buffer<_Sequence, _Buf_sz, _V> _Self;
  180. # endif /* __TYPEDEF_WORKAROUND */
  181. protected:
  182. _Sequence* _M_prefix;
  183. value_type _M_buffer[_Buf_sz];
  184. size_t _M_buf_count;
  185. public:
  186. void flush() {
  187. _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
  188. _M_buf_count = 0;
  189. }
  190. ~sequence_buffer() { flush(); }
  191. sequence_buffer() : _M_prefix(0), _M_buf_count(0) {}
  192. sequence_buffer(const _Self& __x) {
  193. _M_prefix = __x._M_prefix;
  194. _M_buf_count = __x._M_buf_count;
  195. _STLP_STD::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
  196. }
  197. sequence_buffer(_Self& __x) {
  198. __x.flush();
  199. _M_prefix = __x._M_prefix;
  200. _M_buf_count = 0;
  201. }
  202. sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) {}
  203. _Self& operator= (_Self& __x) {
  204. __x.flush();
  205. _M_prefix = __x._M_prefix;
  206. _M_buf_count = 0;
  207. return *this;
  208. }
  209. _Self& operator= (const _Self& __x) {
  210. _M_prefix = __x._M_prefix;
  211. _M_buf_count = __x._M_buf_count;
  212. _STLP_STD::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
  213. return *this;
  214. }
  215. void push_back(value_type __x) {
  216. if (_M_buf_count < _Buf_sz) {
  217. _M_buffer[_M_buf_count] = __x;
  218. ++_M_buf_count;
  219. } else {
  220. flush();
  221. _M_buffer[0] = __x;
  222. _M_buf_count = 1;
  223. }
  224. }
  225. void append(const value_type *__s, size_t __len) {
  226. if (__len + _M_buf_count <= _Buf_sz) {
  227. size_t __i = _M_buf_count;
  228. size_t __j = 0;
  229. for (; __j < __len; __i++, __j++) {
  230. _M_buffer[__i] = __s[__j];
  231. }
  232. _M_buf_count += __len;
  233. } else if (0 == _M_buf_count) {
  234. _M_prefix->append(__s, __s + __len);
  235. } else {
  236. flush();
  237. append(__s, __len);
  238. }
  239. }
  240. _Self& write(const value_type *__s, size_t __len) {
  241. append(__s, __len);
  242. return *this;
  243. }
  244. _Self& put(value_type __x) {
  245. push_back(__x);
  246. return *this;
  247. }
  248. _Self& operator=(const value_type& __rhs) {
  249. push_back(__rhs);
  250. return *this;
  251. }
  252. _Self& operator*() { return *this; }
  253. _Self& operator++() { return *this; }
  254. _Self& operator++(int) { return *this; }
  255. };
  256. // The following should be treated as private, at least for now.
  257. template<class _CharT>
  258. class _Rope_char_consumer {
  259. #if !defined (_STLP_MEMBER_TEMPLATES)
  260. public:
  261. //Without member templates we have to use run-time parameterization.
  262. // The symmetry with char_producer is accidental and temporary.
  263. virtual ~_Rope_char_consumer() {}
  264. virtual bool operator()(const _CharT* __buffer, size_t __len) = 0;
  265. #endif
  266. };
  267. //
  268. // What follows should really be local to rope. Unfortunately,
  269. // that doesn't work, since it makes it impossible to define generic
  270. // equality on rope iterators. According to the draft standard, the
  271. // template parameters for such an equality operator cannot be inferred
  272. // from the occurence of a member class as a parameter.
  273. // (SGI compilers in fact allow this, but the __result wouldn't be
  274. // portable.)
  275. // Similarly, some of the static member functions are member functions
  276. // only to avoid polluting the global namespace, and to circumvent
  277. // restrictions on type inference for template functions.
  278. //
  279. //
  280. // The internal data structure for representing a rope. This is
  281. // private to the implementation. A rope is really just a pointer
  282. // to one of these.
  283. //
  284. // A few basic functions for manipulating this data structure
  285. // are members of _RopeRep. Most of the more complex algorithms
  286. // are implemented as rope members.
  287. //
  288. // Some of the static member functions of _RopeRep have identically
  289. // named functions in rope that simply invoke the _RopeRep versions.
  290. //
  291. template<class _CharT, class _Alloc>
  292. struct _Rope_RopeRep
  293. : public _Refcount_Base
  294. {
  295. typedef _Rope_RopeRep<_CharT, _Alloc> _Self;
  296. public:
  297. //
  298. // GAB: 11/09/05
  299. //
  300. // "__ROPE_DEPTH_SIZE" is set to one more then the "__ROPE_MAX_DEPTH".
  301. // This was originally just an addition of "__ROPE_MAX_DEPTH + 1"
  302. // but this addition causes the sunpro compiler to complain about
  303. // multiple declarations during the initialization of "_S_min_len".
  304. // Changed to be a fixed value and the sunpro compiler appears to
  305. // be happy???
  306. //
  307. # define __ROPE_MAX_DEPTH 45
  308. # define __ROPE_DEPTH_SIZE 46 // __ROPE_MAX_DEPTH + 1
  309. enum { _S_max_rope_depth = __ROPE_MAX_DEPTH };
  310. enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
  311. // Apparently needed by VC++
  312. // The data fields of leaves are allocated with some
  313. // extra space, to accomodate future growth and for basic
  314. // character types, to hold a trailing eos character.
  315. enum { _S_alloc_granularity = 8 };
  316. _Tag _M_tag:8;
  317. bool _M_is_balanced:8;
  318. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  319. typedef _Alloc allocator_type;
  320. allocator_type get_allocator() const { return allocator_type(_M_size); }
  321. unsigned char _M_depth;
  322. _CharT* _STLP_VOLATILE _M_c_string;
  323. _STLP_PRIV _STLP_alloc_proxy<size_t, _CharT, allocator_type> _M_size;
  324. #ifdef _STLP_NO_ARROW_OPERATOR
  325. _Rope_RopeRep() : _Refcount_Base(1), _M_size(allocator_type(), 0) {
  326. # if defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
  327. _STLP_CHECK_RUNTIME_COMPATIBILITY();
  328. # endif
  329. }
  330. #endif
  331. /* Flattened version of string, if needed. */
  332. /* typically 0. */
  333. /* If it's not 0, then the memory is owned */
  334. /* by this node. */
  335. /* In the case of a leaf, this may point to */
  336. /* the same memory as the data field. */
  337. _Rope_RopeRep(_Tag __t, unsigned char __d, bool __b, size_t _p_size,
  338. allocator_type __a) :
  339. _Refcount_Base(1),
  340. _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0), _M_size(__a, _p_size) {
  341. #if defined (_STLP_CHECK_RUNTIME_COMPATIBILITY)
  342. _STLP_CHECK_RUNTIME_COMPATIBILITY();
  343. #endif
  344. }
  345. typedef _STLP_TYPENAME _STLP_PRIV _BasicCharType<_CharT>::_Ret _IsBasicCharType;
  346. #if 0
  347. /* Please tell why this code is necessary if you uncomment it.
  348. * Problem with it is that rope implementation expect that _S_rounded_up_size(n)
  349. * returns a size > n in order to store the terminating null charater. When
  350. * instanciation type is not a char or wchar_t this is not guaranty resulting in
  351. * memory overrun.
  352. */
  353. static size_t _S_rounded_up_size_aux(size_t __n, __true_type const& /*_IsBasicCharType*/) {
  354. // Allow slop for in-place expansion.
  355. return (__n + _S_alloc_granularity) & ~(_S_alloc_granularity - 1);
  356. }
  357. static size_t _S_rounded_up_size_aux(size_t __n, __false_type const& /*_IsBasicCharType*/) {
  358. // Allow slop for in-place expansion.
  359. return (__n + _S_alloc_granularity - 1) & ~(_S_alloc_granularity - 1);
  360. }
  361. #endif
  362. // fbp : moved from RopeLeaf
  363. static size_t _S_rounded_up_size(size_t __n)
  364. //{ return _S_rounded_up_size_aux(__n, _IsBasicCharType()); }
  365. { return (__n + _S_alloc_granularity) & ~(_S_alloc_granularity - 1); }
  366. static void _S_free_string( _CharT* __s, size_t __len,
  367. allocator_type __a) {
  368. _STLP_STD::_Destroy_Range(__s, __s + __len);
  369. // This has to be a static member, so this gets a bit messy
  370. # ifndef _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE
  371. __a.deallocate(__s, _S_rounded_up_size(__len)); //*ty 03/24/2001 - restored not to use __stl_alloc_rebind() since it is not defined under _STLP_MEMBER_TEMPLATE_CLASSES
  372. # else
  373. __stl_alloc_rebind (__a, (_CharT*)0).deallocate(__s, _S_rounded_up_size(__len));
  374. # endif
  375. }
  376. // Deallocate data section of a leaf.
  377. // This shouldn't be a member function.
  378. // But its hard to do anything else at the
  379. // moment, because it's templatized w.r.t.
  380. // an allocator.
  381. // Does nothing if __GC is defined.
  382. void _M_free_c_string();
  383. void _M_free_tree();
  384. // Deallocate t. Assumes t is not 0.
  385. void _M_unref_nonnil() {
  386. if (_M_decr() == 0) _M_free_tree();
  387. }
  388. void _M_ref_nonnil() {
  389. _M_incr();
  390. }
  391. static void _S_unref(_Self* __t) {
  392. if (0 != __t) {
  393. __t->_M_unref_nonnil();
  394. }
  395. }
  396. static void _S_ref(_Self* __t) {
  397. if (0 != __t) __t->_M_incr();
  398. }
  399. //static void _S_free_if_unref(_Self* __t) {
  400. // if (0 != __t && 0 == __t->_M_ref_count) __t->_M_free_tree();
  401. //}
  402. };
  403. template<class _CharT, class _Alloc>
  404. struct _Rope_RopeLeaf : public _Rope_RopeRep<_CharT,_Alloc> {
  405. public:
  406. _CharT* _M_data; /* Not necessarily 0 terminated. */
  407. /* The allocated size is */
  408. /* _S_rounded_up_size(size), except */
  409. /* in the GC case, in which it */
  410. /* doesn't matter. */
  411. private:
  412. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  413. typedef typename _RopeRep::_IsBasicCharType _IsBasicCharType;
  414. void _M_init(__true_type const& /*_IsBasicCharType*/) {
  415. this->_M_c_string = _M_data;
  416. }
  417. void _M_init(__false_type const& /*_IsBasicCharType*/) {}
  418. public:
  419. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  420. typedef typename _RopeRep::allocator_type allocator_type;
  421. _Rope_RopeLeaf( _CharT* __d, size_t _p_size, allocator_type __a)
  422. : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_leaf, 0, true, _p_size, __a),
  423. _M_data(__d) {
  424. _STLP_ASSERT(_p_size > 0)
  425. _M_init(_IsBasicCharType());
  426. }
  427. # ifdef _STLP_NO_ARROW_OPERATOR
  428. _Rope_RopeLeaf() {}
  429. _Rope_RopeLeaf(const _Rope_RopeLeaf<_CharT, _Alloc>& ) {}
  430. # endif
  431. // The constructor assumes that d has been allocated with
  432. // the proper allocator and the properly padded size.
  433. // In contrast, the destructor deallocates the data:
  434. ~_Rope_RopeLeaf() {
  435. if (_M_data != this->_M_c_string) {
  436. this->_M_free_c_string();
  437. }
  438. _RopeRep::_S_free_string(_M_data, this->_M_size._M_data, this->get_allocator());
  439. }
  440. };
  441. template<class _CharT, class _Alloc>
  442. struct _Rope_RopeConcatenation : public _Rope_RopeRep<_CharT, _Alloc> {
  443. private:
  444. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  445. public:
  446. _RopeRep* _M_left;
  447. _RopeRep* _M_right;
  448. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  449. typedef typename _RopeRep::allocator_type allocator_type;
  450. _Rope_RopeConcatenation(_RopeRep* __l, _RopeRep* __r, allocator_type __a)
  451. : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_concat,
  452. (max)(__l->_M_depth, __r->_M_depth) + 1, false,
  453. __l->_M_size._M_data + __r->_M_size._M_data, __a), _M_left(__l), _M_right(__r)
  454. {}
  455. # ifdef _STLP_NO_ARROW_OPERATOR
  456. _Rope_RopeConcatenation() {}
  457. _Rope_RopeConcatenation(const _Rope_RopeConcatenation<_CharT, _Alloc>&) {}
  458. # endif
  459. ~_Rope_RopeConcatenation() {
  460. this->_M_free_c_string();
  461. _M_left->_M_unref_nonnil();
  462. _M_right->_M_unref_nonnil();
  463. }
  464. };
  465. template <class _CharT, class _Alloc>
  466. struct _Rope_RopeFunction : public _Rope_RopeRep<_CharT, _Alloc> {
  467. private:
  468. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  469. public:
  470. char_producer<_CharT>* _M_fn;
  471. /*
  472. * Char_producer is owned by the
  473. * rope and should be explicitly
  474. * deleted when the rope becomes
  475. * inaccessible.
  476. */
  477. bool _M_delete_when_done;
  478. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  479. typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
  480. # ifdef _STLP_NO_ARROW_OPERATOR
  481. _Rope_RopeFunction() {}
  482. _Rope_RopeFunction(const _Rope_RopeFunction<_CharT, _Alloc>& ) {}
  483. # endif
  484. _Rope_RopeFunction(char_producer<_CharT>* __f, size_t _p_size,
  485. bool __d, allocator_type __a)
  486. : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_function, 0, true, _p_size, __a), _M_fn(__f)
  487. , _M_delete_when_done(__d)
  488. { _STLP_ASSERT(_p_size > 0) }
  489. ~_Rope_RopeFunction() {
  490. this->_M_free_c_string();
  491. if (_M_delete_when_done) {
  492. delete _M_fn;
  493. }
  494. }
  495. };
  496. /*
  497. * Substring results are usually represented using just
  498. * concatenation nodes. But in the case of very long flat ropes
  499. * or ropes with a functional representation that isn't practical.
  500. * In that case, we represent the __result as a special case of
  501. * RopeFunction, whose char_producer points back to the rope itself.
  502. * In all cases except repeated substring operations and
  503. * deallocation, we treat the __result as a RopeFunction.
  504. */
  505. template<class _CharT, class _Alloc>
  506. struct _Rope_RopeSubstring : public char_producer<_CharT>, public _Rope_RopeFunction<_CharT,_Alloc> {
  507. public:
  508. // XXX this whole class should be rewritten.
  509. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  510. _RopeRep *_M_base; // not 0
  511. size_t _M_start;
  512. /* virtual */ void operator()(size_t __start_pos, size_t __req_len,
  513. _CharT* __buffer) {
  514. typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
  515. typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
  516. switch (_M_base->_M_tag) {
  517. case _RopeRep::_S_function:
  518. case _RopeRep::_S_substringfn:
  519. {
  520. char_producer<_CharT>* __fn =
  521. __STATIC_CAST(_RopeFunction*, _M_base)->_M_fn;
  522. _STLP_ASSERT(__start_pos + __req_len <= this->_M_size._M_data)
  523. _STLP_ASSERT(_M_start + this->_M_size._M_data <= _M_base->_M_size._M_data)
  524. (*__fn)(__start_pos + _M_start, __req_len, __buffer);
  525. }
  526. break;
  527. case _RopeRep::_S_leaf:
  528. {
  529. _CharT* __s =
  530. __STATIC_CAST(_RopeLeaf*, _M_base)->_M_data;
  531. _STLP_PRIV __ucopy_n(__s + __start_pos + _M_start, __req_len, __buffer);
  532. }
  533. break;
  534. default:
  535. _STLP_ASSERT(false)
  536. ;
  537. }
  538. }
  539. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  540. typedef typename _RopeRep::allocator_type allocator_type;
  541. _Rope_RopeSubstring(_RopeRep* __b, size_t __s, size_t __l, allocator_type __a)
  542. : _Rope_RopeFunction<_CharT,_Alloc>(this, __l, false, __a),
  543. _M_base(__b), _M_start(__s) {
  544. _STLP_ASSERT(__l > 0)
  545. _STLP_ASSERT(__s + __l <= __b->_M_size._M_data)
  546. _M_base->_M_ref_nonnil();
  547. this->_M_tag = _RopeRep::_S_substringfn;
  548. }
  549. virtual ~_Rope_RopeSubstring()
  550. { _M_base->_M_unref_nonnil(); }
  551. };
  552. /*
  553. * Self-destructing pointers to Rope_rep.
  554. * These are not conventional smart pointers. Their
  555. * only purpose in life is to ensure that unref is called
  556. * on the pointer either at normal exit or if an exception
  557. * is raised. It is the caller's responsibility to
  558. * adjust reference counts when these pointers are initialized
  559. * or assigned to. (This convention significantly reduces
  560. * the number of potentially expensive reference count
  561. * updates.)
  562. */
  563. template<class _CharT, class _Alloc>
  564. struct _Rope_self_destruct_ptr {
  565. _Rope_RopeRep<_CharT,_Alloc>* _M_ptr;
  566. ~_Rope_self_destruct_ptr()
  567. { _Rope_RopeRep<_CharT,_Alloc>::_S_unref(_M_ptr); }
  568. # ifdef _STLP_USE_EXCEPTIONS
  569. _Rope_self_destruct_ptr() : _M_ptr(0) {}
  570. # else
  571. _Rope_self_destruct_ptr() {}
  572. # endif
  573. _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT,_Alloc>* __p) : _M_ptr(__p) {}
  574. _Rope_RopeRep<_CharT,_Alloc>& operator*() { return *_M_ptr; }
  575. _Rope_RopeRep<_CharT,_Alloc>* operator->() { return _M_ptr; }
  576. operator _Rope_RopeRep<_CharT,_Alloc>*() { return _M_ptr; }
  577. _Rope_self_destruct_ptr<_CharT, _Alloc>&
  578. operator= (_Rope_RopeRep<_CharT,_Alloc>* __x)
  579. { _M_ptr = __x; return *this; }
  580. };
  581. /*
  582. * Dereferencing a nonconst iterator has to return something
  583. * that behaves almost like a reference. It's not possible to
  584. * return an actual reference since assignment requires extra
  585. * work. And we would get into the same problems as with the
  586. * CD2 version of basic_string.
  587. */
  588. template<class _CharT, class _Alloc>
  589. class _Rope_char_ref_proxy {
  590. typedef _Rope_char_ref_proxy<_CharT, _Alloc> _Self;
  591. friend class rope<_CharT,_Alloc>;
  592. friend class _Rope_iterator<_CharT,_Alloc>;
  593. friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
  594. typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
  595. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  596. typedef rope<_CharT,_Alloc> _My_rope;
  597. size_t _M_pos;
  598. _CharT _M_current;
  599. bool _M_current_valid;
  600. _My_rope* _M_root; // The whole rope.
  601. public:
  602. _Rope_char_ref_proxy(_My_rope* __r, size_t __p) :
  603. _M_pos(__p), _M_current_valid(false), _M_root(__r) {}
  604. _Rope_char_ref_proxy(const _Self& __x) :
  605. _M_pos(__x._M_pos), _M_current_valid(false), _M_root(__x._M_root) {}
  606. // Don't preserve cache if the reference can outlive the
  607. // expression. We claim that's not possible without calling
  608. // a copy constructor or generating reference to a proxy
  609. // reference. We declare the latter to have undefined semantics.
  610. _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
  611. : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
  612. inline operator _CharT () const;
  613. _Self& operator= (_CharT __c);
  614. _Rope_char_ptr_proxy<_CharT, _Alloc> operator& () const;
  615. _Self& operator= (const _Self& __c) {
  616. return operator=((_CharT)__c);
  617. }
  618. };
  619. #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
  620. template<class _CharT, class __Alloc>
  621. inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
  622. _Rope_char_ref_proxy <_CharT, __Alloc > __b) {
  623. _CharT __tmp = __a;
  624. __a = __b;
  625. __b = __tmp;
  626. }
  627. #else
  628. // There is no really acceptable way to handle this. The default
  629. // definition of swap doesn't work for proxy references.
  630. // It can't really be made to work, even with ugly hacks, since
  631. // the only unusual operation it uses is the copy constructor, which
  632. // is needed for other purposes. We provide a macro for
  633. // full specializations, and instantiate the most common case.
  634. # define _ROPE_SWAP_SPECIALIZATION(_CharT, __Alloc) \
  635. inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a, \
  636. _Rope_char_ref_proxy <_CharT, __Alloc > __b) { \
  637. _CharT __tmp = __a; \
  638. __a = __b; \
  639. __b = __tmp; \
  640. }
  641. _ROPE_SWAP_SPECIALIZATION(char, allocator<char>)
  642. # ifndef _STLP_NO_WCHAR_T
  643. _ROPE_SWAP_SPECIALIZATION(wchar_t, allocator<wchar_t>)
  644. # endif
  645. #endif /* !_STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  646. template<class _CharT, class _Alloc>
  647. class _Rope_char_ptr_proxy {
  648. // XXX this class should be rewritten.
  649. public:
  650. typedef _Rope_char_ptr_proxy<_CharT, _Alloc> _Self;
  651. friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
  652. size_t _M_pos;
  653. rope<_CharT,_Alloc>* _M_root; // The whole rope.
  654. _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
  655. : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
  656. _Rope_char_ptr_proxy(const _Self& __x)
  657. : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
  658. _Rope_char_ptr_proxy() {}
  659. _Rope_char_ptr_proxy(_CharT* __x) : _M_pos(0), _M_root(0) {
  660. _STLP_ASSERT(0 == __x)
  661. }
  662. _Self& operator= (const _Self& __x) {
  663. _M_pos = __x._M_pos;
  664. _M_root = __x._M_root;
  665. return *this;
  666. }
  667. _Rope_char_ref_proxy<_CharT,_Alloc> operator*() const {
  668. return _Rope_char_ref_proxy<_CharT,_Alloc>(_M_root, _M_pos);
  669. }
  670. };
  671. /*
  672. * Rope iterators:
  673. * Unlike in the C version, we cache only part of the stack
  674. * for rope iterators, since they must be efficiently copyable.
  675. * When we run out of cache, we have to reconstruct the iterator
  676. * value.
  677. * Pointers from iterators are not included in reference counts.
  678. * Iterators are assumed to be thread private. Ropes can
  679. * be shared.
  680. */
  681. template<class _CharT, class _Alloc>
  682. class _Rope_iterator_base
  683. /* : public random_access_iterator<_CharT, ptrdiff_t> */
  684. {
  685. friend class rope<_CharT,_Alloc>;
  686. typedef _Rope_iterator_base<_CharT, _Alloc> _Self;
  687. typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcat;
  688. public:
  689. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  690. enum { _S_path_cache_len = 4 }; // Must be <= 9 because of _M_path_direction.
  691. enum { _S_iterator_buf_len = 15 };
  692. size_t _M_current_pos;
  693. // The whole rope.
  694. _RopeRep* _M_root;
  695. // Starting position for current leaf
  696. size_t _M_leaf_pos;
  697. // Buffer possibly containing current char.
  698. _CharT* _M_buf_start;
  699. // Pointer to current char in buffer, != 0 ==> buffer valid.
  700. _CharT* _M_buf_ptr;
  701. // One past __last valid char in buffer.
  702. _CharT* _M_buf_end;
  703. // What follows is the path cache. We go out of our
  704. // way to make this compact.
  705. // Path_end contains the bottom section of the path from
  706. // the root to the current leaf.
  707. struct {
  708. # if defined (__BORLANDC__) && (__BORLANDC__ < 0x560)
  709. _RopeRep const*_M_data[4];
  710. # else
  711. _RopeRep const*_M_data[_S_path_cache_len];
  712. # endif
  713. } _M_path_end;
  714. // Last valid __pos in path_end;
  715. // _M_path_end[0] ... _M_path_end[_M_leaf_index-1]
  716. // point to concatenation nodes.
  717. int _M_leaf_index;
  718. // (_M_path_directions >> __i) & 1 is 1
  719. // if we got from _M_path_end[leaf_index - __i - 1]
  720. // to _M_path_end[leaf_index - __i] by going to the
  721. // __right. Assumes path_cache_len <= 9.
  722. unsigned char _M_path_directions;
  723. // Short buffer for surrounding chars.
  724. // This is useful primarily for
  725. // RopeFunctions. We put the buffer
  726. // here to avoid locking in the
  727. // multithreaded case.
  728. // The cached path is generally assumed to be valid
  729. // only if the buffer is valid.
  730. struct {
  731. # if defined (__BORLANDC__) && (__BORLANDC__ < 0x560)
  732. _CharT _M_data[15];
  733. # else
  734. _CharT _M_data[_S_iterator_buf_len];
  735. # endif
  736. } _M_tmp_buf;
  737. // Set buffer contents given path cache.
  738. static void _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x);
  739. // Set buffer contents and path cache.
  740. static void _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x);
  741. // As above, but assumes path cache is valid for previous posn.
  742. static void _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x);
  743. _Rope_iterator_base() {}
  744. _Rope_iterator_base(_RopeRep* __root, size_t __pos)
  745. : _M_current_pos(__pos),_M_root(__root), _M_buf_ptr(0) {}
  746. void _M_incr(size_t __n);
  747. void _M_decr(size_t __n);
  748. public:
  749. size_t index() const { return _M_current_pos; }
  750. private:
  751. void _M_copy_buf(const _Self& __x) {
  752. _M_tmp_buf = __x._M_tmp_buf;
  753. if (__x._M_buf_start == __x._M_tmp_buf._M_data) {
  754. _M_buf_start = _M_tmp_buf._M_data;
  755. _M_buf_end = _M_buf_start + (__x._M_buf_end - __x._M_buf_start);
  756. _M_buf_ptr = _M_buf_start + (__x._M_buf_ptr - __x._M_buf_start);
  757. } else {
  758. _M_buf_end = __x._M_buf_end;
  759. }
  760. }
  761. public:
  762. _Rope_iterator_base(const _Self& __x) :
  763. _M_current_pos(__x._M_current_pos),
  764. _M_root(__x._M_root),
  765. _M_leaf_pos( __x._M_leaf_pos ),
  766. _M_buf_start(__x._M_buf_start),
  767. _M_buf_ptr(__x._M_buf_ptr),
  768. _M_path_end(__x._M_path_end),
  769. _M_leaf_index(__x._M_leaf_index),
  770. _M_path_directions(__x._M_path_directions)
  771. {
  772. if (0 != __x._M_buf_ptr) {
  773. _M_copy_buf(__x);
  774. }
  775. }
  776. _Self& operator = (const _Self& __x)
  777. {
  778. _M_current_pos = __x._M_current_pos;
  779. _M_root = __x._M_root;
  780. _M_buf_start = __x._M_buf_start;
  781. _M_buf_ptr = __x._M_buf_ptr;
  782. _M_path_end = __x._M_path_end;
  783. _M_leaf_index = __x._M_leaf_index;
  784. _M_path_directions = __x._M_path_directions;
  785. _M_leaf_pos = __x._M_leaf_pos;
  786. if (0 != __x._M_buf_ptr) {
  787. _M_copy_buf(__x);
  788. }
  789. return *this;
  790. }
  791. };
  792. template<class _CharT, class _Alloc> class _Rope_iterator;
  793. template<class _CharT, class _Alloc>
  794. class _Rope_const_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
  795. friend class rope<_CharT,_Alloc>;
  796. typedef _Rope_const_iterator<_CharT, _Alloc> _Self;
  797. typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
  798. // protected:
  799. public:
  800. # ifndef _STLP_HAS_NO_NAMESPACES
  801. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  802. // The one from the base class may not be directly visible.
  803. # endif
  804. _Rope_const_iterator(const _RopeRep* __root, size_t __pos):
  805. _Rope_iterator_base<_CharT,_Alloc>(__CONST_CAST(_RopeRep*,__root), __pos)
  806. // Only nonconst iterators modify root ref count
  807. {}
  808. public:
  809. typedef _CharT reference; // Really a value. Returning a reference
  810. // Would be a mess, since it would have
  811. // to be included in refcount.
  812. typedef const _CharT* pointer;
  813. typedef _CharT value_type;
  814. typedef ptrdiff_t difference_type;
  815. typedef random_access_iterator_tag iterator_category;
  816. public:
  817. _Rope_const_iterator() {}
  818. _Rope_const_iterator(const _Self& __x) :
  819. _Rope_iterator_base<_CharT,_Alloc>(__x) { }
  820. _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x):
  821. _Rope_iterator_base<_CharT,_Alloc>(__x) {}
  822. _Rope_const_iterator(const rope<_CharT,_Alloc>& __r, size_t __pos) :
  823. _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr._M_data, __pos) {}
  824. _Self& operator= (const _Self& __x) {
  825. _Base::operator=(__x);
  826. return *this;
  827. }
  828. reference operator*() {
  829. if (0 == this->_M_buf_ptr)
  830. #if !defined (__DMC__)
  831. _S_setcache(*this);
  832. #else
  833. { _Rope_iterator_base<_CharT, _Alloc>* __x = this; _S_setcache(*__x); }
  834. #endif
  835. return *(this->_M_buf_ptr);
  836. }
  837. _Self& operator++()
  838. {
  839. if ( this->_M_buf_ptr != 0 ) {
  840. _CharT *__next = this->_M_buf_ptr + 1;
  841. if ( __next < this->_M_buf_end ) {
  842. this->_M_buf_ptr = __next;
  843. ++this->_M_current_pos;
  844. return *this;
  845. }
  846. }
  847. this->_M_incr(1);
  848. return *this;
  849. }
  850. _Self& operator+=(ptrdiff_t __n) {
  851. if (__n >= 0) {
  852. this->_M_incr(__n);
  853. } else {
  854. this->_M_decr(-__n);
  855. }
  856. return *this;
  857. }
  858. _Self& operator--() {
  859. this->_M_decr(1);
  860. return *this;
  861. }
  862. _Self& operator-=(ptrdiff_t __n) {
  863. if (__n >= 0) {
  864. this->_M_decr(__n);
  865. } else {
  866. this->_M_incr(-__n);
  867. }
  868. return *this;
  869. }
  870. _Self operator++(int) {
  871. size_t __old_pos = this->_M_current_pos;
  872. this->_M_incr(1);
  873. return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
  874. // This makes a subsequent dereference expensive.
  875. // Perhaps we should instead copy the iterator
  876. // if it has a valid cache?
  877. }
  878. _Self operator--(int) {
  879. size_t __old_pos = this->_M_current_pos;
  880. this->_M_decr(1);
  881. return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
  882. }
  883. inline reference operator[](size_t __n);
  884. };
  885. template<class _CharT, class _Alloc>
  886. class _Rope_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
  887. friend class rope<_CharT,_Alloc>;
  888. typedef _Rope_iterator<_CharT, _Alloc> _Self;
  889. typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
  890. typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
  891. public:
  892. rope<_CharT,_Alloc>* _M_root_rope;
  893. // root is treated as a cached version of this,
  894. // and is used to detect changes to the underlying
  895. // rope.
  896. // Root is included in the reference count.
  897. // This is necessary so that we can detect changes reliably.
  898. // Unfortunately, it requires careful bookkeeping for the
  899. // nonGC case.
  900. _Rope_iterator(rope<_CharT,_Alloc>* __r, size_t __pos);
  901. void _M_check();
  902. public:
  903. typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
  904. typedef _Rope_char_ref_proxy<_CharT,_Alloc>* pointer;
  905. typedef _CharT value_type;
  906. typedef ptrdiff_t difference_type;
  907. typedef random_access_iterator_tag iterator_category;
  908. public:
  909. ~_Rope_iterator() { //*TY 5/6/00 - added dtor to balance reference count
  910. _RopeRep::_S_unref(this->_M_root);
  911. }
  912. rope<_CharT,_Alloc>& container() { return *_M_root_rope; }
  913. _Rope_iterator() {
  914. this->_M_root = 0; // Needed for reference counting.
  915. }
  916. _Rope_iterator(const _Self& __x) :
  917. _Rope_iterator_base<_CharT,_Alloc>(__x) {
  918. _M_root_rope = __x._M_root_rope;
  919. _RopeRep::_S_ref(this->_M_root);
  920. }
  921. _Rope_iterator(rope<_CharT,_Alloc>& __r, size_t __pos);
  922. _Self& operator= (const _Self& __x) {
  923. _RopeRep* __old = this->_M_root;
  924. _RopeRep::_S_ref(__x._M_root);
  925. _Base::operator=(__x);
  926. _M_root_rope = __x._M_root_rope;
  927. _RopeRep::_S_unref(__old);
  928. return *this;
  929. }
  930. reference operator*() {
  931. _M_check();
  932. if (0 == this->_M_buf_ptr) {
  933. return reference(_M_root_rope, this->_M_current_pos);
  934. } else {
  935. return reference(_M_root_rope, this->_M_current_pos, *(this->_M_buf_ptr));
  936. }
  937. }
  938. _Self& operator++() {
  939. this->_M_incr(1);
  940. return *this;
  941. }
  942. _Self& operator+=(ptrdiff_t __n) {
  943. if (__n >= 0) {
  944. this->_M_incr(__n);
  945. } else {
  946. this->_M_decr(-__n);
  947. }
  948. return *this;
  949. }
  950. _Self& operator--() {
  951. this->_M_decr(1);
  952. return *this;
  953. }
  954. _Self& operator-=(ptrdiff_t __n) {
  955. if (__n >= 0) {
  956. this->_M_decr(__n);
  957. } else {
  958. this->_M_incr(-__n);
  959. }
  960. return *this;
  961. }
  962. _Self operator++(int) {
  963. size_t __old_pos = this->_M_current_pos;
  964. this->_M_incr(1);
  965. return _Self(_M_root_rope, __old_pos);
  966. }
  967. _Self operator--(int) {
  968. size_t __old_pos = this->_M_current_pos;
  969. this->_M_decr(1);
  970. return _Self(_M_root_rope, __old_pos);
  971. }
  972. reference operator[](ptrdiff_t __n) {
  973. return reference(_M_root_rope, this->_M_current_pos + __n);
  974. }
  975. };
  976. # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
  977. template <class _CharT, class _Alloc>
  978. inline random_access_iterator_tag
  979. iterator_category(const _Rope_iterator<_CharT,_Alloc>&) { return random_access_iterator_tag();}
  980. template <class _CharT, class _Alloc>
  981. inline _CharT* value_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
  982. template <class _CharT, class _Alloc>
  983. inline ptrdiff_t* distance_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
  984. template <class _CharT, class _Alloc>
  985. inline random_access_iterator_tag
  986. iterator_category(const _Rope_const_iterator<_CharT,_Alloc>&) { return random_access_iterator_tag(); }
  987. template <class _CharT, class _Alloc>
  988. inline _CharT* value_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
  989. template <class _CharT, class _Alloc>
  990. inline ptrdiff_t* distance_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
  991. #endif /* _STLP_USE_OLD_HP_ITERATOR_QUERIES */
  992. template <class _CharT, class _Alloc, class _CharConsumer>
  993. bool _S_apply_to_pieces(_CharConsumer& __c,
  994. _Rope_RopeRep<_CharT, _Alloc> *__r,
  995. size_t __begin, size_t __end);
  996. // begin and end are assumed to be in range.
  997. template <class _CharT, class _Alloc>
  998. class rope
  999. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  1000. : public __stlport_class<rope<_CharT, _Alloc> >
  1001. #endif
  1002. {
  1003. typedef rope<_CharT,_Alloc> _Self;
  1004. public:
  1005. typedef _CharT value_type;
  1006. typedef ptrdiff_t difference_type;
  1007. typedef size_t size_type;
  1008. typedef _CharT const_reference;
  1009. typedef const _CharT* const_pointer;
  1010. typedef _Rope_iterator<_CharT,_Alloc> iterator;
  1011. typedef _Rope_const_iterator<_CharT,_Alloc> const_iterator;
  1012. typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
  1013. typedef _Rope_char_ptr_proxy<_CharT,_Alloc> pointer;
  1014. friend class _Rope_iterator<_CharT,_Alloc>;
  1015. friend class _Rope_const_iterator<_CharT,_Alloc>;
  1016. friend struct _Rope_RopeRep<_CharT,_Alloc>;
  1017. friend class _Rope_iterator_base<_CharT,_Alloc>;
  1018. friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
  1019. friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
  1020. friend struct _Rope_RopeSubstring<_CharT,_Alloc>;
  1021. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  1022. protected:
  1023. typedef _CharT* _Cstrptr;
  1024. static _CharT _S_empty_c_str[1];
  1025. enum { _S_copy_max = 23 };
  1026. // For strings shorter than _S_copy_max, we copy to
  1027. // concatenate.
  1028. typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
  1029. typedef typename _RopeRep::_IsBasicCharType _IsBasicCharType;
  1030. public:
  1031. _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
  1032. typedef _Alloc allocator_type;
  1033. public:
  1034. // The only data member of a rope:
  1035. _STLP_PRIV _STLP_alloc_proxy<_RopeRep*, _CharT, allocator_type> _M_tree_ptr;
  1036. public:
  1037. allocator_type get_allocator() const { return allocator_type(_M_tree_ptr); }
  1038. public:
  1039. typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcatenation;
  1040. typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
  1041. typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
  1042. typedef _Rope_RopeSubstring<_CharT,_Alloc> _RopeSubstring;
  1043. // Retrieve a character at the indicated position.
  1044. static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
  1045. // Obtain a pointer to the character at the indicated position.
  1046. // The pointer can be used to change the character.
  1047. // If such a pointer cannot be produced, as is frequently the
  1048. // case, 0 is returned instead.
  1049. // (Returns nonzero only if all nodes in the path have a refcount
  1050. // of 1.)
  1051. static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
  1052. static void _S_unref(_RopeRep* __t) {
  1053. _RopeRep::_S_unref(__t);
  1054. }
  1055. static void _S_ref(_RopeRep* __t) {
  1056. _RopeRep::_S_ref(__t);
  1057. }
  1058. typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
  1059. // _Result is counted in refcount.
  1060. static _RopeRep* _S_substring(_RopeRep* __base,
  1061. size_t __start, size_t __endp1);
  1062. static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
  1063. const _CharT* __iter, size_t __slen);
  1064. // Concatenate rope and char ptr, copying __s.
  1065. // Should really take an arbitrary iterator.
  1066. // Result is counted in refcount.
  1067. static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
  1068. const _CharT* __iter, size_t __slen);
  1069. // As above, but one reference to __r is about to be
  1070. // destroyed. Thus the pieces may be recycled if all
  1071. // relevent reference counts are 1.
  1072. // General concatenation on _RopeRep. _Result
  1073. // has refcount of 1. Adjusts argument refcounts.
  1074. static _RopeRep* _S_concat_rep(_RopeRep* __left, _RopeRep* __right);
  1075. public:
  1076. #if defined (_STLP_MEMBER_TEMPLATES)
  1077. template <class _CharConsumer>
  1078. #else
  1079. typedef _Rope_char_consumer<_CharT> _CharConsumer;
  1080. #endif
  1081. void apply_to_pieces(size_t __begin, size_t __end,
  1082. _CharConsumer& __c) const
  1083. { _S_apply_to_pieces(__c, _M_tree_ptr._M_data, __begin, __end); }
  1084. protected:
  1085. static size_t _S_rounded_up_size(size_t __n)
  1086. { return _RopeRep::_S_rounded_up_size(__n); }
  1087. // Allocate and construct a RopeLeaf using the supplied allocator
  1088. // Takes ownership of s instead of copying.
  1089. static _RopeLeaf* _S_new_RopeLeaf(_CharT *__s,
  1090. size_t _p_size, allocator_type __a) {
  1091. _RopeLeaf* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
  1092. _RopeLeaf).allocate(1);
  1093. _STLP_TRY {
  1094. new(__space) _RopeLeaf(__s, _p_size, __a);
  1095. }
  1096. _STLP_UNWIND(_STLP_CREATE_ALLOCATOR(allocator_type,__a,
  1097. _RopeLeaf).deallocate(__space, 1))
  1098. return __space;
  1099. }
  1100. static _RopeConcatenation* _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
  1101. allocator_type __a) {
  1102. _RopeConcatenation* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
  1103. _RopeConcatenation).allocate(1);
  1104. return new(__space) _RopeConcatenation(__left, __right, __a);
  1105. }
  1106. static _RopeFunction* _S_new_RopeFunction(char_producer<_CharT>* __f,
  1107. size_t _p_size, bool __d, allocator_type __a) {
  1108. _RopeFunction* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
  1109. _RopeFunction).allocate(1);
  1110. return new(__space) _RopeFunction(__f, _p_size, __d, __a);
  1111. }
  1112. static _RopeSubstring* _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
  1113. size_t __l, allocator_type __a) {
  1114. _RopeSubstring* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
  1115. _RopeSubstring).allocate(1);
  1116. return new(__space) _RopeSubstring(__b, __s, __l, __a);
  1117. }
  1118. static
  1119. _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
  1120. size_t _p_size, allocator_type __a) {
  1121. if (0 == _p_size) return 0;
  1122. _CharT* __buf = _STLP_CREATE_ALLOCATOR(allocator_type,__a, _CharT).allocate(_S_rounded_up_size(_p_size));
  1123. _STLP_PRIV __ucopy_n(__s, _p_size, __buf);
  1124. _S_construct_null(__buf + _p_size);
  1125. _STLP_TRY {
  1126. return _S_new_RopeLeaf(__buf, _p_size, __a);
  1127. }
  1128. _STLP_UNWIND(_RopeRep::_S_free_string(__buf, _p_size, __a))
  1129. _STLP_RET_AFTER_THROW(0)
  1130. }
  1131. // Concatenation of nonempty strings.
  1132. // Always builds a concatenation node.
  1133. // Rebalances if the result is too deep.
  1134. // Result has refcount 1.
  1135. // Does not increment left and right ref counts even though
  1136. // they are referenced.
  1137. static _RopeRep*
  1138. _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
  1139. // Concatenation helper functions
  1140. static _RopeLeaf*
  1141. _S_leaf_concat_char_iter(_RopeLeaf* __r,
  1142. const _CharT* __iter, size_t __slen);
  1143. // Concatenate by copying leaf.
  1144. // should take an arbitrary iterator
  1145. // result has refcount 1.
  1146. static _RopeLeaf* _S_destr_leaf_concat_char_iter
  1147. (_RopeLeaf* __r, const _CharT* __iter, size_t __slen);
  1148. // A version that potentially clobbers __r if __r->_M_ref_count == 1.
  1149. // A helper function for exponentiating strings.
  1150. // This uses a nonstandard refcount convention.
  1151. // The result has refcount 0.
  1152. typedef _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
  1153. #if !defined (__GNUC__) || (__GNUC__ < 3)
  1154. friend _Concat_fn;
  1155. #else
  1156. friend struct _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc>;
  1157. #endif
  1158. public:
  1159. static size_t _S_char_ptr_len(const _CharT* __s) {
  1160. return char_traits<_CharT>::length(__s);
  1161. }
  1162. public: /* for operators */
  1163. rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
  1164. : _M_tree_ptr(__a, __t) { }
  1165. private:
  1166. // Copy __r to the _CharT buffer.
  1167. // Returns __buffer + __r->_M_size._M_data.
  1168. // Assumes that buffer is uninitialized.
  1169. static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
  1170. // Again, with explicit starting position and length.
  1171. // Assumes that buffer is uninitialized.
  1172. static _CharT* _S_flatten(_RopeRep* __r,
  1173. size_t __start, size_t __len,
  1174. _CharT* __buffer);
  1175. // fbp : HP aCC prohibits access to protected min_len from within static methods ( ?? )
  1176. public:
  1177. static const unsigned long _S_min_len[__ROPE_DEPTH_SIZE];
  1178. protected:
  1179. static bool _S_is_balanced(_RopeRep* __r)
  1180. { return (__r->_M_size._M_data >= _S_min_len[__r->_M_depth]); }
  1181. static bool _S_is_almost_balanced(_RopeRep* __r) {
  1182. return (__r->_M_depth == 0 ||
  1183. __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 1]);
  1184. }
  1185. static bool _S_is_roughly_balanced(_RopeRep* __r) {
  1186. return (__r->_M_depth <= 1 ||
  1187. __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 2]);
  1188. }
  1189. // Assumes the result is not empty.
  1190. static _RopeRep* _S_concat_and_set_balanced(_RopeRep* __left,
  1191. _RopeRep* __right) {
  1192. _RopeRep* __result = _S_concat_rep(__left, __right);
  1193. if (_S_is_balanced(__result)) __result->_M_is_balanced = true;
  1194. return __result;
  1195. }
  1196. // The basic rebalancing operation. Logically copies the
  1197. // rope. The result has refcount of 1. The client will
  1198. // usually decrement the reference count of __r.
  1199. // The result is within height 2 of balanced by the above
  1200. // definition.
  1201. static _RopeRep* _S_balance(_RopeRep* __r);
  1202. // Add all unbalanced subtrees to the forest of balanceed trees.
  1203. // Used only by balance.
  1204. static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
  1205. // Add __r to forest, assuming __r is already balanced.
  1206. static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
  1207. #ifdef _STLP_DEBUG
  1208. // Print to stdout, exposing structure
  1209. static void _S_dump(_RopeRep* __r, int __indent = 0);
  1210. #endif
  1211. // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
  1212. static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
  1213. void _STLP_FUNCTION_THROWS _M_throw_out_of_range() const;
  1214. void _M_reset(_RopeRep* __r) {
  1215. //if (__r != _M_tree_ptr._M_data) {
  1216. _S_unref(_M_tree_ptr._M_data);
  1217. _M_tree_ptr._M_data = __r;
  1218. //}
  1219. }
  1220. public:
  1221. bool empty() const { return 0 == _M_tree_ptr._M_data; }
  1222. // Comparison member function. This is public only for those
  1223. // clients that need a ternary comparison. Others
  1224. // should use the comparison operators below.
  1225. int compare(const _Self& __y) const {
  1226. return _S_compare(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data);
  1227. }
  1228. rope(const _CharT* __s, const allocator_type& __a = allocator_type())
  1229. : _M_tree_ptr(__a, _S_RopeLeaf_from_unowned_char_ptr(__s, _S_char_ptr_len(__s),__a))
  1230. {}
  1231. rope(const _CharT* __s, size_t __len,
  1232. const allocator_type& __a = allocator_type())
  1233. : _M_tree_ptr(__a, (_S_RopeLeaf_from_unowned_char_ptr(__s, __len, __a)))
  1234. {}
  1235. // Should perhaps be templatized with respect to the iterator type
  1236. // and use Sequence_buffer. (It should perhaps use sequence_buffer
  1237. // even now.)
  1238. rope(const _CharT *__s, const _CharT *__e,
  1239. const allocator_type& __a = allocator_type())
  1240. : _M_tree_ptr(__a, _S_RopeLeaf_from_unowned_char_ptr(__s, __e - __s, __a))
  1241. {}
  1242. rope(const const_iterator& __s, const const_iterator& __e,
  1243. const allocator_type& __a = allocator_type())
  1244. : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
  1245. __e._M_current_pos))
  1246. {}
  1247. rope(const iterator& __s, const iterator& __e,
  1248. const allocator_type& __a = allocator_type())
  1249. : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
  1250. __e._M_current_pos))
  1251. {}
  1252. rope(_CharT __c, const allocator_type& __a = allocator_type())
  1253. : _M_tree_ptr(__a, (_RopeRep*)0) {
  1254. _CharT* __buf = _M_tree_ptr.allocate(_S_rounded_up_size(1));
  1255. _Copy_Construct(__buf, __c);
  1256. _S_construct_null(__buf + 1);
  1257. _STLP_TRY {
  1258. _M_tree_ptr._M_data = _S_new_RopeLeaf(__buf, 1, __a);
  1259. }
  1260. _STLP_UNWIND(_RopeRep::_S_free_string(__buf, 1, __a))
  1261. }
  1262. rope(size_t __n, _CharT __c,
  1263. const allocator_type& __a = allocator_type()):
  1264. _M_tree_ptr(__a, (_RopeRep*)0) {
  1265. if (0 == __n)
  1266. return;
  1267. rope<_CharT,_Alloc> __result;
  1268. # define __exponentiate_threshold size_t(32)
  1269. _RopeRep* __remainder;
  1270. rope<_CharT,_Alloc> __remainder_rope;
  1271. // gcc-2.7.2 bugs
  1272. typedef _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
  1273. size_t __exponent = __n / __exponentiate_threshold;
  1274. size_t __rest = __n % __exponentiate_threshold;
  1275. if (0 == __rest) {
  1276. __remainder = 0;
  1277. } else {
  1278. _CharT* __rest_buffer = _M_tree_ptr.allocate(_S_rounded_up_size(__rest));
  1279. uninitialized_fill_n(__rest_buffer, __rest, __c);
  1280. _S_construct_null(__rest_buffer + __rest);
  1281. _STLP_TRY {
  1282. __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
  1283. }
  1284. _STLP_UNWIND(_RopeRep::_S_free_string(__rest_buffer, __rest, __a))
  1285. }
  1286. __remainder_rope._M_tree_ptr._M_data = __remainder;
  1287. if (__exponent != 0) {
  1288. _CharT* __base_buffer = _M_tree_ptr.allocate(_S_rounded_up_size(__exponentiate_threshold));
  1289. _RopeLeaf* __base_leaf;
  1290. rope<_CharT,_Alloc> __base_rope;
  1291. uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
  1292. _S_construct_null(__base_buffer + __exponentiate_threshold);
  1293. _STLP_TRY {
  1294. __base_leaf = _S_new_RopeLeaf(__base_buffer,
  1295. __exponentiate_threshold, __a);
  1296. }
  1297. _STLP_UNWIND(_RopeRep::_S_free_string(__base_buffer,
  1298. __exponentiate_threshold, __a))
  1299. __base_rope._M_tree_ptr._M_data = __base_leaf;
  1300. if (1 == __exponent) {
  1301. __result = __base_rope;
  1302. // One each for base_rope and __result
  1303. //_STLP_ASSERT(2 == __result._M_tree_ptr._M_data->_M_ref_count)
  1304. } else {
  1305. __result = _STLP_PRIV __power(__base_rope, __exponent, _Concat_fn());
  1306. }
  1307. if (0 != __remainder) {
  1308. __result += __remainder_rope;
  1309. }
  1310. } else {
  1311. __result = __remainder_rope;
  1312. }
  1313. _M_tree_ptr._M_data = __result._M_tree_ptr._M_data;
  1314. _M_tree_ptr._M_data->_M_ref_nonnil();
  1315. # undef __exponentiate_threshold
  1316. }
  1317. rope(const allocator_type& __a = allocator_type())
  1318. : _M_tree_ptr(__a, (_RopeRep*)0) {}
  1319. // Construct a rope from a function that can compute its members
  1320. rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
  1321. const allocator_type& __a = allocator_type())
  1322. : _M_tree_ptr(__a, (_RopeRep*)0) {
  1323. _M_tree_ptr._M_data = (0 == __len) ?
  1324. 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
  1325. }
  1326. rope(const _Self& __x)
  1327. : _M_tree_ptr(__x._M_tree_ptr, __x._M_tree_ptr._M_data) {
  1328. _S_ref(_M_tree_ptr._M_data);
  1329. }
  1330. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  1331. rope(__move_source<_Self> __src)
  1332. : _M_tree_ptr(__src.get()._M_tree_ptr, __src.get()._M_tree_ptr._M_data) {
  1333. __src.get()._M_tree_ptr._M_data = 0;
  1334. }
  1335. #endif
  1336. ~rope() {
  1337. _S_unref(_M_tree_ptr._M_data);
  1338. }
  1339. _Self& operator=(const _Self& __x) {
  1340. _STLP_ASSERT(get_allocator() == __x.get_allocator())
  1341. _S_ref(__x._M_tree_ptr._M_data);
  1342. _M_reset(__x._M_tree_ptr._M_data);
  1343. return *this;
  1344. }
  1345. void clear() {
  1346. _S_unref(_M_tree_ptr._M_data);
  1347. _M_tree_ptr._M_data = 0;
  1348. }
  1349. void push_back(_CharT __x) {
  1350. _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, &__x, 1));
  1351. }
  1352. void pop_back() {
  1353. _RopeRep* __old = _M_tree_ptr._M_data;
  1354. _M_tree_ptr._M_data =
  1355. _S_substring(_M_tree_ptr._M_data, 0, _M_tree_ptr._M_data->_M_size._M_data - 1);
  1356. _S_unref(__old);
  1357. }
  1358. _CharT back() const {
  1359. return _S_fetch(_M_tree_ptr._M_data, _M_tree_ptr._M_data->_M_size._M_data - 1);
  1360. }
  1361. void push_front(_CharT __x) {
  1362. _RopeRep* __old = _M_tree_ptr._M_data;
  1363. _RopeRep* __left =
  1364. _S_RopeLeaf_from_unowned_char_ptr(&__x, 1, _M_tree_ptr);
  1365. _STLP_TRY {
  1366. _M_tree_ptr._M_data = _S_concat_rep(__left, _M_tree_ptr._M_data);
  1367. _S_unref(__old);
  1368. _S_unref(__left);
  1369. }
  1370. _STLP_UNWIND(_S_unref(__left))
  1371. }
  1372. void pop_front() {
  1373. _RopeRep* __old = _M_tree_ptr._M_data;
  1374. _M_tree_ptr._M_data = _S_substring(_M_tree_ptr._M_data, 1, _M_tree_ptr._M_data->_M_size._M_data);
  1375. _S_unref(__old);
  1376. }
  1377. _CharT front() const {
  1378. return _S_fetch(_M_tree_ptr._M_data, 0);
  1379. }
  1380. void balance() {
  1381. _RopeRep* __old = _M_tree_ptr._M_data;
  1382. _M_tree_ptr._M_data = _S_balance(_M_tree_ptr._M_data);
  1383. _S_unref(__old);
  1384. }
  1385. void copy(_CharT* __buffer) const {
  1386. _STLP_STD::_Destroy_Range(__buffer, __buffer + size());
  1387. _S_flatten(_M_tree_ptr._M_data, __buffer);
  1388. }
  1389. /*
  1390. * This is the copy function from the standard, but
  1391. * with the arguments reordered to make it consistent with the
  1392. * rest of the interface.
  1393. * Note that this guaranteed not to compile if the draft standard
  1394. * order is assumed.
  1395. */
  1396. size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const {
  1397. size_t _p_size = size();
  1398. size_t __len = (__pos + __n > _p_size? _p_size - __pos : __n);
  1399. _STLP_STD::_Destroy_Range(__buffer, __buffer + __len);
  1400. _S_flatten(_M_tree_ptr._M_data, __pos, __len, __buffer);
  1401. return __len;
  1402. }
  1403. # ifdef _STLP_DEBUG
  1404. // Print to stdout, exposing structure. May be useful for
  1405. // performance debugging.
  1406. void dump() {
  1407. _S_dump(_M_tree_ptr._M_data);
  1408. }
  1409. # endif
  1410. // Convert to 0 terminated string in new allocated memory.
  1411. // Embedded 0s in the input do not terminate the copy.
  1412. const _CharT* c_str() const;
  1413. // As above, but also use the flattened representation as the
  1414. // the new rope representation.
  1415. const _CharT* replace_with_c_str();
  1416. // Reclaim memory for the c_str generated flattened string.
  1417. // Intentionally undocumented, since it's hard to say when this
  1418. // is safe for multiple threads.
  1419. void delete_c_str () {
  1420. if (0 == _M_tree_ptr._M_data) return;
  1421. if (_RopeRep::_S_leaf == _M_tree_ptr._M_data->_M_tag &&
  1422. ((_RopeLeaf*)_M_tree_ptr._M_data)->_M_data ==
  1423. _M_tree_ptr._M_data->_M_c_string) {
  1424. // Representation shared
  1425. return;
  1426. }
  1427. _M_tree_ptr._M_data->_M_free_c_string();
  1428. _M_tree_ptr._M_data->_M_c_string = 0;
  1429. }
  1430. _CharT operator[] (size_type __pos) const {
  1431. return _S_fetch(_M_tree_ptr._M_data, __pos);
  1432. }
  1433. _CharT at(size_type __pos) const {
  1434. if (__pos >= size()) _M_throw_out_of_range();
  1435. return (*this)[__pos];
  1436. }
  1437. const_iterator begin() const {
  1438. return(const_iterator(_M_tree_ptr._M_data, 0));
  1439. }
  1440. // An easy way to get a const iterator from a non-const container.
  1441. const_iterator const_begin() const {
  1442. return(const_iterator(_M_tree_ptr._M_data, 0));
  1443. }
  1444. const_iterator end() const {
  1445. return(const_iterator(_M_tree_ptr._M_data, size()));
  1446. }
  1447. const_iterator const_end() const {
  1448. return(const_iterator(_M_tree_ptr._M_data, size()));
  1449. }
  1450. size_type size() const {
  1451. return(0 == _M_tree_ptr._M_data? 0 : _M_tree_ptr._M_data->_M_size._M_data);
  1452. }
  1453. size_type length() const {
  1454. return size();
  1455. }
  1456. size_type max_size() const {
  1457. return _S_min_len[__ROPE_MAX_DEPTH-1] - 1;
  1458. // Guarantees that the result can be sufficiently
  1459. // balanced. Longer ropes will probably still work,
  1460. // but it's harder to make guarantees.
  1461. }
  1462. const_reverse_iterator rbegin() const {
  1463. return const_reverse_iterator(end());
  1464. }
  1465. const_reverse_iterator const_rbegin() const {
  1466. return const_reverse_iterator(end());
  1467. }
  1468. const_reverse_iterator rend() const {
  1469. return const_reverse_iterator(begin());
  1470. }
  1471. const_reverse_iterator const_rend() const {
  1472. return const_reverse_iterator(begin());
  1473. }
  1474. // The symmetric cases are intentionally omitted, since they're presumed
  1475. // to be less common, and we don't handle them as well.
  1476. // The following should really be templatized.
  1477. // The first argument should be an input iterator or
  1478. // forward iterator with value_type _CharT.
  1479. _Self& append(const _CharT* __iter, size_t __n) {
  1480. _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __iter, __n));
  1481. return *this;
  1482. }
  1483. _Self& append(const _CharT* __c_string) {
  1484. size_t __len = _S_char_ptr_len(__c_string);
  1485. append(__c_string, __len);
  1486. return *this;
  1487. }
  1488. _Self& append(const _CharT* __s, const _CharT* __e) {
  1489. _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __s, __e - __s));
  1490. return *this;
  1491. }
  1492. _Self& append(const_iterator __s, const_iterator __e) {
  1493. _STLP_ASSERT(__s._M_root == __e._M_root)
  1494. _STLP_ASSERT(get_allocator() == __s._M_root->get_allocator())
  1495. _Self_destruct_ptr __appendee(_S_substring(__s._M_root, __s._M_current_pos, __e._M_current_pos));
  1496. _M_reset(_S_concat_rep(_M_tree_ptr._M_data, (_RopeRep*)__appendee));
  1497. return *this;
  1498. }
  1499. _Self& append(_CharT __c) {
  1500. _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, &__c, 1));
  1501. return *this;
  1502. }
  1503. _Self& append() { return append(_CharT()); } // XXX why?
  1504. _Self& append(const _Self& __y) {
  1505. _STLP_ASSERT(__y.get_allocator() == get_allocator())
  1506. _M_reset(_S_concat_rep(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data));
  1507. return *this;
  1508. }
  1509. _Self& append(size_t __n, _CharT __c) {
  1510. rope<_CharT,_Alloc> __last(__n, __c);
  1511. return append(__last);
  1512. }
  1513. void swap(_Self& __b) {
  1514. _M_tree_ptr.swap(__b._M_tree_ptr);
  1515. }
  1516. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  1517. void _M_swap_workaround(_Self& __x) { swap(__x); }
  1518. #endif
  1519. protected:
  1520. // Result is included in refcount.
  1521. static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
  1522. size_t __pos2, _RopeRep* __r) {
  1523. if (0 == __old) { _S_ref(__r); return __r; }
  1524. _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
  1525. _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size._M_data));
  1526. _STLP_MPWFIX_TRY //*TY 06/01/2000 -
  1527. _RopeRep* __result;
  1528. if (0 == __r) {
  1529. __result = _S_concat_rep(__left, __right);
  1530. } else {
  1531. _STLP_ASSERT(__old->get_allocator() == __r->get_allocator())
  1532. _Self_destruct_ptr __left_result(_S_concat_rep(__left, __r));
  1533. __result = _S_concat_rep(__left_result, __right);
  1534. }
  1535. return __result;
  1536. _STLP_MPWFIX_CATCH //*TY 06/01/2000 -
  1537. }
  1538. public:
  1539. void insert(size_t __p, const _Self& __r) {
  1540. if (__p > size()) _M_throw_out_of_range();
  1541. _STLP_ASSERT(get_allocator() == __r.get_allocator())
  1542. _M_reset(replace(_M_tree_ptr._M_data, __p, __p, __r._M_tree_ptr._M_data));
  1543. }
  1544. void insert(size_t __p, size_t __n, _CharT __c) {
  1545. rope<_CharT,_Alloc> __r(__n,__c);
  1546. insert(__p, __r);
  1547. }
  1548. void insert(size_t __p, const _CharT* __i, size_t __n) {
  1549. if (__p > size()) _M_throw_out_of_range();
  1550. _Self_destruct_ptr __left(_S_substring(_M_tree_ptr._M_data, 0, __p));
  1551. _Self_destruct_ptr __right(_S_substring(_M_tree_ptr._M_data, __p, size()));
  1552. _Self_destruct_ptr __left_result(
  1553. _S_concat_char_iter(__left, __i, __n));
  1554. // _S_ destr_concat_char_iter should be safe here.
  1555. // But as it stands it's probably not a win, since __left
  1556. // is likely to have additional references.
  1557. _M_reset(_S_concat_rep(__left_result, __right));
  1558. }
  1559. void insert(size_t __p, const _CharT* __c_string) {
  1560. insert(__p, __c_string, _S_char_ptr_len(__c_string));
  1561. }
  1562. void insert(size_t __p, _CharT __c) {
  1563. insert(__p, &__c, 1);
  1564. }
  1565. void insert(size_t __p) {
  1566. _CharT __c = _CharT();
  1567. insert(__p, &__c, 1);
  1568. }
  1569. void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
  1570. _Self __r(__i, __j);
  1571. insert(__p, __r);
  1572. }
  1573. void insert(size_t __p, const const_iterator& __i,
  1574. const const_iterator& __j) {
  1575. _Self __r(__i, __j);
  1576. insert(__p, __r);
  1577. }
  1578. void insert(size_t __p, const iterator& __i,
  1579. const iterator& __j) {
  1580. _Self __r(__i, __j);
  1581. insert(__p, __r);
  1582. }
  1583. // (position, length) versions of replace operations:
  1584. void replace(size_t __p, size_t __n, const _Self& __r) {
  1585. if (__p > size()) _M_throw_out_of_range();
  1586. _M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, __r._M_tree_ptr._M_data));
  1587. }
  1588. void replace(size_t __p, size_t __n,
  1589. const _CharT* __i, size_t __i_len) {
  1590. _Self __r(__i, __i_len);
  1591. replace(__p, __n, __r);
  1592. }
  1593. void replace(size_t __p, size_t __n, _CharT __c) {
  1594. _Self __r(__c);
  1595. replace(__p, __n, __r);
  1596. }
  1597. void replace(size_t __p, size_t __n, const _CharT* __c_string) {
  1598. _Self __r(__c_string);
  1599. replace(__p, __n, __r);
  1600. }
  1601. void replace(size_t __p, size_t __n,
  1602. const _CharT* __i, const _CharT* __j) {
  1603. _Self __r(__i, __j);
  1604. replace(__p, __n, __r);
  1605. }
  1606. void replace(size_t __p, size_t __n,
  1607. const const_iterator& __i, const const_iterator& __j) {
  1608. _Self __r(__i, __j);
  1609. replace(__p, __n, __r);
  1610. }
  1611. void replace(size_t __p, size_t __n,
  1612. const iterator& __i, const iterator& __j) {
  1613. _Self __r(__i, __j);
  1614. replace(__p, __n, __r);
  1615. }
  1616. // Single character variants:
  1617. void replace(size_t __p, _CharT __c) {
  1618. if (__p > size()) _M_throw_out_of_range();
  1619. iterator __i(this, __p);
  1620. *__i = __c;
  1621. }
  1622. void replace(size_t __p, const _Self& __r) {
  1623. replace(__p, 1, __r);
  1624. }
  1625. void replace(size_t __p, const _CharT* __i, size_t __i_len) {
  1626. replace(__p, 1, __i, __i_len);
  1627. }
  1628. void replace(size_t __p, const _CharT* __c_string) {
  1629. replace(__p, 1, __c_string);
  1630. }
  1631. void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
  1632. replace(__p, 1, __i, __j);
  1633. }
  1634. void replace(size_t __p, const const_iterator& __i,
  1635. const const_iterator& __j) {
  1636. replace(__p, 1, __i, __j);
  1637. }
  1638. void replace(size_t __p, const iterator& __i,
  1639. const iterator& __j) {
  1640. replace(__p, 1, __i, __j);
  1641. }
  1642. // Erase, (position, size) variant.
  1643. void erase(size_t __p, size_t __n) {
  1644. if (__p > size()) _M_throw_out_of_range();
  1645. _M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, 0));
  1646. }
  1647. // Erase, single character
  1648. void erase(size_t __p) {
  1649. erase(__p, __p + 1);
  1650. }
  1651. // Insert, iterator variants.
  1652. iterator insert(const iterator& __p, const _Self& __r)
  1653. { insert(__p.index(), __r); return __p; }
  1654. iterator insert(const iterator& __p, size_t __n, _CharT __c)
  1655. { insert(__p.index(), __n, __c); return __p; }
  1656. iterator insert(const iterator& __p, _CharT __c)
  1657. { insert(__p.index(), __c); return __p; }
  1658. iterator insert(const iterator& __p )
  1659. { insert(__p.index()); return __p; }
  1660. iterator insert(const iterator& __p, const _CharT* c_string)
  1661. { insert(__p.index(), c_string); return __p; }
  1662. iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
  1663. { insert(__p.index(), __i, __n); return __p; }
  1664. iterator insert(const iterator& __p, const _CharT* __i,
  1665. const _CharT* __j)
  1666. { insert(__p.index(), __i, __j); return __p; }
  1667. iterator insert(const iterator& __p,
  1668. const const_iterator& __i, const const_iterator& __j)
  1669. { insert(__p.index(), __i, __j); return __p; }
  1670. iterator insert(const iterator& __p,
  1671. const iterator& __i, const iterator& __j)
  1672. { insert(__p.index(), __i, __j); return __p; }
  1673. // Replace, range variants.
  1674. void replace(const iterator& __p, const iterator& __q,
  1675. const _Self& __r)
  1676. { replace(__p.index(), __q.index() - __p.index(), __r); }
  1677. void replace(const iterator& __p, const iterator& __q, _CharT __c)
  1678. { replace(__p.index(), __q.index() - __p.index(), __c); }
  1679. void replace(const iterator& __p, const iterator& __q,
  1680. const _CharT* __c_string)
  1681. { replace(__p.index(), __q.index() - __p.index(), __c_string); }
  1682. void replace(const iterator& __p, const iterator& __q,
  1683. const _CharT* __i, size_t __n)
  1684. { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
  1685. void replace(const iterator& __p, const iterator& __q,
  1686. const _CharT* __i, const _CharT* __j)
  1687. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  1688. void replace(const iterator& __p, const iterator& __q,
  1689. const const_iterator& __i, const const_iterator& __j)
  1690. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  1691. void replace(const iterator& __p, const iterator& __q,
  1692. const iterator& __i, const iterator& __j)
  1693. { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
  1694. // Replace, iterator variants.
  1695. void replace(const iterator& __p, const _Self& __r)
  1696. { replace(__p.index(), __r); }
  1697. void replace(const iterator& __p, _CharT __c)
  1698. { replace(__p.index(), __c); }
  1699. void replace(const iterator& __p, const _CharT* __c_string)
  1700. { replace(__p.index(), __c_string); }
  1701. void replace(const iterator& __p, const _CharT* __i, size_t __n)
  1702. { replace(__p.index(), __i, __n); }
  1703. void replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
  1704. { replace(__p.index(), __i, __j); }
  1705. void replace(const iterator& __p, const_iterator __i,
  1706. const_iterator __j)
  1707. { replace(__p.index(), __i, __j); }
  1708. void replace(const iterator& __p, iterator __i, iterator __j)
  1709. { replace(__p.index(), __i, __j); }
  1710. // Iterator and range variants of erase
  1711. iterator erase(const iterator& __p, const iterator& __q) {
  1712. size_t __p_index = __p.index();
  1713. erase(__p_index, __q.index() - __p_index);
  1714. return iterator(this, __p_index);
  1715. }
  1716. iterator erase(const iterator& __p) {
  1717. size_t __p_index = __p.index();
  1718. erase(__p_index, 1);
  1719. return iterator(this, __p_index);
  1720. }
  1721. _Self substr(size_t __start, size_t __len = 1) const {
  1722. if (__start > size()) _M_throw_out_of_range();
  1723. return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start, __start + __len));
  1724. }
  1725. _Self substr(iterator __start, iterator __end) const {
  1726. return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
  1727. }
  1728. _Self substr(iterator __start) const {
  1729. size_t __pos = __start.index();
  1730. return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
  1731. }
  1732. _Self substr(const_iterator __start, const_iterator __end) const {
  1733. // This might eventually take advantage of the cache in the
  1734. // iterator.
  1735. return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
  1736. }
  1737. rope<_CharT,_Alloc> substr(const_iterator __start) {
  1738. size_t __pos = __start.index();
  1739. return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
  1740. }
  1741. #include <stl/_string_npos.h>
  1742. size_type find(const _Self& __s, size_type __pos = 0) const {
  1743. if (__pos >= size())
  1744. # ifndef _STLP_OLD_ROPE_SEMANTICS
  1745. return npos;
  1746. # else
  1747. return size();
  1748. # endif
  1749. size_type __result_pos;
  1750. const_iterator __result = _STLP_STD::search(const_begin() + (ptrdiff_t)__pos, const_end(), __s.begin(), __s.end() );
  1751. __result_pos = __result.index();
  1752. # ifndef _STLP_OLD_ROPE_SEMANTICS
  1753. if (__result_pos == size()) __result_pos = npos;
  1754. # endif
  1755. return __result_pos;
  1756. }
  1757. size_type find(_CharT __c, size_type __pos = 0) const;
  1758. size_type find(const _CharT* __s, size_type __pos = 0) const {
  1759. size_type __result_pos;
  1760. const_iterator __result = _STLP_STD::search(const_begin() + (ptrdiff_t)__pos, const_end(),
  1761. __s, __s + _S_char_ptr_len(__s));
  1762. __result_pos = __result.index();
  1763. # ifndef _STLP_OLD_ROPE_SEMANTICS
  1764. if (__result_pos == size()) __result_pos = npos;
  1765. # endif
  1766. return __result_pos;
  1767. }
  1768. iterator mutable_begin() {
  1769. return(iterator(this, 0));
  1770. }
  1771. iterator mutable_end() {
  1772. return(iterator(this, size()));
  1773. }
  1774. reverse_iterator mutable_rbegin() {
  1775. return reverse_iterator(mutable_end());
  1776. }
  1777. reverse_iterator mutable_rend() {
  1778. return reverse_iterator(mutable_begin());
  1779. }
  1780. reference mutable_reference_at(size_type __pos) {
  1781. return reference(this, __pos);
  1782. }
  1783. # ifdef __STD_STUFF
  1784. reference operator[] (size_type __pos) {
  1785. return reference(this, __pos);
  1786. }
  1787. reference at(size_type __pos) {
  1788. if (__pos >= size()) _M_throw_out_of_range();
  1789. return (*this)[__pos];
  1790. }
  1791. void resize(size_type, _CharT) {}
  1792. void resize(size_type) {}
  1793. void reserve(size_type = 0) {}
  1794. size_type capacity() const {
  1795. return max_size();
  1796. }
  1797. // Stuff below this line is dangerous because it's error prone.
  1798. // I would really like to get rid of it.
  1799. // copy function with funny arg ordering.
  1800. size_type copy(_CharT* __buffer, size_type __n,
  1801. size_type __pos = 0) const {
  1802. return copy(__pos, __n, __buffer);
  1803. }
  1804. iterator end() { return mutable_end(); }
  1805. iterator begin() { return mutable_begin(); }
  1806. reverse_iterator rend() { return mutable_rend(); }
  1807. reverse_iterator rbegin() { return mutable_rbegin(); }
  1808. # else
  1809. const_iterator end() { return const_end(); }
  1810. const_iterator begin() { return const_begin(); }
  1811. const_reverse_iterator rend() { return const_rend(); }
  1812. const_reverse_iterator rbegin() { return const_rbegin(); }
  1813. # endif
  1814. }; //class rope
  1815. #if defined (__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 96)
  1816. template <class _CharT, class _Alloc>
  1817. const size_t rope<_CharT, _Alloc>::npos = ~(size_t) 0;
  1818. #endif
  1819. template <class _CharT, class _Alloc>
  1820. inline _CharT
  1821. _Rope_const_iterator< _CharT, _Alloc>::operator[](size_t __n)
  1822. { return rope<_CharT,_Alloc>::_S_fetch(this->_M_root, this->_M_current_pos + __n); }
  1823. template <class _CharT, class _Alloc>
  1824. inline bool operator== (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1825. const _Rope_const_iterator<_CharT,_Alloc>& __y) {
  1826. return (__x._M_current_pos == __y._M_current_pos &&
  1827. __x._M_root == __y._M_root);
  1828. }
  1829. template <class _CharT, class _Alloc>
  1830. inline bool operator< (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1831. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1832. { return (__x._M_current_pos < __y._M_current_pos); }
  1833. #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  1834. template <class _CharT, class _Alloc>
  1835. inline bool operator!= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1836. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1837. { return !(__x == __y); }
  1838. template <class _CharT, class _Alloc>
  1839. inline bool operator> (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1840. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1841. { return __y < __x; }
  1842. template <class _CharT, class _Alloc>
  1843. inline bool operator<= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1844. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1845. { return !(__y < __x); }
  1846. template <class _CharT, class _Alloc>
  1847. inline bool operator>= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1848. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1849. { return !(__x < __y); }
  1850. #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  1851. template <class _CharT, class _Alloc>
  1852. inline ptrdiff_t operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x,
  1853. const _Rope_const_iterator<_CharT,_Alloc>& __y)
  1854. { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
  1855. #if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000 // dwa 8/21/97 - "ambiguous access to overloaded function" bug.
  1856. template <class _CharT, class _Alloc>
  1857. inline _Rope_const_iterator<_CharT,_Alloc>
  1858. operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n)
  1859. { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos - __n); }
  1860. # endif
  1861. template <class _CharT, class _Alloc>
  1862. inline _Rope_const_iterator<_CharT,_Alloc>
  1863. operator+(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n)
  1864. { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos + __n); }
  1865. template <class _CharT, class _Alloc>
  1866. inline _Rope_const_iterator<_CharT,_Alloc>
  1867. operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT,_Alloc>& __x)
  1868. { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos + __n); }
  1869. template <class _CharT, class _Alloc>
  1870. inline bool operator== (const _Rope_iterator<_CharT,_Alloc>& __x,
  1871. const _Rope_iterator<_CharT,_Alloc>& __y) {
  1872. return (__x._M_current_pos == __y._M_current_pos &&
  1873. __x._M_root_rope == __y._M_root_rope);
  1874. }
  1875. template <class _CharT, class _Alloc>
  1876. inline bool operator< (const _Rope_iterator<_CharT,_Alloc>& __x,
  1877. const _Rope_iterator<_CharT,_Alloc>& __y)
  1878. { return (__x._M_current_pos < __y._M_current_pos); }
  1879. #if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
  1880. template <class _CharT, class _Alloc>
  1881. inline bool operator!= (const _Rope_iterator<_CharT,_Alloc>& __x,
  1882. const _Rope_iterator<_CharT,_Alloc>& __y)
  1883. { return !(__x == __y); }
  1884. template <class _CharT, class _Alloc>
  1885. inline bool operator> (const _Rope_iterator<_CharT,_Alloc>& __x,
  1886. const _Rope_iterator<_CharT,_Alloc>& __y)
  1887. { return __y < __x; }
  1888. template <class _CharT, class _Alloc>
  1889. inline bool operator<= (const _Rope_iterator<_CharT,_Alloc>& __x,
  1890. const _Rope_iterator<_CharT,_Alloc>& __y)
  1891. { return !(__y < __x); }
  1892. template <class _CharT, class _Alloc>
  1893. inline bool operator>= (const _Rope_iterator<_CharT,_Alloc>& __x,
  1894. const _Rope_iterator<_CharT,_Alloc>& __y)
  1895. { return !(__x < __y); }
  1896. #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  1897. template <class _CharT, class _Alloc>
  1898. inline ptrdiff_t operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
  1899. const _Rope_iterator<_CharT,_Alloc>& __y)
  1900. { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
  1901. #if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000 // dwa 8/21/97 - "ambiguous access to overloaded function" bug.
  1902. template <class _CharT, class _Alloc>
  1903. inline _Rope_iterator<_CharT,_Alloc>
  1904. operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
  1905. ptrdiff_t __n) {
  1906. return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos - __n);
  1907. }
  1908. # endif
  1909. template <class _CharT, class _Alloc>
  1910. inline _Rope_iterator<_CharT,_Alloc>
  1911. operator+(const _Rope_iterator<_CharT,_Alloc>& __x,
  1912. ptrdiff_t __n) {
  1913. return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos + __n);
  1914. }
  1915. template <class _CharT, class _Alloc>
  1916. inline _Rope_iterator<_CharT,_Alloc>
  1917. operator+(ptrdiff_t __n, const _Rope_iterator<_CharT,_Alloc>& __x) {
  1918. return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos + __n);
  1919. }
  1920. template <class _CharT, class _Alloc>
  1921. inline rope<_CharT,_Alloc>
  1922. operator+ (const rope<_CharT,_Alloc>& __left,
  1923. const rope<_CharT,_Alloc>& __right) {
  1924. _STLP_ASSERT(__left.get_allocator() == __right.get_allocator())
  1925. return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_rep(__left._M_tree_ptr._M_data, __right._M_tree_ptr._M_data));
  1926. // Inlining this should make it possible to keep __left and __right in registers.
  1927. }
  1928. template <class _CharT, class _Alloc>
  1929. inline rope<_CharT,_Alloc>&
  1930. operator+= (rope<_CharT,_Alloc>& __left,
  1931. const rope<_CharT,_Alloc>& __right) {
  1932. __left.append(__right);
  1933. return __left;
  1934. }
  1935. template <class _CharT, class _Alloc>
  1936. inline rope<_CharT,_Alloc>
  1937. operator+ (const rope<_CharT,_Alloc>& __left,
  1938. const _CharT* __right) {
  1939. size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
  1940. return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_char_iter(__left._M_tree_ptr._M_data, __right, __rlen));
  1941. }
  1942. template <class _CharT, class _Alloc>
  1943. inline rope<_CharT,_Alloc>&
  1944. operator+= (rope<_CharT,_Alloc>& __left,
  1945. const _CharT* __right) {
  1946. __left.append(__right);
  1947. return __left;
  1948. }
  1949. template <class _CharT, class _Alloc>
  1950. inline rope<_CharT,_Alloc>
  1951. operator+ (const rope<_CharT,_Alloc>& __left, _CharT __right) {
  1952. return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_char_iter(__left._M_tree_ptr._M_data, &__right, 1));
  1953. }
  1954. template <class _CharT, class _Alloc>
  1955. inline rope<_CharT,_Alloc>&
  1956. operator+= (rope<_CharT,_Alloc>& __left, _CharT __right) {
  1957. __left.append(__right);
  1958. return __left;
  1959. }
  1960. template <class _CharT, class _Alloc>
  1961. inline bool
  1962. operator< (const rope<_CharT,_Alloc>& __left,
  1963. const rope<_CharT,_Alloc>& __right) {
  1964. return __left.compare(__right) < 0;
  1965. }
  1966. template <class _CharT, class _Alloc>
  1967. inline bool
  1968. operator== (const rope<_CharT,_Alloc>& __left,
  1969. const rope<_CharT,_Alloc>& __right) {
  1970. return __left.compare(__right) == 0;
  1971. }
  1972. #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  1973. template <class _CharT, class _Alloc>
  1974. inline bool
  1975. operator!= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
  1976. return !(__x == __y);
  1977. }
  1978. template <class _CharT, class _Alloc>
  1979. inline bool
  1980. operator> (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
  1981. return __y < __x;
  1982. }
  1983. template <class _CharT, class _Alloc>
  1984. inline bool
  1985. operator<= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
  1986. return !(__y < __x);
  1987. }
  1988. template <class _CharT, class _Alloc>
  1989. inline bool
  1990. operator>= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
  1991. return !(__x < __y);
  1992. }
  1993. template <class _CharT, class _Alloc>
  1994. inline bool operator!= (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
  1995. const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
  1996. return !(__x == __y);
  1997. }
  1998. #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  1999. template <class _CharT, class _Alloc>
  2000. inline bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
  2001. const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
  2002. return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root);
  2003. }
  2004. #if !defined (_STLP_USE_NO_IOSTREAMS)
  2005. template<class _CharT, class _Traits, class _Alloc>
  2006. basic_ostream<_CharT, _Traits>& operator<< (basic_ostream<_CharT, _Traits>& __o,
  2007. const rope<_CharT, _Alloc>& __r);
  2008. #endif
  2009. typedef rope<char, allocator<char> > crope;
  2010. #if defined (_STLP_HAS_WCHAR_T)
  2011. typedef rope<wchar_t, allocator<wchar_t> > wrope;
  2012. #endif
  2013. inline crope::reference __mutable_reference_at(crope& __c, size_t __i)
  2014. { return __c.mutable_reference_at(__i); }
  2015. #if defined (_STLP_HAS_WCHAR_T)
  2016. inline wrope::reference __mutable_reference_at(wrope& __c, size_t __i)
  2017. { return __c.mutable_reference_at(__i); }
  2018. #endif
  2019. #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  2020. template <class _CharT, class _Alloc>
  2021. inline void swap(rope<_CharT,_Alloc>& __x, rope<_CharT,_Alloc>& __y)
  2022. { __x.swap(__y); }
  2023. #else
  2024. inline void swap(crope& __x, crope& __y) { __x.swap(__y); }
  2025. # ifdef _STLP_HAS_WCHAR_T // dwa 8/21/97
  2026. inline void swap(wrope& __x, wrope& __y) { __x.swap(__y); }
  2027. # endif
  2028. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  2029. // Hash functions should probably be revisited later:
  2030. _STLP_TEMPLATE_NULL struct hash<crope> {
  2031. size_t operator()(const crope& __str) const {
  2032. size_t _p_size = __str.size();
  2033. if (0 == _p_size) return 0;
  2034. return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
  2035. }
  2036. };
  2037. #if defined (_STLP_HAS_WCHAR_T) // dwa 8/21/97
  2038. _STLP_TEMPLATE_NULL struct hash<wrope> {
  2039. size_t operator()(const wrope& __str) const {
  2040. size_t _p_size = __str.size();
  2041. if (0 == _p_size) return 0;
  2042. return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
  2043. }
  2044. };
  2045. #endif
  2046. #if (!defined (_STLP_MSVC) || (_STLP_MSVC >= 1310))
  2047. // I couldn't get this to work with VC++
  2048. template<class _CharT,class _Alloc>
  2049. # if defined (__DMC__)
  2050. extern
  2051. # endif
  2052. void _Rope_rotate(_Rope_iterator<_CharT, _Alloc> __first,
  2053. _Rope_iterator<_CharT, _Alloc> __middle,
  2054. _Rope_iterator<_CharT, _Alloc> __last);
  2055. inline void rotate(_Rope_iterator<char, allocator<char> > __first,
  2056. _Rope_iterator<char, allocator<char> > __middle,
  2057. _Rope_iterator<char, allocator<char> > __last)
  2058. { _Rope_rotate(__first, __middle, __last); }
  2059. #endif
  2060. template <class _CharT, class _Alloc>
  2061. inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const {
  2062. if (_M_current_valid) {
  2063. return _M_current;
  2064. } else {
  2065. return _My_rope::_S_fetch(_M_root->_M_tree_ptr._M_data, _M_pos);
  2066. }
  2067. }
  2068. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_MOVE_SEMANTIC)
  2069. template <class _CharT, class _Alloc>
  2070. struct __move_traits<rope<_CharT, _Alloc> > {
  2071. typedef __true_type implemented;
  2072. //Completness depends on the allocator:
  2073. typedef typename __move_traits<_Alloc>::complete complete;
  2074. };
  2075. #endif
  2076. _STLP_END_NAMESPACE
  2077. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  2078. # include <stl/_rope.c>
  2079. #endif
  2080. #endif /* _STLP_INTERNAL_ROPE_H */
  2081. // Local Variables:
  2082. // mode:C++
  2083. // End: