1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 * This file is a GNU extension to the Standard C++ Library (possibly
45 * containing extensions from the HP/SGI STL subset).
51 #include <bits/stl_algobase.h>
52 #include <bits/stl_construct.h>
53 #include <bits/stl_uninitialized.h>
54 #include <bits/stl_algo.h>
55 #include <bits/stl_function.h>
56 #include <bits/stl_numeric.h>
57 #include <bits/allocator.h>
58 #include <ext/hash_fun.h>
61 # define __GC_CONST const
63 # include <bits/gthr.h>
64 # define __GC_CONST // constant except for deallocation
67 #include <ext/memory> // For uninitialized_copy_n
75 using std::reverse_iterator;
78 // The _S_eos function is used for those functions that
79 // convert to/from C-like strings to detect the end of the string.
81 // The end-of-C-string character.
82 // This is what the draft standard says it should be.
83 template <class _CharT>
88 // Test for basic character types.
89 // For basic character types leaves having a trailing eos.
90 template <class _CharT>
92 _S_is_basic_char_type(_CharT*)
95 template <class _CharT>
97 _S_is_one_byte_char_type(_CharT*)
101 _S_is_basic_char_type(char*)
105 _S_is_one_byte_char_type(char*)
109 _S_is_basic_char_type(wchar_t*)
112 // Store an eos iff _CharT is a basic character type.
113 // Do not reference _S_eos if it isn't.
114 template <class _CharT>
116 _S_cond_store_eos(_CharT&) { }
119 _S_cond_store_eos(char& __c)
123 _S_cond_store_eos(wchar_t& __c)
126 // char_producers are logically functions that generate a section of
127 // a string. These can be convereted to ropes. The resulting rope
128 // invokes the char_producer on demand. This allows, for example,
129 // files to be viewed as ropes without reading the entire file.
130 template <class _CharT>
134 virtual ~char_producer() {};
137 operator()(size_t __start_pos, size_t __len,
138 _CharT* __buffer) = 0;
139 // Buffer should really be an arbitrary output iterator.
140 // That way we could flatten directly into an ostream, etc.
141 // This is thoroughly impossible, since iterator types don't
142 // have runtime descriptions.
147 // Sequence must provide an append operation that appends an
148 // array to the sequence. Sequence buffers are useful only if
149 // appending an entire array is cheaper than appending element by element.
150 // This is true for many string representations.
151 // This should perhaps inherit from ostream<sequence::value_type>
152 // and be implemented correspondingly, so that they can be used
153 // for formatted. For the sake of portability, we don't do this yet.
155 // For now, sequence buffers behave as output iterators. But they also
156 // behave a little like basic_ostringstream<sequence::value_type> and a
157 // little like containers.
159 template<class _Sequence, size_t _Buf_sz = 100>
160 class sequence_buffer
161 : public iterator<std::output_iterator_tag, void, void, void, void>
164 typedef typename _Sequence::value_type value_type;
166 _Sequence* _M_prefix;
167 value_type _M_buffer[_Buf_sz];
174 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
182 : _M_prefix(0), _M_buf_count(0) { }
184 sequence_buffer(const sequence_buffer& __x)
186 _M_prefix = __x._M_prefix;
187 _M_buf_count = __x._M_buf_count;
188 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
191 sequence_buffer(sequence_buffer& __x)
194 _M_prefix = __x._M_prefix;
198 sequence_buffer(_Sequence& __s)
199 : _M_prefix(&__s), _M_buf_count(0) { }
202 operator=(sequence_buffer& __x)
205 _M_prefix = __x._M_prefix;
211 operator=(const sequence_buffer& __x)
213 _M_prefix = __x._M_prefix;
214 _M_buf_count = __x._M_buf_count;
215 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
220 push_back(value_type __x)
222 if (_M_buf_count < _Buf_sz)
224 _M_buffer[_M_buf_count] = __x;
236 append(value_type* __s, size_t __len)
238 if (__len + _M_buf_count <= _Buf_sz)
240 size_t __i = _M_buf_count;
241 for (size_t __j = 0; __j < __len; __i++, __j++)
242 _M_buffer[__i] = __s[__j];
243 _M_buf_count += __len;
245 else if (0 == _M_buf_count)
246 _M_prefix->append(__s, __s + __len);
255 write(value_type* __s, size_t __len)
269 operator=(const value_type& __rhs)
288 // The following should be treated as private, at least for now.
289 template<class _CharT>
290 class _Rope_char_consumer
293 // If we had member templates, these should not be virtual.
294 // For now we need to use run-time parametrization where
295 // compile-time would do. Hence this should all be private
297 // The symmetry with char_producer is accidental and temporary.
298 virtual ~_Rope_char_consumer() {};
301 operator()(const _CharT* __buffer, size_t __len) = 0;
304 // First a lot of forward declarations. The standard seems to require
305 // much stricter "declaration before use" than many of the implementations
307 template<class _CharT, class _Alloc = allocator<_CharT> >
310 template<class _CharT, class _Alloc>
311 struct _Rope_RopeConcatenation;
313 template<class _CharT, class _Alloc>
314 struct _Rope_RopeLeaf;
316 template<class _CharT, class _Alloc>
317 struct _Rope_RopeFunction;
319 template<class _CharT, class _Alloc>
320 struct _Rope_RopeSubstring;
322 template<class _CharT, class _Alloc>
323 class _Rope_iterator;
325 template<class _CharT, class _Alloc>
326 class _Rope_const_iterator;
328 template<class _CharT, class _Alloc>
329 class _Rope_char_ref_proxy;
331 template<class _CharT, class _Alloc>
332 class _Rope_char_ptr_proxy;
334 template<class _CharT, class _Alloc>
336 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
337 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
339 template<class _CharT, class _Alloc>
340 _Rope_const_iterator<_CharT, _Alloc>
341 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
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+(ptrdiff_t __n,
352 const _Rope_const_iterator<_CharT, _Alloc>& __x);
354 template<class _CharT, class _Alloc>
356 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
357 const _Rope_const_iterator<_CharT, _Alloc>& __y);
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>
370 _Rope_iterator<_CharT, _Alloc>
371 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
373 template<class _CharT, class _Alloc>
374 _Rope_iterator<_CharT, _Alloc>
375 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
377 template<class _CharT, class _Alloc>
378 _Rope_iterator<_CharT, _Alloc>
379 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
381 template<class _CharT, class _Alloc>
383 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
384 const _Rope_iterator<_CharT, _Alloc>& __y);
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<_CharT, _Alloc>& __left,
399 const rope<_CharT, _Alloc>& __right);
401 template<class _CharT, class _Alloc>
403 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
405 template<class _CharT, class _Alloc>
407 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
409 // Some helpers, so we can use power on ropes.
410 // See below for why this isn't local to the implementation.
412 // This uses a nonstandard refcount convention.
413 // The result has refcount 0.
414 template<class _CharT, class _Alloc>
415 struct _Rope_Concat_fn
416 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
417 rope<_CharT, _Alloc> >
420 operator()(const rope<_CharT, _Alloc>& __x,
421 const rope<_CharT, _Alloc>& __y)
422 { return __x + __y; }
425 template <class _CharT, class _Alloc>
426 inline rope<_CharT, _Alloc>
427 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
428 { return rope<_CharT, _Alloc>(); }
430 // Class _Refcount_Base provides a type, _RC_t, a data member,
431 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
432 // atomic preincrement/predecrement. The constructor initializes
434 struct _Refcount_Base
437 typedef size_t _RC_t;
439 // The data member _M_ref_count
440 volatile _RC_t _M_ref_count;
443 __gthread_mutex_t _M_ref_count_lock;
445 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
447 #ifdef __GTHREAD_MUTEX_INIT
448 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
449 _M_ref_count_lock = __tmp;
450 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
451 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
453 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
460 __gthread_mutex_lock(&_M_ref_count_lock);
462 __gthread_mutex_unlock(&_M_ref_count_lock);
468 __gthread_mutex_lock(&_M_ref_count_lock);
469 volatile _RC_t __tmp = --_M_ref_count;
470 __gthread_mutex_unlock(&_M_ref_count_lock);
476 // What follows should really be local to rope. Unfortunately,
477 // that doesn't work, since it makes it impossible to define generic
478 // equality on rope iterators. According to the draft standard, the
479 // template parameters for such an equality operator cannot be inferred
480 // from the occurrence of a member class as a parameter.
481 // (SGI compilers in fact allow this, but the __result wouldn't be
483 // Similarly, some of the static member functions are member functions
484 // only to avoid polluting the global namespace, and to circumvent
485 // restrictions on type inference for template functions.
489 // The internal data structure for representing a rope. This is
490 // private to the implementation. A rope is really just a pointer
493 // A few basic functions for manipulating this data structure
494 // are members of _RopeRep. Most of the more complex algorithms
495 // are implemented as rope members.
497 // Some of the static member functions of _RopeRep have identically
498 // named functions in rope that simply invoke the _RopeRep versions.
500 #define __ROPE_DEFINE_ALLOCS(__a) \
501 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
502 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
503 __ROPE_DEFINE_ALLOC(__C,_C) \
504 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
505 __ROPE_DEFINE_ALLOC(__L,_L) \
506 typedef _Rope_RopeFunction<_CharT,__a> __F; \
507 __ROPE_DEFINE_ALLOC(__F,_F) \
508 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
509 __ROPE_DEFINE_ALLOC(__S,_S)
511 // Internal rope nodes potentially store a copy of the allocator
512 // instance used to allocate them. This is mostly redundant.
513 // But the alternative would be to pass allocator instances around
514 // in some form to nearly all internal functions, since any pointer
515 // assignment may result in a zero reference count and thus require
518 #define __STATIC_IF_SGI_ALLOC /* not static */
520 template <class _CharT, class _Alloc>
521 struct _Rope_rep_base
524 typedef _Alloc allocator_type;
527 get_allocator() const
528 { return *static_cast<const _Alloc*>(this); }
530 _Rope_rep_base(size_t __size, const allocator_type&)
535 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
537 _Alloc::template rebind<_Tp>::other __name##Alloc; \
538 static _Tp* __name##_allocate(size_t __n) \
539 { return __name##Alloc().allocate(__n); } \
540 static void __name##_deallocate(_Tp *__p, size_t __n) \
541 { __name##Alloc().deallocate(__p, __n); }
542 __ROPE_DEFINE_ALLOCS(_Alloc)
543 # undef __ROPE_DEFINE_ALLOC
546 namespace _Rope_constants
548 enum { _S_max_rope_depth = 45 };
549 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
552 template<class _CharT, class _Alloc>
554 : public _Rope_rep_base<_CharT, _Alloc>
560 _Rope_constants::_Tag _M_tag:8;
561 bool _M_is_balanced:8;
562 unsigned char _M_depth;
563 __GC_CONST _CharT* _M_c_string;
564 __gthread_mutex_t _M_c_string_lock;
565 /* Flattened version of string, if needed. */
567 /* If it's not 0, then the memory is owned */
569 /* In the case of a leaf, this may point to */
570 /* the same memory as the data field. */
571 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
574 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
576 _Rope_RopeRep(_Rope_constants::_Tag __t, int __d, bool __b, size_t __size,
578 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
582 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
583 #ifdef __GTHREAD_MUTEX_INIT
585 // Do not copy a POSIX/gthr mutex once in use. However, bits are bits.
586 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
587 _M_c_string_lock = __tmp;
590 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
597 _S_free_string(__GC_CONST _CharT*, size_t __len,
599 #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
600 // Deallocate data section of a leaf.
601 // This shouldn't be a member function.
602 // But its hard to do anything else at the
603 // moment, because it's templatized w.r.t.
605 // Does nothing if __GC is defined.
607 void _M_free_c_string();
609 // Deallocate t. Assumes t is not 0.
622 _S_unref(_Rope_RopeRep* __t)
625 __t->_M_unref_nonnil();
629 _S_ref(_Rope_RopeRep* __t)
636 _S_free_if_unref(_Rope_RopeRep* __t)
638 if (0 != __t && 0 == __t->_M_ref_count)
642 void _M_unref_nonnil() {}
643 void _M_ref_nonnil() {}
644 static void _S_unref(_Rope_RopeRep*) {}
645 static void _S_ref(_Rope_RopeRep*) {}
646 static void _S_free_if_unref(_Rope_RopeRep*) {}
650 operator=(const _Rope_RopeRep&);
652 _Rope_RopeRep(const _Rope_RopeRep&);
655 template<class _CharT, class _Alloc>
656 struct _Rope_RopeLeaf
657 : public _Rope_RopeRep<_CharT, _Alloc>
660 // Apparently needed by VC++
661 // The data fields of leaves are allocated with some
662 // extra space, to accommodate future growth and for basic
663 // character types, to hold a trailing eos character.
664 enum { _S_alloc_granularity = 8 };
667 _S_rounded_up_size(size_t __n)
669 size_t __size_with_eos;
671 if (_S_is_basic_char_type((_CharT*)0))
672 __size_with_eos = __n + 1;
674 __size_with_eos = __n;
676 return __size_with_eos;
678 // Allow slop for in-place expansion.
679 return ((__size_with_eos + size_t(_S_alloc_granularity) - 1)
680 &~ (size_t(_S_alloc_granularity) - 1));
683 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
684 /* The allocated size is */
685 /* _S_rounded_up_size(size), except */
686 /* in the GC case, in which it */
687 /* doesn't matter. */
688 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
691 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size,
693 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_leaf, 0, true,
694 __size, __a), _M_data(__d)
696 if (_S_is_basic_char_type((_CharT *)0))
698 // already eos terminated.
699 this->_M_c_string = __d;
702 // The constructor assumes that d has been allocated with
703 // the proper allocator and the properly padded size.
704 // In contrast, the destructor deallocates the data:
706 ~_Rope_RopeLeaf() throw()
708 if (_M_data != this->_M_c_string)
709 this->_M_free_c_string();
711 __STL_FREE_STRING(_M_data, this->_M_size, this->get_allocator());
716 operator=(const _Rope_RopeLeaf&);
718 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
721 template<class _CharT, class _Alloc>
722 struct _Rope_RopeConcatenation
723 : public _Rope_RopeRep<_CharT, _Alloc>
726 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
727 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
729 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
732 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
733 _Rope_RopeRep<_CharT, _Alloc>* __r,
735 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_concat,
736 std::max(__l->_M_depth,
739 __l->_M_size + __r->_M_size, __a),
740 _M_left(__l), _M_right(__r)
743 ~_Rope_RopeConcatenation() throw()
745 this->_M_free_c_string();
746 _M_left->_M_unref_nonnil();
747 _M_right->_M_unref_nonnil();
751 _Rope_RopeConcatenation&
752 operator=(const _Rope_RopeConcatenation&);
754 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
757 template<class _CharT, class _Alloc>
758 struct _Rope_RopeFunction
759 : public _Rope_RopeRep<_CharT, _Alloc>
762 char_producer<_CharT>* _M_fn;
764 bool _M_delete_when_done; // Char_producer is owned by the
765 // rope and should be explicitly
766 // deleted when the rope becomes
769 // In the GC case, we either register the rope for
770 // finalization, or not. Thus the field is unnecessary;
771 // the information is stored in the collector data structures.
772 // We do need a finalization procedure to be invoked by the
775 _S_fn_finalization_proc(void * __tree, void *)
776 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
778 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
781 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
782 bool __d, allocator_type __a)
783 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_function,
784 0, true, __size, __a)
787 , _M_delete_when_done(__d)
793 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
794 _S_fn_finalization_proc, 0, 0, 0);
799 ~_Rope_RopeFunction() throw()
801 this->_M_free_c_string();
802 if (_M_delete_when_done)
808 operator=(const _Rope_RopeFunction&);
810 _Rope_RopeFunction(const _Rope_RopeFunction&);
812 // Substring results are usually represented using just
813 // concatenation nodes. But in the case of very long flat ropes
814 // or ropes with a functional representation that isn't practical.
815 // In that case, we represent the __result as a special case of
816 // RopeFunction, whose char_producer points back to the rope itself.
817 // In all cases except repeated substring operations and
818 // deallocation, we treat the __result as a RopeFunction.
819 template<class _CharT, class _Alloc>
820 struct _Rope_RopeSubstring
821 : public _Rope_RopeFunction<_CharT, _Alloc>,
822 public char_producer<_CharT>
825 // XXX this whole class should be rewritten.
826 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
830 operator()(size_t __start_pos, size_t __req_len,
833 switch(_M_base->_M_tag)
835 case _Rope_constants::_S_function:
836 case _Rope_constants::_S_substringfn:
838 char_producer<_CharT>* __fn =
839 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
840 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
843 case _Rope_constants::_S_leaf:
845 __GC_CONST _CharT* __s =
846 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
847 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
856 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
859 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_t __s,
860 size_t __l, allocator_type __a)
861 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
862 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
865 _M_base->_M_ref_nonnil();
867 this->_M_tag = _Rope_constants::_S_substringfn;
869 virtual ~_Rope_RopeSubstring() throw()
872 _M_base->_M_unref_nonnil();
873 // _M_free_c_string(); -- done by parent class
878 // Self-destructing pointers to Rope_rep.
879 // These are not conventional smart pointers. Their
880 // only purpose in life is to ensure that unref is called
881 // on the pointer either at normal exit or if an exception
882 // is raised. It is the caller's responsibility to
883 // adjust reference counts when these pointers are initialized
884 // or assigned to. (This convention significantly reduces
885 // the number of potentially expensive reference count
888 template<class _CharT, class _Alloc>
889 struct _Rope_self_destruct_ptr
891 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
893 ~_Rope_self_destruct_ptr()
894 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
896 _Rope_self_destruct_ptr() : _M_ptr(0) {};
898 _Rope_self_destruct_ptr() {};
900 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
903 _Rope_RopeRep<_CharT, _Alloc>&
907 _Rope_RopeRep<_CharT, _Alloc>*
911 operator _Rope_RopeRep<_CharT, _Alloc>*()
914 _Rope_self_destruct_ptr&
915 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
916 { _M_ptr = __x; return *this; }
920 // Dereferencing a nonconst iterator has to return something
921 // that behaves almost like a reference. It's not possible to
922 // return an actual reference since assignment requires extra
923 // work. And we would get into the same problems as with the
924 // CD2 version of basic_string.
925 template<class _CharT, class _Alloc>
926 class _Rope_char_ref_proxy
928 friend class rope<_CharT, _Alloc>;
929 friend class _Rope_iterator<_CharT, _Alloc>;
930 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
932 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
934 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
936 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
937 typedef rope<_CharT, _Alloc> _My_rope;
940 bool _M_current_valid;
941 _My_rope* _M_root; // The whole rope.
943 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
944 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) {}
946 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
947 : _M_pos(__x._M_pos), _M_current(__x._M_current), _M_current_valid(false),
948 _M_root(__x._M_root) {}
950 // Don't preserve cache if the reference can outlive the
951 // expression. We claim that's not possible without calling
952 // a copy constructor or generating reference to a proxy
953 // reference. We declare the latter to have undefined semantics.
954 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
955 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
957 inline operator _CharT () const;
959 _Rope_char_ref_proxy&
960 operator=(_CharT __c);
962 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
964 _Rope_char_ref_proxy&
965 operator=(const _Rope_char_ref_proxy& __c)
966 { return operator=((_CharT)__c); }
969 template<class _CharT, class __Alloc>
971 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
972 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
979 template<class _CharT, class _Alloc>
980 class _Rope_char_ptr_proxy
982 // XXX this class should be rewritten.
983 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
985 rope<_CharT,_Alloc>* _M_root; // The whole rope.
987 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
988 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
990 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
991 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
993 _Rope_char_ptr_proxy() {}
995 _Rope_char_ptr_proxy(_CharT* __x)
996 : _M_root(0), _M_pos(0) { }
998 _Rope_char_ptr_proxy&
999 operator=(const _Rope_char_ptr_proxy& __x)
1001 _M_pos = __x._M_pos;
1002 _M_root = __x._M_root;
1006 template<class _CharT2, class _Alloc2>
1008 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1009 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1011 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1012 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1016 // Unlike in the C version, we cache only part of the stack
1017 // for rope iterators, since they must be efficiently copyable.
1018 // When we run out of cache, we have to reconstruct the iterator
1020 // Pointers from iterators are not included in reference counts.
1021 // Iterators are assumed to be thread private. Ropes can
1024 template<class _CharT, class _Alloc>
1025 class _Rope_iterator_base
1026 : public iterator<std::random_access_iterator_tag, _CharT>
1028 friend class rope<_CharT, _Alloc>;
1030 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1031 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1032 // Borland doesn't want this to be protected.
1034 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1035 enum { _S_iterator_buf_len = 15 };
1036 size_t _M_current_pos;
1037 _RopeRep* _M_root; // The whole rope.
1038 size_t _M_leaf_pos; // Starting position for current leaf
1039 __GC_CONST _CharT* _M_buf_start;
1041 // containing current char.
1042 __GC_CONST _CharT* _M_buf_ptr;
1043 // Pointer to current char in buffer.
1044 // != 0 ==> buffer valid.
1045 __GC_CONST _CharT* _M_buf_end;
1046 // One past __last valid char in buffer.
1047 // What follows is the path cache. We go out of our
1048 // way to make this compact.
1049 // Path_end contains the bottom section of the path from
1050 // the root to the current leaf.
1051 const _RopeRep* _M_path_end[_S_path_cache_len];
1052 int _M_leaf_index; // Last valid __pos in path_end;
1053 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1054 // point to concatenation nodes.
1055 unsigned char _M_path_directions;
1056 // (path_directions >> __i) & 1 is 1
1057 // iff we got from _M_path_end[leaf_index - __i - 1]
1058 // to _M_path_end[leaf_index - __i] by going to the
1059 // __right. Assumes path_cache_len <= 9.
1060 _CharT _M_tmp_buf[_S_iterator_buf_len];
1061 // Short buffer for surrounding chars.
1062 // This is useful primarily for
1063 // RopeFunctions. We put the buffer
1064 // here to avoid locking in the
1065 // multithreaded case.
1066 // The cached path is generally assumed to be valid
1067 // only if the buffer is valid.
1068 static void _S_setbuf(_Rope_iterator_base& __x);
1069 // Set buffer contents given
1071 static void _S_setcache(_Rope_iterator_base& __x);
1072 // Set buffer contents and
1074 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1075 // As above, but assumes path
1076 // cache is valid for previous posn.
1077 _Rope_iterator_base() {}
1079 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
1080 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) {}
1082 void _M_incr(size_t __n);
1083 void _M_decr(size_t __n);
1087 { return _M_current_pos; }
1089 _Rope_iterator_base(const _Rope_iterator_base& __x)
1091 if (0 != __x._M_buf_ptr)
1095 _M_current_pos = __x._M_current_pos;
1096 _M_root = __x._M_root;
1102 template<class _CharT, class _Alloc>
1103 class _Rope_iterator;
1105 template<class _CharT, class _Alloc>
1106 class _Rope_const_iterator
1107 : public _Rope_iterator_base<_CharT, _Alloc>
1109 friend class rope<_CharT, _Alloc>;
1111 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1112 // The one from the base class may not be directly visible.
1113 _Rope_const_iterator(const _RopeRep* __root, size_t __pos)
1114 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1116 // Only nonconst iterators modify root ref count
1119 typedef _CharT reference; // Really a value. Returning a reference
1120 // Would be a mess, since it would have
1121 // to be included in refcount.
1122 typedef const _CharT* pointer;
1125 _Rope_const_iterator() {};
1127 _Rope_const_iterator(const _Rope_const_iterator& __x)
1128 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1130 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1132 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, size_t __pos)
1133 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1135 _Rope_const_iterator&
1136 operator=(const _Rope_const_iterator& __x)
1138 if (0 != __x._M_buf_ptr)
1139 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1142 this->_M_current_pos = __x._M_current_pos;
1143 this->_M_root = __x._M_root;
1144 this->_M_buf_ptr = 0;
1152 if (0 == this->_M_buf_ptr)
1154 return *this->_M_buf_ptr;
1157 _Rope_const_iterator&
1160 __GC_CONST _CharT* __next;
1161 if (0 != this->_M_buf_ptr
1162 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1164 this->_M_buf_ptr = __next;
1165 ++this->_M_current_pos;
1172 _Rope_const_iterator&
1173 operator+=(ptrdiff_t __n)
1178 this->_M_decr(-__n);
1182 _Rope_const_iterator&
1189 _Rope_const_iterator&
1190 operator-=(ptrdiff_t __n)
1195 this->_M_incr(-__n);
1199 _Rope_const_iterator
1202 size_t __old_pos = this->_M_current_pos;
1204 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1205 // This makes a subsequent dereference expensive.
1206 // Perhaps we should instead copy the iterator
1207 // if it has a valid cache?
1210 _Rope_const_iterator
1213 size_t __old_pos = this->_M_current_pos;
1215 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1218 template<class _CharT2, class _Alloc2>
1219 friend _Rope_const_iterator<_CharT2, _Alloc2>
1220 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1223 template<class _CharT2, class _Alloc2>
1224 friend _Rope_const_iterator<_CharT2, _Alloc2>
1225 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1228 template<class _CharT2, class _Alloc2>
1229 friend _Rope_const_iterator<_CharT2, _Alloc2>
1230 operator+(ptrdiff_t __n,
1231 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1234 operator[](size_t __n)
1235 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1236 this->_M_current_pos + __n); }
1238 template<class _CharT2, class _Alloc2>
1240 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1241 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1243 template<class _CharT2, class _Alloc2>
1245 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1246 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1248 template<class _CharT2, class _Alloc2>
1250 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1251 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1254 template<class _CharT, class _Alloc>
1255 class _Rope_iterator
1256 : public _Rope_iterator_base<_CharT, _Alloc>
1258 friend class rope<_CharT, _Alloc>;
1260 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1261 rope<_CharT, _Alloc>* _M_root_rope;
1262 // root is treated as a cached version of this,
1263 // and is used to detect changes to the underlying
1265 // Root is included in the reference count.
1266 // This is necessary so that we can detect changes reliably.
1267 // Unfortunately, it requires careful bookkeeping for the
1269 _Rope_iterator(rope<_CharT, _Alloc>* __r, size_t __pos)
1270 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1272 { _RopeRep::_S_ref(this->_M_root);
1273 if (!(__r -> empty()))
1279 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1280 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1283 rope<_CharT, _Alloc>&
1285 { return *_M_root_rope; }
1289 this->_M_root = 0; // Needed for reference counting.
1292 _Rope_iterator(const _Rope_iterator& __x)
1293 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1295 _M_root_rope = __x._M_root_rope;
1296 _RopeRep::_S_ref(this->_M_root);
1299 _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos);
1302 { _RopeRep::_S_unref(this->_M_root); }
1305 operator=(const _Rope_iterator& __x)
1307 _RopeRep* __old = this->_M_root;
1309 _RopeRep::_S_ref(__x._M_root);
1310 if (0 != __x._M_buf_ptr)
1312 _M_root_rope = __x._M_root_rope;
1313 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1317 this->_M_current_pos = __x._M_current_pos;
1318 this->_M_root = __x._M_root;
1319 _M_root_rope = __x._M_root_rope;
1320 this->_M_buf_ptr = 0;
1322 _RopeRep::_S_unref(__old);
1330 if (0 == this->_M_buf_ptr)
1331 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1332 this->_M_current_pos);
1334 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1335 this->_M_current_pos,
1347 operator+=(ptrdiff_t __n)
1352 this->_M_decr(-__n);
1364 operator-=(ptrdiff_t __n)
1369 this->_M_incr(-__n);
1376 size_t __old_pos = this->_M_current_pos;
1378 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1384 size_t __old_pos = this->_M_current_pos;
1386 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1390 operator[](ptrdiff_t __n)
1391 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1392 this->_M_current_pos
1395 template<class _CharT2, class _Alloc2>
1397 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1398 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1400 template<class _CharT2, class _Alloc2>
1402 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1403 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1405 template<class _CharT2, class _Alloc2>
1407 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1408 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1410 template<class _CharT2, class _Alloc2>
1411 friend _Rope_iterator<_CharT2, _Alloc2>
1412 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1414 template<class _CharT2, class _Alloc2>
1415 friend _Rope_iterator<_CharT2, _Alloc2>
1416 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1418 template<class _CharT2, class _Alloc2>
1419 friend _Rope_iterator<_CharT2, _Alloc2>
1420 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT2, _Alloc2>& __x);
1424 template <class _CharT, class _Alloc>
1428 typedef _Alloc allocator_type;
1431 get_allocator() const
1432 { return *static_cast<const _Alloc*>(this); }
1434 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1435 // The one in _Base may not be visible due to template rules.
1437 _Rope_base(_RopeRep* __t, const allocator_type&)
1438 : _M_tree_ptr(__t) {}
1440 _Rope_base(const allocator_type&) {}
1442 // The only data member of a rope:
1443 _RopeRep *_M_tree_ptr;
1445 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1447 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1448 static _Tp* __name##_allocate(size_t __n) \
1449 { return __name##Alloc().allocate(__n); } \
1450 static void __name##_deallocate(_Tp *__p, size_t __n) \
1451 { __name##Alloc().deallocate(__p, __n); }
1452 __ROPE_DEFINE_ALLOCS(_Alloc)
1453 #undef __ROPE_DEFINE_ALLOC
1457 operator=(const _Rope_base&);
1459 _Rope_base(const _Rope_base&);
1463 * This is an SGI extension.
1464 * @ingroup SGIextensions
1467 template <class _CharT, class _Alloc>
1468 class rope : public _Rope_base<_CharT, _Alloc>
1471 typedef _CharT value_type;
1472 typedef ptrdiff_t difference_type;
1473 typedef size_t size_type;
1474 typedef _CharT const_reference;
1475 typedef const _CharT* const_pointer;
1476 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1477 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1478 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1479 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1481 friend class _Rope_iterator<_CharT, _Alloc>;
1482 friend class _Rope_const_iterator<_CharT, _Alloc>;
1483 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1484 friend class _Rope_iterator_base<_CharT, _Alloc>;
1485 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1486 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1487 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1490 typedef _Rope_base<_CharT, _Alloc> _Base;
1491 typedef typename _Base::allocator_type allocator_type;
1492 using _Base::_M_tree_ptr;
1493 using _Base::get_allocator;
1494 typedef __GC_CONST _CharT* _Cstrptr;
1496 static _CharT _S_empty_c_str[1];
1500 { return __c == _S_eos((_CharT*)0); }
1502 enum { _S_copy_max = 23 };
1503 // For strings shorter than _S_copy_max, we copy to
1506 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1507 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1508 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1509 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1510 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1512 // Retrieve a character at the indicated position.
1513 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1516 // Obtain a pointer to the character at the indicated position.
1517 // The pointer can be used to change the character.
1518 // If such a pointer cannot be produced, as is frequently the
1519 // case, 0 is returned instead.
1520 // (Returns nonzero only if all nodes in the path have a refcount
1522 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1526 _S_apply_to_pieces(// should be template parameter
1527 _Rope_char_consumer<_CharT>& __c,
1528 const _RopeRep* __r,
1529 size_t __begin, size_t __end);
1530 // begin and end are assumed to be in range.
1534 _S_unref(_RopeRep* __t)
1535 { _RopeRep::_S_unref(__t); }
1538 _S_ref(_RopeRep* __t)
1539 { _RopeRep::_S_ref(__t); }
1542 static void _S_unref(_RopeRep*) {}
1543 static void _S_ref(_RopeRep*) {}
1547 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1549 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1552 // _Result is counted in refcount.
1553 static _RopeRep* _S_substring(_RopeRep* __base,
1554 size_t __start, size_t __endp1);
1556 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1557 const _CharT* __iter, size_t __slen);
1558 // Concatenate rope and char ptr, copying __s.
1559 // Should really take an arbitrary iterator.
1560 // Result is counted in refcount.
1561 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1562 const _CharT* __iter,
1564 // As above, but one reference to __r is about to be
1565 // destroyed. Thus the pieces may be recycled if all
1566 // relevant reference counts are 1.
1568 // We can't really do anything since refcounts are unavailable.
1569 { return _S_concat_char_iter(__r, __iter, __slen); }
1574 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1575 // General concatenation on _RopeRep. _Result
1576 // has refcount of 1. Adjusts argument refcounts.
1580 apply_to_pieces(size_t __begin, size_t __end,
1581 _Rope_char_consumer<_CharT>& __c) const
1582 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1587 _S_rounded_up_size(size_t __n)
1588 { return _RopeLeaf::_S_rounded_up_size(__n); }
1591 _S_allocated_capacity(size_t __n)
1593 if (_S_is_basic_char_type((_CharT*)0))
1594 return _S_rounded_up_size(__n) - 1;
1596 return _S_rounded_up_size(__n);
1600 // Allocate and construct a RopeLeaf using the supplied allocator
1601 // Takes ownership of s instead of copying.
1603 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1604 size_t __size, allocator_type __a)
1606 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1607 return new(__space) _RopeLeaf(__s, __size, __a);
1610 static _RopeConcatenation*
1611 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1614 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1615 return new(__space) _RopeConcatenation(__left, __right, __a);
1618 static _RopeFunction*
1619 _S_new_RopeFunction(char_producer<_CharT>* __f,
1620 size_t __size, bool __d, allocator_type __a)
1622 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1623 return new(__space) _RopeFunction(__f, __size, __d, __a);
1626 static _RopeSubstring*
1627 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1628 size_t __l, allocator_type __a)
1630 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1631 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1635 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1636 size_t __size, allocator_type __a)
1637 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1638 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1642 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1644 __uninitialized_copy_n_a(__s, __size, __buf, __a);
1645 _S_cond_store_eos(__buf[__size]);
1647 { return _S_new_RopeLeaf(__buf, __size, __a); }
1650 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1651 __throw_exception_again;
1655 // Concatenation of nonempty strings.
1656 // Always builds a concatenation node.
1657 // Rebalances if the result is too deep.
1658 // Result has refcount 1.
1659 // Does not increment left and right ref counts even though
1660 // they are referenced.
1662 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1664 // Concatenation helper functions
1666 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1667 const _CharT* __iter, size_t __slen);
1668 // Concatenate by copying leaf.
1669 // should take an arbitrary iterator
1670 // result has refcount 1.
1673 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1674 const _CharT* __iter, size_t __slen);
1675 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1680 static size_t _S_char_ptr_len(const _CharT* __s);
1681 // slightly generalized strlen
1683 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1684 : _Base(__t, __a) { }
1687 // Copy __r to the _CharT buffer.
1688 // Returns __buffer + __r->_M_size.
1689 // Assumes that buffer is uninitialized.
1690 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1692 // Again, with explicit starting position and length.
1693 // Assumes that buffer is uninitialized.
1694 static _CharT* _S_flatten(_RopeRep* __r,
1695 size_t __start, size_t __len,
1698 static const unsigned long
1699 _S_min_len[_Rope_constants::_S_max_rope_depth + 1];
1702 _S_is_balanced(_RopeRep* __r)
1703 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1706 _S_is_almost_balanced(_RopeRep* __r)
1707 { return (__r->_M_depth == 0
1708 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1711 _S_is_roughly_balanced(_RopeRep* __r)
1712 { return (__r->_M_depth <= 1
1713 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1715 // Assumes the result is not empty.
1717 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1719 _RopeRep* __result = _S_concat(__left, __right);
1720 if (_S_is_balanced(__result))
1721 __result->_M_is_balanced = true;
1725 // The basic rebalancing operation. Logically copies the
1726 // rope. The result has refcount of 1. The client will
1727 // usually decrement the reference count of __r.
1728 // The result is within height 2 of balanced by the above
1730 static _RopeRep* _S_balance(_RopeRep* __r);
1732 // Add all unbalanced subtrees to the forest of balanceed trees.
1733 // Used only by balance.
1734 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1736 // Add __r to forest, assuming __r is already balanced.
1737 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1739 // Print to stdout, exposing structure
1740 static void _S_dump(_RopeRep* __r, int __indent = 0);
1742 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1743 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1748 { return 0 == this->_M_tree_ptr; }
1750 // Comparison member function. This is public only for those
1751 // clients that need a ternary comparison. Others
1752 // should use the comparison operators below.
1754 compare(const rope& __y) const
1755 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1757 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1758 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1762 rope(const _CharT* __s, size_t __len,
1763 const allocator_type& __a = allocator_type())
1764 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
1767 // Should perhaps be templatized with respect to the iterator type
1768 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1770 rope(const _CharT *__s, const _CharT *__e,
1771 const allocator_type& __a = allocator_type())
1772 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
1775 rope(const const_iterator& __s, const const_iterator& __e,
1776 const allocator_type& __a = allocator_type())
1777 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1778 __e._M_current_pos), __a)
1781 rope(const iterator& __s, const iterator& __e,
1782 const allocator_type& __a = allocator_type())
1783 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1784 __e._M_current_pos), __a)
1787 rope(_CharT __c, const allocator_type& __a = allocator_type())
1790 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1792 get_allocator().construct(__buf, __c);
1794 { this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a); }
1797 _RopeRep::__STL_FREE_STRING(__buf, 1, __a);
1798 __throw_exception_again;
1802 rope(size_t __n, _CharT __c,
1803 const allocator_type& __a = allocator_type());
1805 rope(const allocator_type& __a = allocator_type())
1808 // Construct a rope from a function that can compute its members
1809 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1810 const allocator_type& __a = allocator_type())
1813 this->_M_tree_ptr = (0 == __len) ?
1814 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1817 rope(const rope& __x, const allocator_type& __a = allocator_type())
1818 : _Base(__x._M_tree_ptr, __a)
1819 { _S_ref(this->_M_tree_ptr); }
1822 { _S_unref(this->_M_tree_ptr); }
1825 operator=(const rope& __x)
1827 _RopeRep* __old = this->_M_tree_ptr;
1828 this->_M_tree_ptr = __x._M_tree_ptr;
1829 _S_ref(this->_M_tree_ptr);
1837 _S_unref(this->_M_tree_ptr);
1838 this->_M_tree_ptr = 0;
1842 push_back(_CharT __x)
1844 _RopeRep* __old = this->_M_tree_ptr;
1846 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1853 _RopeRep* __old = this->_M_tree_ptr;
1855 _S_substring(this->_M_tree_ptr,
1856 0, this->_M_tree_ptr->_M_size - 1);
1862 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1865 push_front(_CharT __x)
1867 _RopeRep* __old = this->_M_tree_ptr;
1869 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, this->get_allocator());
1872 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1879 __throw_exception_again;
1886 _RopeRep* __old = this->_M_tree_ptr;
1888 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1894 { return _S_fetch(this->_M_tree_ptr, 0); }
1899 _RopeRep* __old = this->_M_tree_ptr;
1900 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1905 copy(_CharT* __buffer) const
1907 _Destroy(__buffer, __buffer + size(), get_allocator());
1908 _S_flatten(this->_M_tree_ptr, __buffer);
1911 // This is the copy function from the standard, but
1912 // with the arguments reordered to make it consistent with the
1913 // rest of the interface.
1914 // Note that this guaranteed not to compile if the draft standard
1915 // order is assumed.
1917 copy(size_type __pos, size_type __n, _CharT* __buffer) const
1919 size_t __size = size();
1920 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1922 _Destroy(__buffer, __buffer + __len, get_allocator());
1923 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1927 // Print to stdout, exposing structure. May be useful for
1928 // performance debugging.
1931 { _S_dump(this->_M_tree_ptr); }
1933 // Convert to 0 terminated string in new allocated memory.
1934 // Embedded 0s in the input do not terminate the copy.
1935 const _CharT* c_str() const;
1937 // As above, but lso use the flattened representation as the
1938 // the new rope representation.
1939 const _CharT* replace_with_c_str();
1941 // Reclaim memory for the c_str generated flattened string.
1942 // Intentionally undocumented, since it's hard to say when this
1943 // is safe for multiple threads.
1947 if (0 == this->_M_tree_ptr)
1949 if (_Rope_constants::_S_leaf == this->_M_tree_ptr->_M_tag &&
1950 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1951 this->_M_tree_ptr->_M_c_string)
1953 // Representation shared
1957 this->_M_tree_ptr->_M_free_c_string();
1959 this->_M_tree_ptr->_M_c_string = 0;
1963 operator[] (size_type __pos) const
1964 { return _S_fetch(this->_M_tree_ptr, __pos); }
1967 at(size_type __pos) const
1969 // if (__pos >= size()) throw out_of_range; // XXX
1970 return (*this)[__pos];
1975 { return(const_iterator(this->_M_tree_ptr, 0)); }
1977 // An easy way to get a const iterator from a non-const container.
1980 { return(const_iterator(this->_M_tree_ptr, 0)); }
1984 { return(const_iterator(this->_M_tree_ptr, size())); }
1988 { return(const_iterator(this->_M_tree_ptr, size())); }
1992 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
2001 return _S_min_len[int(_Rope_constants::_S_max_rope_depth) - 1] - 1;
2002 // Guarantees that the result can be sufficirntly
2003 // balanced. Longer ropes will probably still work,
2004 // but it's harder to make guarantees.
2007 typedef reverse_iterator<const_iterator> const_reverse_iterator;
2009 const_reverse_iterator
2011 { return const_reverse_iterator(end()); }
2013 const_reverse_iterator
2014 const_rbegin() const
2015 { return const_reverse_iterator(end()); }
2017 const_reverse_iterator
2019 { return const_reverse_iterator(begin()); }
2021 const_reverse_iterator
2023 { return const_reverse_iterator(begin()); }
2025 template<class _CharT2, class _Alloc2>
2026 friend rope<_CharT2, _Alloc2>
2027 operator+(const rope<_CharT2, _Alloc2>& __left,
2028 const rope<_CharT2, _Alloc2>& __right);
2030 template<class _CharT2, class _Alloc2>
2031 friend rope<_CharT2, _Alloc2>
2032 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2034 template<class _CharT2, class _Alloc2>
2035 friend rope<_CharT2, _Alloc2>
2036 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2037 // The symmetric cases are intentionally omitted, since they're presumed
2038 // to be less common, and we don't handle them as well.
2040 // The following should really be templatized.
2041 // The first argument should be an input iterator or
2042 // forward iterator with value_type _CharT.
2044 append(const _CharT* __iter, size_t __n)
2046 _RopeRep* __result =
2047 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
2048 _S_unref(this->_M_tree_ptr);
2049 this->_M_tree_ptr = __result;
2054 append(const _CharT* __c_string)
2056 size_t __len = _S_char_ptr_len(__c_string);
2057 append(__c_string, __len);
2062 append(const _CharT* __s, const _CharT* __e)
2064 _RopeRep* __result =
2065 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
2066 _S_unref(this->_M_tree_ptr);
2067 this->_M_tree_ptr = __result;
2072 append(const_iterator __s, const_iterator __e)
2074 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2076 __e._M_current_pos));
2077 _RopeRep* __result =
2078 _S_concat(this->_M_tree_ptr, (_RopeRep*)__appendee);
2079 _S_unref(this->_M_tree_ptr);
2080 this->_M_tree_ptr = __result;
2087 _RopeRep* __result =
2088 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
2089 _S_unref(this->_M_tree_ptr);
2090 this->_M_tree_ptr = __result;
2096 { return append(_CharT()); } // XXX why?
2099 append(const rope& __y)
2101 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2102 _S_unref(this->_M_tree_ptr);
2103 this->_M_tree_ptr = __result;
2108 append(size_t __n, _CharT __c)
2110 rope<_CharT,_Alloc> __last(__n, __c);
2111 return append(__last);
2117 _RopeRep* __tmp = this->_M_tree_ptr;
2118 this->_M_tree_ptr = __b._M_tree_ptr;
2119 __b._M_tree_ptr = __tmp;
2123 // Result is included in refcount.
2125 replace(_RopeRep* __old, size_t __pos1,
2126 size_t __pos2, _RopeRep* __r)
2133 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2134 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2138 __result = _S_concat(__left, __right);
2141 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2142 __result = _S_concat(__left_result, __right);
2149 insert(size_t __p, const rope& __r)
2151 _RopeRep* __result =
2152 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2153 _S_unref(this->_M_tree_ptr);
2154 this->_M_tree_ptr = __result;
2158 insert(size_t __p, size_t __n, _CharT __c)
2160 rope<_CharT,_Alloc> __r(__n,__c);
2165 insert(size_t __p, const _CharT* __i, size_t __n)
2167 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2168 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2170 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n));
2171 // _S_ destr_concat_char_iter should be safe here.
2172 // But as it stands it's probably not a win, since __left
2173 // is likely to have additional references.
2174 _RopeRep* __result = _S_concat(__left_result, __right);
2175 _S_unref(this->_M_tree_ptr);
2176 this->_M_tree_ptr = __result;
2180 insert(size_t __p, const _CharT* __c_string)
2181 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2184 insert(size_t __p, _CharT __c)
2185 { insert(__p, &__c, 1); }
2190 _CharT __c = _CharT();
2191 insert(__p, &__c, 1);
2195 insert(size_t __p, const _CharT* __i, const _CharT* __j)
2202 insert(size_t __p, const const_iterator& __i,
2203 const const_iterator& __j)
2210 insert(size_t __p, const iterator& __i,
2211 const iterator& __j)
2217 // (position, length) versions of replace operations:
2220 replace(size_t __p, size_t __n, const rope& __r)
2222 _RopeRep* __result =
2223 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2224 _S_unref(this->_M_tree_ptr);
2225 this->_M_tree_ptr = __result;
2229 replace(size_t __p, size_t __n,
2230 const _CharT* __i, size_t __i_len)
2232 rope __r(__i, __i_len);
2233 replace(__p, __n, __r);
2237 replace(size_t __p, size_t __n, _CharT __c)
2240 replace(__p, __n, __r);
2244 replace(size_t __p, size_t __n, const _CharT* __c_string)
2246 rope __r(__c_string);
2247 replace(__p, __n, __r);
2251 replace(size_t __p, size_t __n,
2252 const _CharT* __i, const _CharT* __j)
2255 replace(__p, __n, __r);
2259 replace(size_t __p, size_t __n,
2260 const const_iterator& __i, const const_iterator& __j)
2263 replace(__p, __n, __r);
2267 replace(size_t __p, size_t __n,
2268 const iterator& __i, const iterator& __j)
2271 replace(__p, __n, __r);
2274 // Single character variants:
2276 replace(size_t __p, _CharT __c)
2278 iterator __i(this, __p);
2283 replace(size_t __p, const rope& __r)
2284 { replace(__p, 1, __r); }
2287 replace(size_t __p, const _CharT* __i, size_t __i_len)
2288 { replace(__p, 1, __i, __i_len); }
2291 replace(size_t __p, const _CharT* __c_string)
2292 { replace(__p, 1, __c_string); }
2295 replace(size_t __p, const _CharT* __i, const _CharT* __j)
2296 { replace(__p, 1, __i, __j); }
2299 replace(size_t __p, const const_iterator& __i,
2300 const const_iterator& __j)
2301 { replace(__p, 1, __i, __j); }
2304 replace(size_t __p, const iterator& __i,
2305 const iterator& __j)
2306 { replace(__p, 1, __i, __j); }
2308 // Erase, (position, size) variant.
2310 erase(size_t __p, size_t __n)
2312 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2314 _S_unref(this->_M_tree_ptr);
2315 this->_M_tree_ptr = __result;
2318 // Erase, single character
2321 { erase(__p, __p + 1); }
2323 // Insert, iterator variants.
2325 insert(const iterator& __p, const rope& __r)
2327 insert(__p.index(), __r);
2332 insert(const iterator& __p, size_t __n, _CharT __c)
2334 insert(__p.index(), __n, __c);
2338 iterator insert(const iterator& __p, _CharT __c)
2340 insert(__p.index(), __c);
2345 insert(const iterator& __p )
2347 insert(__p.index());
2352 insert(const iterator& __p, const _CharT* c_string)
2354 insert(__p.index(), c_string);
2359 insert(const iterator& __p, const _CharT* __i, size_t __n)
2361 insert(__p.index(), __i, __n);
2366 insert(const iterator& __p, const _CharT* __i,
2369 insert(__p.index(), __i, __j);
2374 insert(const iterator& __p,
2375 const const_iterator& __i, const const_iterator& __j)
2377 insert(__p.index(), __i, __j);
2382 insert(const iterator& __p,
2383 const iterator& __i, const iterator& __j)
2385 insert(__p.index(), __i, __j);
2389 // Replace, range variants.
2391 replace(const iterator& __p, const iterator& __q, const rope& __r)
2392 { replace(__p.index(), __q.index() - __p.index(), __r); }
2395 replace(const iterator& __p, const iterator& __q, _CharT __c)
2396 { replace(__p.index(), __q.index() - __p.index(), __c); }
2399 replace(const iterator& __p, const iterator& __q,
2400 const _CharT* __c_string)
2401 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2404 replace(const iterator& __p, const iterator& __q,
2405 const _CharT* __i, size_t __n)
2406 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2409 replace(const iterator& __p, const iterator& __q,
2410 const _CharT* __i, const _CharT* __j)
2411 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2414 replace(const iterator& __p, const iterator& __q,
2415 const const_iterator& __i, const const_iterator& __j)
2416 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2419 replace(const iterator& __p, const iterator& __q,
2420 const iterator& __i, const iterator& __j)
2421 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2423 // Replace, iterator variants.
2425 replace(const iterator& __p, const rope& __r)
2426 { replace(__p.index(), __r); }
2429 replace(const iterator& __p, _CharT __c)
2430 { replace(__p.index(), __c); }
2433 replace(const iterator& __p, const _CharT* __c_string)
2434 { replace(__p.index(), __c_string); }
2437 replace(const iterator& __p, const _CharT* __i, size_t __n)
2438 { replace(__p.index(), __i, __n); }
2441 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2442 { replace(__p.index(), __i, __j); }
2445 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2446 { replace(__p.index(), __i, __j); }
2449 replace(const iterator& __p, iterator __i, iterator __j)
2450 { replace(__p.index(), __i, __j); }
2452 // Iterator and range variants of erase
2454 erase(const iterator& __p, const iterator& __q)
2456 size_t __p_index = __p.index();
2457 erase(__p_index, __q.index() - __p_index);
2458 return iterator(this, __p_index);
2462 erase(const iterator& __p)
2464 size_t __p_index = __p.index();
2465 erase(__p_index, 1);
2466 return iterator(this, __p_index);
2470 substr(size_t __start, size_t __len = 1) const
2472 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2478 substr(iterator __start, iterator __end) const
2480 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2486 substr(iterator __start) const
2488 size_t __pos = __start.index();
2489 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2494 substr(const_iterator __start, const_iterator __end) const
2496 // This might eventually take advantage of the cache in the
2498 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2503 rope<_CharT, _Alloc>
2504 substr(const_iterator __start)
2506 size_t __pos = __start.index();
2507 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2511 static const size_type npos;
2513 size_type find(_CharT __c, size_type __pos = 0) const;
2516 find(const _CharT* __s, size_type __pos = 0) const
2518 size_type __result_pos;
2519 const_iterator __result =
2520 std::search(const_begin() + __pos, const_end(),
2521 __s, __s + _S_char_ptr_len(__s));
2522 __result_pos = __result.index();
2523 #ifndef __STL_OLD_ROPE_SEMANTICS
2524 if (__result_pos == size())
2525 __result_pos = npos;
2527 return __result_pos;
2532 { return(iterator(this, 0)); }
2536 { return(iterator(this, size())); }
2538 typedef reverse_iterator<iterator> reverse_iterator;
2542 { return reverse_iterator(mutable_end()); }
2546 { return reverse_iterator(mutable_begin()); }
2549 mutable_reference_at(size_type __pos)
2550 { return reference(this, __pos); }
2554 operator[] (size_type __pos)
2555 { return _char_ref_proxy(this, __pos); }
2560 // if (__pos >= size()) throw out_of_range; // XXX
2561 return (*this)[__pos];
2564 void resize(size_type __n, _CharT __c) {}
2565 void resize(size_type __n) {}
2566 void reserve(size_type __res_arg = 0) {}
2570 { return max_size(); }
2572 // Stuff below this line is dangerous because it's error prone.
2573 // I would really like to get rid of it.
2574 // copy function with funny arg ordering.
2576 copy(_CharT* __buffer, size_type __n,
2577 size_type __pos = 0) const
2578 { return copy(__pos, __n, __buffer); }
2582 { return mutable_end(); }
2586 { return mutable_begin(); }
2590 { return mutable_rend(); }
2594 { return mutable_rbegin(); }
2599 { return const_end(); }
2603 { return const_begin(); }
2605 const_reverse_iterator
2607 { return const_rend(); }
2609 const_reverse_iterator
2611 { return const_rbegin(); }
2616 template <class _CharT, class _Alloc>
2617 const typename rope<_CharT, _Alloc>::size_type
2618 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2620 template <class _CharT, class _Alloc>
2621 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2622 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2623 { return (__x._M_current_pos == __y._M_current_pos
2624 && __x._M_root == __y._M_root); }
2626 template <class _CharT, class _Alloc>
2627 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2628 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2629 { return (__x._M_current_pos < __y._M_current_pos); }
2631 template <class _CharT, class _Alloc>
2632 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2633 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2634 { return !(__x == __y); }
2636 template <class _CharT, class _Alloc>
2637 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2638 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2639 { return __y < __x; }
2641 template <class _CharT, class _Alloc>
2643 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2644 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2645 { return !(__y < __x); }
2647 template <class _CharT, class _Alloc>
2649 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2650 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2651 { return !(__x < __y); }
2653 template <class _CharT, class _Alloc>
2655 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2656 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2657 { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
2659 template <class _CharT, class _Alloc>
2660 inline _Rope_const_iterator<_CharT, _Alloc>
2661 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2662 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2663 __x._M_current_pos - __n); }
2665 template <class _CharT, class _Alloc>
2666 inline _Rope_const_iterator<_CharT, _Alloc>
2667 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2668 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2669 __x._M_current_pos + __n); }
2671 template <class _CharT, class _Alloc>
2672 inline _Rope_const_iterator<_CharT, _Alloc>
2673 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT, _Alloc>& __x)
2674 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2675 __x._M_current_pos + __n); }
2677 template <class _CharT, class _Alloc>
2679 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2680 const _Rope_iterator<_CharT, _Alloc>& __y)
2681 {return (__x._M_current_pos == __y._M_current_pos
2682 && __x._M_root_rope == __y._M_root_rope); }
2684 template <class _CharT, class _Alloc>
2686 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2687 const _Rope_iterator<_CharT, _Alloc>& __y)
2688 { return (__x._M_current_pos < __y._M_current_pos); }
2690 template <class _CharT, class _Alloc>
2692 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2693 const _Rope_iterator<_CharT, _Alloc>& __y)
2694 { return !(__x == __y); }
2696 template <class _CharT, class _Alloc>
2698 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2699 const _Rope_iterator<_CharT, _Alloc>& __y)
2700 { return __y < __x; }
2702 template <class _CharT, class _Alloc>
2704 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2705 const _Rope_iterator<_CharT, _Alloc>& __y)
2706 { return !(__y < __x); }
2708 template <class _CharT, class _Alloc>
2710 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2711 const _Rope_iterator<_CharT, _Alloc>& __y)
2712 { return !(__x < __y); }
2714 template <class _CharT, class _Alloc>
2716 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2717 const _Rope_iterator<_CharT, _Alloc>& __y)
2718 { return ((ptrdiff_t)__x._M_current_pos
2719 - (ptrdiff_t)__y._M_current_pos); }
2721 template <class _CharT, class _Alloc>
2722 inline _Rope_iterator<_CharT, _Alloc>
2723 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2725 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2726 __x._M_current_pos - __n); }
2728 template <class _CharT, class _Alloc>
2729 inline _Rope_iterator<_CharT, _Alloc>
2730 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2731 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2732 __x._M_current_pos + __n); }
2734 template <class _CharT, class _Alloc>
2735 inline _Rope_iterator<_CharT,_Alloc>
2736 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2737 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2738 __x._M_current_pos + __n); }
2740 template <class _CharT, class _Alloc>
2741 inline rope<_CharT,_Alloc>
2742 operator+(const rope<_CharT, _Alloc>& __left,
2743 const rope<_CharT, _Alloc>& __right)
2745 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2746 _S_concat(__left._M_tree_ptr,
2747 __right._M_tree_ptr));
2748 // Inlining this should make it possible to keep __left and
2749 // __right in registers.
2752 template <class _CharT, class _Alloc>
2753 inline rope<_CharT, _Alloc>&
2754 operator+=(rope<_CharT, _Alloc>& __left,
2755 const rope<_CharT, _Alloc>& __right)
2757 __left.append(__right);
2761 template <class _CharT, class _Alloc>
2762 inline rope<_CharT, _Alloc>
2763 operator+(const rope<_CharT, _Alloc>& __left,
2764 const _CharT* __right)
2766 size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
2767 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2768 _S_concat_char_iter(__left._M_tree_ptr,
2772 template <class _CharT, class _Alloc>
2773 inline rope<_CharT, _Alloc>&
2774 operator+=(rope<_CharT, _Alloc>& __left,
2775 const _CharT* __right)
2777 __left.append(__right);
2781 template <class _CharT, class _Alloc>
2782 inline rope<_CharT, _Alloc>
2783 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2785 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2786 _S_concat_char_iter(__left._M_tree_ptr,
2790 template <class _CharT, class _Alloc>
2791 inline rope<_CharT, _Alloc>&
2792 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2794 __left.append(__right);
2798 template <class _CharT, class _Alloc>
2800 operator<(const rope<_CharT, _Alloc>& __left,
2801 const rope<_CharT, _Alloc>& __right)
2802 { return __left.compare(__right) < 0; }
2804 template <class _CharT, class _Alloc>
2806 operator==(const rope<_CharT, _Alloc>& __left,
2807 const rope<_CharT, _Alloc>& __right)
2808 { return __left.compare(__right) == 0; }
2810 template <class _CharT, class _Alloc>
2812 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2813 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2814 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2816 template <class _CharT, class _Alloc>
2818 operator!=(const rope<_CharT, _Alloc>& __x,
2819 const rope<_CharT, _Alloc>& __y)
2820 { return !(__x == __y); }
2822 template <class _CharT, class _Alloc>
2824 operator>(const rope<_CharT, _Alloc>& __x,
2825 const rope<_CharT, _Alloc>& __y)
2826 { return __y < __x; }
2828 template <class _CharT, class _Alloc>
2830 operator<=(const rope<_CharT, _Alloc>& __x,
2831 const rope<_CharT, _Alloc>& __y)
2832 { return !(__y < __x); }
2834 template <class _CharT, class _Alloc>
2836 operator>=(const rope<_CharT, _Alloc>& __x,
2837 const rope<_CharT, _Alloc>& __y)
2838 { return !(__x < __y); }
2840 template <class _CharT, class _Alloc>
2842 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2843 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2844 { return !(__x == __y); }
2846 template<class _CharT, class _Traits, class _Alloc>
2847 std::basic_ostream<_CharT, _Traits>&
2848 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2849 const rope<_CharT, _Alloc>& __r);
2851 typedef rope<char> crope;
2852 typedef rope<wchar_t> wrope;
2854 inline crope::reference
2855 __mutable_reference_at(crope& __c, size_t __i)
2856 { return __c.mutable_reference_at(__i); }
2858 inline wrope::reference
2859 __mutable_reference_at(wrope& __c, size_t __i)
2860 { return __c.mutable_reference_at(__i); }
2862 template <class _CharT, class _Alloc>
2864 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2867 // Hash functions should probably be revisited later:
2872 operator()(const crope& __str) const
2874 size_t __size = __str.size();
2878 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2887 operator()(const wrope& __str) const
2889 size_t __size = __str.size();
2893 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2897 } // namespace __gnu_cxx
2899 # include <ext/ropeimpl.h>