ropeimpl.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. // SGI's rope class implementation -*- C++ -*-
  2. // Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file ropeimpl.h
  33. * This is an internal header file, included by other library headers.
  34. * Do not attempt to use it directly. @headername{ext/rope}
  35. */
  36. #include <cstdio>
  37. #include <ostream>
  38. #include <bits/functexcept.h>
  39. #include <ext/algorithm> // For copy_n and lexicographical_compare_3way
  40. #include <ext/memory> // For uninitialized_copy_n
  41. #include <ext/numeric> // For power
  42. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. using std::size_t;
  46. using std::printf;
  47. using std::basic_ostream;
  48. using std::__throw_length_error;
  49. using std::_Destroy;
  50. using std::__uninitialized_fill_n_a;
  51. // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
  52. // if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct.
  53. // Results in a valid buf_ptr if the iterator can be legitimately
  54. // dereferenced.
  55. template <class _CharT, class _Alloc>
  56. void
  57. _Rope_iterator_base<_CharT, _Alloc>::
  58. _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x)
  59. {
  60. const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index];
  61. size_t __leaf_pos = __x._M_leaf_pos;
  62. size_t __pos = __x._M_current_pos;
  63. switch(__leaf->_M_tag)
  64. {
  65. case __detail::_S_leaf:
  66. __x._M_buf_start = ((_Rope_RopeLeaf<_CharT, _Alloc>*)__leaf)->_M_data;
  67. __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos);
  68. __x._M_buf_end = __x._M_buf_start + __leaf->_M_size;
  69. break;
  70. case __detail::_S_function:
  71. case __detail::_S_substringfn:
  72. {
  73. size_t __len = _S_iterator_buf_len;
  74. size_t __buf_start_pos = __leaf_pos;
  75. size_t __leaf_end = __leaf_pos + __leaf->_M_size;
  76. char_producer<_CharT>* __fn = ((_Rope_RopeFunction<_CharT,
  77. _Alloc>*)__leaf)->_M_fn;
  78. if (__buf_start_pos + __len <= __pos)
  79. {
  80. __buf_start_pos = __pos - __len / 4;
  81. if (__buf_start_pos + __len > __leaf_end)
  82. __buf_start_pos = __leaf_end - __len;
  83. }
  84. if (__buf_start_pos + __len > __leaf_end)
  85. __len = __leaf_end - __buf_start_pos;
  86. (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
  87. __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
  88. __x._M_buf_start = __x._M_tmp_buf;
  89. __x._M_buf_end = __x._M_tmp_buf + __len;
  90. }
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. // Set path and buffer inside a rope iterator. We assume that
  97. // pos and root are already set.
  98. template <class _CharT, class _Alloc>
  99. void
  100. _Rope_iterator_base<_CharT, _Alloc>::
  101. _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x)
  102. {
  103. const _RopeRep* __path[int(__detail::_S_max_rope_depth) + 1];
  104. const _RopeRep* __curr_rope;
  105. int __curr_depth = -1; /* index into path */
  106. size_t __curr_start_pos = 0;
  107. size_t __pos = __x._M_current_pos;
  108. unsigned char __dirns = 0; // Bit vector marking right turns in the path
  109. if (__pos >= __x._M_root->_M_size)
  110. {
  111. __x._M_buf_ptr = 0;
  112. return;
  113. }
  114. __curr_rope = __x._M_root;
  115. if (0 != __curr_rope->_M_c_string)
  116. {
  117. /* Treat the root as a leaf. */
  118. __x._M_buf_start = __curr_rope->_M_c_string;
  119. __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
  120. __x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
  121. __x._M_path_end[0] = __curr_rope;
  122. __x._M_leaf_index = 0;
  123. __x._M_leaf_pos = 0;
  124. return;
  125. }
  126. for(;;)
  127. {
  128. ++__curr_depth;
  129. __path[__curr_depth] = __curr_rope;
  130. switch(__curr_rope->_M_tag)
  131. {
  132. case __detail::_S_leaf:
  133. case __detail::_S_function:
  134. case __detail::_S_substringfn:
  135. __x._M_leaf_pos = __curr_start_pos;
  136. goto done;
  137. case __detail::_S_concat:
  138. {
  139. _Rope_RopeConcatenation<_CharT, _Alloc>* __c =
  140. (_Rope_RopeConcatenation<_CharT, _Alloc>*)__curr_rope;
  141. _RopeRep* __left = __c->_M_left;
  142. size_t __left_len = __left->_M_size;
  143. __dirns <<= 1;
  144. if (__pos >= __curr_start_pos + __left_len)
  145. {
  146. __dirns |= 1;
  147. __curr_rope = __c->_M_right;
  148. __curr_start_pos += __left_len;
  149. }
  150. else
  151. __curr_rope = __left;
  152. }
  153. break;
  154. }
  155. }
  156. done:
  157. // Copy last section of path into _M_path_end.
  158. {
  159. int __i = -1;
  160. int __j = __curr_depth + 1 - int(_S_path_cache_len);
  161. if (__j < 0) __j = 0;
  162. while (__j <= __curr_depth)
  163. __x._M_path_end[++__i] = __path[__j++];
  164. __x._M_leaf_index = __i;
  165. }
  166. __x._M_path_directions = __dirns;
  167. _S_setbuf(__x);
  168. }
  169. // Specialized version of the above. Assumes that
  170. // the path cache is valid for the previous position.
  171. template <class _CharT, class _Alloc>
  172. void
  173. _Rope_iterator_base<_CharT, _Alloc>::
  174. _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x)
  175. {
  176. int __current_index = __x._M_leaf_index;
  177. const _RopeRep* __current_node = __x._M_path_end[__current_index];
  178. size_t __len = __current_node->_M_size;
  179. size_t __node_start_pos = __x._M_leaf_pos;
  180. unsigned char __dirns = __x._M_path_directions;
  181. _Rope_RopeConcatenation<_CharT, _Alloc>* __c;
  182. if (__x._M_current_pos - __node_start_pos < __len)
  183. {
  184. /* More stuff in this leaf, we just didn't cache it. */
  185. _S_setbuf(__x);
  186. return;
  187. }
  188. // node_start_pos is starting position of last_node.
  189. while (--__current_index >= 0)
  190. {
  191. if (!(__dirns & 1) /* Path turned left */)
  192. break;
  193. __current_node = __x._M_path_end[__current_index];
  194. __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node;
  195. // Otherwise we were in the right child. Thus we should pop
  196. // the concatenation node.
  197. __node_start_pos -= __c->_M_left->_M_size;
  198. __dirns >>= 1;
  199. }
  200. if (__current_index < 0)
  201. {
  202. // We underflowed the cache. Punt.
  203. _S_setcache(__x);
  204. return;
  205. }
  206. __current_node = __x._M_path_end[__current_index];
  207. __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node;
  208. // current_node is a concatenation node. We are positioned on the first
  209. // character in its right child.
  210. // node_start_pos is starting position of current_node.
  211. __node_start_pos += __c->_M_left->_M_size;
  212. __current_node = __c->_M_right;
  213. __x._M_path_end[++__current_index] = __current_node;
  214. __dirns |= 1;
  215. while (__detail::_S_concat == __current_node->_M_tag)
  216. {
  217. ++__current_index;
  218. if (int(_S_path_cache_len) == __current_index)
  219. {
  220. int __i;
  221. for (__i = 0; __i < int(_S_path_cache_len) - 1; __i++)
  222. __x._M_path_end[__i] = __x._M_path_end[__i+1];
  223. --__current_index;
  224. }
  225. __current_node =
  226. ((_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node)->_M_left;
  227. __x._M_path_end[__current_index] = __current_node;
  228. __dirns <<= 1;
  229. // node_start_pos is unchanged.
  230. }
  231. __x._M_leaf_index = __current_index;
  232. __x._M_leaf_pos = __node_start_pos;
  233. __x._M_path_directions = __dirns;
  234. _S_setbuf(__x);
  235. }
  236. template <class _CharT, class _Alloc>
  237. void
  238. _Rope_iterator_base<_CharT, _Alloc>::
  239. _M_incr(size_t __n)
  240. {
  241. _M_current_pos += __n;
  242. if (0 != _M_buf_ptr)
  243. {
  244. size_t __chars_left = _M_buf_end - _M_buf_ptr;
  245. if (__chars_left > __n)
  246. _M_buf_ptr += __n;
  247. else if (__chars_left == __n)
  248. {
  249. _M_buf_ptr += __n;
  250. _S_setcache_for_incr(*this);
  251. }
  252. else
  253. _M_buf_ptr = 0;
  254. }
  255. }
  256. template <class _CharT, class _Alloc>
  257. void
  258. _Rope_iterator_base<_CharT, _Alloc>::
  259. _M_decr(size_t __n)
  260. {
  261. if (0 != _M_buf_ptr)
  262. {
  263. size_t __chars_left = _M_buf_ptr - _M_buf_start;
  264. if (__chars_left >= __n)
  265. _M_buf_ptr -= __n;
  266. else
  267. _M_buf_ptr = 0;
  268. }
  269. _M_current_pos -= __n;
  270. }
  271. template <class _CharT, class _Alloc>
  272. void
  273. _Rope_iterator<_CharT, _Alloc>::
  274. _M_check()
  275. {
  276. if (_M_root_rope->_M_tree_ptr != this->_M_root)
  277. {
  278. // _Rope was modified. Get things fixed up.
  279. _RopeRep::_S_unref(this->_M_root);
  280. this->_M_root = _M_root_rope->_M_tree_ptr;
  281. _RopeRep::_S_ref(this->_M_root);
  282. this->_M_buf_ptr = 0;
  283. }
  284. }
  285. template <class _CharT, class _Alloc>
  286. inline
  287. _Rope_const_iterator<_CharT, _Alloc>::
  288. _Rope_const_iterator(const _Rope_iterator<_CharT, _Alloc>& __x)
  289. : _Rope_iterator_base<_CharT, _Alloc>(__x)
  290. { }
  291. template <class _CharT, class _Alloc>
  292. inline
  293. _Rope_iterator<_CharT, _Alloc>::
  294. _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos)
  295. : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos),
  296. _M_root_rope(&__r)
  297. { _RopeRep::_S_ref(this->_M_root); }
  298. template <class _CharT, class _Alloc>
  299. inline size_t
  300. rope<_CharT, _Alloc>::
  301. _S_char_ptr_len(const _CharT* __s)
  302. {
  303. const _CharT* __p = __s;
  304. while (!_S_is0(*__p))
  305. ++__p;
  306. return (__p - __s);
  307. }
  308. #ifndef __GC
  309. template <class _CharT, class _Alloc>
  310. inline void
  311. _Rope_RopeRep<_CharT, _Alloc>::
  312. _M_free_c_string()
  313. {
  314. _CharT* __cstr = _M_c_string;
  315. if (0 != __cstr)
  316. {
  317. size_t __size = this->_M_size + 1;
  318. _Destroy(__cstr, __cstr + __size, _M_get_allocator());
  319. this->_Data_deallocate(__cstr, __size);
  320. }
  321. }
  322. template <class _CharT, class _Alloc>
  323. inline void
  324. _Rope_RopeRep<_CharT, _Alloc>::
  325. _S_free_string(_CharT* __s, size_t __n, allocator_type& __a)
  326. {
  327. if (!_S_is_basic_char_type((_CharT*)0))
  328. _Destroy(__s, __s + __n, __a);
  329. // This has to be a static member, so this gets a bit messy
  330. __a.deallocate(__s,
  331. _Rope_RopeLeaf<_CharT, _Alloc>::_S_rounded_up_size(__n));
  332. }
  333. // There are several reasons for not doing this with virtual destructors
  334. // and a class specific delete operator:
  335. // - A class specific delete operator can't easily get access to
  336. // allocator instances if we need them.
  337. // - Any virtual function would need a 4 or byte vtable pointer;
  338. // this only requires a one byte tag per object.
  339. template <class _CharT, class _Alloc>
  340. void
  341. _Rope_RopeRep<_CharT, _Alloc>::
  342. _M_free_tree()
  343. {
  344. switch(_M_tag)
  345. {
  346. case __detail::_S_leaf:
  347. {
  348. _Rope_RopeLeaf<_CharT, _Alloc>* __l
  349. = (_Rope_RopeLeaf<_CharT, _Alloc>*)this;
  350. __l->_Rope_RopeLeaf<_CharT, _Alloc>::~_Rope_RopeLeaf();
  351. this->_L_deallocate(__l, 1);
  352. break;
  353. }
  354. case __detail::_S_concat:
  355. {
  356. _Rope_RopeConcatenation<_CharT,_Alloc>* __c
  357. = (_Rope_RopeConcatenation<_CharT, _Alloc>*)this;
  358. __c->_Rope_RopeConcatenation<_CharT, _Alloc>::
  359. ~_Rope_RopeConcatenation();
  360. this->_C_deallocate(__c, 1);
  361. break;
  362. }
  363. case __detail::_S_function:
  364. {
  365. _Rope_RopeFunction<_CharT, _Alloc>* __f
  366. = (_Rope_RopeFunction<_CharT, _Alloc>*)this;
  367. __f->_Rope_RopeFunction<_CharT, _Alloc>::~_Rope_RopeFunction();
  368. this->_F_deallocate(__f, 1);
  369. break;
  370. }
  371. case __detail::_S_substringfn:
  372. {
  373. _Rope_RopeSubstring<_CharT, _Alloc>* __ss =
  374. (_Rope_RopeSubstring<_CharT, _Alloc>*)this;
  375. __ss->_Rope_RopeSubstring<_CharT, _Alloc>::
  376. ~_Rope_RopeSubstring();
  377. this->_S_deallocate(__ss, 1);
  378. break;
  379. }
  380. }
  381. }
  382. #else
  383. template <class _CharT, class _Alloc>
  384. inline void
  385. _Rope_RopeRep<_CharT, _Alloc>::
  386. _S_free_string(const _CharT*, size_t, allocator_type)
  387. { }
  388. #endif
  389. // Concatenate a C string onto a leaf rope by copying the rope data.
  390. // Used for short ropes.
  391. template <class _CharT, class _Alloc>
  392. typename rope<_CharT, _Alloc>::_RopeLeaf*
  393. rope<_CharT, _Alloc>::
  394. _S_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter, size_t __len)
  395. {
  396. size_t __old_len = __r->_M_size;
  397. _CharT* __new_data = (_CharT*)
  398. rope::_Data_allocate(_S_rounded_up_size(__old_len + __len));
  399. _RopeLeaf* __result;
  400. uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
  401. uninitialized_copy_n(__iter, __len, __new_data + __old_len);
  402. _S_cond_store_eos(__new_data[__old_len + __len]);
  403. __try
  404. {
  405. __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
  406. __r->_M_get_allocator());
  407. }
  408. __catch(...)
  409. {
  410. _RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
  411. __r->_M_get_allocator());
  412. __throw_exception_again;
  413. }
  414. return __result;
  415. }
  416. #ifndef __GC
  417. // As above, but it's OK to clobber original if refcount is 1
  418. template <class _CharT, class _Alloc>
  419. typename rope<_CharT,_Alloc>::_RopeLeaf*
  420. rope<_CharT, _Alloc>::
  421. _S_destr_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter,
  422. size_t __len)
  423. {
  424. if (__r->_M_ref_count > 1)
  425. return _S_leaf_concat_char_iter(__r, __iter, __len);
  426. size_t __old_len = __r->_M_size;
  427. if (_S_allocated_capacity(__old_len) >= __old_len + __len)
  428. {
  429. // The space has been partially initialized for the standard
  430. // character types. But that doesn't matter for those types.
  431. uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
  432. if (_S_is_basic_char_type((_CharT*)0))
  433. _S_cond_store_eos(__r->_M_data[__old_len + __len]);
  434. else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string)
  435. {
  436. __r->_M_free_c_string();
  437. __r->_M_c_string = 0;
  438. }
  439. __r->_M_size = __old_len + __len;
  440. __r->_M_ref_count = 2;
  441. return __r;
  442. }
  443. else
  444. {
  445. _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
  446. return __result;
  447. }
  448. }
  449. #endif
  450. // Assumes left and right are not 0.
  451. // Does not increment (nor decrement on exception) child reference counts.
  452. // Result has ref count 1.
  453. template <class _CharT, class _Alloc>
  454. typename rope<_CharT, _Alloc>::_RopeRep*
  455. rope<_CharT, _Alloc>::
  456. _S_tree_concat(_RopeRep* __left, _RopeRep* __right)
  457. {
  458. _RopeConcatenation* __result = _S_new_RopeConcatenation(__left, __right,
  459. __left->
  460. _M_get_allocator());
  461. size_t __depth = __result->_M_depth;
  462. if (__depth > 20
  463. && (__result->_M_size < 1000
  464. || __depth > size_t(__detail::_S_max_rope_depth)))
  465. {
  466. _RopeRep* __balanced;
  467. __try
  468. {
  469. __balanced = _S_balance(__result);
  470. __result->_M_unref_nonnil();
  471. }
  472. __catch(...)
  473. {
  474. rope::_C_deallocate(__result,1);
  475. __throw_exception_again;
  476. }
  477. // In case of exception, we need to deallocate
  478. // otherwise dangling result node. But caller
  479. // still owns its children. Thus unref is
  480. // inappropriate.
  481. return __balanced;
  482. }
  483. else
  484. return __result;
  485. }
  486. template <class _CharT, class _Alloc>
  487. typename rope<_CharT, _Alloc>::_RopeRep*
  488. rope<_CharT, _Alloc>::
  489. _S_concat_char_iter(_RopeRep* __r, const _CharT*__s, size_t __slen)
  490. {
  491. _RopeRep* __result;
  492. if (0 == __slen)
  493. {
  494. _S_ref(__r);
  495. return __r;
  496. }
  497. if (0 == __r)
  498. return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
  499. __r->_M_get_allocator());
  500. if (__r->_M_tag == __detail::_S_leaf
  501. && __r->_M_size + __slen <= size_t(_S_copy_max))
  502. {
  503. __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
  504. return __result;
  505. }
  506. if (__detail::_S_concat == __r->_M_tag
  507. && __detail::_S_leaf == ((_RopeConcatenation*) __r)->_M_right->_M_tag)
  508. {
  509. _RopeLeaf* __right =
  510. (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
  511. if (__right->_M_size + __slen <= size_t(_S_copy_max))
  512. {
  513. _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
  514. _RopeRep* __nright =
  515. _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
  516. __left->_M_ref_nonnil();
  517. __try
  518. { __result = _S_tree_concat(__left, __nright); }
  519. __catch(...)
  520. {
  521. _S_unref(__left);
  522. _S_unref(__nright);
  523. __throw_exception_again;
  524. }
  525. return __result;
  526. }
  527. }
  528. _RopeRep* __nright =
  529. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->_M_get_allocator());
  530. __try
  531. {
  532. __r->_M_ref_nonnil();
  533. __result = _S_tree_concat(__r, __nright);
  534. }
  535. __catch(...)
  536. {
  537. _S_unref(__r);
  538. _S_unref(__nright);
  539. __throw_exception_again;
  540. }
  541. return __result;
  542. }
  543. #ifndef __GC
  544. template <class _CharT, class _Alloc>
  545. typename rope<_CharT,_Alloc>::_RopeRep*
  546. rope<_CharT,_Alloc>::
  547. _S_destr_concat_char_iter(_RopeRep* __r, const _CharT* __s, size_t __slen)
  548. {
  549. _RopeRep* __result;
  550. if (0 == __r)
  551. return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
  552. __r->_M_get_allocator());
  553. size_t __count = __r->_M_ref_count;
  554. size_t __orig_size = __r->_M_size;
  555. if (__count > 1)
  556. return _S_concat_char_iter(__r, __s, __slen);
  557. if (0 == __slen)
  558. {
  559. __r->_M_ref_count = 2; // One more than before
  560. return __r;
  561. }
  562. if (__orig_size + __slen <= size_t(_S_copy_max)
  563. && __detail::_S_leaf == __r->_M_tag)
  564. {
  565. __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s,
  566. __slen);
  567. return __result;
  568. }
  569. if (__detail::_S_concat == __r->_M_tag)
  570. {
  571. _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)
  572. __r)->_M_right);
  573. if (__detail::_S_leaf == __right->_M_tag
  574. && __right->_M_size + __slen <= size_t(_S_copy_max))
  575. {
  576. _RopeRep* __new_right =
  577. _S_destr_leaf_concat_char_iter(__right, __s, __slen);
  578. if (__right == __new_right)
  579. __new_right->_M_ref_count = 1;
  580. else
  581. __right->_M_unref_nonnil();
  582. __r->_M_ref_count = 2; // One more than before.
  583. ((_RopeConcatenation*)__r)->_M_right = __new_right;
  584. __r->_M_size = __orig_size + __slen;
  585. if (0 != __r->_M_c_string)
  586. {
  587. __r->_M_free_c_string();
  588. __r->_M_c_string = 0;
  589. }
  590. return __r;
  591. }
  592. }
  593. _RopeRep* __right =
  594. __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->_M_get_allocator());
  595. __r->_M_ref_nonnil();
  596. __try
  597. { __result = _S_tree_concat(__r, __right); }
  598. __catch(...)
  599. {
  600. _S_unref(__r);
  601. _S_unref(__right);
  602. __throw_exception_again;
  603. }
  604. return __result;
  605. }
  606. #endif /* !__GC */
  607. template <class _CharT, class _Alloc>
  608. typename rope<_CharT, _Alloc>::_RopeRep*
  609. rope<_CharT, _Alloc>::
  610. _S_concat(_RopeRep* __left, _RopeRep* __right)
  611. {
  612. if (0 == __left)
  613. {
  614. _S_ref(__right);
  615. return __right;
  616. }
  617. if (0 == __right)
  618. {
  619. __left->_M_ref_nonnil();
  620. return __left;
  621. }
  622. if (__detail::_S_leaf == __right->_M_tag)
  623. {
  624. if (__detail::_S_leaf == __left->_M_tag)
  625. {
  626. if (__right->_M_size + __left->_M_size <= size_t(_S_copy_max))
  627. return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
  628. ((_RopeLeaf*)__right)->_M_data,
  629. __right->_M_size);
  630. }
  631. else if (__detail::_S_concat == __left->_M_tag
  632. && __detail::_S_leaf == ((_RopeConcatenation*)
  633. __left)->_M_right->_M_tag)
  634. {
  635. _RopeLeaf* __leftright =
  636. (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right);
  637. if (__leftright->_M_size
  638. + __right->_M_size <= size_t(_S_copy_max))
  639. {
  640. _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
  641. _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
  642. ((_RopeLeaf*)
  643. __right)->
  644. _M_data,
  645. __right->_M_size);
  646. __leftleft->_M_ref_nonnil();
  647. __try
  648. { return(_S_tree_concat(__leftleft, __rest)); }
  649. __catch(...)
  650. {
  651. _S_unref(__leftleft);
  652. _S_unref(__rest);
  653. __throw_exception_again;
  654. }
  655. }
  656. }
  657. }
  658. __left->_M_ref_nonnil();
  659. __right->_M_ref_nonnil();
  660. __try
  661. { return(_S_tree_concat(__left, __right)); }
  662. __catch(...)
  663. {
  664. _S_unref(__left);
  665. _S_unref(__right);
  666. __throw_exception_again;
  667. }
  668. }
  669. template <class _CharT, class _Alloc>
  670. typename rope<_CharT, _Alloc>::_RopeRep*
  671. rope<_CharT, _Alloc>::
  672. _S_substring(_RopeRep* __base, size_t __start, size_t __endp1)
  673. {
  674. if (0 == __base)
  675. return 0;
  676. size_t __len = __base->_M_size;
  677. size_t __adj_endp1;
  678. const size_t __lazy_threshold = 128;
  679. if (__endp1 >= __len)
  680. {
  681. if (0 == __start)
  682. {
  683. __base->_M_ref_nonnil();
  684. return __base;
  685. }
  686. else
  687. __adj_endp1 = __len;
  688. }
  689. else
  690. __adj_endp1 = __endp1;
  691. switch(__base->_M_tag)
  692. {
  693. case __detail::_S_concat:
  694. {
  695. _RopeConcatenation* __c = (_RopeConcatenation*)__base;
  696. _RopeRep* __left = __c->_M_left;
  697. _RopeRep* __right = __c->_M_right;
  698. size_t __left_len = __left->_M_size;
  699. _RopeRep* __result;
  700. if (__adj_endp1 <= __left_len)
  701. return _S_substring(__left, __start, __endp1);
  702. else if (__start >= __left_len)
  703. return _S_substring(__right, __start - __left_len,
  704. __adj_endp1 - __left_len);
  705. _Self_destruct_ptr __left_result(_S_substring(__left,
  706. __start,
  707. __left_len));
  708. _Self_destruct_ptr __right_result(_S_substring(__right, 0,
  709. __endp1
  710. - __left_len));
  711. __result = _S_concat(__left_result, __right_result);
  712. return __result;
  713. }
  714. case __detail::_S_leaf:
  715. {
  716. _RopeLeaf* __l = (_RopeLeaf*)__base;
  717. _RopeLeaf* __result;
  718. size_t __result_len;
  719. if (__start >= __adj_endp1)
  720. return 0;
  721. __result_len = __adj_endp1 - __start;
  722. if (__result_len > __lazy_threshold)
  723. goto lazy;
  724. #ifdef __GC
  725. const _CharT* __section = __l->_M_data + __start;
  726. __result = _S_new_RopeLeaf(__section, __result_len,
  727. __base->_M_get_allocator());
  728. __result->_M_c_string = 0; // Not eos terminated.
  729. #else
  730. // We should sometimes create substring node instead.
  731. __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__l->_M_data + __start,
  732. __result_len,
  733. __base->
  734. _M_get_allocator());
  735. #endif
  736. return __result;
  737. }
  738. case __detail::_S_substringfn:
  739. // Avoid introducing multiple layers of substring nodes.
  740. {
  741. _RopeSubstring* __old = (_RopeSubstring*)__base;
  742. size_t __result_len;
  743. if (__start >= __adj_endp1)
  744. return 0;
  745. __result_len = __adj_endp1 - __start;
  746. if (__result_len > __lazy_threshold)
  747. {
  748. _RopeSubstring* __result =
  749. _S_new_RopeSubstring(__old->_M_base,
  750. __start + __old->_M_start,
  751. __adj_endp1 - __start,
  752. __base->_M_get_allocator());
  753. return __result;
  754. } // *** else fall through: ***
  755. }
  756. case __detail::_S_function:
  757. {
  758. _RopeFunction* __f = (_RopeFunction*)__base;
  759. _CharT* __section;
  760. size_t __result_len;
  761. if (__start >= __adj_endp1)
  762. return 0;
  763. __result_len = __adj_endp1 - __start;
  764. if (__result_len > __lazy_threshold)
  765. goto lazy;
  766. __section = (_CharT*)
  767. rope::_Data_allocate(_S_rounded_up_size(__result_len));
  768. __try
  769. { (*(__f->_M_fn))(__start, __result_len, __section); }
  770. __catch(...)
  771. {
  772. _RopeRep::__STL_FREE_STRING(__section, __result_len,
  773. __base->_M_get_allocator());
  774. __throw_exception_again;
  775. }
  776. _S_cond_store_eos(__section[__result_len]);
  777. return _S_new_RopeLeaf(__section, __result_len,
  778. __base->_M_get_allocator());
  779. }
  780. }
  781. lazy:
  782. {
  783. // Create substring node.
  784. return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
  785. __base->_M_get_allocator());
  786. }
  787. }
  788. template<class _CharT>
  789. class _Rope_flatten_char_consumer
  790. : public _Rope_char_consumer<_CharT>
  791. {
  792. private:
  793. _CharT* _M_buf_ptr;
  794. public:
  795. _Rope_flatten_char_consumer(_CharT* __buffer)
  796. { _M_buf_ptr = __buffer; };
  797. ~_Rope_flatten_char_consumer() {}
  798. bool
  799. operator()(const _CharT* __leaf, size_t __n)
  800. {
  801. uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
  802. _M_buf_ptr += __n;
  803. return true;
  804. }
  805. };
  806. template<class _CharT>
  807. class _Rope_find_char_char_consumer
  808. : public _Rope_char_consumer<_CharT>
  809. {
  810. private:
  811. _CharT _M_pattern;
  812. public:
  813. size_t _M_count; // Number of nonmatching characters
  814. _Rope_find_char_char_consumer(_CharT __p)
  815. : _M_pattern(__p), _M_count(0) {}
  816. ~_Rope_find_char_char_consumer() {}
  817. bool
  818. operator()(const _CharT* __leaf, size_t __n)
  819. {
  820. size_t __i;
  821. for (__i = 0; __i < __n; __i++)
  822. {
  823. if (__leaf[__i] == _M_pattern)
  824. {
  825. _M_count += __i;
  826. return false;
  827. }
  828. }
  829. _M_count += __n; return true;
  830. }
  831. };
  832. template<class _CharT, class _Traits>
  833. // Here _CharT is both the stream and rope character type.
  834. class _Rope_insert_char_consumer
  835. : public _Rope_char_consumer<_CharT>
  836. {
  837. private:
  838. typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
  839. _Insert_ostream& _M_o;
  840. public:
  841. _Rope_insert_char_consumer(_Insert_ostream& __writer)
  842. : _M_o(__writer) {};
  843. ~_Rope_insert_char_consumer() { };
  844. // Caller is presumed to own the ostream
  845. bool operator() (const _CharT* __leaf, size_t __n);
  846. // Returns true to continue traversal.
  847. };
  848. template<class _CharT, class _Traits>
  849. bool
  850. _Rope_insert_char_consumer<_CharT, _Traits>::
  851. operator()(const _CharT* __leaf, size_t __n)
  852. {
  853. size_t __i;
  854. // We assume that formatting is set up correctly for each element.
  855. for (__i = 0; __i < __n; __i++)
  856. _M_o.put(__leaf[__i]);
  857. return true;
  858. }
  859. template <class _CharT, class _Alloc>
  860. bool
  861. rope<_CharT, _Alloc>::
  862. _S_apply_to_pieces(_Rope_char_consumer<_CharT>& __c,
  863. const _RopeRep* __r, size_t __begin, size_t __end)
  864. {
  865. if (0 == __r)
  866. return true;
  867. switch(__r->_M_tag)
  868. {
  869. case __detail::_S_concat:
  870. {
  871. _RopeConcatenation* __conc = (_RopeConcatenation*)__r;
  872. _RopeRep* __left = __conc->_M_left;
  873. size_t __left_len = __left->_M_size;
  874. if (__begin < __left_len)
  875. {
  876. size_t __left_end = std::min(__left_len, __end);
  877. if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
  878. return false;
  879. }
  880. if (__end > __left_len)
  881. {
  882. _RopeRep* __right = __conc->_M_right;
  883. size_t __right_start = std::max(__left_len, __begin);
  884. if (!_S_apply_to_pieces(__c, __right,
  885. __right_start - __left_len,
  886. __end - __left_len))
  887. return false;
  888. }
  889. }
  890. return true;
  891. case __detail::_S_leaf:
  892. {
  893. _RopeLeaf* __l = (_RopeLeaf*)__r;
  894. return __c(__l->_M_data + __begin, __end - __begin);
  895. }
  896. case __detail::_S_function:
  897. case __detail::_S_substringfn:
  898. {
  899. _RopeFunction* __f = (_RopeFunction*)__r;
  900. size_t __len = __end - __begin;
  901. bool __result;
  902. _CharT* __buffer =
  903. (_CharT*)_Alloc().allocate(__len * sizeof(_CharT));
  904. __try
  905. {
  906. (*(__f->_M_fn))(__begin, __len, __buffer);
  907. __result = __c(__buffer, __len);
  908. _Alloc().deallocate(__buffer, __len * sizeof(_CharT));
  909. }
  910. __catch(...)
  911. {
  912. _Alloc().deallocate(__buffer, __len * sizeof(_CharT));
  913. __throw_exception_again;
  914. }
  915. return __result;
  916. }
  917. default:
  918. return false;
  919. }
  920. }
  921. template<class _CharT, class _Traits>
  922. inline void
  923. _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
  924. {
  925. char __f = __o.fill();
  926. size_t __i;
  927. for (__i = 0; __i < __n; __i++)
  928. __o.put(__f);
  929. }
  930. template <class _CharT>
  931. inline bool
  932. _Rope_is_simple(_CharT*)
  933. { return false; }
  934. inline bool
  935. _Rope_is_simple(char*)
  936. { return true; }
  937. inline bool
  938. _Rope_is_simple(wchar_t*)
  939. { return true; }
  940. template<class _CharT, class _Traits, class _Alloc>
  941. basic_ostream<_CharT, _Traits>&
  942. operator<<(basic_ostream<_CharT, _Traits>& __o,
  943. const rope<_CharT, _Alloc>& __r)
  944. {
  945. size_t __w = __o.width();
  946. bool __left = bool(__o.flags() & std::ios::left);
  947. size_t __pad_len;
  948. size_t __rope_len = __r.size();
  949. _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
  950. bool __is_simple = _Rope_is_simple((_CharT*)0);
  951. if (__rope_len < __w)
  952. __pad_len = __w - __rope_len;
  953. else
  954. __pad_len = 0;
  955. if (!__is_simple)
  956. __o.width(__w / __rope_len);
  957. __try
  958. {
  959. if (__is_simple && !__left && __pad_len > 0)
  960. _Rope_fill(__o, __pad_len);
  961. __r.apply_to_pieces(0, __r.size(), __c);
  962. if (__is_simple && __left && __pad_len > 0)
  963. _Rope_fill(__o, __pad_len);
  964. if (!__is_simple)
  965. __o.width(__w);
  966. }
  967. __catch(...)
  968. {
  969. if (!__is_simple)
  970. __o.width(__w);
  971. __throw_exception_again;
  972. }
  973. return __o;
  974. }
  975. template <class _CharT, class _Alloc>
  976. _CharT*
  977. rope<_CharT, _Alloc>::
  978. _S_flatten(_RopeRep* __r, size_t __start, size_t __len,
  979. _CharT* __buffer)
  980. {
  981. _Rope_flatten_char_consumer<_CharT> __c(__buffer);
  982. _S_apply_to_pieces(__c, __r, __start, __start + __len);
  983. return(__buffer + __len);
  984. }
  985. template <class _CharT, class _Alloc>
  986. size_t
  987. rope<_CharT, _Alloc>::
  988. find(_CharT __pattern, size_t __start) const
  989. {
  990. _Rope_find_char_char_consumer<_CharT> __c(__pattern);
  991. _S_apply_to_pieces(__c, this->_M_tree_ptr, __start, size());
  992. size_type __result_pos = __start + __c._M_count;
  993. #ifndef __STL_OLD_ROPE_SEMANTICS
  994. if (__result_pos == size())
  995. __result_pos = npos;
  996. #endif
  997. return __result_pos;
  998. }
  999. template <class _CharT, class _Alloc>
  1000. _CharT*
  1001. rope<_CharT, _Alloc>::
  1002. _S_flatten(_RopeRep* __r, _CharT* __buffer)
  1003. {
  1004. if (0 == __r)
  1005. return __buffer;
  1006. switch(__r->_M_tag)
  1007. {
  1008. case __detail::_S_concat:
  1009. {
  1010. _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1011. _RopeRep* __left = __c->_M_left;
  1012. _RopeRep* __right = __c->_M_right;
  1013. _CharT* __rest = _S_flatten(__left, __buffer);
  1014. return _S_flatten(__right, __rest);
  1015. }
  1016. case __detail::_S_leaf:
  1017. {
  1018. _RopeLeaf* __l = (_RopeLeaf*)__r;
  1019. return copy_n(__l->_M_data, __l->_M_size, __buffer).second;
  1020. }
  1021. case __detail::_S_function:
  1022. case __detail::_S_substringfn:
  1023. // We don't yet do anything with substring nodes.
  1024. // This needs to be fixed before ropefiles will work well.
  1025. {
  1026. _RopeFunction* __f = (_RopeFunction*)__r;
  1027. (*(__f->_M_fn))(0, __f->_M_size, __buffer);
  1028. return __buffer + __f->_M_size;
  1029. }
  1030. default:
  1031. return 0;
  1032. }
  1033. }
  1034. // This needs work for _CharT != char
  1035. template <class _CharT, class _Alloc>
  1036. void
  1037. rope<_CharT, _Alloc>::
  1038. _S_dump(_RopeRep* __r, int __indent)
  1039. {
  1040. for (int __i = 0; __i < __indent; __i++)
  1041. putchar(' ');
  1042. if (0 == __r)
  1043. {
  1044. printf("NULL\n");
  1045. return;
  1046. }
  1047. if (_S_concat == __r->_M_tag)
  1048. {
  1049. _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1050. _RopeRep* __left = __c->_M_left;
  1051. _RopeRep* __right = __c->_M_right;
  1052. #ifdef __GC
  1053. printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n",
  1054. __r, __r->_M_depth, __r->_M_size,
  1055. __r->_M_is_balanced? "" : "not");
  1056. #else
  1057. printf("Concatenation %p (rc = %ld, depth = %d, "
  1058. "len = %ld, %s balanced)\n",
  1059. __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size,
  1060. __r->_M_is_balanced? "" : "not");
  1061. #endif
  1062. _S_dump(__left, __indent + 2);
  1063. _S_dump(__right, __indent + 2);
  1064. return;
  1065. }
  1066. else
  1067. {
  1068. char* __kind;
  1069. switch (__r->_M_tag)
  1070. {
  1071. case __detail::_S_leaf:
  1072. __kind = "Leaf";
  1073. break;
  1074. case __detail::_S_function:
  1075. __kind = "Function";
  1076. break;
  1077. case __detail::_S_substringfn:
  1078. __kind = "Function representing substring";
  1079. break;
  1080. default:
  1081. __kind = "(corrupted kind field!)";
  1082. }
  1083. #ifdef __GC
  1084. printf("%s %p (depth = %d, len = %ld) ",
  1085. __kind, __r, __r->_M_depth, __r->_M_size);
  1086. #else
  1087. printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
  1088. __kind, __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size);
  1089. #endif
  1090. if (_S_is_one_byte_char_type((_CharT*)0))
  1091. {
  1092. const int __max_len = 40;
  1093. _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len));
  1094. _CharT __buffer[__max_len + 1];
  1095. bool __too_big = __r->_M_size > __prefix->_M_size;
  1096. _S_flatten(__prefix, __buffer);
  1097. __buffer[__prefix->_M_size] = _S_eos((_CharT*)0);
  1098. printf("%s%s\n", (char*)__buffer,
  1099. __too_big? "...\n" : "\n");
  1100. }
  1101. else
  1102. printf("\n");
  1103. }
  1104. }
  1105. template <class _CharT, class _Alloc>
  1106. const unsigned long
  1107. rope<_CharT, _Alloc>::
  1108. _S_min_len[int(__detail::_S_max_rope_depth) + 1] = {
  1109. /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21,
  1110. /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377,
  1111. /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181,
  1112. /* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368,
  1113. /* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811,
  1114. /* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309,
  1115. /* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352,
  1116. /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155,
  1117. /* 39 */165580141, /* 40 */267914296, /* 41 */433494437,
  1118. /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903,
  1119. /* 45 */2971215073u };
  1120. // These are Fibonacci numbers < 2**32.
  1121. template <class _CharT, class _Alloc>
  1122. typename rope<_CharT, _Alloc>::_RopeRep*
  1123. rope<_CharT, _Alloc>::
  1124. _S_balance(_RopeRep* __r)
  1125. {
  1126. _RopeRep* __forest[int(__detail::_S_max_rope_depth) + 1];
  1127. _RopeRep* __result = 0;
  1128. int __i;
  1129. // Invariant:
  1130. // The concatenation of forest in descending order is equal to __r.
  1131. // __forest[__i]._M_size >= _S_min_len[__i]
  1132. // __forest[__i]._M_depth = __i
  1133. // References from forest are included in refcount.
  1134. for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i)
  1135. __forest[__i] = 0;
  1136. __try
  1137. {
  1138. _S_add_to_forest(__r, __forest);
  1139. for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i)
  1140. if (0 != __forest[__i])
  1141. {
  1142. #ifndef __GC
  1143. _Self_destruct_ptr __old(__result);
  1144. #endif
  1145. __result = _S_concat(__forest[__i], __result);
  1146. __forest[__i]->_M_unref_nonnil();
  1147. #if !defined(__GC) && __cpp_exceptions
  1148. __forest[__i] = 0;
  1149. #endif
  1150. }
  1151. }
  1152. __catch(...)
  1153. {
  1154. for(__i = 0; __i <= int(__detail::_S_max_rope_depth); __i++)
  1155. _S_unref(__forest[__i]);
  1156. __throw_exception_again;
  1157. }
  1158. if (__result->_M_depth > int(__detail::_S_max_rope_depth))
  1159. __throw_length_error(__N("rope::_S_balance"));
  1160. return(__result);
  1161. }
  1162. template <class _CharT, class _Alloc>
  1163. void
  1164. rope<_CharT, _Alloc>::
  1165. _S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
  1166. {
  1167. if (__r->_M_is_balanced)
  1168. {
  1169. _S_add_leaf_to_forest(__r, __forest);
  1170. return;
  1171. }
  1172. {
  1173. _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1174. _S_add_to_forest(__c->_M_left, __forest);
  1175. _S_add_to_forest(__c->_M_right, __forest);
  1176. }
  1177. }
  1178. template <class _CharT, class _Alloc>
  1179. void
  1180. rope<_CharT, _Alloc>::
  1181. _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
  1182. {
  1183. _RopeRep* __insertee; // included in refcount
  1184. _RopeRep* __too_tiny = 0; // included in refcount
  1185. int __i; // forest[0..__i-1] is empty
  1186. size_t __s = __r->_M_size;
  1187. for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i)
  1188. {
  1189. if (0 != __forest[__i])
  1190. {
  1191. #ifndef __GC
  1192. _Self_destruct_ptr __old(__too_tiny);
  1193. #endif
  1194. __too_tiny = _S_concat_and_set_balanced(__forest[__i],
  1195. __too_tiny);
  1196. __forest[__i]->_M_unref_nonnil();
  1197. __forest[__i] = 0;
  1198. }
  1199. }
  1200. {
  1201. #ifndef __GC
  1202. _Self_destruct_ptr __old(__too_tiny);
  1203. #endif
  1204. __insertee = _S_concat_and_set_balanced(__too_tiny, __r);
  1205. }
  1206. // Too_tiny dead, and no longer included in refcount.
  1207. // Insertee is live and included.
  1208. for (;; ++__i)
  1209. {
  1210. if (0 != __forest[__i])
  1211. {
  1212. #ifndef __GC
  1213. _Self_destruct_ptr __old(__insertee);
  1214. #endif
  1215. __insertee = _S_concat_and_set_balanced(__forest[__i],
  1216. __insertee);
  1217. __forest[__i]->_M_unref_nonnil();
  1218. __forest[__i] = 0;
  1219. }
  1220. if (__i == int(__detail::_S_max_rope_depth)
  1221. || __insertee->_M_size < _S_min_len[__i+1])
  1222. {
  1223. __forest[__i] = __insertee;
  1224. // refcount is OK since __insertee is now dead.
  1225. return;
  1226. }
  1227. }
  1228. }
  1229. template <class _CharT, class _Alloc>
  1230. _CharT
  1231. rope<_CharT, _Alloc>::
  1232. _S_fetch(_RopeRep* __r, size_type __i)
  1233. {
  1234. __GC_CONST _CharT* __cstr = __r->_M_c_string;
  1235. if (0 != __cstr)
  1236. return __cstr[__i];
  1237. for(;;)
  1238. {
  1239. switch(__r->_M_tag)
  1240. {
  1241. case __detail::_S_concat:
  1242. {
  1243. _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1244. _RopeRep* __left = __c->_M_left;
  1245. size_t __left_len = __left->_M_size;
  1246. if (__i >= __left_len)
  1247. {
  1248. __i -= __left_len;
  1249. __r = __c->_M_right;
  1250. }
  1251. else
  1252. __r = __left;
  1253. }
  1254. break;
  1255. case __detail::_S_leaf:
  1256. {
  1257. _RopeLeaf* __l = (_RopeLeaf*)__r;
  1258. return __l->_M_data[__i];
  1259. }
  1260. case __detail::_S_function:
  1261. case __detail::_S_substringfn:
  1262. {
  1263. _RopeFunction* __f = (_RopeFunction*)__r;
  1264. _CharT __result;
  1265. (*(__f->_M_fn))(__i, 1, &__result);
  1266. return __result;
  1267. }
  1268. }
  1269. }
  1270. }
  1271. #ifndef __GC
  1272. // Return a uniquely referenced character slot for the given
  1273. // position, or 0 if that's not possible.
  1274. template <class _CharT, class _Alloc>
  1275. _CharT*
  1276. rope<_CharT, _Alloc>::
  1277. _S_fetch_ptr(_RopeRep* __r, size_type __i)
  1278. {
  1279. _RopeRep* __clrstack[__detail::_S_max_rope_depth];
  1280. size_t __csptr = 0;
  1281. for(;;)
  1282. {
  1283. if (__r->_M_ref_count > 1)
  1284. return 0;
  1285. switch(__r->_M_tag)
  1286. {
  1287. case __detail::_S_concat:
  1288. {
  1289. _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1290. _RopeRep* __left = __c->_M_left;
  1291. size_t __left_len = __left->_M_size;
  1292. if (__c->_M_c_string != 0)
  1293. __clrstack[__csptr++] = __c;
  1294. if (__i >= __left_len)
  1295. {
  1296. __i -= __left_len;
  1297. __r = __c->_M_right;
  1298. }
  1299. else
  1300. __r = __left;
  1301. }
  1302. break;
  1303. case __detail::_S_leaf:
  1304. {
  1305. _RopeLeaf* __l = (_RopeLeaf*)__r;
  1306. if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0)
  1307. __clrstack[__csptr++] = __l;
  1308. while (__csptr > 0)
  1309. {
  1310. -- __csptr;
  1311. _RopeRep* __d = __clrstack[__csptr];
  1312. __d->_M_free_c_string();
  1313. __d->_M_c_string = 0;
  1314. }
  1315. return __l->_M_data + __i;
  1316. }
  1317. case __detail::_S_function:
  1318. case __detail::_S_substringfn:
  1319. return 0;
  1320. }
  1321. }
  1322. }
  1323. #endif /* __GC */
  1324. // The following could be implemented trivially using
  1325. // lexicographical_compare_3way.
  1326. // We do a little more work to avoid dealing with rope iterators for
  1327. // flat strings.
  1328. template <class _CharT, class _Alloc>
  1329. int
  1330. rope<_CharT, _Alloc>::
  1331. _S_compare (const _RopeRep* __left, const _RopeRep* __right)
  1332. {
  1333. size_t __left_len;
  1334. size_t __right_len;
  1335. if (0 == __right)
  1336. return 0 != __left;
  1337. if (0 == __left)
  1338. return -1;
  1339. __left_len = __left->_M_size;
  1340. __right_len = __right->_M_size;
  1341. if (__detail::_S_leaf == __left->_M_tag)
  1342. {
  1343. _RopeLeaf* __l = (_RopeLeaf*) __left;
  1344. if (__detail::_S_leaf == __right->_M_tag)
  1345. {
  1346. _RopeLeaf* __r = (_RopeLeaf*) __right;
  1347. return lexicographical_compare_3way(__l->_M_data,
  1348. __l->_M_data + __left_len,
  1349. __r->_M_data, __r->_M_data
  1350. + __right_len);
  1351. }
  1352. else
  1353. {
  1354. const_iterator __rstart(__right, 0);
  1355. const_iterator __rend(__right, __right_len);
  1356. return lexicographical_compare_3way(__l->_M_data, __l->_M_data
  1357. + __left_len,
  1358. __rstart, __rend);
  1359. }
  1360. }
  1361. else
  1362. {
  1363. const_iterator __lstart(__left, 0);
  1364. const_iterator __lend(__left, __left_len);
  1365. if (__detail::_S_leaf == __right->_M_tag)
  1366. {
  1367. _RopeLeaf* __r = (_RopeLeaf*) __right;
  1368. return lexicographical_compare_3way(__lstart, __lend,
  1369. __r->_M_data, __r->_M_data
  1370. + __right_len);
  1371. }
  1372. else
  1373. {
  1374. const_iterator __rstart(__right, 0);
  1375. const_iterator __rend(__right, __right_len);
  1376. return lexicographical_compare_3way(__lstart, __lend,
  1377. __rstart, __rend);
  1378. }
  1379. }
  1380. }
  1381. // Assignment to reference proxies.
  1382. template <class _CharT, class _Alloc>
  1383. _Rope_char_ref_proxy<_CharT, _Alloc>&
  1384. _Rope_char_ref_proxy<_CharT, _Alloc>::
  1385. operator=(_CharT __c)
  1386. {
  1387. _RopeRep* __old = _M_root->_M_tree_ptr;
  1388. #ifndef __GC
  1389. // First check for the case in which everything is uniquely
  1390. // referenced. In that case we can do this destructively.
  1391. _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos);
  1392. if (0 != __ptr)
  1393. {
  1394. *__ptr = __c;
  1395. return *this;
  1396. }
  1397. #endif
  1398. _Self_destruct_ptr __left(_My_rope::_S_substring(__old, 0, _M_pos));
  1399. _Self_destruct_ptr __right(_My_rope::_S_substring(__old, _M_pos + 1,
  1400. __old->_M_size));
  1401. _Self_destruct_ptr __result_left(_My_rope::
  1402. _S_destr_concat_char_iter(__left,
  1403. &__c, 1));
  1404. _RopeRep* __result = _My_rope::_S_concat(__result_left, __right);
  1405. #ifndef __GC
  1406. _RopeRep::_S_unref(__old);
  1407. #endif
  1408. _M_root->_M_tree_ptr = __result;
  1409. return *this;
  1410. }
  1411. template <class _CharT, class _Alloc>
  1412. inline _Rope_char_ref_proxy<_CharT, _Alloc>::
  1413. operator _CharT() const
  1414. {
  1415. if (_M_current_valid)
  1416. return _M_current;
  1417. else
  1418. return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos);
  1419. }
  1420. template <class _CharT, class _Alloc>
  1421. _Rope_char_ptr_proxy<_CharT, _Alloc>
  1422. _Rope_char_ref_proxy<_CharT, _Alloc>::
  1423. operator&() const
  1424. { return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this); }
  1425. template <class _CharT, class _Alloc>
  1426. rope<_CharT, _Alloc>::
  1427. rope(size_t __n, _CharT __c, const allocator_type& __a)
  1428. : _Base(__a)
  1429. {
  1430. rope<_CharT,_Alloc> __result;
  1431. const size_t __exponentiate_threshold = 32;
  1432. size_t __exponent;
  1433. size_t __rest;
  1434. _CharT* __rest_buffer;
  1435. _RopeRep* __remainder;
  1436. rope<_CharT, _Alloc> __remainder_rope;
  1437. if (0 == __n)
  1438. return;
  1439. __exponent = __n / __exponentiate_threshold;
  1440. __rest = __n % __exponentiate_threshold;
  1441. if (0 == __rest)
  1442. __remainder = 0;
  1443. else
  1444. {
  1445. __rest_buffer = this->_Data_allocate(_S_rounded_up_size(__rest));
  1446. __uninitialized_fill_n_a(__rest_buffer, __rest, __c,
  1447. _M_get_allocator());
  1448. _S_cond_store_eos(__rest_buffer[__rest]);
  1449. __try
  1450. { __remainder = _S_new_RopeLeaf(__rest_buffer, __rest,
  1451. _M_get_allocator()); }
  1452. __catch(...)
  1453. {
  1454. _RopeRep::__STL_FREE_STRING(__rest_buffer, __rest,
  1455. _M_get_allocator());
  1456. __throw_exception_again;
  1457. }
  1458. }
  1459. __remainder_rope._M_tree_ptr = __remainder;
  1460. if (__exponent != 0)
  1461. {
  1462. _CharT* __base_buffer =
  1463. this->_Data_allocate(_S_rounded_up_size(__exponentiate_threshold));
  1464. _RopeLeaf* __base_leaf;
  1465. rope __base_rope;
  1466. __uninitialized_fill_n_a(__base_buffer, __exponentiate_threshold, __c,
  1467. _M_get_allocator());
  1468. _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
  1469. __try
  1470. {
  1471. __base_leaf = _S_new_RopeLeaf(__base_buffer,
  1472. __exponentiate_threshold,
  1473. _M_get_allocator());
  1474. }
  1475. __catch(...)
  1476. {
  1477. _RopeRep::__STL_FREE_STRING(__base_buffer,
  1478. __exponentiate_threshold,
  1479. _M_get_allocator());
  1480. __throw_exception_again;
  1481. }
  1482. __base_rope._M_tree_ptr = __base_leaf;
  1483. if (1 == __exponent)
  1484. __result = __base_rope;
  1485. else
  1486. __result = power(__base_rope, __exponent,
  1487. _Rope_Concat_fn<_CharT, _Alloc>());
  1488. if (0 != __remainder)
  1489. __result += __remainder_rope;
  1490. }
  1491. else
  1492. __result = __remainder_rope;
  1493. this->_M_tree_ptr = __result._M_tree_ptr;
  1494. this->_M_tree_ptr->_M_ref_nonnil();
  1495. }
  1496. template<class _CharT, class _Alloc>
  1497. _CharT
  1498. rope<_CharT, _Alloc>::_S_empty_c_str[1];
  1499. template<class _CharT, class _Alloc>
  1500. const _CharT*
  1501. rope<_CharT, _Alloc>::
  1502. c_str() const
  1503. {
  1504. if (0 == this->_M_tree_ptr)
  1505. {
  1506. _S_empty_c_str[0] = _S_eos((_CharT*)0); // Possibly redundant,
  1507. // but probably fast.
  1508. return _S_empty_c_str;
  1509. }
  1510. __gthread_mutex_lock (&this->_M_tree_ptr->_M_c_string_lock);
  1511. __GC_CONST _CharT* __result = this->_M_tree_ptr->_M_c_string;
  1512. if (0 == __result)
  1513. {
  1514. size_t __s = size();
  1515. __result = this->_Data_allocate(__s + 1);
  1516. _S_flatten(this->_M_tree_ptr, __result);
  1517. __result[__s] = _S_eos((_CharT*)0);
  1518. this->_M_tree_ptr->_M_c_string = __result;
  1519. }
  1520. __gthread_mutex_unlock (&this->_M_tree_ptr->_M_c_string_lock);
  1521. return(__result);
  1522. }
  1523. template<class _CharT, class _Alloc>
  1524. const _CharT* rope<_CharT, _Alloc>::
  1525. replace_with_c_str()
  1526. {
  1527. if (0 == this->_M_tree_ptr)
  1528. {
  1529. _S_empty_c_str[0] = _S_eos((_CharT*)0);
  1530. return _S_empty_c_str;
  1531. }
  1532. __GC_CONST _CharT* __old_c_string = this->_M_tree_ptr->_M_c_string;
  1533. if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag
  1534. && 0 != __old_c_string)
  1535. return(__old_c_string);
  1536. size_t __s = size();
  1537. _CharT* __result = this->_Data_allocate(_S_rounded_up_size(__s));
  1538. _S_flatten(this->_M_tree_ptr, __result);
  1539. __result[__s] = _S_eos((_CharT*)0);
  1540. this->_M_tree_ptr->_M_unref_nonnil();
  1541. this->_M_tree_ptr = _S_new_RopeLeaf(__result, __s,
  1542. this->_M_get_allocator());
  1543. return(__result);
  1544. }
  1545. // Algorithm specializations. More should be added.
  1546. template<class _Rope_iterator> // was templated on CharT and Alloc
  1547. void // VC++ workaround
  1548. _Rope_rotate(_Rope_iterator __first,
  1549. _Rope_iterator __middle,
  1550. _Rope_iterator __last)
  1551. {
  1552. typedef typename _Rope_iterator::value_type _CharT;
  1553. typedef typename _Rope_iterator::_allocator_type _Alloc;
  1554. rope<_CharT, _Alloc>& __r(__first.container());
  1555. rope<_CharT, _Alloc> __prefix = __r.substr(0, __first.index());
  1556. rope<_CharT, _Alloc> __suffix =
  1557. __r.substr(__last.index(), __r.size() - __last.index());
  1558. rope<_CharT, _Alloc> __part1 =
  1559. __r.substr(__middle.index(), __last.index() - __middle.index());
  1560. rope<_CharT, _Alloc> __part2 =
  1561. __r.substr(__first.index(), __middle.index() - __first.index());
  1562. __r = __prefix;
  1563. __r += __part1;
  1564. __r += __part2;
  1565. __r += __suffix;
  1566. }
  1567. #if !defined(__GNUC__)
  1568. // Appears to confuse g++
  1569. inline void
  1570. rotate(_Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __first,
  1571. _Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __middle,
  1572. _Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __last)
  1573. { _Rope_rotate(__first, __middle, __last); }
  1574. #endif
  1575. # if 0
  1576. // Probably not useful for several reasons:
  1577. // - for SGIs 7.1 compiler and probably some others,
  1578. // this forces lots of rope<wchar_t, ...> instantiations, creating a
  1579. // code bloat and compile time problem. (Fixed in 7.2.)
  1580. // - wchar_t is 4 bytes wide on most UNIX platforms, making it
  1581. // unattractive for unicode strings. Unsigned short may be a better
  1582. // character type.
  1583. inline void
  1584. rotate(_Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __first,
  1585. _Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __middle,
  1586. _Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __last)
  1587. { _Rope_rotate(__first, __middle, __last); }
  1588. # endif
  1589. _GLIBCXX_END_NAMESPACE_VERSION
  1590. } // namespace