]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/ropeimpl.h
* gensupport.c (target_flags): Add.
[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
43/* NOTE: This is an internal header file, included by other STL headers.
44 * You should not attempt to use it directly.
45 */
46
d53d7f6e
PE
47#include <bits/std_cstdio.h>
48#include <bits/std_iostream.h>
725dc051
BK
49
50#ifdef __STL_USE_EXCEPTIONS
51# include <bits/std_stdexcept.h>
52#endif
53
d53d7f6e
PE
54namespace std
55{
725dc051
BK
56
57// Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
58// if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct.
59// Results in a valid buf_ptr if the iterator can be legitimately
60// dereferenced.
61template <class _CharT, class _Alloc>
62void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf(
63 _Rope_iterator_base<_CharT,_Alloc>& __x)
64{
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 case _RopeRep::_S_leaf:
71 __x._M_buf_start =
72 ((_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 _RopeRep::_S_function:
77 case _RopeRep::_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 =
83 ((_Rope_RopeFunction<_CharT,_Alloc>*)__leaf)->_M_fn;
84
85 if (__buf_start_pos + __len <= __pos) {
86 __buf_start_pos = __pos - __len/4;
87 if (__buf_start_pos + __len > __leaf_end) {
88 __buf_start_pos = __leaf_end - __len;
89 }
90 }
91 if (__buf_start_pos + __len > __leaf_end) {
92 __len = __leaf_end - __buf_start_pos;
93 }
94 (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
95 __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
96 __x._M_buf_start = __x._M_tmp_buf;
97 __x._M_buf_end = __x._M_tmp_buf + __len;
98 }
99 break;
100 default:
101 __stl_assert(0);
102 }
103}
104
105// Set path and buffer inside a rope iterator. We assume that
106// pos and root are already set.
107template <class _CharT, class _Alloc>
108void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
109(_Rope_iterator_base<_CharT,_Alloc>& __x)
110{
111 const _RopeRep* __path[_RopeRep::_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 __stl_assert(__pos <= __x._M_root->_M_size);
119 if (__pos >= __x._M_root->_M_size) {
120 __x._M_buf_ptr = 0;
121 return;
122 }
123 __curr_rope = __x._M_root;
124 if (0 != __curr_rope->_M_c_string) {
125 /* Treat the root as a leaf. */
126 __x._M_buf_start = __curr_rope->_M_c_string;
127 __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
128 __x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
129 __x._M_path_end[0] = __curr_rope;
130 __x._M_leaf_index = 0;
131 __x._M_leaf_pos = 0;
132 return;
133 }
134 for(;;) {
135 ++__curr_depth;
136 __stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth);
137 __path[__curr_depth] = __curr_rope;
138 switch(__curr_rope->_M_tag) {
139 case _RopeRep::_S_leaf:
140 case _RopeRep::_S_function:
141 case _RopeRep::_S_substringfn:
142 __x._M_leaf_pos = __curr_start_pos;
143 goto done;
144 case _RopeRep::_S_concat:
145 {
146 _Rope_RopeConcatenation<_CharT,_Alloc>* __c =
147 (_Rope_RopeConcatenation<_CharT,_Alloc>*)__curr_rope;
148 _RopeRep* __left = __c->_M_left;
149 size_t __left_len = __left->_M_size;
150
151 __dirns <<= 1;
152 if (__pos >= __curr_start_pos + __left_len) {
153 __dirns |= 1;
154 __curr_rope = __c->_M_right;
155 __curr_start_pos += __left_len;
156 } else {
157 __curr_rope = __left;
158 }
159 }
160 break;
161 }
162 }
163 done:
164 // Copy last section of path into _M_path_end.
165 {
166 int __i = -1;
167 int __j = __curr_depth + 1 - _S_path_cache_len;
168
169 if (__j < 0) __j = 0;
170 while (__j <= __curr_depth) {
171 __x._M_path_end[++__i] = __path[__j++];
172 }
173 __x._M_leaf_index = __i;
174 }
175 __x._M_path_directions = __dirns;
176 _S_setbuf(__x);
177}
178
179// Specialized version of the above. Assumes that
180// the path cache is valid for the previous position.
181template <class _CharT, class _Alloc>
182void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr
183(_Rope_iterator_base<_CharT,_Alloc>& __x)
184{
185 int __current_index = __x._M_leaf_index;
186 const _RopeRep* __current_node = __x._M_path_end[__current_index];
187 size_t __len = __current_node->_M_size;
188 size_t __node_start_pos = __x._M_leaf_pos;
189 unsigned char __dirns = __x._M_path_directions;
190 _Rope_RopeConcatenation<_CharT,_Alloc>* __c;
191
192 __stl_assert(__x._M_current_pos <= __x._M_root->_M_size);
193 if (__x._M_current_pos - __node_start_pos < __len) {
194 /* More stuff in this leaf, we just didn't cache it. */
195 _S_setbuf(__x);
196 return;
197 }
198 __stl_assert(__node_start_pos + __len == __x._M_current_pos);
199 // node_start_pos is starting position of last_node.
200 while (--__current_index >= 0) {
201 if (!(__dirns & 1) /* Path turned left */)
202 break;
203 __current_node = __x._M_path_end[__current_index];
204 __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
205 // Otherwise we were in the right child. Thus we should pop
206 // the concatenation node.
207 __node_start_pos -= __c->_M_left->_M_size;
208 __dirns >>= 1;
209 }
210 if (__current_index < 0) {
211 // We underflowed the cache. Punt.
212 _S_setcache(__x);
213 return;
214 }
215 __current_node = __x._M_path_end[__current_index];
216 __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
217 // current_node is a concatenation node. We are positioned on the first
218 // character in its right child.
219 // node_start_pos is starting position of current_node.
220 __node_start_pos += __c->_M_left->_M_size;
221 __current_node = __c->_M_right;
222 __x._M_path_end[++__current_index] = __current_node;
223 __dirns |= 1;
224 while (_RopeRep::_S_concat == __current_node->_M_tag) {
225 ++__current_index;
226 if (_S_path_cache_len == __current_index) {
227 int __i;
228 for (__i = 0; __i < _S_path_cache_len-1; __i++) {
229 __x._M_path_end[__i] = __x._M_path_end[__i+1];
230 }
231 --__current_index;
232 }
233 __current_node =
234 ((_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node)->_M_left;
235 __x._M_path_end[__current_index] = __current_node;
236 __dirns <<= 1;
237 // node_start_pos is unchanged.
238 }
239 __x._M_leaf_index = __current_index;
240 __x._M_leaf_pos = __node_start_pos;
241 __x._M_path_directions = __dirns;
242 _S_setbuf(__x);
243}
244
245template <class _CharT, class _Alloc>
246void _Rope_iterator_base<_CharT,_Alloc>::_M_incr(size_t __n) {
247 _M_current_pos += __n;
248 if (0 != _M_buf_ptr) {
249 size_t __chars_left = _M_buf_end - _M_buf_ptr;
250 if (__chars_left > __n) {
251 _M_buf_ptr += __n;
252 } else if (__chars_left == __n) {
253 _M_buf_ptr += __n;
254 _S_setcache_for_incr(*this);
255 } else {
256 _M_buf_ptr = 0;
257 }
258 }
259}
260
261template <class _CharT, class _Alloc>
262void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) {
263 if (0 != _M_buf_ptr) {
264 size_t __chars_left = _M_buf_ptr - _M_buf_start;
265 if (__chars_left >= __n) {
266 _M_buf_ptr -= __n;
267 } else {
268 _M_buf_ptr = 0;
269 }
270 }
271 _M_current_pos -= __n;
272}
273
274template <class _CharT, class _Alloc>
275void _Rope_iterator<_CharT,_Alloc>::_M_check() {
276 if (_M_root_rope->_M_tree_ptr != _M_root) {
277 // _Rope was modified. Get things fixed up.
278 _RopeRep::_S_unref(_M_root);
279 _M_root = _M_root_rope->_M_tree_ptr;
280 _RopeRep::_S_ref(_M_root);
281 _M_buf_ptr = 0;
282 }
283}
284
285template <class _CharT, class _Alloc>
286inline
287_Rope_const_iterator<_CharT, _Alloc>::_Rope_const_iterator(
288 const _Rope_iterator<_CharT,_Alloc>& __x)
289: _Rope_iterator_base<_CharT,_Alloc>(__x)
290{ }
291
292template <class _CharT, class _Alloc>
293inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator(
294 rope<_CharT,_Alloc>& __r, size_t __pos)
295: _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos),
296 _M_root_rope(&__r)
297{
298 _RopeRep::_S_ref(_M_root);
299}
300
301template <class _CharT, class _Alloc>
302inline size_t
303rope<_CharT,_Alloc>::_S_char_ptr_len(const _CharT* __s)
304{
305 const _CharT* __p = __s;
306
307 while (!_S_is0(*__p)) { ++__p; }
308 return (__p - __s);
309}
310
311
312#ifndef __GC
313
314template <class _CharT, class _Alloc>
315inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
316{
317 _CharT* __cstr = _M_c_string;
318 if (0 != __cstr) {
319 size_t __size = _M_size + 1;
98aff0b5 320 _Destroy(__cstr, __cstr + __size);
725dc051
BK
321 _Data_deallocate(__cstr, __size);
322 }
323}
324
325
326template <class _CharT, class _Alloc>
725dc051
BK
327 inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
328 size_t __n,
329 allocator_type __a)
725dc051
BK
330{
331 if (!_S_is_basic_char_type((_CharT*)0)) {
98aff0b5 332 _Destroy(__s, __s + __n);
725dc051
BK
333 }
334// This has to be a static member, so this gets a bit messy
725dc051
BK
335 __a.deallocate(
336 __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
725dc051
BK
337}
338
339
340// There are several reasons for not doing this with virtual destructors
341// and a class specific delete operator:
342// - A class specific delete operator can't easily get access to
343// allocator instances if we need them.
344// - Any virtual function would need a 4 or byte vtable pointer;
345// this only requires a one byte tag per object.
346template <class _CharT, class _Alloc>
347void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree()
348{
349 switch(_M_tag) {
350 case _S_leaf:
351 {
352 _Rope_RopeLeaf<_CharT,_Alloc>* __l
353 = (_Rope_RopeLeaf<_CharT,_Alloc>*)this;
354 __l->_Rope_RopeLeaf<_CharT,_Alloc>::~_Rope_RopeLeaf();
355 _L_deallocate(__l, 1);
356 break;
357 }
358 case _S_concat:
359 {
360 _Rope_RopeConcatenation<_CharT,_Alloc>* __c
361 = (_Rope_RopeConcatenation<_CharT,_Alloc>*)this;
362 __c->_Rope_RopeConcatenation<_CharT,_Alloc>::
363 ~_Rope_RopeConcatenation();
364 _C_deallocate(__c, 1);
365 break;
366 }
367 case _S_function:
368 {
369 _Rope_RopeFunction<_CharT,_Alloc>* __f
370 = (_Rope_RopeFunction<_CharT,_Alloc>*)this;
371 __f->_Rope_RopeFunction<_CharT,_Alloc>::~_Rope_RopeFunction();
372 _F_deallocate(__f, 1);
373 break;
374 }
375 case _S_substringfn:
376 {
377 _Rope_RopeSubstring<_CharT,_Alloc>* __ss =
378 (_Rope_RopeSubstring<_CharT,_Alloc>*)this;
379 __ss->_Rope_RopeSubstring<_CharT,_Alloc>::
380 ~_Rope_RopeSubstring();
381 _S_deallocate(__ss, 1);
382 break;
383 }
384 }
385}
386#else
387
388template <class _CharT, class _Alloc>
725dc051
BK
389 inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
390 (const _CharT*, size_t, allocator_type)
725dc051
BK
391{}
392
393#endif
394
395
396// Concatenate a C string onto a leaf rope by copying the rope data.
397// Used for short ropes.
398template <class _CharT, class _Alloc>
399rope<_CharT,_Alloc>::_RopeLeaf*
400rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
401 (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
402{
403 size_t __old_len = __r->_M_size;
404 _CharT* __new_data = (_CharT*)
405 _Data_allocate(_S_rounded_up_size(__old_len + __len));
406 _RopeLeaf* __result;
407
408 uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
409 uninitialized_copy_n(__iter, __len, __new_data + __old_len);
410 _S_cond_store_eos(__new_data[__old_len + __len]);
411 __STL_TRY {
412 __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
413 __r->get_allocator());
414 }
415 __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
416 __r->get_allocator()));
417 return __result;
418}
419
420#ifndef __GC
421// As above, but it's OK to clobber original if refcount is 1
422template <class _CharT, class _Alloc>
423rope<_CharT,_Alloc>::_RopeLeaf*
424rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
425 (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
426{
427 __stl_assert(__r->_M_ref_count >= 1);
428 if (__r->_M_ref_count > 1)
429 return _S_leaf_concat_char_iter(__r, __iter, __len);
430 size_t __old_len = __r->_M_size;
431 if (_S_allocated_capacity(__old_len) >= __old_len + __len) {
432 // The space has been partially initialized for the standard
433 // character types. But that doesn't matter for those types.
434 uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
435 if (_S_is_basic_char_type((_CharT*)0)) {
436 _S_cond_store_eos(__r->_M_data[__old_len + __len]);
437 __stl_assert(__r->_M_c_string == __r->_M_data);
438 } else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) {
439 __r->_M_free_c_string();
440 __r->_M_c_string = 0;
441 }
442 __r->_M_size = __old_len + __len;
443 __stl_assert(__r->_M_ref_count == 1);
444 __r->_M_ref_count = 2;
445 return __r;
446 } else {
447 _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
448 __stl_assert(__result->_M_ref_count == 1);
449 return __result;
450 }
451}
452#endif
453
454// Assumes left and right are not 0.
455// Does not increment (nor decrement on exception) child reference counts.
456// Result has ref count 1.
457template <class _CharT, class _Alloc>
458rope<_CharT,_Alloc>::_RopeRep*
459rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
460{
461 _RopeConcatenation* __result =
462 _S_new_RopeConcatenation(__left, __right, __left->get_allocator());
463 size_t __depth = __result->_M_depth;
464
725dc051 465 __stl_assert(__left->get_allocator() == __right->get_allocator());
725dc051
BK
466 if (__depth > 20 && (__result->_M_size < 1000 ||
467 __depth > _RopeRep::_S_max_rope_depth)) {
468 _RopeRep* __balanced;
469
470 __STL_TRY {
471 __balanced = _S_balance(__result);
472# ifndef __GC
473 if (__result != __balanced) {
474 __stl_assert(1 == __result->_M_ref_count
475 && 1 == __balanced->_M_ref_count);
476 }
477# endif
478 __result->_M_unref_nonnil();
479 }
480 __STL_UNWIND((_C_deallocate(__result,1)));
481 // In case of exception, we need to deallocate
482 // otherwise dangling result node. But caller
483 // still owns its children. Thus unref is
484 // inappropriate.
485 return __balanced;
486 } else {
487 return __result;
488 }
489}
490
491template <class _CharT, class _Alloc>
492rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
493 (_RopeRep* __r, const _CharT*__s, size_t __slen)
494{
495 _RopeRep* __result;
496 if (0 == __slen) {
497 _S_ref(__r);
498 return __r;
499 }
500 if (0 == __r)
501 return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
502 __r->get_allocator());
503 if (_RopeRep::_S_leaf == __r->_M_tag &&
504 __r->_M_size + __slen <= _S_copy_max) {
505 __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
506# ifndef __GC
507 __stl_assert(1 == __result->_M_ref_count);
508# endif
509 return __result;
510 }
511 if (_RopeRep::_S_concat == __r->_M_tag
512 && _RopeRep::_S_leaf == ((_RopeConcatenation*)__r)->_M_right->_M_tag) {
513 _RopeLeaf* __right =
514 (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
515 if (__right->_M_size + __slen <= _S_copy_max) {
516 _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
517 _RopeRep* __nright =
518 _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
519 __left->_M_ref_nonnil();
520 __STL_TRY {
521 __result = _S_tree_concat(__left, __nright);
522 }
523 __STL_UNWIND(_S_unref(__left); _S_unref(__nright));
524# ifndef __GC
525 __stl_assert(1 == __result->_M_ref_count);
526# endif
527 return __result;
528 }
529 }
530 _RopeRep* __nright =
531 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
532 __STL_TRY {
533 __r->_M_ref_nonnil();
534 __result = _S_tree_concat(__r, __nright);
535 }
536 __STL_UNWIND(_S_unref(__r); _S_unref(__nright));
537# ifndef __GC
538 __stl_assert(1 == __result->_M_ref_count);
539# endif
540 return __result;
541}
542
543#ifndef __GC
544template <class _CharT, class _Alloc>
545rope<_CharT,_Alloc>::_RopeRep*
546rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
547 _RopeRep* __r, const _CharT* __s, size_t __slen)
548{
549 _RopeRep* __result;
550 if (0 == __r)
551 return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
552 __r->get_allocator());
553 size_t __count = __r->_M_ref_count;
554 size_t __orig_size = __r->_M_size;
555 __stl_assert(__count >= 1);
556 if (__count > 1) return _S_concat_char_iter(__r, __s, __slen);
557 if (0 == __slen) {
558 __r->_M_ref_count = 2; // One more than before
559 return __r;
560 }
561 if (__orig_size + __slen <= _S_copy_max &&
562 _RopeRep::_S_leaf == __r->_M_tag) {
563 __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
564 return __result;
565 }
566 if (_RopeRep::_S_concat == __r->_M_tag) {
567 _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)__r)->_M_right);
568 if (_RopeRep::_S_leaf == __right->_M_tag
569 && __right->_M_size + __slen <= _S_copy_max) {
570 _RopeRep* __new_right =
571 _S_destr_leaf_concat_char_iter(__right, __s, __slen);
572 if (__right == __new_right) {
573 __stl_assert(__new_right->_M_ref_count == 2);
574 __new_right->_M_ref_count = 1;
575 } else {
576 __stl_assert(__new_right->_M_ref_count >= 1);
577 __right->_M_unref_nonnil();
578 }
579 __stl_assert(__r->_M_ref_count == 1);
580 __r->_M_ref_count = 2; // One more than before.
581 ((_RopeConcatenation*)__r)->_M_right = __new_right;
582 __r->_M_size = __orig_size + __slen;
583 if (0 != __r->_M_c_string) {
584 __r->_M_free_c_string();
585 __r->_M_c_string = 0;
586 }
587 return __r;
588 }
589 }
590 _RopeRep* __right =
591 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
592 __r->_M_ref_nonnil();
593 __STL_TRY {
594 __result = _S_tree_concat(__r, __right);
595 }
596 __STL_UNWIND(_S_unref(__r); _S_unref(__right))
597 __stl_assert(1 == __result->_M_ref_count);
598 return __result;
599}
600#endif /* !__GC */
601
602template <class _CharT, class _Alloc>
603rope<_CharT,_Alloc>::_RopeRep*
604rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
605{
606 if (0 == __left) {
607 _S_ref(__right);
608 return __right;
609 }
610 if (0 == __right) {
611 __left->_M_ref_nonnil();
612 return __left;
613 }
614 if (_RopeRep::_S_leaf == __right->_M_tag) {
615 if (_RopeRep::_S_leaf == __left->_M_tag) {
616 if (__right->_M_size + __left->_M_size <= _S_copy_max) {
617 return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
618 ((_RopeLeaf*)__right)->_M_data,
619 __right->_M_size);
620 }
621 } else if (_RopeRep::_S_concat == __left->_M_tag
622 && _RopeRep::_S_leaf ==
623 ((_RopeConcatenation*)__left)->_M_right->_M_tag) {
624 _RopeLeaf* __leftright =
625 (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right);
626 if (__leftright->_M_size + __right->_M_size <= _S_copy_max) {
627 _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
628 _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
629 ((_RopeLeaf*)__right)->_M_data,
630 __right->_M_size);
631 __leftleft->_M_ref_nonnil();
632 __STL_TRY {
633 return(_S_tree_concat(__leftleft, __rest));
634 }
635 __STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest))
636 }
637 }
638 }
639 __left->_M_ref_nonnil();
640 __right->_M_ref_nonnil();
641 __STL_TRY {
642 return(_S_tree_concat(__left, __right));
643 }
644 __STL_UNWIND(_S_unref(__left); _S_unref(__right));
645}
646
647template <class _CharT, class _Alloc>
648rope<_CharT,_Alloc>::_RopeRep*
649rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base,
650 size_t __start, size_t __endp1)
651{
652 if (0 == __base) return 0;
653 size_t __len = __base->_M_size;
654 size_t __adj_endp1;
655 const size_t __lazy_threshold = 128;
656
657 if (__endp1 >= __len) {
658 if (0 == __start) {
659 __base->_M_ref_nonnil();
660 return __base;
661 } else {
662 __adj_endp1 = __len;
663 }
664 } else {
665 __adj_endp1 = __endp1;
666 }
667 switch(__base->_M_tag) {
668 case _RopeRep::_S_concat:
669 {
670 _RopeConcatenation* __c = (_RopeConcatenation*)__base;
671 _RopeRep* __left = __c->_M_left;
672 _RopeRep* __right = __c->_M_right;
673 size_t __left_len = __left->_M_size;
674 _RopeRep* __result;
675
676 if (__adj_endp1 <= __left_len) {
677 return _S_substring(__left, __start, __endp1);
678 } else if (__start >= __left_len) {
679 return _S_substring(__right, __start - __left_len,
680 __adj_endp1 - __left_len);
681 }
682 _Self_destruct_ptr __left_result(
683 _S_substring(__left, __start, __left_len));
684 _Self_destruct_ptr __right_result(
685 _S_substring(__right, 0, __endp1 - __left_len));
686 __result = _S_concat(__left_result, __right_result);
687# ifndef __GC
688 __stl_assert(1 == __result->_M_ref_count);
689# endif
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));
741 __STL_TRY {
742 (*(__f->_M_fn))(__start, __result_len, __section);
743 }
744 __STL_UNWIND(_RopeRep::__STL_FREE_STRING(
745 __section, __result_len, __base->get_allocator()));
746 _S_cond_store_eos(__section[__result_len]);
747 return _S_new_RopeLeaf(__section, __result_len,
748 __base->get_allocator());
749 }
750 }
751 /*NOTREACHED*/
752 __stl_assert(false);
753 lazy:
754 {
755 // Create substring node.
756 return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
757 __base->get_allocator());
758 }
759}
760
761template<class _CharT>
762class _Rope_flatten_char_consumer : public _Rope_char_consumer<_CharT> {
763 private:
764 _CharT* _M_buf_ptr;
765 public:
766
767 _Rope_flatten_char_consumer(_CharT* __buffer) {
768 _M_buf_ptr = __buffer;
769 };
770 ~_Rope_flatten_char_consumer() {}
771 bool operator() (const _CharT* __leaf, size_t __n) {
772 uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
773 _M_buf_ptr += __n;
774 return true;
775 }
776};
777
778template<class _CharT>
779class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> {
780 private:
781 _CharT _M_pattern;
782 public:
783 size_t _M_count; // Number of nonmatching characters
784 _Rope_find_char_char_consumer(_CharT __p)
785 : _M_pattern(__p), _M_count(0) {}
786 ~_Rope_find_char_char_consumer() {}
787 bool operator() (const _CharT* __leaf, size_t __n) {
788 size_t __i;
789 for (__i = 0; __i < __n; __i++) {
790 if (__leaf[__i] == _M_pattern) {
791 _M_count += __i; return false;
792 }
793 }
794 _M_count += __n; return true;
795 }
796};
797
725dc051
BK
798 template<class _CharT, class _Traits>
799 // Here _CharT is both the stream and rope character type.
725dc051
BK
800class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
801 private:
725dc051 802 typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
725dc051
BK
803 _Insert_ostream& _M_o;
804 public:
805 _Rope_insert_char_consumer(_Insert_ostream& __writer)
806 : _M_o(__writer) {};
807 ~_Rope_insert_char_consumer() { };
808 // Caller is presumed to own the ostream
809 bool operator() (const _CharT* __leaf, size_t __n);
810 // Returns true to continue traversal.
811};
812
d53d7f6e
PE
813template<class _CharT, class _Traits>
814bool _Rope_insert_char_consumer<_CharT, _Traits>::operator()
815 (const _CharT* __leaf, size_t __n)
816{
817 size_t __i;
818 // We assume that formatting is set up correctly for each element.
819 for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
820 return true;
821}
725dc051
BK
822
823template <class _CharT, class _Alloc>
824bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
825 _Rope_char_consumer<_CharT>& __c,
826 const _RopeRep* __r,
827 size_t __begin, size_t __end)
828{
829 if (0 == __r) return true;
830 switch(__r->_M_tag) {
831 case _RopeRep::_S_concat:
832 {
833 _RopeConcatenation* __conc = (_RopeConcatenation*)__r;
834 _RopeRep* __left = __conc->_M_left;
835 size_t __left_len = __left->_M_size;
836 if (__begin < __left_len) {
837 size_t __left_end = min(__left_len, __end);
838 if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
839 return false;
840 }
841 if (__end > __left_len) {
842 _RopeRep* __right = __conc->_M_right;
843 size_t __right_start = max(__left_len, __begin);
844 if (!_S_apply_to_pieces(__c, __right,
845 __right_start - __left_len,
846 __end - __left_len)) {
847 return false;
848 }
849 }
850 }
851 return true;
852 case _RopeRep::_S_leaf:
853 {
854 _RopeLeaf* __l = (_RopeLeaf*)__r;
855 return __c(__l->_M_data + __begin, __end - __begin);
856 }
857 case _RopeRep::_S_function:
858 case _RopeRep::_S_substringfn:
859 {
860 _RopeFunction* __f = (_RopeFunction*)__r;
861 size_t __len = __end - __begin;
862 bool __result;
863 _CharT* __buffer =
864 (_CharT*)alloc::allocate(__len * sizeof(_CharT));
865 __STL_TRY {
866 (*(__f->_M_fn))(__begin, __len, __buffer);
867 __result = __c(__buffer, __len);
868 alloc::deallocate(__buffer, __len * sizeof(_CharT));
869 }
870 __STL_UNWIND((alloc::deallocate(__buffer,
871 __len * sizeof(_CharT))))
872 return __result;
873 }
874 default:
875 __stl_assert(false);
876 /*NOTREACHED*/
877 return false;
878 }
879}
880
725dc051
BK
881 template<class _CharT, class _Traits>
882 inline void _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
725dc051
BK
883{
884 char __f = __o.fill();
885 size_t __i;
886
887 for (__i = 0; __i < __n; __i++) __o.put(__f);
888}
889
890
891template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; }
892inline bool _Rope_is_simple(char*) { return true; }
893inline bool _Rope_is_simple(wchar_t*) { return true; }
894
d53d7f6e
PE
895template<class _CharT, class _Traits, class _Alloc>
896basic_ostream<_CharT, _Traits>& operator<< (basic_ostream<_CharT, _Traits>& __o,
897 const rope<_CharT, _Alloc>& __r)
725dc051
BK
898{
899 size_t __w = __o.width();
900 bool __left = bool(__o.flags() & ios::left);
901 size_t __pad_len;
902 size_t __rope_len = __r.size();
725dc051 903 _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
725dc051
BK
904 bool __is_simple = _Rope_is_simple((_CharT*)0);
905
906 if (__rope_len < __w) {
907 __pad_len = __w - __rope_len;
908 } else {
909 __pad_len = 0;
910 }
911 if (!__is_simple) __o.width(__w/__rope_len);
912 __STL_TRY {
913 if (__is_simple && !__left && __pad_len > 0) {
914 _Rope_fill(__o, __pad_len);
915 }
916 __r.apply_to_pieces(0, __r.size(), __c);
917 if (__is_simple && __left && __pad_len > 0) {
918 _Rope_fill(__o, __pad_len);
919 }
920 if (!__is_simple)
921 __o.width(__w);
922 }
923 __STL_UNWIND(if (!__is_simple) __o.width(__w))
924 return __o;
925}
926
927template <class _CharT, class _Alloc>
928_CharT*
929rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r,
930 size_t __start, size_t __len,
931 _CharT* __buffer)
932{
933 _Rope_flatten_char_consumer<_CharT> __c(__buffer);
934 _S_apply_to_pieces(__c, __r, __start, __start + __len);
935 return(__buffer + __len);
936}
937
938template <class _CharT, class _Alloc>
939size_t
940rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const
941{
942 _Rope_find_char_char_consumer<_CharT> __c(__pattern);
943 _S_apply_to_pieces(__c, _M_tree_ptr, __start, size());
944 size_type __result_pos = __start + __c._M_count;
945# ifndef __STL_OLD_ROPE_SEMANTICS
946 if (__result_pos == size()) __result_pos = npos;
947# endif
948 return __result_pos;
949}
950
951template <class _CharT, class _Alloc>
952_CharT*
953rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
954{
955 if (0 == __r) return __buffer;
956 switch(__r->_M_tag) {
957 case _RopeRep::_S_concat:
958 {
959 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
960 _RopeRep* __left = __c->_M_left;
961 _RopeRep* __right = __c->_M_right;
962 _CharT* __rest = _S_flatten(__left, __buffer);
963 return _S_flatten(__right, __rest);
964 }
965 case _RopeRep::_S_leaf:
966 {
967 _RopeLeaf* __l = (_RopeLeaf*)__r;
968 return copy_n(__l->_M_data, __l->_M_size, __buffer).second;
969 }
970 case _RopeRep::_S_function:
971 case _RopeRep::_S_substringfn:
972 // We dont yet do anything with substring nodes.
973 // This needs to be fixed before ropefiles will work well.
974 {
975 _RopeFunction* __f = (_RopeFunction*)__r;
976 (*(__f->_M_fn))(0, __f->_M_size, __buffer);
977 return __buffer + __f->_M_size;
978 }
979 default:
980 __stl_assert(false);
981 /*NOTREACHED*/
982 return 0;
983 }
984}
985
986
987// This needs work for _CharT != char
988template <class _CharT, class _Alloc>
989void
990rope<_CharT,_Alloc>::_S_dump(_RopeRep* __r, int __indent)
991{
992 for (int __i = 0; __i < __indent; __i++) putchar(' ');
993 if (0 == __r) {
994 printf("NULL\n"); return;
995 }
996 if (_RopeRep::_S_concat == __r->_M_tag) {
997 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
998 _RopeRep* __left = __c->_M_left;
999 _RopeRep* __right = __c->_M_right;
1000
1001# ifdef __GC
1002 printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n",
1003 __r, __r->_M_depth, __r->_M_size, __r->_M_is_balanced? "" : "not");
1004# else
1005 printf("Concatenation %p (rc = %ld, depth = %d, "
1006 "len = %ld, %s balanced)\n",
1007 __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size,
1008 __r->_M_is_balanced? "" : "not");
1009# endif
1010 _S_dump(__left, __indent + 2);
1011 _S_dump(__right, __indent + 2);
1012 return;
1013 } else {
1014 char* __kind;
1015
1016 switch (__r->_M_tag) {
1017 case _RopeRep::_S_leaf:
1018 __kind = "Leaf";
1019 break;
1020 case _RopeRep::_S_function:
1021 __kind = "Function";
1022 break;
1023 case _RopeRep::_S_substringfn:
1024 __kind = "Function representing substring";
1025 break;
1026 default:
1027 __kind = "(corrupted kind field!)";
1028 }
1029# ifdef __GC
1030 printf("%s %p (depth = %d, len = %ld) ",
1031 __kind, __r, __r->_M_depth, __r->_M_size);
1032# else
1033 printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
1034 __kind, __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size);
1035# endif
1036 if (_S_is_one_byte_char_type((_CharT*)0)) {
1037 const int __max_len = 40;
1038 _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len));
1039 _CharT __buffer[__max_len + 1];
1040 bool __too_big = __r->_M_size > __prefix->_M_size;
1041
1042 _S_flatten(__prefix, __buffer);
1043 __buffer[__prefix->_M_size] = _S_eos((_CharT*)0);
1044 printf("%s%s\n",
1045 (char*)__buffer, __too_big? "...\n" : "\n");
1046 } else {
1047 printf("\n");
1048 }
1049 }
1050}
1051
1052template <class _CharT, class _Alloc>
1053const unsigned long
1054rope<_CharT,_Alloc>::_S_min_len[
1055 _Rope_RopeRep<_CharT,_Alloc>::_S_max_rope_depth + 1] = {
1056/* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21,
1057/* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377,
1058/* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181,
1059/* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368,
1060/* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811,
1061/* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309,
1062/* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352,
1063/* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155,
1064/* 39 */165580141, /* 40 */267914296, /* 41 */433494437,
1065/* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903,
1066/* 45 */2971215073u };
1067// These are Fibonacci numbers < 2**32.
1068
1069template <class _CharT, class _Alloc>
1070rope<_CharT,_Alloc>::_RopeRep*
1071rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
1072{
1073 _RopeRep* __forest[_RopeRep::_S_max_rope_depth + 1];
1074 _RopeRep* __result = 0;
1075 int __i;
1076 // Invariant:
1077 // The concatenation of forest in descending order is equal to __r.
1078 // __forest[__i]._M_size >= _S_min_len[__i]
1079 // __forest[__i]._M_depth = __i
1080 // References from forest are included in refcount.
1081
1082 for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i)
1083 __forest[__i] = 0;
1084 __STL_TRY {
1085 _S_add_to_forest(__r, __forest);
1086 for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i)
1087 if (0 != __forest[__i]) {
1088# ifndef __GC
1089 _Self_destruct_ptr __old(__result);
1090# endif
1091 __result = _S_concat(__forest[__i], __result);
1092 __forest[__i]->_M_unref_nonnil();
1093# if !defined(__GC) && defined(__STL_USE_EXCEPTIONS)
1094 __forest[__i] = 0;
1095# endif
1096 }
1097 }
1098 __STL_UNWIND(for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++)
1099 _S_unref(__forest[__i]))
1100 if (__result->_M_depth > _RopeRep::_S_max_rope_depth) {
1101# ifdef __STL_USE_EXCEPTIONS
1102 __STL_THROW(length_error("rope too long"));
1103# else
1104 abort();
1105# endif
1106 }
1107 return(__result);
1108}
1109
1110
1111template <class _CharT, class _Alloc>
1112void
1113rope<_CharT,_Alloc>::_S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
1114{
1115 if (__r->_M_is_balanced) {
1116 _S_add_leaf_to_forest(__r, __forest);
1117 return;
1118 }
1119 __stl_assert(__r->_M_tag == _RopeRep::_S_concat);
1120 {
1121 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
1122
1123 _S_add_to_forest(__c->_M_left, __forest);
1124 _S_add_to_forest(__c->_M_right, __forest);
1125 }
1126}
1127
1128
1129template <class _CharT, class _Alloc>
1130void
1131rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
1132{
1133 _RopeRep* __insertee; // included in refcount
1134 _RopeRep* __too_tiny = 0; // included in refcount
1135 int __i; // forest[0..__i-1] is empty
1136 size_t __s = __r->_M_size;
1137
1138 for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i) {
1139 if (0 != __forest[__i]) {
1140# ifndef __GC
1141 _Self_destruct_ptr __old(__too_tiny);
1142# endif
1143 __too_tiny = _S_concat_and_set_balanced(__forest[__i], __too_tiny);
1144 __forest[__i]->_M_unref_nonnil();
1145 __forest[__i] = 0;
1146 }
1147 }
1148 {
1149# ifndef __GC
1150 _Self_destruct_ptr __old(__too_tiny);
1151# endif
1152 __insertee = _S_concat_and_set_balanced(__too_tiny, __r);
1153 }
1154 // Too_tiny dead, and no longer included in refcount.
1155 // Insertee is live and included.
1156 __stl_assert(_S_is_almost_balanced(__insertee));
1157 __stl_assert(__insertee->_M_depth <= __r->_M_depth + 1);
1158 for (;; ++__i) {
1159 if (0 != __forest[__i]) {
1160# ifndef __GC
1161 _Self_destruct_ptr __old(__insertee);
1162# endif
1163 __insertee = _S_concat_and_set_balanced(__forest[__i], __insertee);
1164 __forest[__i]->_M_unref_nonnil();
1165 __forest[__i] = 0;
1166 __stl_assert(_S_is_almost_balanced(__insertee));
1167 }
1168 __stl_assert(_S_min_len[__i] <= __insertee->_M_size);
1169 __stl_assert(__forest[__i] == 0);
1170 if (__i == _RopeRep::_S_max_rope_depth ||
1171 __insertee->_M_size < _S_min_len[__i+1]) {
1172 __forest[__i] = __insertee;
1173 // refcount is OK since __insertee is now dead.
1174 return;
1175 }
1176 }
1177}
1178
1179template <class _CharT, class _Alloc>
1180_CharT
1181rope<_CharT,_Alloc>::_S_fetch(_RopeRep* __r, size_type __i)
1182{
1183 __GC_CONST _CharT* __cstr = __r->_M_c_string;
1184
1185 __stl_assert(__i < __r->_M_size);
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
1339# ifndef __GC
1340 __stl_assert(__left == __result_left || 1 == __result_left->_M_ref_count);
1341# endif
1342 _RopeRep* __result =
1343 _My_rope::_S_concat(__result_left, __right);
1344# ifndef __GC
1345 __stl_assert(1 <= __result->_M_ref_count);
1346 _RopeRep::_S_unref(__old);
1347# endif
1348 _M_root->_M_tree_ptr = __result;
1349 return *this;
1350}
1351
1352template <class _CharT, class _Alloc>
1353inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const
1354{
1355 if (_M_current_valid) {
1356 return _M_current;
1357 } else {
1358 return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos);
1359 }
1360}
1361template <class _CharT, class _Alloc>
1362_Rope_char_ptr_proxy<_CharT, _Alloc>
1363_Rope_char_ref_proxy<_CharT, _Alloc>::operator& () const {
1364 return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this);
1365}
1366
1367template <class _CharT, class _Alloc>
1368rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
1369 const allocator_type& __a)
1370: _Base(__a)
1371{
1372 rope<_CharT,_Alloc> __result;
1373 const size_t __exponentiate_threshold = 32;
1374 size_t __exponent;
1375 size_t __rest;
1376 _CharT* __rest_buffer;
1377 _RopeRep* __remainder;
1378 rope<_CharT,_Alloc> __remainder_rope;
1379
1380 if (0 == __n)
1381 return;
1382
1383 __exponent = __n / __exponentiate_threshold;
1384 __rest = __n % __exponentiate_threshold;
1385 if (0 == __rest) {
1386 __remainder = 0;
1387 } else {
1388 __rest_buffer = _Data_allocate(_S_rounded_up_size(__rest));
1389 uninitialized_fill_n(__rest_buffer, __rest, __c);
1390 _S_cond_store_eos(__rest_buffer[__rest]);
1391 __STL_TRY {
1392 __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
1393 }
1394 __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a))
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]);
1404 __STL_TRY {
1405 __base_leaf = _S_new_RopeLeaf(__base_buffer,
1406 __exponentiate_threshold, __a);
1407 }
1408 __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__base_buffer,
1409 __exponentiate_threshold, __a))
1410 __base_rope._M_tree_ptr = __base_leaf;
1411 if (1 == __exponent) {
1412 __result = __base_rope;
1413# ifndef __GC
1414 __stl_assert(2 == __result._M_tree_ptr->_M_ref_count);
1415 // One each for base_rope and __result
1416# endif
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
1493 __stl_assert(__first.container() == __middle.container()
1494 && __middle.container() == __last.container());
1495 rope<_CharT,_Alloc>& __r(__first.container());
1496 rope<_CharT,_Alloc> __prefix = __r.substr(0, __first.index());
1497 rope<_CharT,_Alloc> __suffix =
1498 __r.substr(__last.index(), __r.size() - __last.index());
1499 rope<_CharT,_Alloc> __part1 =
1500 __r.substr(__middle.index(), __last.index() - __middle.index());
1501 rope<_CharT,_Alloc> __part2 =
1502 __r.substr(__first.index(), __middle.index() - __first.index());
1503 __r = __prefix;
1504 __r += __part1;
1505 __r += __part2;
1506 __r += __suffix;
1507}
1508
1509#if !defined(__GNUC__)
1510// Appears to confuse g++
1511inline void rotate(_Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __first,
1512 _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __middle,
1513 _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __last) {
1514 _Rope_rotate(__first, __middle, __last);
1515}
1516#endif
1517
1518# if 0
1519// Probably not useful for several reasons:
1520// - for SGIs 7.1 compiler and probably some others,
1521// this forces lots of rope<wchar_t, ...> instantiations, creating a
1522// code bloat and compile time problem. (Fixed in 7.2.)
1523// - wchar_t is 4 bytes wide on most UNIX platforms, making it unattractive
1524// for unicode strings. Unsigned short may be a better character
1525// type.
1526inline void rotate(
1527 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __first,
1528 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __middle,
1529 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __last) {
1530 _Rope_rotate(__first, __middle, __last);
1531}
1532# endif
1533
d53d7f6e 1534} // namespace std
725dc051
BK
1535
1536// Local Variables:
1537// mode:C++
1538// End: