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