1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001-2022 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
39 * This file is a GNU extension to the Standard C++ Library (possibly
40 * containing extensions from the HP/SGI STL subset).
46 #pragma GCC system_header
50 #include <bits/stl_construct.h>
51 #include <bits/stl_uninitialized.h>
52 #include <bits/stl_function.h>
53 #include <bits/stl_numeric.h>
54 #include <bits/allocator.h>
55 #include <bits/gthr.h>
56 #include <ext/alloc_traits.h>
57 #include <tr1/functional>
60 # define __GC_CONST const
62 # define __GC_CONST // constant except for deallocation
65 #include <ext/memory> // For uninitialized_copy_n
67 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 enum { _S_max_rope_depth = 45 };
74 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
75 } // namespace __detail
77 // See libstdc++/36832.
78 template<typename _ForwardIterator, typename _Allocator>
80 _Destroy_const(_ForwardIterator __first,
81 _ForwardIterator __last, _Allocator __alloc)
83 for (; __first != __last; ++__first)
84 __alloc.destroy(&*__first);
87 template<typename _ForwardIterator, typename _Tp>
89 _Destroy_const(_ForwardIterator __first,
90 _ForwardIterator __last, std::allocator<_Tp>)
91 { std::_Destroy(__first, __last); }
93 // The _S_eos function is used for those functions that
94 // convert to/from C-like strings to detect the end of the string.
96 // The end-of-C-string character.
97 // This is what the draft standard says it should be.
98 template <class _CharT>
103 // Test for basic character types.
104 // For basic character types leaves having a trailing eos.
105 template <class _CharT>
107 _S_is_basic_char_type(_CharT*)
110 template <class _CharT>
112 _S_is_one_byte_char_type(_CharT*)
116 _S_is_basic_char_type(char*)
120 _S_is_one_byte_char_type(char*)
124 _S_is_basic_char_type(wchar_t*)
127 // Store an eos iff _CharT is a basic character type.
128 // Do not reference _S_eos if it isn't.
129 template <class _CharT>
131 _S_cond_store_eos(_CharT&) { }
134 _S_cond_store_eos(char& __c)
138 _S_cond_store_eos(wchar_t& __c)
141 // char_producers are logically functions that generate a section of
142 // a string. These can be converted to ropes. The resulting rope
143 // invokes the char_producer on demand. This allows, for example,
144 // files to be viewed as ropes without reading the entire file.
145 template <class _CharT>
149 virtual ~char_producer() { }
152 operator()(std::size_t __start_pos, std::size_t __len,
153 _CharT* __buffer) = 0;
154 // Buffer should really be an arbitrary output iterator.
155 // That way we could flatten directly into an ostream, etc.
156 // This is thoroughly impossible, since iterator types don't
157 // have runtime descriptions.
162 // Sequence must provide an append operation that appends an
163 // array to the sequence. Sequence buffers are useful only if
164 // appending an entire array is cheaper than appending element by element.
165 // This is true for many string representations.
166 // This should perhaps inherit from ostream<sequence::value_type>
167 // and be implemented correspondingly, so that they can be used
168 // for formatted. For the sake of portability, we don't do this yet.
170 // For now, sequence buffers behave as output iterators. But they also
171 // behave a little like basic_ostringstream<sequence::value_type> and a
172 // little like containers.
174 template<class _Sequence, std::size_t _Buf_sz = 100>
175 class sequence_buffer
176 : public std::iterator<std::output_iterator_tag, void, void, void, void>
179 typedef typename _Sequence::value_type value_type;
181 _Sequence* _M_prefix;
182 value_type _M_buffer[_Buf_sz];
183 std::size_t _M_buf_count;
189 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
197 : _M_prefix(0), _M_buf_count(0) { }
199 sequence_buffer(const sequence_buffer& __x)
201 _M_prefix = __x._M_prefix;
202 _M_buf_count = __x._M_buf_count;
203 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
206 // Non-const "copy" modifies the parameter - yuck
207 sequence_buffer(sequence_buffer& __x)
210 _M_prefix = __x._M_prefix;
214 sequence_buffer(_Sequence& __s)
215 : _M_prefix(&__s), _M_buf_count(0) { }
217 // Non-const "copy" modifies the parameter - yuck
219 operator=(sequence_buffer& __x)
222 _M_prefix = __x._M_prefix;
228 operator=(const sequence_buffer& __x)
230 _M_prefix = __x._M_prefix;
231 _M_buf_count = __x._M_buf_count;
232 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
236 #if __cplusplus >= 201103L
237 sequence_buffer(sequence_buffer&& __x) : sequence_buffer(__x) { }
238 sequence_buffer& operator=(sequence_buffer&& __x) { return *this = __x; }
242 push_back(value_type __x)
244 if (_M_buf_count < _Buf_sz)
246 _M_buffer[_M_buf_count] = __x;
258 append(value_type* __s, std::size_t __len)
260 if (__len + _M_buf_count <= _Buf_sz)
262 std::size_t __i = _M_buf_count;
263 for (std::size_t __j = 0; __j < __len; __i++, __j++)
264 _M_buffer[__i] = __s[__j];
265 _M_buf_count += __len;
267 else if (0 == _M_buf_count)
268 _M_prefix->append(__s, __s + __len);
277 write(value_type* __s, std::size_t __len)
291 operator=(const value_type& __rhs)
310 // The following should be treated as private, at least for now.
311 template<class _CharT>
312 class _Rope_char_consumer
315 // If we had member templates, these should not be virtual.
316 // For now we need to use run-time parametrization where
317 // compile-time would do. Hence this should all be private
319 // The symmetry with char_producer is accidental and temporary.
320 virtual ~_Rope_char_consumer() { }
323 operator()(const _CharT* __buffer, std::size_t __len) = 0;
326 // First a lot of forward declarations. The standard seems to require
327 // much stricter "declaration before use" than many of the implementations
329 template<class _CharT, class _Alloc = std::allocator<_CharT> >
332 template<class _CharT, class _Alloc>
333 struct _Rope_RopeConcatenation;
335 template<class _CharT, class _Alloc>
336 struct _Rope_RopeLeaf;
338 template<class _CharT, class _Alloc>
339 struct _Rope_RopeFunction;
341 template<class _CharT, class _Alloc>
342 struct _Rope_RopeSubstring;
344 template<class _CharT, class _Alloc>
345 class _Rope_iterator;
347 template<class _CharT, class _Alloc>
348 class _Rope_const_iterator;
350 template<class _CharT, class _Alloc>
351 class _Rope_char_ref_proxy;
353 template<class _CharT, class _Alloc>
354 class _Rope_char_ptr_proxy;
356 template<class _CharT, class _Alloc>
358 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
359 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
361 template<class _CharT, class _Alloc>
362 _Rope_const_iterator<_CharT, _Alloc>
363 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
366 template<class _CharT, class _Alloc>
367 _Rope_const_iterator<_CharT, _Alloc>
368 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
371 template<class _CharT, class _Alloc>
372 _Rope_const_iterator<_CharT, _Alloc>
373 operator+(std::ptrdiff_t __n,
374 const _Rope_const_iterator<_CharT, _Alloc>& __x);
376 template<class _CharT, class _Alloc>
378 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
379 const _Rope_const_iterator<_CharT, _Alloc>& __y);
381 template<class _CharT, class _Alloc>
383 operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
384 const _Rope_const_iterator<_CharT, _Alloc>& __y);
386 template<class _CharT, class _Alloc>
388 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
389 const _Rope_const_iterator<_CharT, _Alloc>& __y);
391 template<class _CharT, class _Alloc>
392 _Rope_iterator<_CharT, _Alloc>
393 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n);
395 template<class _CharT, class _Alloc>
396 _Rope_iterator<_CharT, _Alloc>
397 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n);
399 template<class _CharT, class _Alloc>
400 _Rope_iterator<_CharT, _Alloc>
401 operator+(std::ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
403 template<class _CharT, class _Alloc>
405 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
406 const _Rope_iterator<_CharT, _Alloc>& __y);
408 template<class _CharT, class _Alloc>
410 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
411 const _Rope_iterator<_CharT, _Alloc>& __y);
413 template<class _CharT, class _Alloc>
415 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
416 const _Rope_iterator<_CharT, _Alloc>& __y);
418 template<class _CharT, class _Alloc>
420 operator+(const rope<_CharT, _Alloc>& __left,
421 const rope<_CharT, _Alloc>& __right);
423 template<class _CharT, class _Alloc>
425 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
427 template<class _CharT, class _Alloc>
429 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
431 // Some helpers, so we can use power on ropes.
432 // See below for why this isn't local to the implementation.
434 // This uses a nonstandard refcount convention.
435 // The result has refcount 0.
436 template<class _CharT, class _Alloc>
437 struct _Rope_Concat_fn
438 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
439 rope<_CharT, _Alloc> >
442 operator()(const rope<_CharT, _Alloc>& __x,
443 const rope<_CharT, _Alloc>& __y)
444 { return __x + __y; }
447 template <class _CharT, class _Alloc>
448 inline rope<_CharT, _Alloc>
449 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
450 { return rope<_CharT, _Alloc>(); }
452 // Class _Refcount_Base provides a type, _RC_t, a data member,
453 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
454 // atomic preincrement/predecrement. The constructor initializes
456 struct _Refcount_Base
459 typedef std::size_t _RC_t;
461 // The data member _M_ref_count
465 #ifdef __GTHREAD_MUTEX_INIT
466 __gthread_mutex_t _M_ref_count_lock = __GTHREAD_MUTEX_INIT;
468 __gthread_mutex_t _M_ref_count_lock;
471 _Refcount_Base(_RC_t __n) : _M_ref_count(__n)
473 #ifndef __GTHREAD_MUTEX_INIT
474 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
475 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
477 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
482 #ifndef __GTHREAD_MUTEX_INIT
484 { __gthread_mutex_destroy(&_M_ref_count_lock); }
490 __gthread_mutex_lock(&_M_ref_count_lock);
492 __gthread_mutex_unlock(&_M_ref_count_lock);
498 __gthread_mutex_lock(&_M_ref_count_lock);
499 _RC_t __tmp = --_M_ref_count;
500 __gthread_mutex_unlock(&_M_ref_count_lock);
506 // What follows should really be local to rope. Unfortunately,
507 // that doesn't work, since it makes it impossible to define generic
508 // equality on rope iterators. According to the draft standard, the
509 // template parameters for such an equality operator cannot be inferred
510 // from the occurrence of a member class as a parameter.
511 // (SGI compilers in fact allow this, but the __result wouldn't be
513 // Similarly, some of the static member functions are member functions
514 // only to avoid polluting the global namespace, and to circumvent
515 // restrictions on type inference for template functions.
519 // The internal data structure for representing a rope. This is
520 // private to the implementation. A rope is really just a pointer
523 // A few basic functions for manipulating this data structure
524 // are members of _RopeRep. Most of the more complex algorithms
525 // are implemented as rope members.
527 // Some of the static member functions of _RopeRep have identically
528 // named functions in rope that simply invoke the _RopeRep versions.
530 #define __ROPE_DEFINE_ALLOCS(__a) \
531 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
532 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
533 __ROPE_DEFINE_ALLOC(__C,_C) \
534 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
535 __ROPE_DEFINE_ALLOC(__L,_L) \
536 typedef _Rope_RopeFunction<_CharT,__a> __F; \
537 __ROPE_DEFINE_ALLOC(__F,_F) \
538 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
539 __ROPE_DEFINE_ALLOC(__S,_S)
541 // Internal rope nodes potentially store a copy of the allocator
542 // instance used to allocate them. This is mostly redundant.
543 // But the alternative would be to pass allocator instances around
544 // in some form to nearly all internal functions, since any pointer
545 // assignment may result in a zero reference count and thus require
548 #define __STATIC_IF_SGI_ALLOC /* not static */
550 template <class _CharT, class _Alloc>
551 struct _Rope_rep_base
554 typedef std::size_t size_type;
555 typedef _Alloc allocator_type;
558 get_allocator() const
559 { return *static_cast<const _Alloc*>(this); }
563 { return *static_cast<_Alloc*>(this); }
565 const allocator_type&
566 _M_get_allocator() const
567 { return *static_cast<const _Alloc*>(this); }
569 _Rope_rep_base(size_type __size, const allocator_type&)
570 : _M_size(__size) { }
574 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
576 __alloc_traits<_Alloc>::template rebind<_Tp>::other __name##Alloc; \
577 static _Tp* __name##_allocate(size_type __n) \
578 { return __name##Alloc().allocate(__n); } \
579 static void __name##_deallocate(_Tp *__p, size_type __n) \
580 { __name##Alloc().deallocate(__p, __n); }
581 __ROPE_DEFINE_ALLOCS(_Alloc)
582 # undef __ROPE_DEFINE_ALLOC
585 template<class _CharT, class _Alloc>
587 : public _Rope_rep_base<_CharT, _Alloc>
593 __detail::_Tag _M_tag:8;
594 bool _M_is_balanced:8;
595 unsigned char _M_depth;
596 __GC_CONST _CharT* _M_c_string;
597 #ifdef __GTHREAD_MUTEX_INIT
598 __gthread_mutex_t _M_c_string_lock = __GTHREAD_MUTEX_INIT;
600 __gthread_mutex_t _M_c_string_lock;
602 /* Flattened version of string, if needed. */
604 /* If it's not 0, then the memory is owned */
606 /* In the case of a leaf, this may point to */
607 /* the same memory as the data field. */
608 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
610 typedef std::size_t size_type;
612 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
613 using _Rope_rep_base<_CharT, _Alloc>::_M_get_allocator;
615 _Rope_RopeRep(__detail::_Tag __t, int __d, bool __b, size_type __size,
616 const allocator_type& __a)
617 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
621 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
622 #ifdef __GTHREAD_MUTEX_INIT
625 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
627 { __gthread_mutex_destroy (&_M_c_string_lock); }
634 _S_free_string(__GC_CONST _CharT*, size_type __len,
635 allocator_type& __a);
636 #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
637 // Deallocate data section of a leaf.
638 // This shouldn't be a member function.
639 // But its hard to do anything else at the
640 // moment, because it's templatized w.r.t.
642 // Does nothing if __GC is defined.
644 void _M_free_c_string();
646 // Deallocate t. Assumes t is not 0.
659 _S_unref(_Rope_RopeRep* __t)
662 __t->_M_unref_nonnil();
666 _S_ref(_Rope_RopeRep* __t)
673 _S_free_if_unref(_Rope_RopeRep* __t)
675 if (0 != __t && 0 == __t->_M_ref_count)
679 void _M_unref_nonnil() { }
680 void _M_ref_nonnil() { }
681 static void _S_unref(_Rope_RopeRep*) { }
682 static void _S_ref(_Rope_RopeRep*) { }
683 static void _S_free_if_unref(_Rope_RopeRep*) { }
687 operator=(const _Rope_RopeRep&);
689 _Rope_RopeRep(const _Rope_RopeRep&);
692 template<class _CharT, class _Alloc>
693 struct _Rope_RopeLeaf
694 : public _Rope_RopeRep<_CharT, _Alloc>
696 typedef std::size_t size_type;
698 // Apparently needed by VC++
699 // The data fields of leaves are allocated with some
700 // extra space, to accommodate future growth and for basic
701 // character types, to hold a trailing eos character.
702 enum { _S_alloc_granularity = 8 };
705 _S_rounded_up_size(size_type __n)
707 size_type __size_with_eos;
709 if (_S_is_basic_char_type((_CharT*)0))
710 __size_with_eos = __n + 1;
712 __size_with_eos = __n;
714 return __size_with_eos;
716 // Allow slop for in-place expansion.
717 return ((__size_with_eos + size_type(_S_alloc_granularity) - 1)
718 &~ (size_type(_S_alloc_granularity) - 1));
721 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
722 /* The allocated size is */
723 /* _S_rounded_up_size(size), except */
724 /* in the GC case, in which it */
725 /* doesn't matter. */
726 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
729 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_type __size,
730 const allocator_type& __a)
731 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_leaf, 0, true,
732 __size, __a), _M_data(__d)
734 if (_S_is_basic_char_type((_CharT *)0))
736 // already eos terminated.
737 this->_M_c_string = __d;
740 // The constructor assumes that d has been allocated with
741 // the proper allocator and the properly padded size.
742 // In contrast, the destructor deallocates the data:
744 ~_Rope_RopeLeaf() throw()
746 if (_M_data != this->_M_c_string)
747 this->_M_free_c_string();
749 this->__STL_FREE_STRING(_M_data, this->_M_size, this->_M_get_allocator());
754 operator=(const _Rope_RopeLeaf&);
756 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
759 template<class _CharT, class _Alloc>
760 struct _Rope_RopeConcatenation
761 : public _Rope_RopeRep<_CharT, _Alloc>
764 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
765 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
767 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
770 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
771 _Rope_RopeRep<_CharT, _Alloc>* __r,
772 const allocator_type& __a)
773 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_concat,
774 std::max(__l->_M_depth,
777 __l->_M_size + __r->_M_size, __a),
778 _M_left(__l), _M_right(__r)
781 ~_Rope_RopeConcatenation() throw()
783 this->_M_free_c_string();
784 _M_left->_M_unref_nonnil();
785 _M_right->_M_unref_nonnil();
789 _Rope_RopeConcatenation&
790 operator=(const _Rope_RopeConcatenation&);
792 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
795 template<class _CharT, class _Alloc>
796 struct _Rope_RopeFunction
797 : public _Rope_RopeRep<_CharT, _Alloc>
800 char_producer<_CharT>* _M_fn;
802 bool _M_delete_when_done; // Char_producer is owned by the
803 // rope and should be explicitly
804 // deleted when the rope becomes
807 // In the GC case, we either register the rope for
808 // finalization, or not. Thus the field is unnecessary;
809 // the information is stored in the collector data structures.
810 // We do need a finalization procedure to be invoked by the
813 _S_fn_finalization_proc(void * __tree, void *)
814 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
816 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
819 _Rope_RopeFunction(char_producer<_CharT>* __f, std::size_t __size,
820 bool __d, const allocator_type& __a)
821 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_function, 0, true, __size, __a)
824 , _M_delete_when_done(__d)
830 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
831 _S_fn_finalization_proc, 0, 0, 0);
836 ~_Rope_RopeFunction() throw()
838 this->_M_free_c_string();
839 if (_M_delete_when_done)
845 operator=(const _Rope_RopeFunction&);
847 _Rope_RopeFunction(const _Rope_RopeFunction&);
849 // Substring results are usually represented using just
850 // concatenation nodes. But in the case of very long flat ropes
851 // or ropes with a functional representation that isn't practical.
852 // In that case, we represent the __result as a special case of
853 // RopeFunction, whose char_producer points back to the rope itself.
854 // In all cases except repeated substring operations and
855 // deallocation, we treat the __result as a RopeFunction.
856 template<class _CharT, class _Alloc>
857 struct _Rope_RopeSubstring
858 : public _Rope_RopeFunction<_CharT, _Alloc>,
859 public char_producer<_CharT>
861 typedef std::size_t size_type;
863 // XXX this whole class should be rewritten.
864 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
868 operator()(size_type __start_pos, size_type __req_len,
871 switch(_M_base->_M_tag)
873 case __detail::_S_function:
874 case __detail::_S_substringfn:
876 char_producer<_CharT>* __fn =
877 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
878 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
881 case __detail::_S_leaf:
883 __GC_CONST _CharT* __s =
884 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
885 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
894 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
897 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_type __s,
898 size_type __l, const allocator_type& __a)
899 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
900 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
903 _M_base->_M_ref_nonnil();
905 this->_M_tag = __detail::_S_substringfn;
907 virtual ~_Rope_RopeSubstring() throw()
910 _M_base->_M_unref_nonnil();
911 // _M_free_c_string(); -- done by parent class
916 // Self-destructing pointers to Rope_rep.
917 // These are not conventional smart pointers. Their
918 // only purpose in life is to ensure that unref is called
919 // on the pointer either at normal exit or if an exception
920 // is raised. It is the caller's responsibility to
921 // adjust reference counts when these pointers are initialized
922 // or assigned to. (This convention significantly reduces
923 // the number of potentially expensive reference count
926 template<class _CharT, class _Alloc>
927 struct _Rope_self_destruct_ptr
929 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
931 ~_Rope_self_destruct_ptr()
932 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
934 _Rope_self_destruct_ptr() : _M_ptr(0) { }
936 _Rope_self_destruct_ptr() { }
938 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
941 _Rope_RopeRep<_CharT, _Alloc>&
945 _Rope_RopeRep<_CharT, _Alloc>*
949 operator _Rope_RopeRep<_CharT, _Alloc>*()
952 _Rope_self_destruct_ptr&
953 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
954 { _M_ptr = __x; return *this; }
958 // Dereferencing a nonconst iterator has to return something
959 // that behaves almost like a reference. It's not possible to
960 // return an actual reference since assignment requires extra
961 // work. And we would get into the same problems as with the
962 // CD2 version of basic_string.
963 template<class _CharT, class _Alloc>
964 class _Rope_char_ref_proxy
966 friend class rope<_CharT, _Alloc>;
967 friend class _Rope_iterator<_CharT, _Alloc>;
968 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
970 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
972 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
974 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
975 typedef rope<_CharT, _Alloc> _My_rope;
978 bool _M_current_valid;
979 _My_rope* _M_root; // The whole rope.
981 _Rope_char_ref_proxy(_My_rope* __r, std::size_t __p)
982 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) { }
984 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
985 : _M_pos(__x._M_pos), _M_current(__x._M_current),
986 _M_current_valid(false), _M_root(__x._M_root) { }
988 // Don't preserve cache if the reference can outlive the
989 // expression. We claim that's not possible without calling
990 // a copy constructor or generating reference to a proxy
991 // reference. We declare the latter to have undefined semantics.
992 _Rope_char_ref_proxy(_My_rope* __r, std::size_t __p, _CharT __c)
993 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) { }
995 inline operator _CharT () const;
997 _Rope_char_ref_proxy&
998 operator=(_CharT __c);
1000 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
1002 _Rope_char_ref_proxy&
1003 operator=(const _Rope_char_ref_proxy& __c)
1004 { return operator=((_CharT)__c); }
1007 template<class _CharT, class __Alloc>
1009 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
1010 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
1017 template<class _CharT, class _Alloc>
1018 class _Rope_char_ptr_proxy
1020 // XXX this class should be rewritten.
1021 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1023 rope<_CharT,_Alloc>* _M_root; // The whole rope.
1025 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
1026 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
1028 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
1029 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
1031 _Rope_char_ptr_proxy() { }
1033 _Rope_char_ptr_proxy(_CharT* __x)
1034 : _M_root(0), _M_pos(0) { }
1036 _Rope_char_ptr_proxy&
1037 operator=(const _Rope_char_ptr_proxy& __x)
1039 _M_pos = __x._M_pos;
1040 _M_root = __x._M_root;
1044 template<class _CharT2, class _Alloc2>
1046 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1047 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1049 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1050 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1054 // Unlike in the C version, we cache only part of the stack
1055 // for rope iterators, since they must be efficiently copyable.
1056 // When we run out of cache, we have to reconstruct the iterator
1058 // Pointers from iterators are not included in reference counts.
1059 // Iterators are assumed to be thread private. Ropes can
1062 template<class _CharT, class _Alloc>
1063 class _Rope_iterator_base
1064 : public std::iterator<std::random_access_iterator_tag, _CharT>
1066 friend class rope<_CharT, _Alloc>;
1068 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1069 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1070 // Borland doesn't want this to be protected.
1072 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1073 enum { _S_iterator_buf_len = 15 };
1074 std::size_t _M_current_pos;
1075 _RopeRep* _M_root; // The whole rope.
1076 std::size_t _M_leaf_pos; // Starting position for current leaf
1077 __GC_CONST _CharT* _M_buf_start;
1079 // containing current char.
1080 __GC_CONST _CharT* _M_buf_ptr;
1081 // Pointer to current char in buffer.
1082 // != 0 ==> buffer valid.
1083 __GC_CONST _CharT* _M_buf_end;
1084 // One past __last valid char in buffer.
1085 // What follows is the path cache. We go out of our
1086 // way to make this compact.
1087 // Path_end contains the bottom section of the path from
1088 // the root to the current leaf.
1089 const _RopeRep* _M_path_end[_S_path_cache_len];
1090 int _M_leaf_index; // Last valid __pos in path_end;
1091 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1092 // point to concatenation nodes.
1093 unsigned char _M_path_directions;
1094 // (path_directions >> __i) & 1 is 1
1095 // iff we got from _M_path_end[leaf_index - __i - 1]
1096 // to _M_path_end[leaf_index - __i] by going to the
1097 // __right. Assumes path_cache_len <= 9.
1098 _CharT _M_tmp_buf[_S_iterator_buf_len];
1099 // Short buffer for surrounding chars.
1100 // This is useful primarily for
1101 // RopeFunctions. We put the buffer
1102 // here to avoid locking in the
1103 // multithreaded case.
1104 // The cached path is generally assumed to be valid
1105 // only if the buffer is valid.
1106 static void _S_setbuf(_Rope_iterator_base& __x);
1107 // Set buffer contents given
1109 static void _S_setcache(_Rope_iterator_base& __x);
1110 // Set buffer contents and
1112 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1113 // As above, but assumes path
1114 // cache is valid for previous posn.
1115 _Rope_iterator_base() { }
1117 _Rope_iterator_base(_RopeRep* __root, std::size_t __pos)
1118 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) { }
1120 void _M_incr(std::size_t __n);
1121 void _M_decr(std::size_t __n);
1125 { return _M_current_pos; }
1127 _Rope_iterator_base(const _Rope_iterator_base& __x)
1129 if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
1133 _M_current_pos = __x._M_current_pos;
1134 _M_root = __x._M_root;
1140 template<class _CharT, class _Alloc>
1141 class _Rope_iterator;
1143 template<class _CharT, class _Alloc>
1144 class _Rope_const_iterator
1145 : public _Rope_iterator_base<_CharT, _Alloc>
1147 friend class rope<_CharT, _Alloc>;
1149 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1150 // The one from the base class may not be directly visible.
1151 _Rope_const_iterator(const _RopeRep* __root, std::size_t __pos)
1152 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1154 // Only nonconst iterators modify root ref count
1157 typedef _CharT reference; // Really a value. Returning a reference
1158 // Would be a mess, since it would have
1159 // to be included in refcount.
1160 typedef const _CharT* pointer;
1163 _Rope_const_iterator() { }
1165 _Rope_const_iterator(const _Rope_const_iterator& __x)
1166 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1168 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1170 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, std::size_t __pos)
1171 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1173 _Rope_const_iterator&
1174 operator=(const _Rope_const_iterator& __x)
1176 if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
1177 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1180 this->_M_current_pos = __x._M_current_pos;
1181 this->_M_root = __x._M_root;
1182 this->_M_buf_ptr = 0;
1190 if (0 == this->_M_buf_ptr)
1191 this->_S_setcache(*this);
1192 return *this->_M_buf_ptr;
1195 // Without this const version, Rope iterators do not meet the
1196 // requirements of an Input Iterator.
1200 return *const_cast<_Rope_const_iterator&>(*this);
1203 _Rope_const_iterator&
1206 __GC_CONST _CharT* __next;
1207 if (0 != this->_M_buf_ptr
1208 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1210 this->_M_buf_ptr = __next;
1211 ++this->_M_current_pos;
1218 _Rope_const_iterator&
1219 operator+=(std::ptrdiff_t __n)
1224 this->_M_decr(-__n);
1228 _Rope_const_iterator&
1235 _Rope_const_iterator&
1236 operator-=(std::ptrdiff_t __n)
1241 this->_M_incr(-__n);
1245 _Rope_const_iterator
1248 std::size_t __old_pos = this->_M_current_pos;
1250 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1251 // This makes a subsequent dereference expensive.
1252 // Perhaps we should instead copy the iterator
1253 // if it has a valid cache?
1256 _Rope_const_iterator
1259 std::size_t __old_pos = this->_M_current_pos;
1261 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1264 template<class _CharT2, class _Alloc2>
1265 friend _Rope_const_iterator<_CharT2, _Alloc2>
1266 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1267 std::ptrdiff_t __n);
1269 template<class _CharT2, class _Alloc2>
1270 friend _Rope_const_iterator<_CharT2, _Alloc2>
1271 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1272 std::ptrdiff_t __n);
1274 template<class _CharT2, class _Alloc2>
1275 friend _Rope_const_iterator<_CharT2, _Alloc2>
1276 operator+(std::ptrdiff_t __n,
1277 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1280 operator[](std::size_t __n)
1281 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1282 this->_M_current_pos + __n); }
1284 template<class _CharT2, class _Alloc2>
1286 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1287 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1289 template<class _CharT2, class _Alloc2>
1291 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1292 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1294 template<class _CharT2, class _Alloc2>
1295 friend std::ptrdiff_t
1296 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1297 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1300 template<class _CharT, class _Alloc>
1301 class _Rope_iterator
1302 : public _Rope_iterator_base<_CharT, _Alloc>
1304 friend class rope<_CharT, _Alloc>;
1306 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1307 rope<_CharT, _Alloc>* _M_root_rope;
1309 // root is treated as a cached version of this, and is used to
1310 // detect changes to the underlying rope.
1312 // Root is included in the reference count. This is necessary
1313 // so that we can detect changes reliably. Unfortunately, it
1314 // requires careful bookkeeping for the nonGC case.
1315 _Rope_iterator(rope<_CharT, _Alloc>* __r, std::size_t __pos)
1316 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1318 { _RopeRep::_S_ref(this->_M_root);
1319 if (!(__r -> empty()))
1320 this->_S_setcache(*this);
1325 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1326 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1328 rope<_CharT, _Alloc>&
1330 { return *_M_root_rope; }
1334 this->_M_root = 0; // Needed for reference counting.
1337 _Rope_iterator(const _Rope_iterator& __x)
1338 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1340 _M_root_rope = __x._M_root_rope;
1341 _RopeRep::_S_ref(this->_M_root);
1344 _Rope_iterator(rope<_CharT, _Alloc>& __r, std::size_t __pos);
1347 { _RopeRep::_S_unref(this->_M_root); }
1350 operator=(const _Rope_iterator& __x)
1352 _RopeRep* __old = this->_M_root;
1354 _RopeRep::_S_ref(__x._M_root);
1355 if (0 != __x._M_buf_ptr && __x._M_buf_start != __x._M_tmp_buf)
1357 _M_root_rope = __x._M_root_rope;
1358 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1362 this->_M_current_pos = __x._M_current_pos;
1363 this->_M_root = __x._M_root;
1364 _M_root_rope = __x._M_root_rope;
1365 this->_M_buf_ptr = 0;
1367 _RopeRep::_S_unref(__old);
1375 if (0 == this->_M_buf_ptr)
1376 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1377 this->_M_current_pos);
1379 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1380 this->_M_current_pos,
1384 // See above comment.
1388 return *const_cast<_Rope_iterator&>(*this);
1399 operator+=(std::ptrdiff_t __n)
1404 this->_M_decr(-__n);
1416 operator-=(std::ptrdiff_t __n)
1421 this->_M_incr(-__n);
1428 std::size_t __old_pos = this->_M_current_pos;
1430 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1436 std::size_t __old_pos = this->_M_current_pos;
1438 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1442 operator[](std::ptrdiff_t __n)
1443 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1444 this->_M_current_pos
1447 template<class _CharT2, class _Alloc2>
1449 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1450 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1452 template<class _CharT2, class _Alloc2>
1454 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1455 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1457 template<class _CharT2, class _Alloc2>
1458 friend std::ptrdiff_t
1459 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1460 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1462 template<class _CharT2, class _Alloc2>
1463 friend _Rope_iterator<_CharT2, _Alloc2>
1464 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1465 std::ptrdiff_t __n);
1467 template<class _CharT2, class _Alloc2>
1468 friend _Rope_iterator<_CharT2, _Alloc2>
1469 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1470 std::ptrdiff_t __n);
1472 template<class _CharT2, class _Alloc2>
1473 friend _Rope_iterator<_CharT2, _Alloc2>
1474 operator+(std::ptrdiff_t __n,
1475 const _Rope_iterator<_CharT2, _Alloc2>& __x);
1479 template <class _CharT, class _Alloc>
1483 typedef _Alloc allocator_type;
1486 get_allocator() const
1487 { return *static_cast<const _Alloc*>(this); }
1491 { return *static_cast<_Alloc*>(this); }
1493 const allocator_type&
1494 _M_get_allocator() const
1495 { return *static_cast<const _Alloc*>(this); }
1497 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1498 // The one in _Base may not be visible due to template rules.
1500 _Rope_base(_RopeRep* __t, const allocator_type&)
1501 : _M_tree_ptr(__t) { }
1503 _Rope_base(const allocator_type&) { }
1505 // The only data member of a rope:
1506 _RopeRep *_M_tree_ptr;
1508 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1510 __alloc_traits<_Alloc>::template rebind<_Tp>::other __name##Alloc; \
1511 static _Tp* __name##_allocate(std::size_t __n) \
1512 { return __name##Alloc().allocate(__n); } \
1513 static void __name##_deallocate(_Tp *__p, std::size_t __n) \
1514 { __name##Alloc().deallocate(__p, __n); }
1515 __ROPE_DEFINE_ALLOCS(_Alloc)
1516 #undef __ROPE_DEFINE_ALLOC
1520 operator=(const _Rope_base&);
1522 _Rope_base(const _Rope_base&);
1526 * This is an SGI extension.
1527 * @ingroup SGIextensions
1530 template <class _CharT, class _Alloc>
1531 class rope : public _Rope_base<_CharT, _Alloc>
1534 typedef _CharT value_type;
1535 typedef std::ptrdiff_t difference_type;
1536 typedef std::size_t size_type;
1537 typedef _CharT const_reference;
1538 typedef const _CharT* const_pointer;
1539 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1540 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1541 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1542 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1544 friend class _Rope_iterator<_CharT, _Alloc>;
1545 friend class _Rope_const_iterator<_CharT, _Alloc>;
1546 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1547 friend class _Rope_iterator_base<_CharT, _Alloc>;
1548 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1549 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1550 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1553 typedef _Rope_base<_CharT, _Alloc> _Base;
1554 typedef typename _Base::allocator_type allocator_type;
1555 using _Base::_M_tree_ptr;
1556 using _Base::get_allocator;
1557 using _Base::_M_get_allocator;
1558 typedef __GC_CONST _CharT* _Cstrptr;
1560 static _CharT _S_empty_c_str[1];
1564 { return __c == _S_eos((_CharT*)0); }
1566 enum { _S_copy_max = 23 };
1567 // For strings shorter than _S_copy_max, we copy to
1570 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1571 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1572 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1573 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1574 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1576 // Retrieve a character at the indicated position.
1577 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1580 // Obtain a pointer to the character at the indicated position.
1581 // The pointer can be used to change the character.
1582 // If such a pointer cannot be produced, as is frequently the
1583 // case, 0 is returned instead.
1584 // (Returns nonzero only if all nodes in the path have a refcount
1586 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1590 _S_apply_to_pieces(// should be template parameter
1591 _Rope_char_consumer<_CharT>& __c,
1592 const _RopeRep* __r,
1593 size_type __begin, size_type __end);
1594 // begin and end are assumed to be in range.
1598 _S_unref(_RopeRep* __t)
1599 { _RopeRep::_S_unref(__t); }
1602 _S_ref(_RopeRep* __t)
1603 { _RopeRep::_S_ref(__t); }
1606 static void _S_unref(_RopeRep*) { }
1607 static void _S_ref(_RopeRep*) { }
1611 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1613 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1616 // _Result is counted in refcount.
1617 static _RopeRep* _S_substring(_RopeRep* __base,
1618 size_type __start, size_type __endp1);
1620 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1621 const _CharT* __iter,
1623 allocator_type& __a);
1624 // Concatenate rope and char ptr, copying __iter.
1625 // Should really take an arbitrary iterator.
1626 // Result is counted in refcount.
1627 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1628 const _CharT* __iter,
1630 allocator_type& __a)
1631 // As above, but one reference to __r is about to be
1632 // destroyed. Thus the pieces may be recycled if all
1633 // relevant reference counts are 1.
1635 // We can't really do anything since refcounts are unavailable.
1636 { return _S_concat_char_iter(__r, __iter, __slen, __a); }
1641 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1642 // General concatenation on _RopeRep. _Result
1643 // has refcount of 1. Adjusts argument refcounts.
1647 apply_to_pieces(size_type __begin, size_type __end,
1648 _Rope_char_consumer<_CharT>& __c) const
1649 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1654 _S_rounded_up_size(size_type __n)
1655 { return _RopeLeaf::_S_rounded_up_size(__n); }
1658 _S_allocated_capacity(size_type __n)
1660 if (_S_is_basic_char_type((_CharT*)0))
1661 return _S_rounded_up_size(__n) - 1;
1663 return _S_rounded_up_size(__n);
1667 // Allocate and construct a RopeLeaf using the supplied allocator
1668 // Takes ownership of s instead of copying.
1670 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1671 size_type __size, allocator_type& __a)
1673 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1674 return new(__space) _RopeLeaf(__s, __size, __a);
1677 static _RopeConcatenation*
1678 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1679 allocator_type& __a)
1681 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1682 return new(__space) _RopeConcatenation(__left, __right, __a);
1685 static _RopeFunction*
1686 _S_new_RopeFunction(char_producer<_CharT>* __f,
1687 size_type __size, bool __d, allocator_type& __a)
1689 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1690 return new(__space) _RopeFunction(__f, __size, __d, __a);
1693 static _RopeSubstring*
1694 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_type __s,
1695 size_type __l, allocator_type& __a)
1697 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1698 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1702 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1703 size_type __size, allocator_type& __a)
1704 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1705 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1709 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1711 __uninitialized_copy_n_a(__s, __size, __buf, __a);
1712 _S_cond_store_eos(__buf[__size]);
1714 { return _S_new_RopeLeaf(__buf, __size, __a); }
1717 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1718 __throw_exception_again;
1722 // Concatenation of nonempty strings.
1723 // Always builds a concatenation node.
1724 // Rebalances if the result is too deep.
1725 // Result has refcount 1.
1726 // Does not increment left and right ref counts even though
1727 // they are referenced.
1729 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1731 // Concatenation helper functions
1733 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1734 const _CharT* __iter, size_type __slen);
1735 // Concatenate by copying leaf.
1736 // should take an arbitrary iterator
1737 // result has refcount 1.
1740 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1741 const _CharT* __iter, size_type __slen);
1742 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1747 static size_type _S_char_ptr_len(const _CharT* __s);
1748 // slightly generalized strlen
1750 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1751 : _Base(__t, __a) { }
1754 // Copy __r to the _CharT buffer.
1755 // Returns __buffer + __r->_M_size.
1756 // Assumes that buffer is uninitialized.
1757 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1759 // Again, with explicit starting position and length.
1760 // Assumes that buffer is uninitialized.
1761 static _CharT* _S_flatten(_RopeRep* __r,
1762 size_type __start, size_type __len,
1765 static const unsigned long
1766 _S_min_len[__detail::_S_max_rope_depth + 1];
1769 _S_is_balanced(_RopeRep* __r)
1770 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1773 _S_is_almost_balanced(_RopeRep* __r)
1774 { return (__r->_M_depth == 0
1775 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1778 _S_is_roughly_balanced(_RopeRep* __r)
1779 { return (__r->_M_depth <= 1
1780 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1782 // Assumes the result is not empty.
1784 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1786 _RopeRep* __result = _S_concat(__left, __right);
1787 if (_S_is_balanced(__result))
1788 __result->_M_is_balanced = true;
1792 // The basic rebalancing operation. Logically copies the
1793 // rope. The result has refcount of 1. The client will
1794 // usually decrement the reference count of __r.
1795 // The result is within height 2 of balanced by the above
1797 static _RopeRep* _S_balance(_RopeRep* __r);
1799 // Add all unbalanced subtrees to the forest of balanced trees.
1800 // Used only by balance.
1801 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1803 // Add __r to forest, assuming __r is already balanced.
1804 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1806 // Print to stdout, exposing structure
1807 static void _S_dump(_RopeRep* __r, int __indent = 0);
1809 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1810 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1813 _GLIBCXX_NODISCARD bool
1815 { return 0 == this->_M_tree_ptr; }
1817 // Comparison member function. This is public only for those
1818 // clients that need a ternary comparison. Others
1819 // should use the comparison operators below.
1821 compare(const rope& __y) const
1822 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1824 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1828 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1829 _M_get_allocator());
1832 rope(const _CharT* __s, size_type __len,
1833 const allocator_type& __a = allocator_type())
1837 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, _M_get_allocator());
1840 // Should perhaps be templatized with respect to the iterator type
1841 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1843 rope(const _CharT* __s, const _CharT* __e,
1844 const allocator_type& __a = allocator_type())
1848 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, _M_get_allocator());
1851 rope(const const_iterator& __s, const const_iterator& __e,
1852 const allocator_type& __a = allocator_type())
1853 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1854 __e._M_current_pos), __a)
1857 rope(const iterator& __s, const iterator& __e,
1858 const allocator_type& __a = allocator_type())
1859 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1860 __e._M_current_pos), __a)
1863 rope(_CharT __c, const allocator_type& __a = allocator_type())
1866 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1868 __alloc_traits<allocator_type>::construct(_M_get_allocator(),
1872 this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1,
1873 _M_get_allocator());
1877 _RopeRep::__STL_FREE_STRING(__buf, 1, _M_get_allocator());
1878 __throw_exception_again;
1882 rope(size_type __n, _CharT __c,
1883 const allocator_type& __a = allocator_type());
1885 rope(const allocator_type& __a = allocator_type())
1888 // Construct a rope from a function that can compute its members
1889 rope(char_producer<_CharT> *__fn, size_type __len, bool __delete_fn,
1890 const allocator_type& __a = allocator_type())
1893 this->_M_tree_ptr = (0 == __len)
1895 : _S_new_RopeFunction(__fn, __len, __delete_fn, _M_get_allocator());
1898 rope(const rope& __x, const allocator_type& __a = allocator_type())
1899 : _Base(__x._M_tree_ptr, __a)
1900 { _S_ref(this->_M_tree_ptr); }
1903 { _S_unref(this->_M_tree_ptr); }
1906 operator=(const rope& __x)
1908 _RopeRep* __old = this->_M_tree_ptr;
1909 this->_M_tree_ptr = __x._M_tree_ptr;
1910 _S_ref(this->_M_tree_ptr);
1918 _S_unref(this->_M_tree_ptr);
1919 this->_M_tree_ptr = 0;
1923 push_back(_CharT __x)
1925 allocator_type __a = _M_get_allocator();
1926 _RopeRep* __old = this->_M_tree_ptr;
1928 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1, __a);
1935 _RopeRep* __old = this->_M_tree_ptr;
1936 this->_M_tree_ptr = _S_substring(this->_M_tree_ptr,
1937 0, this->_M_tree_ptr->_M_size - 1);
1943 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1946 push_front(_CharT __x)
1948 _RopeRep* __old = this->_M_tree_ptr;
1950 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, _M_get_allocator());
1953 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1960 __throw_exception_again;
1967 _RopeRep* __old = this->_M_tree_ptr;
1969 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1975 { return _S_fetch(this->_M_tree_ptr, 0); }
1980 _RopeRep* __old = this->_M_tree_ptr;
1981 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1986 copy(_CharT* __buffer) const
1988 _Destroy_const(__buffer, __buffer + size(), _M_get_allocator());
1989 _S_flatten(this->_M_tree_ptr, __buffer);
1992 // This is the copy function from the standard, but
1993 // with the arguments reordered to make it consistent with the
1994 // rest of the interface.
1995 // Note that this guaranteed not to compile if the draft standard
1996 // order is assumed.
1998 copy(size_type __pos, size_type __n, _CharT* __buffer) const
2000 size_type __size = size();
2001 size_type __len = (__pos + __n > __size? __size - __pos : __n);
2003 _Destroy_const(__buffer, __buffer + __len, _M_get_allocator());
2004 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
2008 // Print to stdout, exposing structure. May be useful for
2009 // performance debugging.
2012 { _S_dump(this->_M_tree_ptr); }
2014 // Convert to 0 terminated string in new allocated memory.
2015 // Embedded 0s in the input do not terminate the copy.
2016 const _CharT* c_str() const;
2018 // As above, but also use the flattened representation as
2019 // the new rope representation.
2020 const _CharT* replace_with_c_str();
2022 // Reclaim memory for the c_str generated flattened string.
2023 // Intentionally undocumented, since it's hard to say when this
2024 // is safe for multiple threads.
2028 if (0 == this->_M_tree_ptr)
2030 if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag &&
2031 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
2032 this->_M_tree_ptr->_M_c_string)
2034 // Representation shared
2038 this->_M_tree_ptr->_M_free_c_string();
2040 this->_M_tree_ptr->_M_c_string = 0;
2044 operator[] (size_type __pos) const
2045 { return _S_fetch(this->_M_tree_ptr, __pos); }
2048 at(size_type __pos) const
2050 // if (__pos >= size()) throw out_of_range; // XXX
2051 return (*this)[__pos];
2056 { return(const_iterator(this->_M_tree_ptr, 0)); }
2058 // An easy way to get a const iterator from a non-const container.
2061 { return(const_iterator(this->_M_tree_ptr, 0)); }
2065 { return(const_iterator(this->_M_tree_ptr, size())); }
2069 { return(const_iterator(this->_M_tree_ptr, size())); }
2073 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
2082 return _S_min_len[int(__detail::_S_max_rope_depth) - 1] - 1;
2083 // Guarantees that the result can be sufficiently
2084 // balanced. Longer ropes will probably still work,
2085 // but it's harder to make guarantees.
2088 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
2090 const_reverse_iterator
2092 { return const_reverse_iterator(end()); }
2094 const_reverse_iterator
2095 const_rbegin() const
2096 { return const_reverse_iterator(end()); }
2098 const_reverse_iterator
2100 { return const_reverse_iterator(begin()); }
2102 const_reverse_iterator
2104 { return const_reverse_iterator(begin()); }
2106 template<class _CharT2, class _Alloc2>
2107 friend rope<_CharT2, _Alloc2>
2108 operator+(const rope<_CharT2, _Alloc2>& __left,
2109 const rope<_CharT2, _Alloc2>& __right);
2111 template<class _CharT2, class _Alloc2>
2112 friend rope<_CharT2, _Alloc2>
2113 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2115 template<class _CharT2, class _Alloc2>
2116 friend rope<_CharT2, _Alloc2>
2117 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2119 // The symmetric cases are intentionally omitted, since they're
2120 // presumed to be less common, and we don't handle them as well.
2122 // The following should really be templatized. The first
2123 // argument should be an input iterator or forward iterator with
2124 // value_type _CharT.
2126 append(const _CharT* __iter, size_type __n)
2128 allocator_type __a = _M_get_allocator();
2129 _RopeRep* __result =
2130 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n, __a);
2131 _S_unref(this->_M_tree_ptr);
2132 this->_M_tree_ptr = __result;
2137 append(const _CharT* __c_string)
2139 size_type __len = _S_char_ptr_len(__c_string);
2140 append(__c_string, __len);
2145 append(const _CharT* __s, const _CharT* __e)
2147 allocator_type __a = _M_get_allocator();
2148 _RopeRep* __result =
2149 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s, __a);
2150 _S_unref(this->_M_tree_ptr);
2151 this->_M_tree_ptr = __result;
2156 append(const_iterator __s, const_iterator __e)
2158 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2160 __e._M_current_pos));
2161 _RopeRep* __result = _S_concat(this->_M_tree_ptr,
2162 (_RopeRep*)__appendee);
2163 _S_unref(this->_M_tree_ptr);
2164 this->_M_tree_ptr = __result;
2171 allocator_type __a = _M_get_allocator();
2172 _RopeRep* __result =
2173 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1, __a);
2174 _S_unref(this->_M_tree_ptr);
2175 this->_M_tree_ptr = __result;
2181 { return append(_CharT()); } // XXX why?
2184 append(const rope& __y)
2186 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2187 _S_unref(this->_M_tree_ptr);
2188 this->_M_tree_ptr = __result;
2193 append(size_type __n, _CharT __c)
2195 rope<_CharT,_Alloc> __last(__n, __c);
2196 return append(__last);
2202 _RopeRep* __tmp = this->_M_tree_ptr;
2203 this->_M_tree_ptr = __b._M_tree_ptr;
2204 __b._M_tree_ptr = __tmp;
2208 // Result is included in refcount.
2210 replace(_RopeRep* __old, size_type __pos1,
2211 size_type __pos2, _RopeRep* __r)
2218 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2219 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2223 __result = _S_concat(__left, __right);
2226 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2227 __result = _S_concat(__left_result, __right);
2234 insert(size_type __p, const rope& __r)
2236 _RopeRep* __result =
2237 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2238 _S_unref(this->_M_tree_ptr);
2239 this->_M_tree_ptr = __result;
2243 insert(size_type __p, size_type __n, _CharT __c)
2245 rope<_CharT,_Alloc> __r(__n,__c);
2250 insert(size_type __p, const _CharT* __i, size_type __n)
2252 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2253 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2255 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n,
2256 _M_get_allocator()));
2257 // _S_ destr_concat_char_iter should be safe here.
2258 // But as it stands it's probably not a win, since __left
2259 // is likely to have additional references.
2260 _RopeRep* __result = _S_concat(__left_result, __right);
2261 _S_unref(this->_M_tree_ptr);
2262 this->_M_tree_ptr = __result;
2266 insert(size_type __p, const _CharT* __c_string)
2267 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2270 insert(size_type __p, _CharT __c)
2271 { insert(__p, &__c, 1); }
2274 insert(size_type __p)
2276 _CharT __c = _CharT();
2277 insert(__p, &__c, 1);
2281 insert(size_type __p, const _CharT* __i, const _CharT* __j)
2288 insert(size_type __p, const const_iterator& __i,
2289 const const_iterator& __j)
2296 insert(size_type __p, const iterator& __i,
2297 const iterator& __j)
2303 // (position, length) versions of replace operations:
2306 replace(size_type __p, size_type __n, const rope& __r)
2308 _RopeRep* __result =
2309 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2310 _S_unref(this->_M_tree_ptr);
2311 this->_M_tree_ptr = __result;
2315 replace(size_type __p, size_type __n,
2316 const _CharT* __i, size_type __i_len)
2318 rope __r(__i, __i_len);
2319 replace(__p, __n, __r);
2323 replace(size_type __p, size_type __n, _CharT __c)
2326 replace(__p, __n, __r);
2330 replace(size_type __p, size_type __n, const _CharT* __c_string)
2332 rope __r(__c_string);
2333 replace(__p, __n, __r);
2337 replace(size_type __p, size_type __n,
2338 const _CharT* __i, const _CharT* __j)
2341 replace(__p, __n, __r);
2345 replace(size_type __p, size_type __n,
2346 const const_iterator& __i, const const_iterator& __j)
2349 replace(__p, __n, __r);
2353 replace(size_type __p, size_type __n,
2354 const iterator& __i, const iterator& __j)
2357 replace(__p, __n, __r);
2360 // Single character variants:
2362 replace(size_type __p, _CharT __c)
2364 iterator __i(this, __p);
2369 replace(size_type __p, const rope& __r)
2370 { replace(__p, 1, __r); }
2373 replace(size_type __p, const _CharT* __i, size_type __i_len)
2374 { replace(__p, 1, __i, __i_len); }
2377 replace(size_type __p, const _CharT* __c_string)
2378 { replace(__p, 1, __c_string); }
2381 replace(size_type __p, const _CharT* __i, const _CharT* __j)
2382 { replace(__p, 1, __i, __j); }
2385 replace(size_type __p, const const_iterator& __i,
2386 const const_iterator& __j)
2387 { replace(__p, 1, __i, __j); }
2390 replace(size_type __p, const iterator& __i,
2391 const iterator& __j)
2392 { replace(__p, 1, __i, __j); }
2394 // Erase, (position, size) variant.
2396 erase(size_type __p, size_type __n)
2398 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2400 _S_unref(this->_M_tree_ptr);
2401 this->_M_tree_ptr = __result;
2404 // Insert, iterator variants.
2406 insert(const iterator& __p, const rope& __r)
2408 insert(__p.index(), __r);
2413 insert(const iterator& __p, size_type __n, _CharT __c)
2415 insert(__p.index(), __n, __c);
2419 iterator insert(const iterator& __p, _CharT __c)
2421 insert(__p.index(), __c);
2426 insert(const iterator& __p )
2428 insert(__p.index());
2433 insert(const iterator& __p, const _CharT* c_string)
2435 insert(__p.index(), c_string);
2440 insert(const iterator& __p, const _CharT* __i, size_type __n)
2442 insert(__p.index(), __i, __n);
2447 insert(const iterator& __p, const _CharT* __i,
2450 insert(__p.index(), __i, __j);
2455 insert(const iterator& __p,
2456 const const_iterator& __i, const const_iterator& __j)
2458 insert(__p.index(), __i, __j);
2463 insert(const iterator& __p,
2464 const iterator& __i, const iterator& __j)
2466 insert(__p.index(), __i, __j);
2470 // Replace, range variants.
2472 replace(const iterator& __p, const iterator& __q, const rope& __r)
2473 { replace(__p.index(), __q.index() - __p.index(), __r); }
2476 replace(const iterator& __p, const iterator& __q, _CharT __c)
2477 { replace(__p.index(), __q.index() - __p.index(), __c); }
2480 replace(const iterator& __p, const iterator& __q,
2481 const _CharT* __c_string)
2482 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2485 replace(const iterator& __p, const iterator& __q,
2486 const _CharT* __i, size_type __n)
2487 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2490 replace(const iterator& __p, const iterator& __q,
2491 const _CharT* __i, const _CharT* __j)
2492 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2495 replace(const iterator& __p, const iterator& __q,
2496 const const_iterator& __i, const const_iterator& __j)
2497 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2500 replace(const iterator& __p, const iterator& __q,
2501 const iterator& __i, const iterator& __j)
2502 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2504 // Replace, iterator variants.
2506 replace(const iterator& __p, const rope& __r)
2507 { replace(__p.index(), __r); }
2510 replace(const iterator& __p, _CharT __c)
2511 { replace(__p.index(), __c); }
2514 replace(const iterator& __p, const _CharT* __c_string)
2515 { replace(__p.index(), __c_string); }
2518 replace(const iterator& __p, const _CharT* __i, size_type __n)
2519 { replace(__p.index(), __i, __n); }
2522 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2523 { replace(__p.index(), __i, __j); }
2526 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2527 { replace(__p.index(), __i, __j); }
2530 replace(const iterator& __p, iterator __i, iterator __j)
2531 { replace(__p.index(), __i, __j); }
2533 // Iterator and range variants of erase
2535 erase(const iterator& __p, const iterator& __q)
2537 size_type __p_index = __p.index();
2538 erase(__p_index, __q.index() - __p_index);
2539 return iterator(this, __p_index);
2543 erase(const iterator& __p)
2545 size_type __p_index = __p.index();
2546 erase(__p_index, 1);
2547 return iterator(this, __p_index);
2551 substr(size_type __start, size_type __len = 1) const
2553 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2559 substr(iterator __start, iterator __end) const
2561 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2567 substr(iterator __start) const
2569 size_type __pos = __start.index();
2570 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2575 substr(const_iterator __start, const_iterator __end) const
2577 // This might eventually take advantage of the cache in the
2579 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2584 rope<_CharT, _Alloc>
2585 substr(const_iterator __start)
2587 size_type __pos = __start.index();
2588 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2592 static const size_type npos;
2594 size_type find(_CharT __c, size_type __pos = 0) const;
2597 find(const _CharT* __s, size_type __pos = 0) const
2599 size_type __result_pos;
2600 const_iterator __result =
2601 std::search(const_begin() + __pos, const_end(),
2602 __s, __s + _S_char_ptr_len(__s));
2603 __result_pos = __result.index();
2604 #ifndef __STL_OLD_ROPE_SEMANTICS
2605 if (__result_pos == size())
2606 __result_pos = npos;
2608 return __result_pos;
2613 { return(iterator(this, 0)); }
2617 { return(iterator(this, size())); }
2619 typedef std::reverse_iterator<iterator> reverse_iterator;
2623 { return reverse_iterator(mutable_end()); }
2627 { return reverse_iterator(mutable_begin()); }
2630 mutable_reference_at(size_type __pos)
2631 { return reference(this, __pos); }
2635 operator[] (size_type __pos)
2636 { return _char_ref_proxy(this, __pos); }
2641 // if (__pos >= size()) throw out_of_range; // XXX
2642 return (*this)[__pos];
2645 void resize(size_type __n, _CharT __c) { }
2646 void resize(size_type __n) { }
2647 void reserve(size_type __res_arg = 0) { }
2651 { return max_size(); }
2653 // Stuff below this line is dangerous because it's error prone.
2654 // I would really like to get rid of it.
2655 // copy function with funny arg ordering.
2657 copy(_CharT* __buffer, size_type __n,
2658 size_type __pos = 0) const
2659 { return copy(__pos, __n, __buffer); }
2663 { return mutable_end(); }
2667 { return mutable_begin(); }
2671 { return mutable_rend(); }
2675 { return mutable_rbegin(); }
2680 { return const_end(); }
2684 { return const_begin(); }
2686 const_reverse_iterator
2688 { return const_rend(); }
2690 const_reverse_iterator
2692 { return const_rbegin(); }
2697 template <class _CharT, class _Alloc>
2698 const typename rope<_CharT, _Alloc>::size_type
2699 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2701 template <class _CharT, class _Alloc>
2702 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2703 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2704 { return (__x._M_current_pos == __y._M_current_pos
2705 && __x._M_root == __y._M_root); }
2707 template <class _CharT, class _Alloc>
2708 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2709 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2710 { return (__x._M_current_pos < __y._M_current_pos); }
2712 template <class _CharT, class _Alloc>
2713 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2714 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2715 { return !(__x == __y); }
2717 template <class _CharT, class _Alloc>
2718 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2719 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2720 { return __y < __x; }
2722 template <class _CharT, class _Alloc>
2724 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2725 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2726 { return !(__y < __x); }
2728 template <class _CharT, class _Alloc>
2730 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2731 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2732 { return !(__x < __y); }
2734 template <class _CharT, class _Alloc>
2735 inline std::ptrdiff_t
2736 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2737 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2739 return (std::ptrdiff_t)__x._M_current_pos
2740 - (std::ptrdiff_t)__y._M_current_pos;
2743 template <class _CharT, class _Alloc>
2744 inline _Rope_const_iterator<_CharT, _Alloc>
2745 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2747 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2748 __x._M_current_pos - __n); }
2750 template <class _CharT, class _Alloc>
2751 inline _Rope_const_iterator<_CharT, _Alloc>
2752 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2754 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2755 __x._M_current_pos + __n); }
2757 template <class _CharT, class _Alloc>
2758 inline _Rope_const_iterator<_CharT, _Alloc>
2759 operator+(std::ptrdiff_t __n,
2760 const _Rope_const_iterator<_CharT, _Alloc>& __x)
2761 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2762 __x._M_current_pos + __n); }
2764 template <class _CharT, class _Alloc>
2766 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2767 const _Rope_iterator<_CharT, _Alloc>& __y)
2768 {return (__x._M_current_pos == __y._M_current_pos
2769 && __x._M_root_rope == __y._M_root_rope); }
2771 template <class _CharT, class _Alloc>
2773 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2774 const _Rope_iterator<_CharT, _Alloc>& __y)
2775 { return (__x._M_current_pos < __y._M_current_pos); }
2777 template <class _CharT, class _Alloc>
2779 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2780 const _Rope_iterator<_CharT, _Alloc>& __y)
2781 { return !(__x == __y); }
2783 template <class _CharT, class _Alloc>
2785 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2786 const _Rope_iterator<_CharT, _Alloc>& __y)
2787 { return __y < __x; }
2789 template <class _CharT, class _Alloc>
2791 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2792 const _Rope_iterator<_CharT, _Alloc>& __y)
2793 { return !(__y < __x); }
2795 template <class _CharT, class _Alloc>
2797 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2798 const _Rope_iterator<_CharT, _Alloc>& __y)
2799 { return !(__x < __y); }
2801 template <class _CharT, class _Alloc>
2802 inline std::ptrdiff_t
2803 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2804 const _Rope_iterator<_CharT, _Alloc>& __y)
2805 { return ((std::ptrdiff_t)__x._M_current_pos
2806 - (std::ptrdiff_t)__y._M_current_pos); }
2808 template <class _CharT, class _Alloc>
2809 inline _Rope_iterator<_CharT, _Alloc>
2810 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2812 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2813 __x._M_current_pos - __n); }
2815 template <class _CharT, class _Alloc>
2816 inline _Rope_iterator<_CharT, _Alloc>
2817 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, std::ptrdiff_t __n)
2818 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2819 __x._M_current_pos + __n); }
2821 template <class _CharT, class _Alloc>
2822 inline _Rope_iterator<_CharT, _Alloc>
2823 operator+(std::ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2824 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2825 __x._M_current_pos + __n); }
2827 template <class _CharT, class _Alloc>
2828 inline rope<_CharT, _Alloc>
2829 operator+(const rope<_CharT, _Alloc>& __left,
2830 const rope<_CharT, _Alloc>& __right)
2832 // Inlining this should make it possible to keep __left and
2833 // __right in registers.
2834 typedef rope<_CharT, _Alloc> rope_type;
2835 return rope_type(rope_type::_S_concat(__left._M_tree_ptr,
2836 __right._M_tree_ptr));
2839 template <class _CharT, class _Alloc>
2840 inline rope<_CharT, _Alloc>&
2841 operator+=(rope<_CharT, _Alloc>& __left,
2842 const rope<_CharT, _Alloc>& __right)
2844 __left.append(__right);
2848 template <class _CharT, class _Alloc>
2849 inline rope<_CharT, _Alloc>
2850 operator+(const rope<_CharT, _Alloc>& __left,
2851 const _CharT* __right)
2853 typedef rope<_CharT, _Alloc> rope_type;
2854 std::size_t __rlen = rope_type::_S_char_ptr_len(__right);
2855 _Alloc __a = __left.get_allocator();
2856 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2857 __right, __rlen, __a));
2860 template <class _CharT, class _Alloc>
2861 inline rope<_CharT, _Alloc>&
2862 operator+=(rope<_CharT, _Alloc>& __left,
2863 const _CharT* __right)
2865 __left.append(__right);
2869 template <class _CharT, class _Alloc>
2870 inline rope<_CharT, _Alloc>
2871 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2873 typedef rope<_CharT, _Alloc> rope_type;
2874 _Alloc __a = __left.get_allocator();
2875 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2879 template <class _CharT, class _Alloc>
2880 inline rope<_CharT, _Alloc>&
2881 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2883 __left.append(__right);
2887 template <class _CharT, class _Alloc>
2889 operator<(const rope<_CharT, _Alloc>& __left,
2890 const rope<_CharT, _Alloc>& __right)
2891 { return __left.compare(__right) < 0; }
2893 template <class _CharT, class _Alloc>
2895 operator==(const rope<_CharT, _Alloc>& __left,
2896 const rope<_CharT, _Alloc>& __right)
2897 { return __left.compare(__right) == 0; }
2899 template <class _CharT, class _Alloc>
2901 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2902 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2903 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2905 template <class _CharT, class _Alloc>
2907 operator!=(const rope<_CharT, _Alloc>& __x,
2908 const rope<_CharT, _Alloc>& __y)
2909 { return !(__x == __y); }
2911 template <class _CharT, class _Alloc>
2913 operator>(const rope<_CharT, _Alloc>& __x,
2914 const rope<_CharT, _Alloc>& __y)
2915 { return __y < __x; }
2917 template <class _CharT, class _Alloc>
2919 operator<=(const rope<_CharT, _Alloc>& __x,
2920 const rope<_CharT, _Alloc>& __y)
2921 { return !(__y < __x); }
2923 template <class _CharT, class _Alloc>
2925 operator>=(const rope<_CharT, _Alloc>& __x,
2926 const rope<_CharT, _Alloc>& __y)
2927 { return !(__x < __y); }
2929 template <class _CharT, class _Alloc>
2931 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2932 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2933 { return !(__x == __y); }
2935 template<class _CharT, class _Traits, class _Alloc>
2936 std::basic_ostream<_CharT, _Traits>&
2937 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2938 const rope<_CharT, _Alloc>& __r);
2940 typedef rope<char> crope;
2941 typedef rope<wchar_t> wrope;
2943 inline crope::reference
2944 __mutable_reference_at(crope& __c, std::size_t __i)
2945 { return __c.mutable_reference_at(__i); }
2947 inline wrope::reference
2948 __mutable_reference_at(wrope& __c, std::size_t __i)
2949 { return __c.mutable_reference_at(__i); }
2951 template <class _CharT, class _Alloc>
2953 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2956 _GLIBCXX_END_NAMESPACE_VERSION
2960 namespace std _GLIBCXX_VISIBILITY(default)
2962 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2967 struct hash<__gnu_cxx::crope>
2970 operator()(const __gnu_cxx::crope& __str) const
2972 size_t __size = __str.size();
2975 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2981 struct hash<__gnu_cxx::wrope>
2984 operator()(const __gnu_cxx::wrope& __str) const
2986 size_t __size = __str.size();
2989 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2994 _GLIBCXX_END_NAMESPACE_VERSION
2997 # include <ext/ropeimpl.h>