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