1 // SGI's rope class implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 * This is an internal header file, included by other library headers.
45 * You should not attempt to use it directly.
50 #include <bits/functexcept.h>
52 #include <ext/algorithm> // For copy_n and lexicographical_compare_3way
53 #include <ext/memory> // For uninitialized_copy_n
54 #include <ext/numeric> // For power
60 using std::basic_ostream
;
61 using std::__throw_length_error
;
63 using std::uninitialized_fill_n
;
65 // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
66 // if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct.
67 // Results in a valid buf_ptr if the iterator can be legitimately
69 template <class _CharT
, class _Alloc
>
71 _Rope_iterator_base
<_CharT
, _Alloc
>::
72 _S_setbuf(_Rope_iterator_base
<_CharT
, _Alloc
>& __x
)
74 const _RopeRep
* __leaf
= __x
._M_path_end
[__x
._M_leaf_index
];
75 size_t __leaf_pos
= __x
._M_leaf_pos
;
76 size_t __pos
= __x
._M_current_pos
;
78 switch(__leaf
->_M_tag
)
80 case _Rope_constants::_S_leaf
:
81 __x
._M_buf_start
= ((_Rope_RopeLeaf
<_CharT
, _Alloc
>*)__leaf
)->_M_data
;
82 __x
._M_buf_ptr
= __x
._M_buf_start
+ (__pos
- __leaf_pos
);
83 __x
._M_buf_end
= __x
._M_buf_start
+ __leaf
->_M_size
;
85 case _Rope_constants::_S_function
:
86 case _Rope_constants::_S_substringfn
:
88 size_t __len
= _S_iterator_buf_len
;
89 size_t __buf_start_pos
= __leaf_pos
;
90 size_t __leaf_end
= __leaf_pos
+ __leaf
->_M_size
;
91 char_producer
<_CharT
>* __fn
= ((_Rope_RopeFunction
<_CharT
,
92 _Alloc
>*)__leaf
)->_M_fn
;
93 if (__buf_start_pos
+ __len
<= __pos
)
95 __buf_start_pos
= __pos
- __len
/ 4;
96 if (__buf_start_pos
+ __len
> __leaf_end
)
97 __buf_start_pos
= __leaf_end
- __len
;
99 if (__buf_start_pos
+ __len
> __leaf_end
)
100 __len
= __leaf_end
- __buf_start_pos
;
101 (*__fn
)(__buf_start_pos
- __leaf_pos
, __len
, __x
._M_tmp_buf
);
102 __x
._M_buf_ptr
= __x
._M_tmp_buf
+ (__pos
- __buf_start_pos
);
103 __x
._M_buf_start
= __x
._M_tmp_buf
;
104 __x
._M_buf_end
= __x
._M_tmp_buf
+ __len
;
112 // Set path and buffer inside a rope iterator. We assume that
113 // pos and root are already set.
114 template <class _CharT
, class _Alloc
>
116 _Rope_iterator_base
<_CharT
, _Alloc
>::
117 _S_setcache(_Rope_iterator_base
<_CharT
, _Alloc
>& __x
)
119 const _RopeRep
* __path
[int(_Rope_constants::_S_max_rope_depth
) + 1];
120 const _RopeRep
* __curr_rope
;
121 int __curr_depth
= -1; /* index into path */
122 size_t __curr_start_pos
= 0;
123 size_t __pos
= __x
._M_current_pos
;
124 unsigned char __dirns
= 0; // Bit vector marking right turns in the path
126 if (__pos
>= __x
._M_root
->_M_size
)
131 __curr_rope
= __x
._M_root
;
132 if (0 != __curr_rope
->_M_c_string
)
134 /* Treat the root as a leaf. */
135 __x
._M_buf_start
= __curr_rope
->_M_c_string
;
136 __x
._M_buf_end
= __curr_rope
->_M_c_string
+ __curr_rope
->_M_size
;
137 __x
._M_buf_ptr
= __curr_rope
->_M_c_string
+ __pos
;
138 __x
._M_path_end
[0] = __curr_rope
;
139 __x
._M_leaf_index
= 0;
146 __path
[__curr_depth
] = __curr_rope
;
147 switch(__curr_rope
->_M_tag
)
149 case _Rope_constants::_S_leaf
:
150 case _Rope_constants::_S_function
:
151 case _Rope_constants::_S_substringfn
:
152 __x
._M_leaf_pos
= __curr_start_pos
;
154 case _Rope_constants::_S_concat
:
156 _Rope_RopeConcatenation
<_CharT
, _Alloc
>* __c
=
157 (_Rope_RopeConcatenation
<_CharT
, _Alloc
>*)__curr_rope
;
158 _RopeRep
* __left
= __c
->_M_left
;
159 size_t __left_len
= __left
->_M_size
;
162 if (__pos
>= __curr_start_pos
+ __left_len
)
165 __curr_rope
= __c
->_M_right
;
166 __curr_start_pos
+= __left_len
;
169 __curr_rope
= __left
;
175 // Copy last section of path into _M_path_end.
178 int __j
= __curr_depth
+ 1 - int(_S_path_cache_len
);
180 if (__j
< 0) __j
= 0;
181 while (__j
<= __curr_depth
)
182 __x
._M_path_end
[++__i
] = __path
[__j
++];
183 __x
._M_leaf_index
= __i
;
185 __x
._M_path_directions
= __dirns
;
189 // Specialized version of the above. Assumes that
190 // the path cache is valid for the previous position.
191 template <class _CharT
, class _Alloc
>
193 _Rope_iterator_base
<_CharT
, _Alloc
>::
194 _S_setcache_for_incr(_Rope_iterator_base
<_CharT
, _Alloc
>& __x
)
196 int __current_index
= __x
._M_leaf_index
;
197 const _RopeRep
* __current_node
= __x
._M_path_end
[__current_index
];
198 size_t __len
= __current_node
->_M_size
;
199 size_t __node_start_pos
= __x
._M_leaf_pos
;
200 unsigned char __dirns
= __x
._M_path_directions
;
201 _Rope_RopeConcatenation
<_CharT
, _Alloc
>* __c
;
203 if (__x
._M_current_pos
- __node_start_pos
< __len
)
205 /* More stuff in this leaf, we just didn't cache it. */
209 // node_start_pos is starting position of last_node.
210 while (--__current_index
>= 0)
212 if (!(__dirns
& 1) /* Path turned left */)
214 __current_node
= __x
._M_path_end
[__current_index
];
215 __c
= (_Rope_RopeConcatenation
<_CharT
, _Alloc
>*)__current_node
;
216 // Otherwise we were in the right child. Thus we should pop
217 // the concatenation node.
218 __node_start_pos
-= __c
->_M_left
->_M_size
;
221 if (__current_index
< 0)
223 // We underflowed the cache. Punt.
227 __current_node
= __x
._M_path_end
[__current_index
];
228 __c
= (_Rope_RopeConcatenation
<_CharT
, _Alloc
>*)__current_node
;
229 // current_node is a concatenation node. We are positioned on the first
230 // character in its right child.
231 // node_start_pos is starting position of current_node.
232 __node_start_pos
+= __c
->_M_left
->_M_size
;
233 __current_node
= __c
->_M_right
;
234 __x
._M_path_end
[++__current_index
] = __current_node
;
236 while (_Rope_constants::_S_concat
== __current_node
->_M_tag
)
239 if (int(_S_path_cache_len
) == __current_index
)
242 for (__i
= 0; __i
< int(_S_path_cache_len
) - 1; __i
++)
243 __x
._M_path_end
[__i
] = __x
._M_path_end
[__i
+1];
247 ((_Rope_RopeConcatenation
<_CharT
, _Alloc
>*)__current_node
)->_M_left
;
248 __x
._M_path_end
[__current_index
] = __current_node
;
250 // node_start_pos is unchanged.
252 __x
._M_leaf_index
= __current_index
;
253 __x
._M_leaf_pos
= __node_start_pos
;
254 __x
._M_path_directions
= __dirns
;
258 template <class _CharT
, class _Alloc
>
260 _Rope_iterator_base
<_CharT
, _Alloc
>::
263 _M_current_pos
+= __n
;
266 size_t __chars_left
= _M_buf_end
- _M_buf_ptr
;
267 if (__chars_left
> __n
)
269 else if (__chars_left
== __n
)
272 _S_setcache_for_incr(*this);
279 template <class _CharT
, class _Alloc
>
281 _Rope_iterator_base
<_CharT
, _Alloc
>::
286 size_t __chars_left
= _M_buf_ptr
- _M_buf_start
;
287 if (__chars_left
>= __n
)
292 _M_current_pos
-= __n
;
295 template <class _CharT
, class _Alloc
>
297 _Rope_iterator
<_CharT
, _Alloc
>::
300 if (_M_root_rope
->_M_tree_ptr
!= this->_M_root
)
302 // _Rope was modified. Get things fixed up.
303 _RopeRep::_S_unref(this->_M_root
);
304 this->_M_root
= _M_root_rope
->_M_tree_ptr
;
305 _RopeRep::_S_ref(this->_M_root
);
306 this->_M_buf_ptr
= 0;
310 template <class _CharT
, class _Alloc
>
312 _Rope_const_iterator
<_CharT
, _Alloc
>::
313 _Rope_const_iterator(const _Rope_iterator
<_CharT
, _Alloc
>& __x
)
314 : _Rope_iterator_base
<_CharT
, _Alloc
>(__x
)
317 template <class _CharT
, class _Alloc
>
319 _Rope_iterator
<_CharT
, _Alloc
>::
320 _Rope_iterator(rope
<_CharT
, _Alloc
>& __r
, size_t __pos
)
321 : _Rope_iterator_base
<_CharT
,_Alloc
>(__r
._M_tree_ptr
, __pos
),
323 { _RopeRep::_S_ref(this->_M_root
); }
325 template <class _CharT
, class _Alloc
>
327 rope
<_CharT
, _Alloc
>::
328 _S_char_ptr_len(const _CharT
* __s
)
330 const _CharT
* __p
= __s
;
332 while (!_S_is0(*__p
))
340 template <class _CharT
, class _Alloc
>
342 _Rope_RopeRep
<_CharT
, _Alloc
>::
345 _CharT
* __cstr
= _M_c_string
;
348 size_t __size
= this->_M_size
+ 1;
349 _Destroy(__cstr
, __cstr
+ __size
, get_allocator());
350 this->_Data_deallocate(__cstr
, __size
);
354 template <class _CharT
, class _Alloc
>
356 _Rope_RopeRep
<_CharT
, _Alloc
>::
357 _S_free_string(_CharT
* __s
, size_t __n
, allocator_type __a
)
359 if (!_S_is_basic_char_type((_CharT
*)0))
360 _Destroy(__s
, __s
+ __n
, __a
);
362 // This has to be a static member, so this gets a bit messy
364 _Rope_RopeLeaf
<_CharT
, _Alloc
>::_S_rounded_up_size(__n
));
367 // There are several reasons for not doing this with virtual destructors
368 // and a class specific delete operator:
369 // - A class specific delete operator can't easily get access to
370 // allocator instances if we need them.
371 // - Any virtual function would need a 4 or byte vtable pointer;
372 // this only requires a one byte tag per object.
373 template <class _CharT
, class _Alloc
>
375 _Rope_RopeRep
<_CharT
, _Alloc
>::
380 case _Rope_constants::_S_leaf
:
382 _Rope_RopeLeaf
<_CharT
, _Alloc
>* __l
383 = (_Rope_RopeLeaf
<_CharT
, _Alloc
>*)this;
384 __l
->_Rope_RopeLeaf
<_CharT
, _Alloc
>::~_Rope_RopeLeaf();
385 _L_deallocate(__l
, 1);
388 case _Rope_constants::_S_concat
:
390 _Rope_RopeConcatenation
<_CharT
,_Alloc
>* __c
391 = (_Rope_RopeConcatenation
<_CharT
, _Alloc
>*)this;
392 __c
->_Rope_RopeConcatenation
<_CharT
, _Alloc
>::
393 ~_Rope_RopeConcatenation();
394 _C_deallocate(__c
, 1);
397 case _Rope_constants::_S_function
:
399 _Rope_RopeFunction
<_CharT
, _Alloc
>* __f
400 = (_Rope_RopeFunction
<_CharT
, _Alloc
>*)this;
401 __f
->_Rope_RopeFunction
<_CharT
, _Alloc
>::~_Rope_RopeFunction();
402 _F_deallocate(__f
, 1);
405 case _Rope_constants::_S_substringfn
:
407 _Rope_RopeSubstring
<_CharT
, _Alloc
>* __ss
=
408 (_Rope_RopeSubstring
<_CharT
, _Alloc
>*)this;
409 __ss
->_Rope_RopeSubstring
<_CharT
, _Alloc
>::
410 ~_Rope_RopeSubstring();
411 _S_deallocate(__ss
, 1);
418 template <class _CharT
, class _Alloc
>
420 _Rope_RopeRep
<_CharT
, _Alloc
>::
421 _S_free_string(const _CharT
*, size_t, allocator_type
)
426 // Concatenate a C string onto a leaf rope by copying the rope data.
427 // Used for short ropes.
428 template <class _CharT
, class _Alloc
>
429 typename rope
<_CharT
, _Alloc
>::_RopeLeaf
*
430 rope
<_CharT
, _Alloc
>::
431 _S_leaf_concat_char_iter(_RopeLeaf
* __r
, const _CharT
* __iter
, size_t __len
)
433 size_t __old_len
= __r
->_M_size
;
434 _CharT
* __new_data
= (_CharT
*)
435 _Data_allocate(_S_rounded_up_size(__old_len
+ __len
));
438 uninitialized_copy_n(__r
->_M_data
, __old_len
, __new_data
);
439 uninitialized_copy_n(__iter
, __len
, __new_data
+ __old_len
);
440 _S_cond_store_eos(__new_data
[__old_len
+ __len
]);
443 __result
= _S_new_RopeLeaf(__new_data
, __old_len
+ __len
,
444 __r
->get_allocator());
448 _RopeRep::__STL_FREE_STRING(__new_data
, __old_len
+ __len
,
449 __r
->get_allocator());
450 __throw_exception_again
;
456 // As above, but it's OK to clobber original if refcount is 1
457 template <class _CharT
, class _Alloc
>
458 typename rope
<_CharT
,_Alloc
>::_RopeLeaf
*
459 rope
<_CharT
, _Alloc
>::
460 _S_destr_leaf_concat_char_iter(_RopeLeaf
* __r
, const _CharT
* __iter
,
463 if (__r
->_M_ref_count
> 1)
464 return _S_leaf_concat_char_iter(__r
, __iter
, __len
);
465 size_t __old_len
= __r
->_M_size
;
466 if (_S_allocated_capacity(__old_len
) >= __old_len
+ __len
)
468 // The space has been partially initialized for the standard
469 // character types. But that doesn't matter for those types.
470 uninitialized_copy_n(__iter
, __len
, __r
->_M_data
+ __old_len
);
471 if (_S_is_basic_char_type((_CharT
*)0))
472 _S_cond_store_eos(__r
->_M_data
[__old_len
+ __len
]);
473 else if (__r
->_M_c_string
!= __r
->_M_data
&& 0 != __r
->_M_c_string
)
475 __r
->_M_free_c_string();
476 __r
->_M_c_string
= 0;
478 __r
->_M_size
= __old_len
+ __len
;
479 __r
->_M_ref_count
= 2;
484 _RopeLeaf
* __result
= _S_leaf_concat_char_iter(__r
, __iter
, __len
);
490 // Assumes left and right are not 0.
491 // Does not increment (nor decrement on exception) child reference counts.
492 // Result has ref count 1.
493 template <class _CharT
, class _Alloc
>
494 typename rope
<_CharT
, _Alloc
>::_RopeRep
*
495 rope
<_CharT
, _Alloc
>::
496 _S_tree_concat(_RopeRep
* __left
, _RopeRep
* __right
)
498 _RopeConcatenation
* __result
= _S_new_RopeConcatenation(__left
, __right
,
501 size_t __depth
= __result
->_M_depth
;
504 && (__result
->_M_size
< 1000
505 || __depth
> size_t(_Rope_constants::_S_max_rope_depth
)))
507 _RopeRep
* __balanced
;
511 __balanced
= _S_balance(__result
);
512 __result
->_M_unref_nonnil();
516 _C_deallocate(__result
,1);
517 __throw_exception_again
;
519 // In case of exception, we need to deallocate
520 // otherwise dangling result node. But caller
521 // still owns its children. Thus unref is
529 template <class _CharT
, class _Alloc
>
530 typename rope
<_CharT
, _Alloc
>::_RopeRep
*
531 rope
<_CharT
, _Alloc
>::
532 _S_concat_char_iter(_RopeRep
* __r
, const _CharT
*__s
, size_t __slen
)
541 return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s
, __slen
,
542 __r
->get_allocator());
543 if (_Rope_constants::_S_leaf
== __r
->_M_tag
544 && __r
->_M_size
+ __slen
<= size_t(_S_copy_max
))
546 __result
= _S_leaf_concat_char_iter((_RopeLeaf
*)__r
, __s
, __slen
);
549 if (_Rope_constants::_S_concat
== __r
->_M_tag
550 && _Rope_constants::_S_leaf
== ((_RopeConcatenation
*)
551 __r
)->_M_right
->_M_tag
)
554 (_RopeLeaf
* )(((_RopeConcatenation
* )__r
)->_M_right
);
555 if (__right
->_M_size
+ __slen
<= size_t(_S_copy_max
))
557 _RopeRep
* __left
= ((_RopeConcatenation
*)__r
)->_M_left
;
559 _S_leaf_concat_char_iter((_RopeLeaf
*)__right
, __s
, __slen
);
560 __left
->_M_ref_nonnil();
562 { __result
= _S_tree_concat(__left
, __nright
); }
567 __throw_exception_again
;
573 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s
, __slen
, __r
->get_allocator());
576 __r
->_M_ref_nonnil();
577 __result
= _S_tree_concat(__r
, __nright
);
583 __throw_exception_again
;
589 template <class _CharT
, class _Alloc
>
590 typename rope
<_CharT
,_Alloc
>::_RopeRep
*
591 rope
<_CharT
,_Alloc
>::
592 _S_destr_concat_char_iter(_RopeRep
* __r
, const _CharT
* __s
, size_t __slen
)
596 return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s
, __slen
,
597 __r
->get_allocator());
598 size_t __count
= __r
->_M_ref_count
;
599 size_t __orig_size
= __r
->_M_size
;
601 return _S_concat_char_iter(__r
, __s
, __slen
);
604 __r
->_M_ref_count
= 2; // One more than before
607 if (__orig_size
+ __slen
<= size_t(_S_copy_max
)
608 && _Rope_constants::_S_leaf
== __r
->_M_tag
)
610 __result
= _S_destr_leaf_concat_char_iter((_RopeLeaf
*)__r
, __s
,
614 if (_Rope_constants::_S_concat
== __r
->_M_tag
)
616 _RopeLeaf
* __right
= (_RopeLeaf
*)(((_RopeConcatenation
*)
618 if (_Rope_constants::_S_leaf
== __right
->_M_tag
619 && __right
->_M_size
+ __slen
<= size_t(_S_copy_max
))
621 _RopeRep
* __new_right
=
622 _S_destr_leaf_concat_char_iter(__right
, __s
, __slen
);
623 if (__right
== __new_right
)
624 __new_right
->_M_ref_count
= 1;
626 __right
->_M_unref_nonnil();
627 __r
->_M_ref_count
= 2; // One more than before.
628 ((_RopeConcatenation
*)__r
)->_M_right
= __new_right
;
629 __r
->_M_size
= __orig_size
+ __slen
;
630 if (0 != __r
->_M_c_string
)
632 __r
->_M_free_c_string();
633 __r
->_M_c_string
= 0;
639 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s
, __slen
, __r
->get_allocator());
640 __r
->_M_ref_nonnil();
642 { __result
= _S_tree_concat(__r
, __right
); }
647 __throw_exception_again
;
653 template <class _CharT
, class _Alloc
>
654 typename rope
<_CharT
, _Alloc
>::_RopeRep
*
655 rope
<_CharT
, _Alloc
>::
656 _S_concat(_RopeRep
* __left
, _RopeRep
* __right
)
665 __left
->_M_ref_nonnil();
668 if (_Rope_constants::_S_leaf
== __right
->_M_tag
)
670 if (_Rope_constants::_S_leaf
== __left
->_M_tag
)
672 if (__right
->_M_size
+ __left
->_M_size
<= size_t(_S_copy_max
))
673 return _S_leaf_concat_char_iter((_RopeLeaf
*)__left
,
674 ((_RopeLeaf
*)__right
)->_M_data
,
677 else if (_Rope_constants::_S_concat
== __left
->_M_tag
678 && _Rope_constants::_S_leaf
== ((_RopeConcatenation
*)
679 __left
)->_M_right
->_M_tag
)
681 _RopeLeaf
* __leftright
=
682 (_RopeLeaf
*)(((_RopeConcatenation
*)__left
)->_M_right
);
683 if (__leftright
->_M_size
684 + __right
->_M_size
<= size_t(_S_copy_max
))
686 _RopeRep
* __leftleft
= ((_RopeConcatenation
*)__left
)->_M_left
;
687 _RopeRep
* __rest
= _S_leaf_concat_char_iter(__leftright
,
692 __leftleft
->_M_ref_nonnil();
694 { return(_S_tree_concat(__leftleft
, __rest
)); }
697 _S_unref(__leftleft
);
699 __throw_exception_again
;
704 __left
->_M_ref_nonnil();
705 __right
->_M_ref_nonnil();
707 { return(_S_tree_concat(__left
, __right
)); }
712 __throw_exception_again
;
716 template <class _CharT
, class _Alloc
>
717 typename rope
<_CharT
, _Alloc
>::_RopeRep
*
718 rope
<_CharT
, _Alloc
>::
719 _S_substring(_RopeRep
* __base
, size_t __start
, size_t __endp1
)
723 size_t __len
= __base
->_M_size
;
725 const size_t __lazy_threshold
= 128;
727 if (__endp1
>= __len
)
731 __base
->_M_ref_nonnil();
739 __adj_endp1
= __endp1
;
741 switch(__base
->_M_tag
)
743 case _Rope_constants::_S_concat
:
745 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__base
;
746 _RopeRep
* __left
= __c
->_M_left
;
747 _RopeRep
* __right
= __c
->_M_right
;
748 size_t __left_len
= __left
->_M_size
;
751 if (__adj_endp1
<= __left_len
)
752 return _S_substring(__left
, __start
, __endp1
);
753 else if (__start
>= __left_len
)
754 return _S_substring(__right
, __start
- __left_len
,
755 __adj_endp1
- __left_len
);
756 _Self_destruct_ptr
__left_result(_S_substring(__left
,
759 _Self_destruct_ptr
__right_result(_S_substring(__right
, 0,
762 __result
= _S_concat(__left_result
, __right_result
);
765 case _Rope_constants::_S_leaf
:
767 _RopeLeaf
* __l
= (_RopeLeaf
*)__base
;
770 if (__start
>= __adj_endp1
)
772 __result_len
= __adj_endp1
- __start
;
773 if (__result_len
> __lazy_threshold
)
776 const _CharT
* __section
= __l
->_M_data
+ __start
;
777 __result
= _S_new_RopeLeaf(__section
, __result_len
,
778 __base
->get_allocator());
779 __result
->_M_c_string
= 0; // Not eos terminated.
781 // We should sometimes create substring node instead.
782 __result
= __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__l
->_M_data
+ __start
,
789 case _Rope_constants::_S_substringfn
:
790 // Avoid introducing multiple layers of substring nodes.
792 _RopeSubstring
* __old
= (_RopeSubstring
*)__base
;
794 if (__start
>= __adj_endp1
)
796 __result_len
= __adj_endp1
- __start
;
797 if (__result_len
> __lazy_threshold
)
799 _RopeSubstring
* __result
=
800 _S_new_RopeSubstring(__old
->_M_base
,
801 __start
+ __old
->_M_start
,
802 __adj_endp1
- __start
,
803 __base
->get_allocator());
806 } // *** else fall through: ***
808 case _Rope_constants::_S_function
:
810 _RopeFunction
* __f
= (_RopeFunction
*)__base
;
813 if (__start
>= __adj_endp1
)
815 __result_len
= __adj_endp1
- __start
;
817 if (__result_len
> __lazy_threshold
)
819 __section
= (_CharT
*)
820 _Data_allocate(_S_rounded_up_size(__result_len
));
822 { (*(__f
->_M_fn
))(__start
, __result_len
, __section
); }
825 _RopeRep::__STL_FREE_STRING(__section
, __result_len
,
826 __base
->get_allocator());
827 __throw_exception_again
;
829 _S_cond_store_eos(__section
[__result_len
]);
830 return _S_new_RopeLeaf(__section
, __result_len
,
831 __base
->get_allocator());
836 // Create substring node.
837 return _S_new_RopeSubstring(__base
, __start
, __adj_endp1
- __start
,
838 __base
->get_allocator());
842 template<class _CharT
>
843 class _Rope_flatten_char_consumer
844 : public _Rope_char_consumer
<_CharT
>
850 _Rope_flatten_char_consumer(_CharT
* __buffer
)
851 { _M_buf_ptr
= __buffer
; };
853 ~_Rope_flatten_char_consumer() {}
856 operator()(const _CharT
* __leaf
, size_t __n
)
858 uninitialized_copy_n(__leaf
, __n
, _M_buf_ptr
);
864 template<class _CharT
>
865 class _Rope_find_char_char_consumer
866 : public _Rope_char_consumer
<_CharT
>
871 size_t _M_count
; // Number of nonmatching characters
873 _Rope_find_char_char_consumer(_CharT __p
)
874 : _M_pattern(__p
), _M_count(0) {}
876 ~_Rope_find_char_char_consumer() {}
879 operator()(const _CharT
* __leaf
, size_t __n
)
882 for (__i
= 0; __i
< __n
; __i
++)
884 if (__leaf
[__i
] == _M_pattern
)
890 _M_count
+= __n
; return true;
894 template<class _CharT
, class _Traits
>
895 // Here _CharT is both the stream and rope character type.
896 class _Rope_insert_char_consumer
897 : public _Rope_char_consumer
<_CharT
>
900 typedef basic_ostream
<_CharT
,_Traits
> _Insert_ostream
;
901 _Insert_ostream
& _M_o
;
903 _Rope_insert_char_consumer(_Insert_ostream
& __writer
)
905 ~_Rope_insert_char_consumer() { };
906 // Caller is presumed to own the ostream
907 bool operator() (const _CharT
* __leaf
, size_t __n
);
908 // Returns true to continue traversal.
911 template<class _CharT
, class _Traits
>
913 _Rope_insert_char_consumer
<_CharT
, _Traits
>::
914 operator()(const _CharT
* __leaf
, size_t __n
)
917 // We assume that formatting is set up correctly for each element.
918 for (__i
= 0; __i
< __n
; __i
++)
919 _M_o
.put(__leaf
[__i
]);
923 template <class _CharT
, class _Alloc
>
925 rope
<_CharT
, _Alloc
>::
926 _S_apply_to_pieces(_Rope_char_consumer
<_CharT
>& __c
,
927 const _RopeRep
* __r
, size_t __begin
, size_t __end
)
933 case _Rope_constants::_S_concat
:
935 _RopeConcatenation
* __conc
= (_RopeConcatenation
*)__r
;
936 _RopeRep
* __left
= __conc
->_M_left
;
937 size_t __left_len
= __left
->_M_size
;
938 if (__begin
< __left_len
)
940 size_t __left_end
= std::min(__left_len
, __end
);
941 if (!_S_apply_to_pieces(__c
, __left
, __begin
, __left_end
))
944 if (__end
> __left_len
)
946 _RopeRep
* __right
= __conc
->_M_right
;
947 size_t __right_start
= std::max(__left_len
, __begin
);
948 if (!_S_apply_to_pieces(__c
, __right
,
949 __right_start
- __left_len
,
955 case _Rope_constants::_S_leaf
:
957 _RopeLeaf
* __l
= (_RopeLeaf
*)__r
;
958 return __c(__l
->_M_data
+ __begin
, __end
- __begin
);
960 case _Rope_constants::_S_function
:
961 case _Rope_constants::_S_substringfn
:
963 _RopeFunction
* __f
= (_RopeFunction
*)__r
;
964 size_t __len
= __end
- __begin
;
967 (_CharT
*)_Alloc().allocate(__len
* sizeof(_CharT
));
970 (*(__f
->_M_fn
))(__begin
, __len
, __buffer
);
971 __result
= __c(__buffer
, __len
);
972 _Alloc().deallocate(__buffer
, __len
* sizeof(_CharT
));
976 _Alloc().deallocate(__buffer
, __len
* sizeof(_CharT
));
977 __throw_exception_again
;
986 template<class _CharT
, class _Traits
>
988 _Rope_fill(basic_ostream
<_CharT
, _Traits
>& __o
, size_t __n
)
990 char __f
= __o
.fill();
993 for (__i
= 0; __i
< __n
; __i
++)
998 template <class _CharT
>
1000 _Rope_is_simple(_CharT
*)
1004 _Rope_is_simple(char*)
1008 _Rope_is_simple(wchar_t*)
1011 template<class _CharT
, class _Traits
, class _Alloc
>
1012 basic_ostream
<_CharT
, _Traits
>&
1013 operator<<(basic_ostream
<_CharT
, _Traits
>& __o
,
1014 const rope
<_CharT
, _Alloc
>& __r
)
1016 size_t __w
= __o
.width();
1017 bool __left
= bool(__o
.flags() & std::ios::left
);
1019 size_t __rope_len
= __r
.size();
1020 _Rope_insert_char_consumer
<_CharT
, _Traits
> __c(__o
);
1021 bool __is_simple
= _Rope_is_simple((_CharT
*)0);
1023 if (__rope_len
< __w
)
1024 __pad_len
= __w
- __rope_len
;
1029 __o
.width(__w
/ __rope_len
);
1032 if (__is_simple
&& !__left
&& __pad_len
> 0)
1033 _Rope_fill(__o
, __pad_len
);
1034 __r
.apply_to_pieces(0, __r
.size(), __c
);
1035 if (__is_simple
&& __left
&& __pad_len
> 0)
1036 _Rope_fill(__o
, __pad_len
);
1044 __throw_exception_again
;
1049 template <class _CharT
, class _Alloc
>
1051 rope
<_CharT
, _Alloc
>::
1052 _S_flatten(_RopeRep
* __r
, size_t __start
, size_t __len
,
1055 _Rope_flatten_char_consumer
<_CharT
> __c(__buffer
);
1056 _S_apply_to_pieces(__c
, __r
, __start
, __start
+ __len
);
1057 return(__buffer
+ __len
);
1060 template <class _CharT
, class _Alloc
>
1062 rope
<_CharT
, _Alloc
>::
1063 find(_CharT __pattern
, size_t __start
) const
1065 _Rope_find_char_char_consumer
<_CharT
> __c(__pattern
);
1066 _S_apply_to_pieces(__c
, this->_M_tree_ptr
, __start
, size());
1067 size_type __result_pos
= __start
+ __c
._M_count
;
1068 #ifndef __STL_OLD_ROPE_SEMANTICS
1069 if (__result_pos
== size())
1070 __result_pos
= npos
;
1072 return __result_pos
;
1075 template <class _CharT
, class _Alloc
>
1077 rope
<_CharT
, _Alloc
>::
1078 _S_flatten(_RopeRep
* __r
, _CharT
* __buffer
)
1084 case _Rope_constants::_S_concat
:
1086 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__r
;
1087 _RopeRep
* __left
= __c
->_M_left
;
1088 _RopeRep
* __right
= __c
->_M_right
;
1089 _CharT
* __rest
= _S_flatten(__left
, __buffer
);
1090 return _S_flatten(__right
, __rest
);
1092 case _Rope_constants::_S_leaf
:
1094 _RopeLeaf
* __l
= (_RopeLeaf
*)__r
;
1095 return copy_n(__l
->_M_data
, __l
->_M_size
, __buffer
).second
;
1097 case _Rope_constants::_S_function
:
1098 case _Rope_constants::_S_substringfn
:
1099 // We don't yet do anything with substring nodes.
1100 // This needs to be fixed before ropefiles will work well.
1102 _RopeFunction
* __f
= (_RopeFunction
*)__r
;
1103 (*(__f
->_M_fn
))(0, __f
->_M_size
, __buffer
);
1104 return __buffer
+ __f
->_M_size
;
1111 // This needs work for _CharT != char
1112 template <class _CharT
, class _Alloc
>
1114 rope
<_CharT
, _Alloc
>::
1115 _S_dump(_RopeRep
* __r
, int __indent
)
1117 for (int __i
= 0; __i
< __indent
; __i
++)
1124 if (_Rope_constants::_S_concat
== __r
->_M_tag
)
1126 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__r
;
1127 _RopeRep
* __left
= __c
->_M_left
;
1128 _RopeRep
* __right
= __c
->_M_right
;
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");
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");
1140 _S_dump(__left
, __indent
+ 2);
1141 _S_dump(__right
, __indent
+ 2);
1148 switch (__r
->_M_tag
)
1150 case _Rope_constants::_S_leaf
:
1153 case _Rope_constants::_S_function
:
1154 __kind
= "Function";
1156 case _Rope_constants::_S_substringfn
:
1157 __kind
= "Function representing substring";
1160 __kind
= "(corrupted kind field!)";
1163 printf("%s %p (depth = %d, len = %ld) ",
1164 __kind
, __r
, __r
->_M_depth
, __r
->_M_size
);
1166 printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
1167 __kind
, __r
, __r
->_M_ref_count
, __r
->_M_depth
, __r
->_M_size
);
1169 if (_S_is_one_byte_char_type((_CharT
*)0))
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
;
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");
1186 template <class _CharT
, class _Alloc
>
1188 rope
<_CharT
, _Alloc
>::
1189 _S_min_len
[int(_Rope_constants::_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.
1203 template <class _CharT
, class _Alloc
>
1204 typename rope
<_CharT
, _Alloc
>::_RopeRep
*
1205 rope
<_CharT
, _Alloc
>::
1206 _S_balance(_RopeRep
* __r
)
1208 _RopeRep
* __forest
[int(_Rope_constants::_S_max_rope_depth
) + 1];
1209 _RopeRep
* __result
= 0;
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.
1217 for (__i
= 0; __i
<= int(_Rope_constants::_S_max_rope_depth
); ++__i
)
1221 _S_add_to_forest(__r
, __forest
);
1222 for (__i
= 0; __i
<= int(_Rope_constants::_S_max_rope_depth
); ++__i
)
1223 if (0 != __forest
[__i
])
1226 _Self_destruct_ptr
__old(__result
);
1228 __result
= _S_concat(__forest
[__i
], __result
);
1229 __forest
[__i
]->_M_unref_nonnil();
1230 #if !defined(__GC) && defined(__EXCEPTIONS)
1237 for(__i
= 0; __i
<= int(_Rope_constants::_S_max_rope_depth
); __i
++)
1238 _S_unref(__forest
[__i
]);
1239 __throw_exception_again
;
1242 if (__result
->_M_depth
> int(_Rope_constants::_S_max_rope_depth
))
1243 __throw_length_error(__N("rope::_S_balance"));
1247 template <class _CharT
, class _Alloc
>
1249 rope
<_CharT
, _Alloc
>::
1250 _S_add_to_forest(_RopeRep
* __r
, _RopeRep
** __forest
)
1252 if (__r
->_M_is_balanced
)
1254 _S_add_leaf_to_forest(__r
, __forest
);
1259 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__r
;
1261 _S_add_to_forest(__c
->_M_left
, __forest
);
1262 _S_add_to_forest(__c
->_M_right
, __forest
);
1267 template <class _CharT
, class _Alloc
>
1269 rope
<_CharT
, _Alloc
>::
1270 _S_add_leaf_to_forest(_RopeRep
* __r
, _RopeRep
** __forest
)
1272 _RopeRep
* __insertee
; // included in refcount
1273 _RopeRep
* __too_tiny
= 0; // included in refcount
1274 int __i
; // forest[0..__i-1] is empty
1275 size_t __s
= __r
->_M_size
;
1277 for (__i
= 0; __s
>= _S_min_len
[__i
+1]/* not this bucket */; ++__i
)
1279 if (0 != __forest
[__i
])
1282 _Self_destruct_ptr
__old(__too_tiny
);
1284 __too_tiny
= _S_concat_and_set_balanced(__forest
[__i
],
1286 __forest
[__i
]->_M_unref_nonnil();
1292 _Self_destruct_ptr
__old(__too_tiny
);
1294 __insertee
= _S_concat_and_set_balanced(__too_tiny
, __r
);
1296 // Too_tiny dead, and no longer included in refcount.
1297 // Insertee is live and included.
1300 if (0 != __forest
[__i
])
1303 _Self_destruct_ptr
__old(__insertee
);
1305 __insertee
= _S_concat_and_set_balanced(__forest
[__i
],
1307 __forest
[__i
]->_M_unref_nonnil();
1310 if (__i
== int(_Rope_constants::_S_max_rope_depth
)
1311 || __insertee
->_M_size
< _S_min_len
[__i
+1])
1313 __forest
[__i
] = __insertee
;
1314 // refcount is OK since __insertee is now dead.
1320 template <class _CharT
, class _Alloc
>
1322 rope
<_CharT
, _Alloc
>::
1323 _S_fetch(_RopeRep
* __r
, size_type __i
)
1325 __GC_CONST _CharT
* __cstr
= __r
->_M_c_string
;
1333 case _Rope_constants::_S_concat
:
1335 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__r
;
1336 _RopeRep
* __left
= __c
->_M_left
;
1337 size_t __left_len
= __left
->_M_size
;
1339 if (__i
>= __left_len
)
1342 __r
= __c
->_M_right
;
1348 case _Rope_constants::_S_leaf
:
1350 _RopeLeaf
* __l
= (_RopeLeaf
*)__r
;
1351 return __l
->_M_data
[__i
];
1353 case _Rope_constants::_S_function
:
1354 case _Rope_constants::_S_substringfn
:
1356 _RopeFunction
* __f
= (_RopeFunction
*)__r
;
1359 (*(__f
->_M_fn
))(__i
, 1, &__result
);
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
>
1371 rope
<_CharT
, _Alloc
>::
1372 _S_fetch_ptr(_RopeRep
* __r
, size_type __i
)
1374 _RopeRep
* __clrstack
[_Rope_constants::_S_max_rope_depth
];
1379 if (__r
->_M_ref_count
> 1)
1383 case _Rope_constants::_S_concat
:
1385 _RopeConcatenation
* __c
= (_RopeConcatenation
*)__r
;
1386 _RopeRep
* __left
= __c
->_M_left
;
1387 size_t __left_len
= __left
->_M_size
;
1389 if (__c
->_M_c_string
!= 0)
1390 __clrstack
[__csptr
++] = __c
;
1391 if (__i
>= __left_len
)
1394 __r
= __c
->_M_right
;
1400 case _Rope_constants::_S_leaf
:
1402 _RopeLeaf
* __l
= (_RopeLeaf
*)__r
;
1403 if (__l
->_M_c_string
!= __l
->_M_data
&& __l
->_M_c_string
!= 0)
1404 __clrstack
[__csptr
++] = __l
;
1408 _RopeRep
* __d
= __clrstack
[__csptr
];
1409 __d
->_M_free_c_string();
1410 __d
->_M_c_string
= 0;
1412 return __l
->_M_data
+ __i
;
1414 case _Rope_constants::_S_function
:
1415 case _Rope_constants::_S_substringfn
:
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
1426 template <class _CharT
, class _Alloc
>
1428 rope
<_CharT
, _Alloc
>::
1429 _S_compare (const _RopeRep
* __left
, const _RopeRep
* __right
)
1438 __left_len
= __left
->_M_size
;
1439 __right_len
= __right
->_M_size
;
1440 if (_Rope_constants::_S_leaf
== __left
->_M_tag
)
1442 _RopeLeaf
* __l
= (_RopeLeaf
*) __left
;
1443 if (_Rope_constants::_S_leaf
== __right
->_M_tag
)
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
1453 const_iterator
__rstart(__right
, 0);
1454 const_iterator
__rend(__right
, __right_len
);
1455 return lexicographical_compare_3way(__l
->_M_data
, __l
->_M_data
1462 const_iterator
__lstart(__left
, 0);
1463 const_iterator
__lend(__left
, __left_len
);
1464 if (_Rope_constants::_S_leaf
== __right
->_M_tag
)
1466 _RopeLeaf
* __r
= (_RopeLeaf
*) __right
;
1467 return lexicographical_compare_3way(__lstart
, __lend
,
1468 __r
->_M_data
, __r
->_M_data
1473 const_iterator
__rstart(__right
, 0);
1474 const_iterator
__rend(__right
, __right_len
);
1475 return lexicographical_compare_3way(__lstart
, __lend
,
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
)
1487 _RopeRep
* __old
= _M_root
->_M_tree_ptr
;
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
);
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,
1501 _Self_destruct_ptr
__result_left(_My_rope::
1502 _S_destr_concat_char_iter(__left
,
1505 _RopeRep
* __result
= _My_rope::_S_concat(__result_left
, __right
);
1507 _RopeRep::_S_unref(__old
);
1509 _M_root
->_M_tree_ptr
= __result
;
1513 template <class _CharT
, class _Alloc
>
1514 inline _Rope_char_ref_proxy
<_CharT
, _Alloc
>::
1515 operator _CharT() const
1517 if (_M_current_valid
)
1520 return _My_rope::_S_fetch(_M_root
->_M_tree_ptr
, _M_pos
);
1523 template <class _CharT
, class _Alloc
>
1524 _Rope_char_ptr_proxy
<_CharT
, _Alloc
>
1525 _Rope_char_ref_proxy
<_CharT
, _Alloc
>::
1527 { return _Rope_char_ptr_proxy
<_CharT
, _Alloc
>(*this); }
1529 template <class _CharT
, class _Alloc
>
1530 rope
<_CharT
, _Alloc
>::
1531 rope(size_t __n
, _CharT __c
, const allocator_type
& __a
)
1534 rope
<_CharT
,_Alloc
> __result
;
1535 const size_t __exponentiate_threshold
= 32;
1538 _CharT
* __rest_buffer
;
1539 _RopeRep
* __remainder
;
1540 rope
<_CharT
, _Alloc
> __remainder_rope
;
1545 __exponent
= __n
/ __exponentiate_threshold
;
1546 __rest
= __n
% __exponentiate_threshold
;
1551 __rest_buffer
= this->_Data_allocate(_S_rounded_up_size(__rest
));
1552 __uninitialized_fill_n_a(__rest_buffer
, __rest
, __c
,
1554 _S_cond_store_eos(__rest_buffer
[__rest
]);
1556 { __remainder
= _S_new_RopeLeaf(__rest_buffer
, __rest
, __a
); }
1559 _RopeRep::__STL_FREE_STRING(__rest_buffer
, __rest
, __a
);
1560 __throw_exception_again
;
1563 __remainder_rope
._M_tree_ptr
= __remainder
;
1564 if (__exponent
!= 0)
1566 _CharT
* __base_buffer
=
1567 this->_Data_allocate(_S_rounded_up_size(__exponentiate_threshold
));
1568 _RopeLeaf
* __base_leaf
;
1570 __uninitialized_fill_n_a(__base_buffer
, __exponentiate_threshold
, __c
,
1572 _S_cond_store_eos(__base_buffer
[__exponentiate_threshold
]);
1575 __base_leaf
= _S_new_RopeLeaf(__base_buffer
,
1576 __exponentiate_threshold
, __a
);
1580 _RopeRep::__STL_FREE_STRING(__base_buffer
,
1581 __exponentiate_threshold
, __a
);
1582 __throw_exception_again
;
1584 __base_rope
._M_tree_ptr
= __base_leaf
;
1585 if (1 == __exponent
)
1586 __result
= __base_rope
;
1588 __result
= power(__base_rope
, __exponent
,
1589 _Rope_Concat_fn
<_CharT
, _Alloc
>());
1591 if (0 != __remainder
)
1592 __result
+= __remainder_rope
;
1595 __result
= __remainder_rope
;
1597 this->_M_tree_ptr
= __result
._M_tree_ptr
;
1598 this->_M_tree_ptr
->_M_ref_nonnil();
1601 template<class _CharT
, class _Alloc
>
1603 rope
<_CharT
, _Alloc
>::_S_empty_c_str
[1];
1605 template<class _CharT
, class _Alloc
>
1607 rope
<_CharT
, _Alloc
>::
1610 if (0 == this->_M_tree_ptr
)
1612 _S_empty_c_str
[0] = _S_eos((_CharT
*)0); // Possibly redundant,
1613 // but probably fast.
1614 return _S_empty_c_str
;
1616 __gthread_mutex_lock (&this->_M_tree_ptr
->_M_c_string_lock
);
1617 __GC_CONST _CharT
* __result
= this->_M_tree_ptr
->_M_c_string
;
1620 size_t __s
= size();
1621 __result
= this->_Data_allocate(__s
+ 1);
1622 _S_flatten(this->_M_tree_ptr
, __result
);
1623 __result
[__s
] = _S_eos((_CharT
*)0);
1624 this->_M_tree_ptr
->_M_c_string
= __result
;
1626 __gthread_mutex_unlock (&this->_M_tree_ptr
->_M_c_string_lock
);
1630 template<class _CharT
, class _Alloc
>
1631 const _CharT
* rope
<_CharT
, _Alloc
>::
1632 replace_with_c_str()
1634 if (0 == this->_M_tree_ptr
)
1636 _S_empty_c_str
[0] = _S_eos((_CharT
*)0);
1637 return _S_empty_c_str
;
1639 __GC_CONST _CharT
* __old_c_string
= this->_M_tree_ptr
->_M_c_string
;
1640 if (_Rope_constants::_S_leaf
== this->_M_tree_ptr
->_M_tag
1641 && 0 != __old_c_string
)
1642 return(__old_c_string
);
1643 size_t __s
= size();
1644 _CharT
* __result
= this->_Data_allocate(_S_rounded_up_size(__s
));
1645 _S_flatten(this->_M_tree_ptr
, __result
);
1646 __result
[__s
] = _S_eos((_CharT
*)0);
1647 this->_M_tree_ptr
->_M_unref_nonnil();
1648 this->_M_tree_ptr
= _S_new_RopeLeaf(__result
, __s
,
1649 this->get_allocator());
1653 // Algorithm specializations. More should be added.
1655 template<class _Rope_iterator
> // was templated on CharT and Alloc
1656 void // VC++ workaround
1657 _Rope_rotate(_Rope_iterator __first
,
1658 _Rope_iterator __middle
,
1659 _Rope_iterator __last
)
1661 typedef typename
_Rope_iterator::value_type _CharT
;
1662 typedef typename
_Rope_iterator::_allocator_type _Alloc
;
1664 rope
<_CharT
, _Alloc
>& __r(__first
.container());
1665 rope
<_CharT
, _Alloc
> __prefix
= __r
.substr(0, __first
.index());
1666 rope
<_CharT
, _Alloc
> __suffix
=
1667 __r
.substr(__last
.index(), __r
.size() - __last
.index());
1668 rope
<_CharT
, _Alloc
> __part1
=
1669 __r
.substr(__middle
.index(), __last
.index() - __middle
.index());
1670 rope
<_CharT
, _Alloc
> __part2
=
1671 __r
.substr(__first
.index(), __middle
.index() - __first
.index());
1678 #if !defined(__GNUC__)
1679 // Appears to confuse g++
1681 rotate(_Rope_iterator
<char, __STL_DEFAULT_ALLOCATOR(char)> __first
,
1682 _Rope_iterator
<char, __STL_DEFAULT_ALLOCATOR(char)> __middle
,
1683 _Rope_iterator
<char, __STL_DEFAULT_ALLOCATOR(char)> __last
)
1684 { _Rope_rotate(__first
, __middle
, __last
); }
1688 // Probably not useful for several reasons:
1689 // - for SGIs 7.1 compiler and probably some others,
1690 // this forces lots of rope<wchar_t, ...> instantiations, creating a
1691 // code bloat and compile time problem. (Fixed in 7.2.)
1692 // - wchar_t is 4 bytes wide on most UNIX platforms, making it
1693 // unattractive for unicode strings. Unsigned short may be a better
1696 rotate(_Rope_iterator
<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __first
,
1697 _Rope_iterator
<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __middle
,
1698 _Rope_iterator
<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __last
)
1699 { _Rope_rotate(__first
, __middle
, __last
); }
1702 } // namespace __gnu_cxx