1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * This file is a GNU extension to the Standard C++ Library (possibly
46 * containing extensions from the HP/SGI STL subset).
54 #include <bits/stl_construct.h>
55 #include <bits/stl_uninitialized.h>
56 #include <bits/stl_function.h>
57 #include <bits/stl_numeric.h>
58 #include <bits/allocator.h>
59 #include <ext/hash_fun.h>
60 #include <bits/gthr.h>
63 # define __GC_CONST const
65 # define __GC_CONST // constant except for deallocation
68 #include <ext/memory> // For uninitialized_copy_n
70 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
74 enum { _S_max_rope_depth = 45 };
75 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
76 } // namespace __detail
83 // The _S_eos function is used for those functions that
84 // convert to/from C-like strings to detect the end of the string.
86 // The end-of-C-string character.
87 // This is what the draft standard says it should be.
88 template <class _CharT>
93 // Test for basic character types.
94 // For basic character types leaves having a trailing eos.
95 template <class _CharT>
97 _S_is_basic_char_type(_CharT*)
100 template <class _CharT>
102 _S_is_one_byte_char_type(_CharT*)
106 _S_is_basic_char_type(char*)
110 _S_is_one_byte_char_type(char*)
114 _S_is_basic_char_type(wchar_t*)
117 // Store an eos iff _CharT is a basic character type.
118 // Do not reference _S_eos if it isn't.
119 template <class _CharT>
121 _S_cond_store_eos(_CharT&) { }
124 _S_cond_store_eos(char& __c)
128 _S_cond_store_eos(wchar_t& __c)
131 // char_producers are logically functions that generate a section of
132 // a string. These can be convereted to ropes. The resulting rope
133 // invokes the char_producer on demand. This allows, for example,
134 // files to be viewed as ropes without reading the entire file.
135 template <class _CharT>
139 virtual ~char_producer() { };
142 operator()(size_t __start_pos, size_t __len,
143 _CharT* __buffer) = 0;
144 // Buffer should really be an arbitrary output iterator.
145 // That way we could flatten directly into an ostream, etc.
146 // This is thoroughly impossible, since iterator types don't
147 // have runtime descriptions.
152 // Sequence must provide an append operation that appends an
153 // array to the sequence. Sequence buffers are useful only if
154 // appending an entire array is cheaper than appending element by element.
155 // This is true for many string representations.
156 // This should perhaps inherit from ostream<sequence::value_type>
157 // and be implemented correspondingly, so that they can be used
158 // for formatted. For the sake of portability, we don't do this yet.
160 // For now, sequence buffers behave as output iterators. But they also
161 // behave a little like basic_ostringstream<sequence::value_type> and a
162 // little like containers.
164 template<class _Sequence, size_t _Buf_sz = 100>
165 class sequence_buffer
166 : public std::iterator<std::output_iterator_tag, void, void, void, void>
169 typedef typename _Sequence::value_type value_type;
171 _Sequence* _M_prefix;
172 value_type _M_buffer[_Buf_sz];
179 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
187 : _M_prefix(0), _M_buf_count(0) { }
189 sequence_buffer(const sequence_buffer& __x)
191 _M_prefix = __x._M_prefix;
192 _M_buf_count = __x._M_buf_count;
193 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
196 sequence_buffer(sequence_buffer& __x)
199 _M_prefix = __x._M_prefix;
203 sequence_buffer(_Sequence& __s)
204 : _M_prefix(&__s), _M_buf_count(0) { }
207 operator=(sequence_buffer& __x)
210 _M_prefix = __x._M_prefix;
216 operator=(const sequence_buffer& __x)
218 _M_prefix = __x._M_prefix;
219 _M_buf_count = __x._M_buf_count;
220 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
225 push_back(value_type __x)
227 if (_M_buf_count < _Buf_sz)
229 _M_buffer[_M_buf_count] = __x;
241 append(value_type* __s, size_t __len)
243 if (__len + _M_buf_count <= _Buf_sz)
245 size_t __i = _M_buf_count;
246 for (size_t __j = 0; __j < __len; __i++, __j++)
247 _M_buffer[__i] = __s[__j];
248 _M_buf_count += __len;
250 else if (0 == _M_buf_count)
251 _M_prefix->append(__s, __s + __len);
260 write(value_type* __s, size_t __len)
274 operator=(const value_type& __rhs)
293 // The following should be treated as private, at least for now.
294 template<class _CharT>
295 class _Rope_char_consumer
298 // If we had member templates, these should not be virtual.
299 // For now we need to use run-time parametrization where
300 // compile-time would do. Hence this should all be private
302 // The symmetry with char_producer is accidental and temporary.
303 virtual ~_Rope_char_consumer() { };
306 operator()(const _CharT* __buffer, size_t __len) = 0;
309 // First a lot of forward declarations. The standard seems to require
310 // much stricter "declaration before use" than many of the implementations
312 template<class _CharT, class _Alloc = allocator<_CharT> >
315 template<class _CharT, class _Alloc>
316 struct _Rope_RopeConcatenation;
318 template<class _CharT, class _Alloc>
319 struct _Rope_RopeLeaf;
321 template<class _CharT, class _Alloc>
322 struct _Rope_RopeFunction;
324 template<class _CharT, class _Alloc>
325 struct _Rope_RopeSubstring;
327 template<class _CharT, class _Alloc>
328 class _Rope_iterator;
330 template<class _CharT, class _Alloc>
331 class _Rope_const_iterator;
333 template<class _CharT, class _Alloc>
334 class _Rope_char_ref_proxy;
336 template<class _CharT, class _Alloc>
337 class _Rope_char_ptr_proxy;
339 template<class _CharT, class _Alloc>
341 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
342 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
344 template<class _CharT, class _Alloc>
345 _Rope_const_iterator<_CharT, _Alloc>
346 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
349 template<class _CharT, class _Alloc>
350 _Rope_const_iterator<_CharT, _Alloc>
351 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
354 template<class _CharT, class _Alloc>
355 _Rope_const_iterator<_CharT, _Alloc>
356 operator+(ptrdiff_t __n,
357 const _Rope_const_iterator<_CharT, _Alloc>& __x);
359 template<class _CharT, class _Alloc>
361 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
362 const _Rope_const_iterator<_CharT, _Alloc>& __y);
364 template<class _CharT, class _Alloc>
366 operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
367 const _Rope_const_iterator<_CharT, _Alloc>& __y);
369 template<class _CharT, class _Alloc>
371 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
372 const _Rope_const_iterator<_CharT, _Alloc>& __y);
374 template<class _CharT, class _Alloc>
375 _Rope_iterator<_CharT, _Alloc>
376 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
378 template<class _CharT, class _Alloc>
379 _Rope_iterator<_CharT, _Alloc>
380 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
382 template<class _CharT, class _Alloc>
383 _Rope_iterator<_CharT, _Alloc>
384 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
386 template<class _CharT, class _Alloc>
388 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
389 const _Rope_iterator<_CharT, _Alloc>& __y);
391 template<class _CharT, class _Alloc>
393 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
394 const _Rope_iterator<_CharT, _Alloc>& __y);
396 template<class _CharT, class _Alloc>
398 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
399 const _Rope_iterator<_CharT, _Alloc>& __y);
401 template<class _CharT, class _Alloc>
403 operator+(const rope<_CharT, _Alloc>& __left,
404 const rope<_CharT, _Alloc>& __right);
406 template<class _CharT, class _Alloc>
408 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
410 template<class _CharT, class _Alloc>
412 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
414 // Some helpers, so we can use power on ropes.
415 // See below for why this isn't local to the implementation.
417 // This uses a nonstandard refcount convention.
418 // The result has refcount 0.
419 template<class _CharT, class _Alloc>
420 struct _Rope_Concat_fn
421 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
422 rope<_CharT, _Alloc> >
425 operator()(const rope<_CharT, _Alloc>& __x,
426 const rope<_CharT, _Alloc>& __y)
427 { return __x + __y; }
430 template <class _CharT, class _Alloc>
431 inline rope<_CharT, _Alloc>
432 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
433 { return rope<_CharT, _Alloc>(); }
435 // Class _Refcount_Base provides a type, _RC_t, a data member,
436 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
437 // atomic preincrement/predecrement. The constructor initializes
439 struct _Refcount_Base
442 typedef size_t _RC_t;
444 // The data member _M_ref_count
445 volatile _RC_t _M_ref_count;
448 __gthread_mutex_t _M_ref_count_lock;
450 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
452 #ifdef __GTHREAD_MUTEX_INIT
453 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
454 _M_ref_count_lock = __tmp;
455 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
456 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
458 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
465 __gthread_mutex_lock(&_M_ref_count_lock);
467 __gthread_mutex_unlock(&_M_ref_count_lock);
473 __gthread_mutex_lock(&_M_ref_count_lock);
474 volatile _RC_t __tmp = --_M_ref_count;
475 __gthread_mutex_unlock(&_M_ref_count_lock);
481 // What follows should really be local to rope. Unfortunately,
482 // that doesn't work, since it makes it impossible to define generic
483 // equality on rope iterators. According to the draft standard, the
484 // template parameters for such an equality operator cannot be inferred
485 // from the occurrence of a member class as a parameter.
486 // (SGI compilers in fact allow this, but the __result wouldn't be
488 // Similarly, some of the static member functions are member functions
489 // only to avoid polluting the global namespace, and to circumvent
490 // restrictions on type inference for template functions.
494 // The internal data structure for representing a rope. This is
495 // private to the implementation. A rope is really just a pointer
498 // A few basic functions for manipulating this data structure
499 // are members of _RopeRep. Most of the more complex algorithms
500 // are implemented as rope members.
502 // Some of the static member functions of _RopeRep have identically
503 // named functions in rope that simply invoke the _RopeRep versions.
505 #define __ROPE_DEFINE_ALLOCS(__a) \
506 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
507 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
508 __ROPE_DEFINE_ALLOC(__C,_C) \
509 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
510 __ROPE_DEFINE_ALLOC(__L,_L) \
511 typedef _Rope_RopeFunction<_CharT,__a> __F; \
512 __ROPE_DEFINE_ALLOC(__F,_F) \
513 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
514 __ROPE_DEFINE_ALLOC(__S,_S)
516 // Internal rope nodes potentially store a copy of the allocator
517 // instance used to allocate them. This is mostly redundant.
518 // But the alternative would be to pass allocator instances around
519 // in some form to nearly all internal functions, since any pointer
520 // assignment may result in a zero reference count and thus require
523 #define __STATIC_IF_SGI_ALLOC /* not static */
525 template <class _CharT, class _Alloc>
526 struct _Rope_rep_base
529 typedef _Alloc allocator_type;
532 get_allocator() const
533 { return *static_cast<const _Alloc*>(this); }
537 { return *static_cast<_Alloc*>(this); }
539 const allocator_type&
540 _M_get_allocator() const
541 { return *static_cast<const _Alloc*>(this); }
543 _Rope_rep_base(size_t __size, const allocator_type&)
544 : _M_size(__size) { }
548 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
550 _Alloc::template rebind<_Tp>::other __name##Alloc; \
551 static _Tp* __name##_allocate(size_t __n) \
552 { return __name##Alloc().allocate(__n); } \
553 static void __name##_deallocate(_Tp *__p, size_t __n) \
554 { __name##Alloc().deallocate(__p, __n); }
555 __ROPE_DEFINE_ALLOCS(_Alloc)
556 # undef __ROPE_DEFINE_ALLOC
559 template<class _CharT, class _Alloc>
561 : public _Rope_rep_base<_CharT, _Alloc>
567 __detail::_Tag _M_tag:8;
568 bool _M_is_balanced:8;
569 unsigned char _M_depth;
570 __GC_CONST _CharT* _M_c_string;
571 __gthread_mutex_t _M_c_string_lock;
572 /* Flattened version of string, if needed. */
574 /* If it's not 0, then the memory is owned */
576 /* In the case of a leaf, this may point to */
577 /* the same memory as the data field. */
578 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
581 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
582 using _Rope_rep_base<_CharT, _Alloc>::_M_get_allocator;
584 _Rope_RopeRep(__detail::_Tag __t, int __d, bool __b, size_t __size,
585 const allocator_type& __a)
586 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
590 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
591 #ifdef __GTHREAD_MUTEX_INIT
593 // Do not copy a POSIX/gthr mutex once in use. However, bits are bits.
594 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
595 _M_c_string_lock = __tmp;
598 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
605 _S_free_string(__GC_CONST _CharT*, size_t __len,
606 allocator_type& __a);
607 #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
608 // Deallocate data section of a leaf.
609 // This shouldn't be a member function.
610 // But its hard to do anything else at the
611 // moment, because it's templatized w.r.t.
613 // Does nothing if __GC is defined.
615 void _M_free_c_string();
617 // Deallocate t. Assumes t is not 0.
630 _S_unref(_Rope_RopeRep* __t)
633 __t->_M_unref_nonnil();
637 _S_ref(_Rope_RopeRep* __t)
644 _S_free_if_unref(_Rope_RopeRep* __t)
646 if (0 != __t && 0 == __t->_M_ref_count)
650 void _M_unref_nonnil() { }
651 void _M_ref_nonnil() { }
652 static void _S_unref(_Rope_RopeRep*) { }
653 static void _S_ref(_Rope_RopeRep*) { }
654 static void _S_free_if_unref(_Rope_RopeRep*) { }
658 operator=(const _Rope_RopeRep&);
660 _Rope_RopeRep(const _Rope_RopeRep&);
663 template<class _CharT, class _Alloc>
664 struct _Rope_RopeLeaf
665 : public _Rope_RopeRep<_CharT, _Alloc>
668 // Apparently needed by VC++
669 // The data fields of leaves are allocated with some
670 // extra space, to accommodate future growth and for basic
671 // character types, to hold a trailing eos character.
672 enum { _S_alloc_granularity = 8 };
675 _S_rounded_up_size(size_t __n)
677 size_t __size_with_eos;
679 if (_S_is_basic_char_type((_CharT*)0))
680 __size_with_eos = __n + 1;
682 __size_with_eos = __n;
684 return __size_with_eos;
686 // Allow slop for in-place expansion.
687 return ((__size_with_eos + size_t(_S_alloc_granularity) - 1)
688 &~ (size_t(_S_alloc_granularity) - 1));
691 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
692 /* The allocated size is */
693 /* _S_rounded_up_size(size), except */
694 /* in the GC case, in which it */
695 /* doesn't matter. */
696 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
699 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size,
700 const allocator_type& __a)
701 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_leaf, 0, true,
702 __size, __a), _M_data(__d)
704 if (_S_is_basic_char_type((_CharT *)0))
706 // already eos terminated.
707 this->_M_c_string = __d;
710 // The constructor assumes that d has been allocated with
711 // the proper allocator and the properly padded size.
712 // In contrast, the destructor deallocates the data:
714 ~_Rope_RopeLeaf() throw()
716 if (_M_data != this->_M_c_string)
717 this->_M_free_c_string();
719 __STL_FREE_STRING(_M_data, this->_M_size, this->_M_get_allocator());
724 operator=(const _Rope_RopeLeaf&);
726 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
729 template<class _CharT, class _Alloc>
730 struct _Rope_RopeConcatenation
731 : public _Rope_RopeRep<_CharT, _Alloc>
734 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
735 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
737 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
740 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
741 _Rope_RopeRep<_CharT, _Alloc>* __r,
742 const allocator_type& __a)
743 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_concat,
744 std::max(__l->_M_depth,
747 __l->_M_size + __r->_M_size, __a),
748 _M_left(__l), _M_right(__r)
751 ~_Rope_RopeConcatenation() throw()
753 this->_M_free_c_string();
754 _M_left->_M_unref_nonnil();
755 _M_right->_M_unref_nonnil();
759 _Rope_RopeConcatenation&
760 operator=(const _Rope_RopeConcatenation&);
762 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
765 template<class _CharT, class _Alloc>
766 struct _Rope_RopeFunction
767 : public _Rope_RopeRep<_CharT, _Alloc>
770 char_producer<_CharT>* _M_fn;
772 bool _M_delete_when_done; // Char_producer is owned by the
773 // rope and should be explicitly
774 // deleted when the rope becomes
777 // In the GC case, we either register the rope for
778 // finalization, or not. Thus the field is unnecessary;
779 // the information is stored in the collector data structures.
780 // We do need a finalization procedure to be invoked by the
783 _S_fn_finalization_proc(void * __tree, void *)
784 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
786 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
789 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
790 bool __d, const allocator_type& __a)
791 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_function, 0, true, __size, __a)
794 , _M_delete_when_done(__d)
800 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
801 _S_fn_finalization_proc, 0, 0, 0);
806 ~_Rope_RopeFunction() throw()
808 this->_M_free_c_string();
809 if (_M_delete_when_done)
815 operator=(const _Rope_RopeFunction&);
817 _Rope_RopeFunction(const _Rope_RopeFunction&);
819 // Substring results are usually represented using just
820 // concatenation nodes. But in the case of very long flat ropes
821 // or ropes with a functional representation that isn't practical.
822 // In that case, we represent the __result as a special case of
823 // RopeFunction, whose char_producer points back to the rope itself.
824 // In all cases except repeated substring operations and
825 // deallocation, we treat the __result as a RopeFunction.
826 template<class _CharT, class _Alloc>
827 struct _Rope_RopeSubstring
828 : public _Rope_RopeFunction<_CharT, _Alloc>,
829 public char_producer<_CharT>
832 // XXX this whole class should be rewritten.
833 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
837 operator()(size_t __start_pos, size_t __req_len,
840 switch(_M_base->_M_tag)
842 case __detail::_S_function:
843 case __detail::_S_substringfn:
845 char_producer<_CharT>* __fn =
846 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
847 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
850 case __detail::_S_leaf:
852 __GC_CONST _CharT* __s =
853 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
854 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
863 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
866 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_t __s,
867 size_t __l, const allocator_type& __a)
868 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
869 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
872 _M_base->_M_ref_nonnil();
874 this->_M_tag = __detail::_S_substringfn;
876 virtual ~_Rope_RopeSubstring() throw()
879 _M_base->_M_unref_nonnil();
880 // _M_free_c_string(); -- done by parent class
885 // Self-destructing pointers to Rope_rep.
886 // These are not conventional smart pointers. Their
887 // only purpose in life is to ensure that unref is called
888 // on the pointer either at normal exit or if an exception
889 // is raised. It is the caller's responsibility to
890 // adjust reference counts when these pointers are initialized
891 // or assigned to. (This convention significantly reduces
892 // the number of potentially expensive reference count
895 template<class _CharT, class _Alloc>
896 struct _Rope_self_destruct_ptr
898 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
900 ~_Rope_self_destruct_ptr()
901 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
903 _Rope_self_destruct_ptr() : _M_ptr(0) { };
905 _Rope_self_destruct_ptr() { };
907 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
910 _Rope_RopeRep<_CharT, _Alloc>&
914 _Rope_RopeRep<_CharT, _Alloc>*
918 operator _Rope_RopeRep<_CharT, _Alloc>*()
921 _Rope_self_destruct_ptr&
922 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
923 { _M_ptr = __x; return *this; }
927 // Dereferencing a nonconst iterator has to return something
928 // that behaves almost like a reference. It's not possible to
929 // return an actual reference since assignment requires extra
930 // work. And we would get into the same problems as with the
931 // CD2 version of basic_string.
932 template<class _CharT, class _Alloc>
933 class _Rope_char_ref_proxy
935 friend class rope<_CharT, _Alloc>;
936 friend class _Rope_iterator<_CharT, _Alloc>;
937 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
939 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
941 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
943 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
944 typedef rope<_CharT, _Alloc> _My_rope;
947 bool _M_current_valid;
948 _My_rope* _M_root; // The whole rope.
950 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
951 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) { }
953 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
954 : _M_pos(__x._M_pos), _M_current(__x._M_current),
955 _M_current_valid(false), _M_root(__x._M_root) { }
957 // Don't preserve cache if the reference can outlive the
958 // expression. We claim that's not possible without calling
959 // a copy constructor or generating reference to a proxy
960 // reference. We declare the latter to have undefined semantics.
961 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
962 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) { }
964 inline operator _CharT () const;
966 _Rope_char_ref_proxy&
967 operator=(_CharT __c);
969 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
971 _Rope_char_ref_proxy&
972 operator=(const _Rope_char_ref_proxy& __c)
973 { return operator=((_CharT)__c); }
976 template<class _CharT, class __Alloc>
978 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
979 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
986 template<class _CharT, class _Alloc>
987 class _Rope_char_ptr_proxy
989 // XXX this class should be rewritten.
990 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
992 rope<_CharT,_Alloc>* _M_root; // The whole rope.
994 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
995 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
997 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
998 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
1000 _Rope_char_ptr_proxy() { }
1002 _Rope_char_ptr_proxy(_CharT* __x)
1003 : _M_root(0), _M_pos(0) { }
1005 _Rope_char_ptr_proxy&
1006 operator=(const _Rope_char_ptr_proxy& __x)
1008 _M_pos = __x._M_pos;
1009 _M_root = __x._M_root;
1013 template<class _CharT2, class _Alloc2>
1015 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1016 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1018 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1019 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1023 // Unlike in the C version, we cache only part of the stack
1024 // for rope iterators, since they must be efficiently copyable.
1025 // When we run out of cache, we have to reconstruct the iterator
1027 // Pointers from iterators are not included in reference counts.
1028 // Iterators are assumed to be thread private. Ropes can
1031 template<class _CharT, class _Alloc>
1032 class _Rope_iterator_base
1033 : public std::iterator<std::random_access_iterator_tag, _CharT>
1035 friend class rope<_CharT, _Alloc>;
1037 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1038 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1039 // Borland doesn't want this to be protected.
1041 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1042 enum { _S_iterator_buf_len = 15 };
1043 size_t _M_current_pos;
1044 _RopeRep* _M_root; // The whole rope.
1045 size_t _M_leaf_pos; // Starting position for current leaf
1046 __GC_CONST _CharT* _M_buf_start;
1048 // containing current char.
1049 __GC_CONST _CharT* _M_buf_ptr;
1050 // Pointer to current char in buffer.
1051 // != 0 ==> buffer valid.
1052 __GC_CONST _CharT* _M_buf_end;
1053 // One past __last valid char in buffer.
1054 // What follows is the path cache. We go out of our
1055 // way to make this compact.
1056 // Path_end contains the bottom section of the path from
1057 // the root to the current leaf.
1058 const _RopeRep* _M_path_end[_S_path_cache_len];
1059 int _M_leaf_index; // Last valid __pos in path_end;
1060 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1061 // point to concatenation nodes.
1062 unsigned char _M_path_directions;
1063 // (path_directions >> __i) & 1 is 1
1064 // iff we got from _M_path_end[leaf_index - __i - 1]
1065 // to _M_path_end[leaf_index - __i] by going to the
1066 // __right. Assumes path_cache_len <= 9.
1067 _CharT _M_tmp_buf[_S_iterator_buf_len];
1068 // Short buffer for surrounding chars.
1069 // This is useful primarily for
1070 // RopeFunctions. We put the buffer
1071 // here to avoid locking in the
1072 // multithreaded case.
1073 // The cached path is generally assumed to be valid
1074 // only if the buffer is valid.
1075 static void _S_setbuf(_Rope_iterator_base& __x);
1076 // Set buffer contents given
1078 static void _S_setcache(_Rope_iterator_base& __x);
1079 // Set buffer contents and
1081 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1082 // As above, but assumes path
1083 // cache is valid for previous posn.
1084 _Rope_iterator_base() { }
1086 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
1087 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) { }
1089 void _M_incr(size_t __n);
1090 void _M_decr(size_t __n);
1094 { return _M_current_pos; }
1096 _Rope_iterator_base(const _Rope_iterator_base& __x)
1098 if (0 != __x._M_buf_ptr)
1102 _M_current_pos = __x._M_current_pos;
1103 _M_root = __x._M_root;
1109 template<class _CharT, class _Alloc>
1110 class _Rope_iterator;
1112 template<class _CharT, class _Alloc>
1113 class _Rope_const_iterator
1114 : public _Rope_iterator_base<_CharT, _Alloc>
1116 friend class rope<_CharT, _Alloc>;
1118 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1119 // The one from the base class may not be directly visible.
1120 _Rope_const_iterator(const _RopeRep* __root, size_t __pos)
1121 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1123 // Only nonconst iterators modify root ref count
1126 typedef _CharT reference; // Really a value. Returning a reference
1127 // Would be a mess, since it would have
1128 // to be included in refcount.
1129 typedef const _CharT* pointer;
1132 _Rope_const_iterator() { };
1134 _Rope_const_iterator(const _Rope_const_iterator& __x)
1135 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1137 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1139 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, size_t __pos)
1140 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1142 _Rope_const_iterator&
1143 operator=(const _Rope_const_iterator& __x)
1145 if (0 != __x._M_buf_ptr)
1146 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1149 this->_M_current_pos = __x._M_current_pos;
1150 this->_M_root = __x._M_root;
1151 this->_M_buf_ptr = 0;
1159 if (0 == this->_M_buf_ptr)
1161 return *this->_M_buf_ptr;
1164 // Without this const version, Rope iterators do not meet the
1165 // requirements of an Input Iterator.
1169 return *const_cast<_Rope_const_iterator&>(*this);
1172 _Rope_const_iterator&
1175 __GC_CONST _CharT* __next;
1176 if (0 != this->_M_buf_ptr
1177 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1179 this->_M_buf_ptr = __next;
1180 ++this->_M_current_pos;
1187 _Rope_const_iterator&
1188 operator+=(ptrdiff_t __n)
1193 this->_M_decr(-__n);
1197 _Rope_const_iterator&
1204 _Rope_const_iterator&
1205 operator-=(ptrdiff_t __n)
1210 this->_M_incr(-__n);
1214 _Rope_const_iterator
1217 size_t __old_pos = this->_M_current_pos;
1219 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1220 // This makes a subsequent dereference expensive.
1221 // Perhaps we should instead copy the iterator
1222 // if it has a valid cache?
1225 _Rope_const_iterator
1228 size_t __old_pos = this->_M_current_pos;
1230 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1233 template<class _CharT2, class _Alloc2>
1234 friend _Rope_const_iterator<_CharT2, _Alloc2>
1235 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1238 template<class _CharT2, class _Alloc2>
1239 friend _Rope_const_iterator<_CharT2, _Alloc2>
1240 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1243 template<class _CharT2, class _Alloc2>
1244 friend _Rope_const_iterator<_CharT2, _Alloc2>
1245 operator+(ptrdiff_t __n,
1246 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1249 operator[](size_t __n)
1250 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1251 this->_M_current_pos + __n); }
1253 template<class _CharT2, class _Alloc2>
1255 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1256 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1258 template<class _CharT2, class _Alloc2>
1260 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1261 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1263 template<class _CharT2, class _Alloc2>
1265 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1266 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1269 template<class _CharT, class _Alloc>
1270 class _Rope_iterator
1271 : public _Rope_iterator_base<_CharT, _Alloc>
1273 friend class rope<_CharT, _Alloc>;
1275 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1276 rope<_CharT, _Alloc>* _M_root_rope;
1278 // root is treated as a cached version of this, and is used to
1279 // detect changes to the underlying rope.
1281 // Root is included in the reference count. This is necessary
1282 // so that we can detect changes reliably. Unfortunately, it
1283 // requires careful bookkeeping for the nonGC case.
1284 _Rope_iterator(rope<_CharT, _Alloc>* __r, size_t __pos)
1285 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1287 { _RopeRep::_S_ref(this->_M_root);
1288 if (!(__r -> empty()))
1294 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1295 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1297 rope<_CharT, _Alloc>&
1299 { return *_M_root_rope; }
1303 this->_M_root = 0; // Needed for reference counting.
1306 _Rope_iterator(const _Rope_iterator& __x)
1307 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1309 _M_root_rope = __x._M_root_rope;
1310 _RopeRep::_S_ref(this->_M_root);
1313 _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos);
1316 { _RopeRep::_S_unref(this->_M_root); }
1319 operator=(const _Rope_iterator& __x)
1321 _RopeRep* __old = this->_M_root;
1323 _RopeRep::_S_ref(__x._M_root);
1324 if (0 != __x._M_buf_ptr)
1326 _M_root_rope = __x._M_root_rope;
1327 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1331 this->_M_current_pos = __x._M_current_pos;
1332 this->_M_root = __x._M_root;
1333 _M_root_rope = __x._M_root_rope;
1334 this->_M_buf_ptr = 0;
1336 _RopeRep::_S_unref(__old);
1344 if (0 == this->_M_buf_ptr)
1345 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1346 this->_M_current_pos);
1348 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1349 this->_M_current_pos,
1353 // See above comment.
1357 return *const_cast<_Rope_iterator&>(*this);
1368 operator+=(ptrdiff_t __n)
1373 this->_M_decr(-__n);
1385 operator-=(ptrdiff_t __n)
1390 this->_M_incr(-__n);
1397 size_t __old_pos = this->_M_current_pos;
1399 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1405 size_t __old_pos = this->_M_current_pos;
1407 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1411 operator[](ptrdiff_t __n)
1412 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1413 this->_M_current_pos
1416 template<class _CharT2, class _Alloc2>
1418 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1419 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1421 template<class _CharT2, class _Alloc2>
1423 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1424 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1426 template<class _CharT2, class _Alloc2>
1428 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1429 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1431 template<class _CharT2, class _Alloc2>
1432 friend _Rope_iterator<_CharT2, _Alloc2>
1433 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1435 template<class _CharT2, class _Alloc2>
1436 friend _Rope_iterator<_CharT2, _Alloc2>
1437 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1439 template<class _CharT2, class _Alloc2>
1440 friend _Rope_iterator<_CharT2, _Alloc2>
1441 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT2, _Alloc2>& __x);
1445 template <class _CharT, class _Alloc>
1449 typedef _Alloc allocator_type;
1452 get_allocator() const
1453 { return *static_cast<const _Alloc*>(this); }
1457 { return *static_cast<_Alloc*>(this); }
1459 const allocator_type&
1460 _M_get_allocator() const
1461 { return *static_cast<const _Alloc*>(this); }
1463 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1464 // The one in _Base may not be visible due to template rules.
1466 _Rope_base(_RopeRep* __t, const allocator_type&)
1467 : _M_tree_ptr(__t) { }
1469 _Rope_base(const allocator_type&) { }
1471 // The only data member of a rope:
1472 _RopeRep *_M_tree_ptr;
1474 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1476 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1477 static _Tp* __name##_allocate(size_t __n) \
1478 { return __name##Alloc().allocate(__n); } \
1479 static void __name##_deallocate(_Tp *__p, size_t __n) \
1480 { __name##Alloc().deallocate(__p, __n); }
1481 __ROPE_DEFINE_ALLOCS(_Alloc)
1482 #undef __ROPE_DEFINE_ALLOC
1486 operator=(const _Rope_base&);
1488 _Rope_base(const _Rope_base&);
1492 * This is an SGI extension.
1493 * @ingroup SGIextensions
1496 template <class _CharT, class _Alloc>
1497 class rope : public _Rope_base<_CharT, _Alloc>
1500 typedef _CharT value_type;
1501 typedef ptrdiff_t difference_type;
1502 typedef size_t size_type;
1503 typedef _CharT const_reference;
1504 typedef const _CharT* const_pointer;
1505 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1506 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1507 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1508 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1510 friend class _Rope_iterator<_CharT, _Alloc>;
1511 friend class _Rope_const_iterator<_CharT, _Alloc>;
1512 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1513 friend class _Rope_iterator_base<_CharT, _Alloc>;
1514 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1515 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1516 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1519 typedef _Rope_base<_CharT, _Alloc> _Base;
1520 typedef typename _Base::allocator_type allocator_type;
1521 using _Base::_M_tree_ptr;
1522 using _Base::get_allocator;
1523 using _Base::_M_get_allocator;
1524 typedef __GC_CONST _CharT* _Cstrptr;
1526 static _CharT _S_empty_c_str[1];
1530 { return __c == _S_eos((_CharT*)0); }
1532 enum { _S_copy_max = 23 };
1533 // For strings shorter than _S_copy_max, we copy to
1536 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1537 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1538 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1539 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1540 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1542 // Retrieve a character at the indicated position.
1543 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1546 // Obtain a pointer to the character at the indicated position.
1547 // The pointer can be used to change the character.
1548 // If such a pointer cannot be produced, as is frequently the
1549 // case, 0 is returned instead.
1550 // (Returns nonzero only if all nodes in the path have a refcount
1552 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1556 _S_apply_to_pieces(// should be template parameter
1557 _Rope_char_consumer<_CharT>& __c,
1558 const _RopeRep* __r,
1559 size_t __begin, size_t __end);
1560 // begin and end are assumed to be in range.
1564 _S_unref(_RopeRep* __t)
1565 { _RopeRep::_S_unref(__t); }
1568 _S_ref(_RopeRep* __t)
1569 { _RopeRep::_S_ref(__t); }
1572 static void _S_unref(_RopeRep*) { }
1573 static void _S_ref(_RopeRep*) { }
1577 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1579 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1582 // _Result is counted in refcount.
1583 static _RopeRep* _S_substring(_RopeRep* __base,
1584 size_t __start, size_t __endp1);
1586 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1587 const _CharT* __iter, size_t __slen);
1588 // Concatenate rope and char ptr, copying __s.
1589 // Should really take an arbitrary iterator.
1590 // Result is counted in refcount.
1591 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1592 const _CharT* __iter,
1594 // As above, but one reference to __r is about to be
1595 // destroyed. Thus the pieces may be recycled if all
1596 // relevant reference counts are 1.
1598 // We can't really do anything since refcounts are unavailable.
1599 { return _S_concat_char_iter(__r, __iter, __slen); }
1604 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1605 // General concatenation on _RopeRep. _Result
1606 // has refcount of 1. Adjusts argument refcounts.
1610 apply_to_pieces(size_t __begin, size_t __end,
1611 _Rope_char_consumer<_CharT>& __c) const
1612 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1617 _S_rounded_up_size(size_t __n)
1618 { return _RopeLeaf::_S_rounded_up_size(__n); }
1621 _S_allocated_capacity(size_t __n)
1623 if (_S_is_basic_char_type((_CharT*)0))
1624 return _S_rounded_up_size(__n) - 1;
1626 return _S_rounded_up_size(__n);
1630 // Allocate and construct a RopeLeaf using the supplied allocator
1631 // Takes ownership of s instead of copying.
1633 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1634 size_t __size, allocator_type& __a)
1636 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1637 return new(__space) _RopeLeaf(__s, __size, __a);
1640 static _RopeConcatenation*
1641 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1642 allocator_type& __a)
1644 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1645 return new(__space) _RopeConcatenation(__left, __right, __a);
1648 static _RopeFunction*
1649 _S_new_RopeFunction(char_producer<_CharT>* __f,
1650 size_t __size, bool __d, allocator_type& __a)
1652 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1653 return new(__space) _RopeFunction(__f, __size, __d, __a);
1656 static _RopeSubstring*
1657 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1658 size_t __l, allocator_type& __a)
1660 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1661 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1665 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1666 size_t __size, allocator_type& __a)
1667 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1668 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1672 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1674 __uninitialized_copy_n_a(__s, __size, __buf, __a);
1675 _S_cond_store_eos(__buf[__size]);
1677 { return _S_new_RopeLeaf(__buf, __size, __a); }
1680 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1681 __throw_exception_again;
1685 // Concatenation of nonempty strings.
1686 // Always builds a concatenation node.
1687 // Rebalances if the result is too deep.
1688 // Result has refcount 1.
1689 // Does not increment left and right ref counts even though
1690 // they are referenced.
1692 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1694 // Concatenation helper functions
1696 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1697 const _CharT* __iter, size_t __slen);
1698 // Concatenate by copying leaf.
1699 // should take an arbitrary iterator
1700 // result has refcount 1.
1703 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1704 const _CharT* __iter, size_t __slen);
1705 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1710 static size_t _S_char_ptr_len(const _CharT* __s);
1711 // slightly generalized strlen
1713 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1714 : _Base(__t, __a) { }
1717 // Copy __r to the _CharT buffer.
1718 // Returns __buffer + __r->_M_size.
1719 // Assumes that buffer is uninitialized.
1720 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1722 // Again, with explicit starting position and length.
1723 // Assumes that buffer is uninitialized.
1724 static _CharT* _S_flatten(_RopeRep* __r,
1725 size_t __start, size_t __len,
1728 static const unsigned long
1729 _S_min_len[__detail::_S_max_rope_depth + 1];
1732 _S_is_balanced(_RopeRep* __r)
1733 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1736 _S_is_almost_balanced(_RopeRep* __r)
1737 { return (__r->_M_depth == 0
1738 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1741 _S_is_roughly_balanced(_RopeRep* __r)
1742 { return (__r->_M_depth <= 1
1743 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1745 // Assumes the result is not empty.
1747 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1749 _RopeRep* __result = _S_concat(__left, __right);
1750 if (_S_is_balanced(__result))
1751 __result->_M_is_balanced = true;
1755 // The basic rebalancing operation. Logically copies the
1756 // rope. The result has refcount of 1. The client will
1757 // usually decrement the reference count of __r.
1758 // The result is within height 2 of balanced by the above
1760 static _RopeRep* _S_balance(_RopeRep* __r);
1762 // Add all unbalanced subtrees to the forest of balanceed trees.
1763 // Used only by balance.
1764 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1766 // Add __r to forest, assuming __r is already balanced.
1767 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1769 // Print to stdout, exposing structure
1770 static void _S_dump(_RopeRep* __r, int __indent = 0);
1772 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1773 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1778 { return 0 == this->_M_tree_ptr; }
1780 // Comparison member function. This is public only for those
1781 // clients that need a ternary comparison. Others
1782 // should use the comparison operators below.
1784 compare(const rope& __y) const
1785 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1787 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1791 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1792 _M_get_allocator());
1795 rope(const _CharT* __s, size_t __len,
1796 const allocator_type& __a = allocator_type())
1800 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, _M_get_allocator());
1803 // Should perhaps be templatized with respect to the iterator type
1804 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1806 rope(const _CharT* __s, const _CharT* __e,
1807 const allocator_type& __a = allocator_type())
1811 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, _M_get_allocator());
1814 rope(const const_iterator& __s, const const_iterator& __e,
1815 const allocator_type& __a = allocator_type())
1816 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1817 __e._M_current_pos), __a)
1820 rope(const iterator& __s, const iterator& __e,
1821 const allocator_type& __a = allocator_type())
1822 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1823 __e._M_current_pos), __a)
1826 rope(_CharT __c, const allocator_type& __a = allocator_type())
1829 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1831 _M_get_allocator().construct(__buf, __c);
1834 this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1,
1835 _M_get_allocator());
1839 _RopeRep::__STL_FREE_STRING(__buf, 1, _M_get_allocator());
1840 __throw_exception_again;
1844 rope(size_t __n, _CharT __c,
1845 const allocator_type& __a = allocator_type());
1847 rope(const allocator_type& __a = allocator_type())
1850 // Construct a rope from a function that can compute its members
1851 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1852 const allocator_type& __a = allocator_type())
1855 this->_M_tree_ptr = (0 == __len) ?
1856 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1859 rope(const rope& __x, const allocator_type& __a = allocator_type())
1860 : _Base(__x._M_tree_ptr, __a)
1861 { _S_ref(this->_M_tree_ptr); }
1864 { _S_unref(this->_M_tree_ptr); }
1867 operator=(const rope& __x)
1869 _RopeRep* __old = this->_M_tree_ptr;
1870 this->_M_tree_ptr = __x._M_tree_ptr;
1871 _S_ref(this->_M_tree_ptr);
1879 _S_unref(this->_M_tree_ptr);
1880 this->_M_tree_ptr = 0;
1884 push_back(_CharT __x)
1886 _RopeRep* __old = this->_M_tree_ptr;
1888 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1895 _RopeRep* __old = this->_M_tree_ptr;
1896 this->_M_tree_ptr = _S_substring(this->_M_tree_ptr,
1897 0, this->_M_tree_ptr->_M_size - 1);
1903 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1906 push_front(_CharT __x)
1908 _RopeRep* __old = this->_M_tree_ptr;
1910 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, _M_get_allocator());
1913 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1920 __throw_exception_again;
1927 _RopeRep* __old = this->_M_tree_ptr;
1929 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1935 { return _S_fetch(this->_M_tree_ptr, 0); }
1940 _RopeRep* __old = this->_M_tree_ptr;
1941 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1946 copy(_CharT* __buffer) const
1948 _Destroy(__buffer, __buffer + size(), _M_get_allocator());
1949 _S_flatten(this->_M_tree_ptr, __buffer);
1952 // This is the copy function from the standard, but
1953 // with the arguments reordered to make it consistent with the
1954 // rest of the interface.
1955 // Note that this guaranteed not to compile if the draft standard
1956 // order is assumed.
1958 copy(size_type __pos, size_type __n, _CharT* __buffer) const
1960 size_t __size = size();
1961 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1963 _Destroy(__buffer, __buffer + __len, _M_get_allocator());
1964 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1968 // Print to stdout, exposing structure. May be useful for
1969 // performance debugging.
1972 { _S_dump(this->_M_tree_ptr); }
1974 // Convert to 0 terminated string in new allocated memory.
1975 // Embedded 0s in the input do not terminate the copy.
1976 const _CharT* c_str() const;
1978 // As above, but lso use the flattened representation as the
1979 // the new rope representation.
1980 const _CharT* replace_with_c_str();
1982 // Reclaim memory for the c_str generated flattened string.
1983 // Intentionally undocumented, since it's hard to say when this
1984 // is safe for multiple threads.
1988 if (0 == this->_M_tree_ptr)
1990 if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag &&
1991 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1992 this->_M_tree_ptr->_M_c_string)
1994 // Representation shared
1998 this->_M_tree_ptr->_M_free_c_string();
2000 this->_M_tree_ptr->_M_c_string = 0;
2004 operator[] (size_type __pos) const
2005 { return _S_fetch(this->_M_tree_ptr, __pos); }
2008 at(size_type __pos) const
2010 // if (__pos >= size()) throw out_of_range; // XXX
2011 return (*this)[__pos];
2016 { return(const_iterator(this->_M_tree_ptr, 0)); }
2018 // An easy way to get a const iterator from a non-const container.
2021 { return(const_iterator(this->_M_tree_ptr, 0)); }
2025 { return(const_iterator(this->_M_tree_ptr, size())); }
2029 { return(const_iterator(this->_M_tree_ptr, size())); }
2033 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
2042 return _S_min_len[int(__detail::_S_max_rope_depth) - 1] - 1;
2043 // Guarantees that the result can be sufficirntly
2044 // balanced. Longer ropes will probably still work,
2045 // but it's harder to make guarantees.
2048 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
2050 const_reverse_iterator
2052 { return const_reverse_iterator(end()); }
2054 const_reverse_iterator
2055 const_rbegin() const
2056 { return const_reverse_iterator(end()); }
2058 const_reverse_iterator
2060 { return const_reverse_iterator(begin()); }
2062 const_reverse_iterator
2064 { return const_reverse_iterator(begin()); }
2066 template<class _CharT2, class _Alloc2>
2067 friend rope<_CharT2, _Alloc2>
2068 operator+(const rope<_CharT2, _Alloc2>& __left,
2069 const rope<_CharT2, _Alloc2>& __right);
2071 template<class _CharT2, class _Alloc2>
2072 friend rope<_CharT2, _Alloc2>
2073 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2075 template<class _CharT2, class _Alloc2>
2076 friend rope<_CharT2, _Alloc2>
2077 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2079 // The symmetric cases are intentionally omitted, since they're
2080 // presumed to be less common, and we don't handle them as well.
2082 // The following should really be templatized. The first
2083 // argument should be an input iterator or forward iterator with
2084 // value_type _CharT.
2086 append(const _CharT* __iter, size_t __n)
2088 _RopeRep* __result =
2089 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
2090 _S_unref(this->_M_tree_ptr);
2091 this->_M_tree_ptr = __result;
2096 append(const _CharT* __c_string)
2098 size_t __len = _S_char_ptr_len(__c_string);
2099 append(__c_string, __len);
2104 append(const _CharT* __s, const _CharT* __e)
2106 _RopeRep* __result =
2107 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
2108 _S_unref(this->_M_tree_ptr);
2109 this->_M_tree_ptr = __result;
2114 append(const_iterator __s, const_iterator __e)
2116 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2118 __e._M_current_pos));
2119 _RopeRep* __result = _S_concat(this->_M_tree_ptr,
2120 (_RopeRep*)__appendee);
2121 _S_unref(this->_M_tree_ptr);
2122 this->_M_tree_ptr = __result;
2129 _RopeRep* __result =
2130 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
2131 _S_unref(this->_M_tree_ptr);
2132 this->_M_tree_ptr = __result;
2138 { return append(_CharT()); } // XXX why?
2141 append(const rope& __y)
2143 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2144 _S_unref(this->_M_tree_ptr);
2145 this->_M_tree_ptr = __result;
2150 append(size_t __n, _CharT __c)
2152 rope<_CharT,_Alloc> __last(__n, __c);
2153 return append(__last);
2159 _RopeRep* __tmp = this->_M_tree_ptr;
2160 this->_M_tree_ptr = __b._M_tree_ptr;
2161 __b._M_tree_ptr = __tmp;
2165 // Result is included in refcount.
2167 replace(_RopeRep* __old, size_t __pos1,
2168 size_t __pos2, _RopeRep* __r)
2175 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2176 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2180 __result = _S_concat(__left, __right);
2183 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2184 __result = _S_concat(__left_result, __right);
2191 insert(size_t __p, const rope& __r)
2193 _RopeRep* __result =
2194 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2195 _S_unref(this->_M_tree_ptr);
2196 this->_M_tree_ptr = __result;
2200 insert(size_t __p, size_t __n, _CharT __c)
2202 rope<_CharT,_Alloc> __r(__n,__c);
2207 insert(size_t __p, const _CharT* __i, size_t __n)
2209 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2210 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2212 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n));
2213 // _S_ destr_concat_char_iter should be safe here.
2214 // But as it stands it's probably not a win, since __left
2215 // is likely to have additional references.
2216 _RopeRep* __result = _S_concat(__left_result, __right);
2217 _S_unref(this->_M_tree_ptr);
2218 this->_M_tree_ptr = __result;
2222 insert(size_t __p, const _CharT* __c_string)
2223 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2226 insert(size_t __p, _CharT __c)
2227 { insert(__p, &__c, 1); }
2232 _CharT __c = _CharT();
2233 insert(__p, &__c, 1);
2237 insert(size_t __p, const _CharT* __i, const _CharT* __j)
2244 insert(size_t __p, const const_iterator& __i,
2245 const const_iterator& __j)
2252 insert(size_t __p, const iterator& __i,
2253 const iterator& __j)
2259 // (position, length) versions of replace operations:
2262 replace(size_t __p, size_t __n, const rope& __r)
2264 _RopeRep* __result =
2265 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2266 _S_unref(this->_M_tree_ptr);
2267 this->_M_tree_ptr = __result;
2271 replace(size_t __p, size_t __n,
2272 const _CharT* __i, size_t __i_len)
2274 rope __r(__i, __i_len);
2275 replace(__p, __n, __r);
2279 replace(size_t __p, size_t __n, _CharT __c)
2282 replace(__p, __n, __r);
2286 replace(size_t __p, size_t __n, const _CharT* __c_string)
2288 rope __r(__c_string);
2289 replace(__p, __n, __r);
2293 replace(size_t __p, size_t __n,
2294 const _CharT* __i, const _CharT* __j)
2297 replace(__p, __n, __r);
2301 replace(size_t __p, size_t __n,
2302 const const_iterator& __i, const const_iterator& __j)
2305 replace(__p, __n, __r);
2309 replace(size_t __p, size_t __n,
2310 const iterator& __i, const iterator& __j)
2313 replace(__p, __n, __r);
2316 // Single character variants:
2318 replace(size_t __p, _CharT __c)
2320 iterator __i(this, __p);
2325 replace(size_t __p, const rope& __r)
2326 { replace(__p, 1, __r); }
2329 replace(size_t __p, const _CharT* __i, size_t __i_len)
2330 { replace(__p, 1, __i, __i_len); }
2333 replace(size_t __p, const _CharT* __c_string)
2334 { replace(__p, 1, __c_string); }
2337 replace(size_t __p, const _CharT* __i, const _CharT* __j)
2338 { replace(__p, 1, __i, __j); }
2341 replace(size_t __p, const const_iterator& __i,
2342 const const_iterator& __j)
2343 { replace(__p, 1, __i, __j); }
2346 replace(size_t __p, const iterator& __i,
2347 const iterator& __j)
2348 { replace(__p, 1, __i, __j); }
2350 // Erase, (position, size) variant.
2352 erase(size_t __p, size_t __n)
2354 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2356 _S_unref(this->_M_tree_ptr);
2357 this->_M_tree_ptr = __result;
2360 // Erase, single character
2363 { erase(__p, __p + 1); }
2365 // Insert, iterator variants.
2367 insert(const iterator& __p, const rope& __r)
2369 insert(__p.index(), __r);
2374 insert(const iterator& __p, size_t __n, _CharT __c)
2376 insert(__p.index(), __n, __c);
2380 iterator insert(const iterator& __p, _CharT __c)
2382 insert(__p.index(), __c);
2387 insert(const iterator& __p )
2389 insert(__p.index());
2394 insert(const iterator& __p, const _CharT* c_string)
2396 insert(__p.index(), c_string);
2401 insert(const iterator& __p, const _CharT* __i, size_t __n)
2403 insert(__p.index(), __i, __n);
2408 insert(const iterator& __p, const _CharT* __i,
2411 insert(__p.index(), __i, __j);
2416 insert(const iterator& __p,
2417 const const_iterator& __i, const const_iterator& __j)
2419 insert(__p.index(), __i, __j);
2424 insert(const iterator& __p,
2425 const iterator& __i, const iterator& __j)
2427 insert(__p.index(), __i, __j);
2431 // Replace, range variants.
2433 replace(const iterator& __p, const iterator& __q, const rope& __r)
2434 { replace(__p.index(), __q.index() - __p.index(), __r); }
2437 replace(const iterator& __p, const iterator& __q, _CharT __c)
2438 { replace(__p.index(), __q.index() - __p.index(), __c); }
2441 replace(const iterator& __p, const iterator& __q,
2442 const _CharT* __c_string)
2443 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2446 replace(const iterator& __p, const iterator& __q,
2447 const _CharT* __i, size_t __n)
2448 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2451 replace(const iterator& __p, const iterator& __q,
2452 const _CharT* __i, const _CharT* __j)
2453 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2456 replace(const iterator& __p, const iterator& __q,
2457 const const_iterator& __i, const const_iterator& __j)
2458 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2461 replace(const iterator& __p, const iterator& __q,
2462 const iterator& __i, const iterator& __j)
2463 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2465 // Replace, iterator variants.
2467 replace(const iterator& __p, const rope& __r)
2468 { replace(__p.index(), __r); }
2471 replace(const iterator& __p, _CharT __c)
2472 { replace(__p.index(), __c); }
2475 replace(const iterator& __p, const _CharT* __c_string)
2476 { replace(__p.index(), __c_string); }
2479 replace(const iterator& __p, const _CharT* __i, size_t __n)
2480 { replace(__p.index(), __i, __n); }
2483 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2484 { replace(__p.index(), __i, __j); }
2487 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2488 { replace(__p.index(), __i, __j); }
2491 replace(const iterator& __p, iterator __i, iterator __j)
2492 { replace(__p.index(), __i, __j); }
2494 // Iterator and range variants of erase
2496 erase(const iterator& __p, const iterator& __q)
2498 size_t __p_index = __p.index();
2499 erase(__p_index, __q.index() - __p_index);
2500 return iterator(this, __p_index);
2504 erase(const iterator& __p)
2506 size_t __p_index = __p.index();
2507 erase(__p_index, 1);
2508 return iterator(this, __p_index);
2512 substr(size_t __start, size_t __len = 1) const
2514 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2520 substr(iterator __start, iterator __end) const
2522 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2528 substr(iterator __start) const
2530 size_t __pos = __start.index();
2531 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2536 substr(const_iterator __start, const_iterator __end) const
2538 // This might eventually take advantage of the cache in the
2540 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2545 rope<_CharT, _Alloc>
2546 substr(const_iterator __start)
2548 size_t __pos = __start.index();
2549 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2553 static const size_type npos;
2555 size_type find(_CharT __c, size_type __pos = 0) const;
2558 find(const _CharT* __s, size_type __pos = 0) const
2560 size_type __result_pos;
2561 const_iterator __result =
2562 std::search(const_begin() + __pos, const_end(),
2563 __s, __s + _S_char_ptr_len(__s));
2564 __result_pos = __result.index();
2565 #ifndef __STL_OLD_ROPE_SEMANTICS
2566 if (__result_pos == size())
2567 __result_pos = npos;
2569 return __result_pos;
2574 { return(iterator(this, 0)); }
2578 { return(iterator(this, size())); }
2580 typedef std::reverse_iterator<iterator> reverse_iterator;
2584 { return reverse_iterator(mutable_end()); }
2588 { return reverse_iterator(mutable_begin()); }
2591 mutable_reference_at(size_type __pos)
2592 { return reference(this, __pos); }
2596 operator[] (size_type __pos)
2597 { return _char_ref_proxy(this, __pos); }
2602 // if (__pos >= size()) throw out_of_range; // XXX
2603 return (*this)[__pos];
2606 void resize(size_type __n, _CharT __c) { }
2607 void resize(size_type __n) { }
2608 void reserve(size_type __res_arg = 0) { }
2612 { return max_size(); }
2614 // Stuff below this line is dangerous because it's error prone.
2615 // I would really like to get rid of it.
2616 // copy function with funny arg ordering.
2618 copy(_CharT* __buffer, size_type __n,
2619 size_type __pos = 0) const
2620 { return copy(__pos, __n, __buffer); }
2624 { return mutable_end(); }
2628 { return mutable_begin(); }
2632 { return mutable_rend(); }
2636 { return mutable_rbegin(); }
2641 { return const_end(); }
2645 { return const_begin(); }
2647 const_reverse_iterator
2649 { return const_rend(); }
2651 const_reverse_iterator
2653 { return const_rbegin(); }
2658 template <class _CharT, class _Alloc>
2659 const typename rope<_CharT, _Alloc>::size_type
2660 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2662 template <class _CharT, class _Alloc>
2663 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2664 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2665 { return (__x._M_current_pos == __y._M_current_pos
2666 && __x._M_root == __y._M_root); }
2668 template <class _CharT, class _Alloc>
2669 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2670 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2671 { return (__x._M_current_pos < __y._M_current_pos); }
2673 template <class _CharT, class _Alloc>
2674 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2675 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2676 { return !(__x == __y); }
2678 template <class _CharT, class _Alloc>
2679 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2680 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2681 { return __y < __x; }
2683 template <class _CharT, class _Alloc>
2685 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2686 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2687 { return !(__y < __x); }
2689 template <class _CharT, class _Alloc>
2691 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2692 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2693 { return !(__x < __y); }
2695 template <class _CharT, class _Alloc>
2697 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2698 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2699 { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
2701 template <class _CharT, class _Alloc>
2702 inline _Rope_const_iterator<_CharT, _Alloc>
2703 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2704 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2705 __x._M_current_pos - __n); }
2707 template <class _CharT, class _Alloc>
2708 inline _Rope_const_iterator<_CharT, _Alloc>
2709 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2710 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2711 __x._M_current_pos + __n); }
2713 template <class _CharT, class _Alloc>
2714 inline _Rope_const_iterator<_CharT, _Alloc>
2715 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT, _Alloc>& __x)
2716 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2717 __x._M_current_pos + __n); }
2719 template <class _CharT, class _Alloc>
2721 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2722 const _Rope_iterator<_CharT, _Alloc>& __y)
2723 {return (__x._M_current_pos == __y._M_current_pos
2724 && __x._M_root_rope == __y._M_root_rope); }
2726 template <class _CharT, class _Alloc>
2728 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2729 const _Rope_iterator<_CharT, _Alloc>& __y)
2730 { return (__x._M_current_pos < __y._M_current_pos); }
2732 template <class _CharT, class _Alloc>
2734 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2735 const _Rope_iterator<_CharT, _Alloc>& __y)
2736 { return !(__x == __y); }
2738 template <class _CharT, class _Alloc>
2740 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2741 const _Rope_iterator<_CharT, _Alloc>& __y)
2742 { return __y < __x; }
2744 template <class _CharT, class _Alloc>
2746 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2747 const _Rope_iterator<_CharT, _Alloc>& __y)
2748 { return !(__y < __x); }
2750 template <class _CharT, class _Alloc>
2752 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2753 const _Rope_iterator<_CharT, _Alloc>& __y)
2754 { return !(__x < __y); }
2756 template <class _CharT, class _Alloc>
2758 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2759 const _Rope_iterator<_CharT, _Alloc>& __y)
2760 { return ((ptrdiff_t)__x._M_current_pos
2761 - (ptrdiff_t)__y._M_current_pos); }
2763 template <class _CharT, class _Alloc>
2764 inline _Rope_iterator<_CharT, _Alloc>
2765 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2767 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2768 __x._M_current_pos - __n); }
2770 template <class _CharT, class _Alloc>
2771 inline _Rope_iterator<_CharT, _Alloc>
2772 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2773 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2774 __x._M_current_pos + __n); }
2776 template <class _CharT, class _Alloc>
2777 inline _Rope_iterator<_CharT, _Alloc>
2778 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2779 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2780 __x._M_current_pos + __n); }
2782 template <class _CharT, class _Alloc>
2783 inline rope<_CharT, _Alloc>
2784 operator+(const rope<_CharT, _Alloc>& __left,
2785 const rope<_CharT, _Alloc>& __right)
2787 // Inlining this should make it possible to keep __left and
2788 // __right in registers.
2789 typedef rope<_CharT, _Alloc> rope_type;
2790 return rope_type(rope_type::_S_concat(__left._M_tree_ptr,
2791 __right._M_tree_ptr));
2794 template <class _CharT, class _Alloc>
2795 inline rope<_CharT, _Alloc>&
2796 operator+=(rope<_CharT, _Alloc>& __left,
2797 const rope<_CharT, _Alloc>& __right)
2799 __left.append(__right);
2803 template <class _CharT, class _Alloc>
2804 inline rope<_CharT, _Alloc>
2805 operator+(const rope<_CharT, _Alloc>& __left,
2806 const _CharT* __right)
2808 typedef rope<_CharT, _Alloc> rope_type;
2809 size_t __rlen = rope_type::_S_char_ptr_len(__right);
2810 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2814 template <class _CharT, class _Alloc>
2815 inline rope<_CharT, _Alloc>&
2816 operator+=(rope<_CharT, _Alloc>& __left,
2817 const _CharT* __right)
2819 __left.append(__right);
2823 template <class _CharT, class _Alloc>
2824 inline rope<_CharT, _Alloc>
2825 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2827 typedef rope<_CharT, _Alloc> rope_type;
2828 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2832 template <class _CharT, class _Alloc>
2833 inline rope<_CharT, _Alloc>&
2834 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2836 __left.append(__right);
2840 template <class _CharT, class _Alloc>
2842 operator<(const rope<_CharT, _Alloc>& __left,
2843 const rope<_CharT, _Alloc>& __right)
2844 { return __left.compare(__right) < 0; }
2846 template <class _CharT, class _Alloc>
2848 operator==(const rope<_CharT, _Alloc>& __left,
2849 const rope<_CharT, _Alloc>& __right)
2850 { return __left.compare(__right) == 0; }
2852 template <class _CharT, class _Alloc>
2854 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2855 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2856 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2858 template <class _CharT, class _Alloc>
2860 operator!=(const rope<_CharT, _Alloc>& __x,
2861 const rope<_CharT, _Alloc>& __y)
2862 { return !(__x == __y); }
2864 template <class _CharT, class _Alloc>
2866 operator>(const rope<_CharT, _Alloc>& __x,
2867 const rope<_CharT, _Alloc>& __y)
2868 { return __y < __x; }
2870 template <class _CharT, class _Alloc>
2872 operator<=(const rope<_CharT, _Alloc>& __x,
2873 const rope<_CharT, _Alloc>& __y)
2874 { return !(__y < __x); }
2876 template <class _CharT, class _Alloc>
2878 operator>=(const rope<_CharT, _Alloc>& __x,
2879 const rope<_CharT, _Alloc>& __y)
2880 { return !(__x < __y); }
2882 template <class _CharT, class _Alloc>
2884 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2885 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2886 { return !(__x == __y); }
2888 template<class _CharT, class _Traits, class _Alloc>
2889 std::basic_ostream<_CharT, _Traits>&
2890 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2891 const rope<_CharT, _Alloc>& __r);
2893 typedef rope<char> crope;
2894 typedef rope<wchar_t> wrope;
2896 inline crope::reference
2897 __mutable_reference_at(crope& __c, size_t __i)
2898 { return __c.mutable_reference_at(__i); }
2900 inline wrope::reference
2901 __mutable_reference_at(wrope& __c, size_t __i)
2902 { return __c.mutable_reference_at(__i); }
2904 template <class _CharT, class _Alloc>
2906 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2909 // Hash functions should probably be revisited later:
2914 operator()(const crope& __str) const
2916 size_t __size = __str.size();
2919 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2928 operator()(const wrope& __str) const
2930 size_t __size = __str.size();
2933 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2937 _GLIBCXX_END_NAMESPACE
2939 # include <ext/ropeimpl.h>