1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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). You should only
46 * include this header if you are using GCC 3 or later.
52 #include <bits/stl_algobase.h>
53 #include <bits/stl_construct.h>
54 #include <bits/stl_uninitialized.h>
55 #include <bits/stl_algo.h>
56 #include <bits/stl_function.h>
57 #include <bits/stl_numeric.h>
58 #include <bits/allocator.h>
59 #include <ext/hash_fun.h>
62 # define __GC_CONST const
64 # include <bits/gthr.h>
65 # define __GC_CONST // constant except for deallocation
68 #include <ext/memory> // For uninitialized_copy_n
76 using std::reverse_iterator;
79 // The _S_eos function is used for those functions that
80 // convert to/from C-like strings to detect the end of the string.
82 // The end-of-C-string character.
83 // This is what the draft standard says it should be.
84 template <class _CharT>
89 // Test for basic character types.
90 // For basic character types leaves having a trailing eos.
91 template <class _CharT>
93 _S_is_basic_char_type(_CharT*)
96 template <class _CharT>
98 _S_is_one_byte_char_type(_CharT*)
102 _S_is_basic_char_type(char*)
106 _S_is_one_byte_char_type(char*)
110 _S_is_basic_char_type(wchar_t*)
113 // Store an eos iff _CharT is a basic character type.
114 // Do not reference _S_eos if it isn't.
115 template <class _CharT>
117 _S_cond_store_eos(_CharT&) { }
120 _S_cond_store_eos(char& __c)
124 _S_cond_store_eos(wchar_t& __c)
127 // char_producers are logically functions that generate a section of
128 // a string. These can be convereted to ropes. The resulting rope
129 // invokes the char_producer on demand. This allows, for example,
130 // files to be viewed as ropes without reading the entire file.
131 template <class _CharT>
135 virtual ~char_producer() {};
138 operator()(size_t __start_pos, size_t __len,
139 _CharT* __buffer) = 0;
140 // Buffer should really be an arbitrary output iterator.
141 // That way we could flatten directly into an ostream, etc.
142 // This is thoroughly impossible, since iterator types don't
143 // have runtime descriptions.
148 // Sequence must provide an append operation that appends an
149 // array to the sequence. Sequence buffers are useful only if
150 // appending an entire array is cheaper than appending element by element.
151 // This is true for many string representations.
152 // This should perhaps inherit from ostream<sequence::value_type>
153 // and be implemented correspondingly, so that they can be used
154 // for formatted. For the sake of portability, we don't do this yet.
156 // For now, sequence buffers behave as output iterators. But they also
157 // behave a little like basic_ostringstream<sequence::value_type> and a
158 // little like containers.
160 template<class _Sequence, size_t _Buf_sz = 100>
161 class sequence_buffer
162 : public iterator<std::output_iterator_tag, void, void, void, void>
165 typedef typename _Sequence::value_type value_type;
167 _Sequence* _M_prefix;
168 value_type _M_buffer[_Buf_sz];
175 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
183 : _M_prefix(0), _M_buf_count(0) { }
185 sequence_buffer(const sequence_buffer& __x)
187 _M_prefix = __x._M_prefix;
188 _M_buf_count = __x._M_buf_count;
189 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
192 sequence_buffer(sequence_buffer& __x)
195 _M_prefix = __x._M_prefix;
199 sequence_buffer(_Sequence& __s)
200 : _M_prefix(&__s), _M_buf_count(0) { }
203 operator=(sequence_buffer& __x)
206 _M_prefix = __x._M_prefix;
212 operator=(const sequence_buffer& __x)
214 _M_prefix = __x._M_prefix;
215 _M_buf_count = __x._M_buf_count;
216 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
221 push_back(value_type __x)
223 if (_M_buf_count < _Buf_sz)
225 _M_buffer[_M_buf_count] = __x;
237 append(value_type* __s, size_t __len)
239 if (__len + _M_buf_count <= _Buf_sz)
241 size_t __i = _M_buf_count;
242 for (size_t __j = 0; __j < __len; __i++, __j++)
243 _M_buffer[__i] = __s[__j];
244 _M_buf_count += __len;
246 else if (0 == _M_buf_count)
247 _M_prefix->append(__s, __s + __len);
256 write(value_type* __s, size_t __len)
270 operator=(const value_type& __rhs)
289 // The following should be treated as private, at least for now.
290 template<class _CharT>
291 class _Rope_char_consumer
294 // If we had member templates, these should not be virtual.
295 // For now we need to use run-time parametrization where
296 // compile-time would do. Hence this should all be private
298 // The symmetry with char_producer is accidental and temporary.
299 virtual ~_Rope_char_consumer() {};
302 operator()(const _CharT* __buffer, size_t __len) = 0;
305 // First a lot of forward declarations. The standard seems to require
306 // much stricter "declaration before use" than many of the implementations
308 template<class _CharT, class _Alloc = allocator<_CharT> >
311 template<class _CharT, class _Alloc>
312 struct _Rope_RopeConcatenation;
314 template<class _CharT, class _Alloc>
315 struct _Rope_RopeLeaf;
317 template<class _CharT, class _Alloc>
318 struct _Rope_RopeFunction;
320 template<class _CharT, class _Alloc>
321 struct _Rope_RopeSubstring;
323 template<class _CharT, class _Alloc>
324 class _Rope_iterator;
326 template<class _CharT, class _Alloc>
327 class _Rope_const_iterator;
329 template<class _CharT, class _Alloc>
330 class _Rope_char_ref_proxy;
332 template<class _CharT, class _Alloc>
333 class _Rope_char_ptr_proxy;
335 template<class _CharT, class _Alloc>
337 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
338 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
340 template<class _CharT, class _Alloc>
341 _Rope_const_iterator<_CharT, _Alloc>
342 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
345 template<class _CharT, class _Alloc>
346 _Rope_const_iterator<_CharT, _Alloc>
347 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
350 template<class _CharT, class _Alloc>
351 _Rope_const_iterator<_CharT, _Alloc>
352 operator+(ptrdiff_t __n,
353 const _Rope_const_iterator<_CharT, _Alloc>& __x);
355 template<class _CharT, class _Alloc>
357 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
358 const _Rope_const_iterator<_CharT, _Alloc>& __y);
360 template<class _CharT, class _Alloc>
362 operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
363 const _Rope_const_iterator<_CharT, _Alloc>& __y);
365 template<class _CharT, class _Alloc>
367 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
368 const _Rope_const_iterator<_CharT, _Alloc>& __y);
370 template<class _CharT, class _Alloc>
371 _Rope_iterator<_CharT, _Alloc>
372 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
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+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
382 template<class _CharT, class _Alloc>
384 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
385 const _Rope_iterator<_CharT, _Alloc>& __y);
387 template<class _CharT, class _Alloc>
389 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
390 const _Rope_iterator<_CharT, _Alloc>& __y);
392 template<class _CharT, class _Alloc>
394 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
395 const _Rope_iterator<_CharT, _Alloc>& __y);
397 template<class _CharT, class _Alloc>
399 operator+(const rope<_CharT, _Alloc>& __left,
400 const rope<_CharT, _Alloc>& __right);
402 template<class _CharT, class _Alloc>
404 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
406 template<class _CharT, class _Alloc>
408 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
410 // Some helpers, so we can use power on ropes.
411 // See below for why this isn't local to the implementation.
413 // This uses a nonstandard refcount convention.
414 // The result has refcount 0.
415 template<class _CharT, class _Alloc>
416 struct _Rope_Concat_fn
417 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
418 rope<_CharT, _Alloc> >
421 operator()(const rope<_CharT, _Alloc>& __x,
422 const rope<_CharT, _Alloc>& __y)
423 { return __x + __y; }
426 template <class _CharT, class _Alloc>
427 inline rope<_CharT, _Alloc>
428 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
429 { return rope<_CharT, _Alloc>(); }
431 // Class _Refcount_Base provides a type, _RC_t, a data member,
432 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
433 // atomic preincrement/predecrement. The constructor initializes
435 struct _Refcount_Base
438 typedef size_t _RC_t;
440 // The data member _M_ref_count
441 volatile _RC_t _M_ref_count;
444 __gthread_mutex_t _M_ref_count_lock;
446 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
448 #ifdef __GTHREAD_MUTEX_INIT
449 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
450 _M_ref_count_lock = __tmp;
451 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
452 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
454 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
461 __gthread_mutex_lock(&_M_ref_count_lock);
463 __gthread_mutex_unlock(&_M_ref_count_lock);
469 __gthread_mutex_lock(&_M_ref_count_lock);
470 volatile _RC_t __tmp = --_M_ref_count;
471 __gthread_mutex_unlock(&_M_ref_count_lock);
477 // What follows should really be local to rope. Unfortunately,
478 // that doesn't work, since it makes it impossible to define generic
479 // equality on rope iterators. According to the draft standard, the
480 // template parameters for such an equality operator cannot be inferred
481 // from the occurrence of a member class as a parameter.
482 // (SGI compilers in fact allow this, but the __result wouldn't be
484 // Similarly, some of the static member functions are member functions
485 // only to avoid polluting the global namespace, and to circumvent
486 // restrictions on type inference for template functions.
490 // The internal data structure for representing a rope. This is
491 // private to the implementation. A rope is really just a pointer
494 // A few basic functions for manipulating this data structure
495 // are members of _RopeRep. Most of the more complex algorithms
496 // are implemented as rope members.
498 // Some of the static member functions of _RopeRep have identically
499 // named functions in rope that simply invoke the _RopeRep versions.
501 #define __ROPE_DEFINE_ALLOCS(__a) \
502 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
503 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
504 __ROPE_DEFINE_ALLOC(__C,_C) \
505 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
506 __ROPE_DEFINE_ALLOC(__L,_L) \
507 typedef _Rope_RopeFunction<_CharT,__a> __F; \
508 __ROPE_DEFINE_ALLOC(__F,_F) \
509 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
510 __ROPE_DEFINE_ALLOC(__S,_S)
512 // Internal rope nodes potentially store a copy of the allocator
513 // instance used to allocate them. This is mostly redundant.
514 // But the alternative would be to pass allocator instances around
515 // in some form to nearly all internal functions, since any pointer
516 // assignment may result in a zero reference count and thus require
519 #define __STATIC_IF_SGI_ALLOC /* not static */
521 template <class _CharT, class _Alloc>
522 struct _Rope_rep_base
525 typedef _Alloc allocator_type;
528 get_allocator() const
529 { return *static_cast<const _Alloc*>(this); }
531 _Rope_rep_base(size_t __size, const allocator_type&)
536 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
538 _Alloc::template rebind<_Tp>::other __name##Alloc; \
539 static _Tp* __name##_allocate(size_t __n) \
540 { return __name##Alloc().allocate(__n); } \
541 static void __name##_deallocate(_Tp *__p, size_t __n) \
542 { __name##Alloc().deallocate(__p, __n); }
543 __ROPE_DEFINE_ALLOCS(_Alloc)
544 # undef __ROPE_DEFINE_ALLOC
547 namespace _Rope_constants
549 enum { _S_max_rope_depth = 45 };
550 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
553 template<class _CharT, class _Alloc>
555 : public _Rope_rep_base<_CharT, _Alloc>
561 _Rope_constants::_Tag _M_tag:8;
562 bool _M_is_balanced:8;
563 unsigned char _M_depth;
564 __GC_CONST _CharT* _M_c_string;
565 __gthread_mutex_t _M_c_string_lock;
566 /* Flattened version of string, if needed. */
568 /* If it's not 0, then the memory is owned */
570 /* In the case of a leaf, this may point to */
571 /* the same memory as the data field. */
572 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
575 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
577 _Rope_RopeRep(_Rope_constants::_Tag __t, int __d, bool __b, size_t __size,
579 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
583 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
584 #ifdef __GTHREAD_MUTEX_INIT
586 // Do not copy a POSIX/gthr mutex once in use. However, bits are bits.
587 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
588 _M_c_string_lock = __tmp;
591 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
598 _S_free_string(__GC_CONST _CharT*, size_t __len,
600 #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
601 // Deallocate data section of a leaf.
602 // This shouldn't be a member function.
603 // But its hard to do anything else at the
604 // moment, because it's templatized w.r.t.
606 // Does nothing if __GC is defined.
608 void _M_free_c_string();
610 // Deallocate t. Assumes t is not 0.
623 _S_unref(_Rope_RopeRep* __t)
626 __t->_M_unref_nonnil();
630 _S_ref(_Rope_RopeRep* __t)
637 _S_free_if_unref(_Rope_RopeRep* __t)
639 if (0 != __t && 0 == __t->_M_ref_count)
643 void _M_unref_nonnil() {}
644 void _M_ref_nonnil() {}
645 static void _S_unref(_Rope_RopeRep*) {}
646 static void _S_ref(_Rope_RopeRep*) {}
647 static void _S_free_if_unref(_Rope_RopeRep*) {}
651 operator=(const _Rope_RopeRep&);
653 _Rope_RopeRep(const _Rope_RopeRep&);
656 template<class _CharT, class _Alloc>
657 struct _Rope_RopeLeaf
658 : public _Rope_RopeRep<_CharT, _Alloc>
661 // Apparently needed by VC++
662 // The data fields of leaves are allocated with some
663 // extra space, to accommodate future growth and for basic
664 // character types, to hold a trailing eos character.
665 enum { _S_alloc_granularity = 8 };
668 _S_rounded_up_size(size_t __n)
670 size_t __size_with_eos;
672 if (_S_is_basic_char_type((_CharT*)0))
673 __size_with_eos = __n + 1;
675 __size_with_eos = __n;
677 return __size_with_eos;
679 // Allow slop for in-place expansion.
680 return ((__size_with_eos + _S_alloc_granularity - 1)
681 &~ (_S_alloc_granularity - 1));
684 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
685 /* The allocated size is */
686 /* _S_rounded_up_size(size), except */
687 /* in the GC case, in which it */
688 /* doesn't matter. */
689 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
692 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size,
694 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_leaf, 0, true,
695 __size, __a), _M_data(__d)
697 if (_S_is_basic_char_type((_CharT *)0))
699 // already eos terminated.
700 this->_M_c_string = __d;
703 // The constructor assumes that d has been allocated with
704 // the proper allocator and the properly padded size.
705 // In contrast, the destructor deallocates the data:
707 ~_Rope_RopeLeaf() throw()
709 if (_M_data != this->_M_c_string)
710 this->_M_free_c_string();
712 __STL_FREE_STRING(_M_data, this->_M_size, this->get_allocator());
717 operator=(const _Rope_RopeLeaf&);
719 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
722 template<class _CharT, class _Alloc>
723 struct _Rope_RopeConcatenation
724 : public _Rope_RopeRep<_CharT, _Alloc>
727 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
728 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
730 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
733 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
734 _Rope_RopeRep<_CharT, _Alloc>* __r,
736 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_concat,
737 std::max(__l->_M_depth,
740 __l->_M_size + __r->_M_size, __a),
741 _M_left(__l), _M_right(__r)
744 ~_Rope_RopeConcatenation() throw()
746 this->_M_free_c_string();
747 _M_left->_M_unref_nonnil();
748 _M_right->_M_unref_nonnil();
752 _Rope_RopeConcatenation&
753 operator=(const _Rope_RopeConcatenation&);
755 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
758 template<class _CharT, class _Alloc>
759 struct _Rope_RopeFunction
760 : public _Rope_RopeRep<_CharT, _Alloc>
763 char_producer<_CharT>* _M_fn;
765 bool _M_delete_when_done; // Char_producer is owned by the
766 // rope and should be explicitly
767 // deleted when the rope becomes
770 // In the GC case, we either register the rope for
771 // finalization, or not. Thus the field is unnecessary;
772 // the information is stored in the collector data structures.
773 // We do need a finalization procedure to be invoked by the
776 _S_fn_finalization_proc(void * __tree, void *)
777 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
779 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
782 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
783 bool __d, allocator_type __a)
784 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_function,
785 0, true, __size, __a)
788 , _M_delete_when_done(__d)
794 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
795 _S_fn_finalization_proc, 0, 0, 0);
800 ~_Rope_RopeFunction() throw()
802 this->_M_free_c_string();
803 if (_M_delete_when_done)
809 operator=(const _Rope_RopeFunction&);
811 _Rope_RopeFunction(const _Rope_RopeFunction&);
813 // Substring results are usually represented using just
814 // concatenation nodes. But in the case of very long flat ropes
815 // or ropes with a functional representation that isn't practical.
816 // In that case, we represent the __result as a special case of
817 // RopeFunction, whose char_producer points back to the rope itself.
818 // In all cases except repeated substring operations and
819 // deallocation, we treat the __result as a RopeFunction.
820 template<class _CharT, class _Alloc>
821 struct _Rope_RopeSubstring
822 : public _Rope_RopeFunction<_CharT, _Alloc>,
823 public char_producer<_CharT>
826 // XXX this whole class should be rewritten.
827 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
831 operator()(size_t __start_pos, size_t __req_len,
834 switch(_M_base->_M_tag)
836 case _Rope_constants::_S_function:
837 case _Rope_constants::_S_substringfn:
839 char_producer<_CharT>* __fn =
840 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
841 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
844 case _Rope_constants::_S_leaf:
846 __GC_CONST _CharT* __s =
847 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
848 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
857 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
860 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_t __s,
861 size_t __l, allocator_type __a)
862 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
863 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
866 _M_base->_M_ref_nonnil();
868 this->_M_tag = _Rope_constants::_S_substringfn;
870 virtual ~_Rope_RopeSubstring() throw()
873 _M_base->_M_unref_nonnil();
874 // _M_free_c_string(); -- done by parent class
879 // Self-destructing pointers to Rope_rep.
880 // These are not conventional smart pointers. Their
881 // only purpose in life is to ensure that unref is called
882 // on the pointer either at normal exit or if an exception
883 // is raised. It is the caller's responsibility to
884 // adjust reference counts when these pointers are initialized
885 // or assigned to. (This convention significantly reduces
886 // the number of potentially expensive reference count
889 template<class _CharT, class _Alloc>
890 struct _Rope_self_destruct_ptr
892 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
894 ~_Rope_self_destruct_ptr()
895 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
897 _Rope_self_destruct_ptr() : _M_ptr(0) {};
899 _Rope_self_destruct_ptr() {};
901 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
904 _Rope_RopeRep<_CharT, _Alloc>&
908 _Rope_RopeRep<_CharT, _Alloc>*
912 operator _Rope_RopeRep<_CharT, _Alloc>*()
915 _Rope_self_destruct_ptr&
916 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
917 { _M_ptr = __x; return *this; }
921 // Dereferencing a nonconst iterator has to return something
922 // that behaves almost like a reference. It's not possible to
923 // return an actual reference since assignment requires extra
924 // work. And we would get into the same problems as with the
925 // CD2 version of basic_string.
926 template<class _CharT, class _Alloc>
927 class _Rope_char_ref_proxy
929 friend class rope<_CharT, _Alloc>;
930 friend class _Rope_iterator<_CharT, _Alloc>;
931 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
933 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
935 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
937 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
938 typedef rope<_CharT, _Alloc> _My_rope;
941 bool _M_current_valid;
942 _My_rope* _M_root; // The whole rope.
944 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
945 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) {}
947 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
948 : _M_pos(__x._M_pos), _M_current(__x._M_current), _M_current_valid(false),
949 _M_root(__x._M_root) {}
951 // Don't preserve cache if the reference can outlive the
952 // expression. We claim that's not possible without calling
953 // a copy constructor or generating reference to a proxy
954 // reference. We declare the latter to have undefined semantics.
955 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
956 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
958 inline operator _CharT () const;
960 _Rope_char_ref_proxy&
961 operator=(_CharT __c);
963 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
965 _Rope_char_ref_proxy&
966 operator=(const _Rope_char_ref_proxy& __c)
967 { return operator=((_CharT)__c); }
970 template<class _CharT, class __Alloc>
972 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
973 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
980 template<class _CharT, class _Alloc>
981 class _Rope_char_ptr_proxy
983 // XXX this class should be rewritten.
984 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
986 rope<_CharT,_Alloc>* _M_root; // The whole rope.
988 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
989 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
991 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
992 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
994 _Rope_char_ptr_proxy() {}
996 _Rope_char_ptr_proxy(_CharT* __x)
997 : _M_root(0), _M_pos(0) { }
999 _Rope_char_ptr_proxy&
1000 operator=(const _Rope_char_ptr_proxy& __x)
1002 _M_pos = __x._M_pos;
1003 _M_root = __x._M_root;
1007 template<class _CharT2, class _Alloc2>
1009 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1010 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1012 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1013 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1017 // Unlike in the C version, we cache only part of the stack
1018 // for rope iterators, since they must be efficiently copyable.
1019 // When we run out of cache, we have to reconstruct the iterator
1021 // Pointers from iterators are not included in reference counts.
1022 // Iterators are assumed to be thread private. Ropes can
1025 template<class _CharT, class _Alloc>
1026 class _Rope_iterator_base
1027 : public iterator<std::random_access_iterator_tag, _CharT>
1029 friend class rope<_CharT, _Alloc>;
1031 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1032 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1033 // Borland doesn't want this to be protected.
1035 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1036 enum { _S_iterator_buf_len = 15 };
1037 size_t _M_current_pos;
1038 _RopeRep* _M_root; // The whole rope.
1039 size_t _M_leaf_pos; // Starting position for current leaf
1040 __GC_CONST _CharT* _M_buf_start;
1042 // containing current char.
1043 __GC_CONST _CharT* _M_buf_ptr;
1044 // Pointer to current char in buffer.
1045 // != 0 ==> buffer valid.
1046 __GC_CONST _CharT* _M_buf_end;
1047 // One past __last valid char in buffer.
1048 // What follows is the path cache. We go out of our
1049 // way to make this compact.
1050 // Path_end contains the bottom section of the path from
1051 // the root to the current leaf.
1052 const _RopeRep* _M_path_end[_S_path_cache_len];
1053 int _M_leaf_index; // Last valid __pos in path_end;
1054 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1055 // point to concatenation nodes.
1056 unsigned char _M_path_directions;
1057 // (path_directions >> __i) & 1 is 1
1058 // iff we got from _M_path_end[leaf_index - __i - 1]
1059 // to _M_path_end[leaf_index - __i] by going to the
1060 // __right. Assumes path_cache_len <= 9.
1061 _CharT _M_tmp_buf[_S_iterator_buf_len];
1062 // Short buffer for surrounding chars.
1063 // This is useful primarily for
1064 // RopeFunctions. We put the buffer
1065 // here to avoid locking in the
1066 // multithreaded case.
1067 // The cached path is generally assumed to be valid
1068 // only if the buffer is valid.
1069 static void _S_setbuf(_Rope_iterator_base& __x);
1070 // Set buffer contents given
1072 static void _S_setcache(_Rope_iterator_base& __x);
1073 // Set buffer contents and
1075 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1076 // As above, but assumes path
1077 // cache is valid for previous posn.
1078 _Rope_iterator_base() {}
1080 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
1081 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) {}
1083 void _M_incr(size_t __n);
1084 void _M_decr(size_t __n);
1088 { return _M_current_pos; }
1090 _Rope_iterator_base(const _Rope_iterator_base& __x)
1092 if (0 != __x._M_buf_ptr)
1096 _M_current_pos = __x._M_current_pos;
1097 _M_root = __x._M_root;
1103 template<class _CharT, class _Alloc>
1104 class _Rope_iterator;
1106 template<class _CharT, class _Alloc>
1107 class _Rope_const_iterator
1108 : public _Rope_iterator_base<_CharT, _Alloc>
1110 friend class rope<_CharT, _Alloc>;
1112 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1113 // The one from the base class may not be directly visible.
1114 _Rope_const_iterator(const _RopeRep* __root, size_t __pos)
1115 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1117 // Only nonconst iterators modify root ref count
1120 typedef _CharT reference; // Really a value. Returning a reference
1121 // Would be a mess, since it would have
1122 // to be included in refcount.
1123 typedef const _CharT* pointer;
1126 _Rope_const_iterator() {};
1128 _Rope_const_iterator(const _Rope_const_iterator& __x)
1129 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1131 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1133 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, size_t __pos)
1134 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1136 _Rope_const_iterator&
1137 operator=(const _Rope_const_iterator& __x)
1139 if (0 != __x._M_buf_ptr)
1140 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1143 this->_M_current_pos = __x._M_current_pos;
1144 this->_M_root = __x._M_root;
1145 this->_M_buf_ptr = 0;
1153 if (0 == this->_M_buf_ptr)
1155 return *this->_M_buf_ptr;
1158 _Rope_const_iterator&
1161 __GC_CONST _CharT* __next;
1162 if (0 != this->_M_buf_ptr
1163 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1165 this->_M_buf_ptr = __next;
1166 ++this->_M_current_pos;
1173 _Rope_const_iterator&
1174 operator+=(ptrdiff_t __n)
1179 this->_M_decr(-__n);
1183 _Rope_const_iterator&
1190 _Rope_const_iterator&
1191 operator-=(ptrdiff_t __n)
1196 this->_M_incr(-__n);
1200 _Rope_const_iterator
1203 size_t __old_pos = this->_M_current_pos;
1205 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1206 // This makes a subsequent dereference expensive.
1207 // Perhaps we should instead copy the iterator
1208 // if it has a valid cache?
1211 _Rope_const_iterator
1214 size_t __old_pos = this->_M_current_pos;
1216 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1219 template<class _CharT2, class _Alloc2>
1220 friend _Rope_const_iterator<_CharT2, _Alloc2>
1221 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1224 template<class _CharT2, class _Alloc2>
1225 friend _Rope_const_iterator<_CharT2, _Alloc2>
1226 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1229 template<class _CharT2, class _Alloc2>
1230 friend _Rope_const_iterator<_CharT2, _Alloc2>
1231 operator+(ptrdiff_t __n,
1232 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1235 operator[](size_t __n)
1236 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1237 this->_M_current_pos + __n); }
1239 template<class _CharT2, class _Alloc2>
1241 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1242 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1244 template<class _CharT2, class _Alloc2>
1246 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1247 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1249 template<class _CharT2, class _Alloc2>
1251 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1252 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1255 template<class _CharT, class _Alloc>
1256 class _Rope_iterator
1257 : public _Rope_iterator_base<_CharT, _Alloc>
1259 friend class rope<_CharT, _Alloc>;
1261 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1262 rope<_CharT, _Alloc>* _M_root_rope;
1263 // root is treated as a cached version of this,
1264 // and is used to detect changes to the underlying
1266 // Root is included in the reference count.
1267 // This is necessary so that we can detect changes reliably.
1268 // Unfortunately, it requires careful bookkeeping for the
1270 _Rope_iterator(rope<_CharT, _Alloc>* __r, size_t __pos)
1271 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1273 { _RopeRep::_S_ref(this->_M_root);
1274 if (!(__r -> empty()))
1280 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1281 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1284 rope<_CharT, _Alloc>&
1286 { return *_M_root_rope; }
1290 this->_M_root = 0; // Needed for reference counting.
1293 _Rope_iterator(const _Rope_iterator& __x)
1294 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1296 _M_root_rope = __x._M_root_rope;
1297 _RopeRep::_S_ref(this->_M_root);
1300 _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos);
1303 { _RopeRep::_S_unref(this->_M_root); }
1306 operator=(const _Rope_iterator& __x)
1308 _RopeRep* __old = this->_M_root;
1310 _RopeRep::_S_ref(__x._M_root);
1311 if (0 != __x._M_buf_ptr)
1313 _M_root_rope = __x._M_root_rope;
1314 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1318 this->_M_current_pos = __x._M_current_pos;
1319 this->_M_root = __x._M_root;
1320 _M_root_rope = __x._M_root_rope;
1321 this->_M_buf_ptr = 0;
1323 _RopeRep::_S_unref(__old);
1331 if (0 == this->_M_buf_ptr)
1332 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1333 this->_M_current_pos);
1335 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1336 this->_M_current_pos,
1348 operator+=(ptrdiff_t __n)
1353 this->_M_decr(-__n);
1365 operator-=(ptrdiff_t __n)
1370 this->_M_incr(-__n);
1377 size_t __old_pos = this->_M_current_pos;
1379 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1385 size_t __old_pos = this->_M_current_pos;
1387 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1391 operator[](ptrdiff_t __n)
1392 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1393 this->_M_current_pos
1396 template<class _CharT2, class _Alloc2>
1398 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1399 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1401 template<class _CharT2, class _Alloc2>
1403 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1404 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1406 template<class _CharT2, class _Alloc2>
1408 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1409 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1411 template<class _CharT2, class _Alloc2>
1412 friend _Rope_iterator<_CharT2, _Alloc2>
1413 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1415 template<class _CharT2, class _Alloc2>
1416 friend _Rope_iterator<_CharT2, _Alloc2>
1417 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1419 template<class _CharT2, class _Alloc2>
1420 friend _Rope_iterator<_CharT2, _Alloc2>
1421 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT2, _Alloc2>& __x);
1425 template <class _CharT, class _Alloc>
1429 typedef _Alloc allocator_type;
1432 get_allocator() const
1433 { return *static_cast<const _Alloc*>(this); }
1435 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1436 // The one in _Base may not be visible due to template rules.
1438 _Rope_base(_RopeRep* __t, const allocator_type&)
1439 : _M_tree_ptr(__t) {}
1441 _Rope_base(const allocator_type&) {}
1443 // The only data member of a rope:
1444 _RopeRep *_M_tree_ptr;
1446 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1448 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1449 static _Tp* __name##_allocate(size_t __n) \
1450 { return __name##Alloc().allocate(__n); } \
1451 static void __name##_deallocate(_Tp *__p, size_t __n) \
1452 { __name##Alloc().deallocate(__p, __n); }
1453 __ROPE_DEFINE_ALLOCS(_Alloc)
1454 #undef __ROPE_DEFINE_ALLOC
1458 operator=(const _Rope_base&);
1460 _Rope_base(const _Rope_base&);
1464 * This is an SGI extension.
1465 * @ingroup SGIextensions
1468 template <class _CharT, class _Alloc>
1469 class rope : public _Rope_base<_CharT, _Alloc>
1472 typedef _CharT value_type;
1473 typedef ptrdiff_t difference_type;
1474 typedef size_t size_type;
1475 typedef _CharT const_reference;
1476 typedef const _CharT* const_pointer;
1477 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1478 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1479 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1480 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1482 friend class _Rope_iterator<_CharT, _Alloc>;
1483 friend class _Rope_const_iterator<_CharT, _Alloc>;
1484 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1485 friend class _Rope_iterator_base<_CharT, _Alloc>;
1486 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1487 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1488 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1491 typedef _Rope_base<_CharT, _Alloc> _Base;
1492 typedef typename _Base::allocator_type allocator_type;
1493 using _Base::_M_tree_ptr;
1494 using _Base::get_allocator;
1495 typedef __GC_CONST _CharT* _Cstrptr;
1497 static _CharT _S_empty_c_str[1];
1501 { return __c == _S_eos((_CharT*)0); }
1503 enum { _S_copy_max = 23 };
1504 // For strings shorter than _S_copy_max, we copy to
1507 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1508 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1509 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1510 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1511 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1513 // Retrieve a character at the indicated position.
1514 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1517 // Obtain a pointer to the character at the indicated position.
1518 // The pointer can be used to change the character.
1519 // If such a pointer cannot be produced, as is frequently the
1520 // case, 0 is returned instead.
1521 // (Returns nonzero only if all nodes in the path have a refcount
1523 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1527 _S_apply_to_pieces(// should be template parameter
1528 _Rope_char_consumer<_CharT>& __c,
1529 const _RopeRep* __r,
1530 size_t __begin, size_t __end);
1531 // begin and end are assumed to be in range.
1535 _S_unref(_RopeRep* __t)
1536 { _RopeRep::_S_unref(__t); }
1539 _S_ref(_RopeRep* __t)
1540 { _RopeRep::_S_ref(__t); }
1543 static void _S_unref(_RopeRep*) {}
1544 static void _S_ref(_RopeRep*) {}
1548 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1550 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1553 // _Result is counted in refcount.
1554 static _RopeRep* _S_substring(_RopeRep* __base,
1555 size_t __start, size_t __endp1);
1557 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1558 const _CharT* __iter, size_t __slen);
1559 // Concatenate rope and char ptr, copying __s.
1560 // Should really take an arbitrary iterator.
1561 // Result is counted in refcount.
1562 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1563 const _CharT* __iter,
1565 // As above, but one reference to __r is about to be
1566 // destroyed. Thus the pieces may be recycled if all
1567 // relevant reference counts are 1.
1569 // We can't really do anything since refcounts are unavailable.
1570 { return _S_concat_char_iter(__r, __iter, __slen); }
1575 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1576 // General concatenation on _RopeRep. _Result
1577 // has refcount of 1. Adjusts argument refcounts.
1581 apply_to_pieces(size_t __begin, size_t __end,
1582 _Rope_char_consumer<_CharT>& __c) const
1583 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1588 _S_rounded_up_size(size_t __n)
1589 { return _RopeLeaf::_S_rounded_up_size(__n); }
1592 _S_allocated_capacity(size_t __n)
1594 if (_S_is_basic_char_type((_CharT*)0))
1595 return _S_rounded_up_size(__n) - 1;
1597 return _S_rounded_up_size(__n);
1601 // Allocate and construct a RopeLeaf using the supplied allocator
1602 // Takes ownership of s instead of copying.
1604 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1605 size_t __size, allocator_type __a)
1607 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1608 return new(__space) _RopeLeaf(__s, __size, __a);
1611 static _RopeConcatenation*
1612 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1615 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1616 return new(__space) _RopeConcatenation(__left, __right, __a);
1619 static _RopeFunction*
1620 _S_new_RopeFunction(char_producer<_CharT>* __f,
1621 size_t __size, bool __d, allocator_type __a)
1623 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1624 return new(__space) _RopeFunction(__f, __size, __d, __a);
1627 static _RopeSubstring*
1628 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1629 size_t __l, allocator_type __a)
1631 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1632 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1636 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1637 size_t __size, allocator_type __a)
1638 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1639 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1643 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1645 uninitialized_copy_n(__s, __size, __buf);
1646 _S_cond_store_eos(__buf[__size]);
1648 { return _S_new_RopeLeaf(__buf, __size, __a); }
1651 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1652 __throw_exception_again;
1656 // Concatenation of nonempty strings.
1657 // Always builds a concatenation node.
1658 // Rebalances if the result is too deep.
1659 // Result has refcount 1.
1660 // Does not increment left and right ref counts even though
1661 // they are referenced.
1663 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1665 // Concatenation helper functions
1667 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1668 const _CharT* __iter, size_t __slen);
1669 // Concatenate by copying leaf.
1670 // should take an arbitrary iterator
1671 // result has refcount 1.
1674 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1675 const _CharT* __iter, size_t __slen);
1676 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1681 static size_t _S_char_ptr_len(const _CharT* __s);
1682 // slightly generalized strlen
1684 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1685 : _Base(__t, __a) { }
1688 // Copy __r to the _CharT buffer.
1689 // Returns __buffer + __r->_M_size.
1690 // Assumes that buffer is uninitialized.
1691 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1693 // Again, with explicit starting position and length.
1694 // Assumes that buffer is uninitialized.
1695 static _CharT* _S_flatten(_RopeRep* __r,
1696 size_t __start, size_t __len,
1699 static const unsigned long
1700 _S_min_len[_Rope_constants::_S_max_rope_depth + 1];
1703 _S_is_balanced(_RopeRep* __r)
1704 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1707 _S_is_almost_balanced(_RopeRep* __r)
1708 { return (__r->_M_depth == 0
1709 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1712 _S_is_roughly_balanced(_RopeRep* __r)
1713 { return (__r->_M_depth <= 1
1714 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1716 // Assumes the result is not empty.
1718 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1720 _RopeRep* __result = _S_concat(__left, __right);
1721 if (_S_is_balanced(__result))
1722 __result->_M_is_balanced = true;
1726 // The basic rebalancing operation. Logically copies the
1727 // rope. The result has refcount of 1. The client will
1728 // usually decrement the reference count of __r.
1729 // The result is within height 2 of balanced by the above
1731 static _RopeRep* _S_balance(_RopeRep* __r);
1733 // Add all unbalanced subtrees to the forest of balanceed trees.
1734 // Used only by balance.
1735 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1737 // Add __r to forest, assuming __r is already balanced.
1738 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1740 // Print to stdout, exposing structure
1741 static void _S_dump(_RopeRep* __r, int __indent = 0);
1743 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1744 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1749 { return 0 == this->_M_tree_ptr; }
1751 // Comparison member function. This is public only for those
1752 // clients that need a ternary comparison. Others
1753 // should use the comparison operators below.
1755 compare(const rope& __y) const
1756 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1758 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1759 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1763 rope(const _CharT* __s, size_t __len,
1764 const allocator_type& __a = allocator_type())
1765 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
1768 // Should perhaps be templatized with respect to the iterator type
1769 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1771 rope(const _CharT *__s, const _CharT *__e,
1772 const allocator_type& __a = allocator_type())
1773 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
1776 rope(const const_iterator& __s, const const_iterator& __e,
1777 const allocator_type& __a = allocator_type())
1778 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1779 __e._M_current_pos), __a)
1782 rope(const iterator& __s, const iterator& __e,
1783 const allocator_type& __a = allocator_type())
1784 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1785 __e._M_current_pos), __a)
1788 rope(_CharT __c, const allocator_type& __a = allocator_type())
1791 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1793 std::_Construct(__buf, __c);
1795 { this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a); }
1798 _RopeRep::__STL_FREE_STRING(__buf, 1, __a);
1799 __throw_exception_again;
1803 rope(size_t __n, _CharT __c,
1804 const allocator_type& __a = allocator_type());
1806 rope(const allocator_type& __a = allocator_type())
1809 // Construct a rope from a function that can compute its members
1810 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1811 const allocator_type& __a = allocator_type())
1814 this->_M_tree_ptr = (0 == __len) ?
1815 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1818 rope(const rope& __x, const allocator_type& __a = allocator_type())
1819 : _Base(__x._M_tree_ptr, __a)
1820 { _S_ref(this->_M_tree_ptr); }
1823 { _S_unref(this->_M_tree_ptr); }
1826 operator=(const rope& __x)
1828 _RopeRep* __old = this->_M_tree_ptr;
1829 this->_M_tree_ptr = __x._M_tree_ptr;
1830 _S_ref(this->_M_tree_ptr);
1838 _S_unref(this->_M_tree_ptr);
1839 this->_M_tree_ptr = 0;
1843 push_back(_CharT __x)
1845 _RopeRep* __old = this->_M_tree_ptr;
1847 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1854 _RopeRep* __old = this->_M_tree_ptr;
1856 _S_substring(this->_M_tree_ptr,
1857 0, this->_M_tree_ptr->_M_size - 1);
1863 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1866 push_front(_CharT __x)
1868 _RopeRep* __old = this->_M_tree_ptr;
1870 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, this->get_allocator());
1873 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1880 __throw_exception_again;
1887 _RopeRep* __old = this->_M_tree_ptr;
1889 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1895 { return _S_fetch(this->_M_tree_ptr, 0); }
1900 _RopeRep* __old = this->_M_tree_ptr;
1901 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1906 copy(_CharT* __buffer) const
1908 _Destroy(__buffer, __buffer + size());
1909 _S_flatten(this->_M_tree_ptr, __buffer);
1912 // This is the copy function from the standard, but
1913 // with the arguments reordered to make it consistent with the
1914 // rest of the interface.
1915 // Note that this guaranteed not to compile if the draft standard
1916 // order is assumed.
1918 copy(size_type __pos, size_type __n, _CharT* __buffer) const
1920 size_t __size = size();
1921 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1923 _Destroy(__buffer, __buffer + __len);
1924 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1928 // Print to stdout, exposing structure. May be useful for
1929 // performance debugging.
1932 { _S_dump(this->_M_tree_ptr); }
1934 // Convert to 0 terminated string in new allocated memory.
1935 // Embedded 0s in the input do not terminate the copy.
1936 const _CharT* c_str() const;
1938 // As above, but lso use the flattened representation as the
1939 // the new rope representation.
1940 const _CharT* replace_with_c_str();
1942 // Reclaim memory for the c_str generated flattened string.
1943 // Intentionally undocumented, since it's hard to say when this
1944 // is safe for multiple threads.
1948 if (0 == this->_M_tree_ptr)
1950 if (_Rope_constants::_S_leaf == this->_M_tree_ptr->_M_tag &&
1951 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1952 this->_M_tree_ptr->_M_c_string)
1954 // Representation shared
1958 this->_M_tree_ptr->_M_free_c_string();
1960 this->_M_tree_ptr->_M_c_string = 0;
1964 operator[] (size_type __pos) const
1965 { return _S_fetch(this->_M_tree_ptr, __pos); }
1968 at(size_type __pos) const
1970 // if (__pos >= size()) throw out_of_range; // XXX
1971 return (*this)[__pos];
1976 { return(const_iterator(this->_M_tree_ptr, 0)); }
1978 // An easy way to get a const iterator from a non-const container.
1981 { return(const_iterator(this->_M_tree_ptr, 0)); }
1985 { return(const_iterator(this->_M_tree_ptr, size())); }
1989 { return(const_iterator(this->_M_tree_ptr, size())); }
1993 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
2002 return _S_min_len[_Rope_constants::_S_max_rope_depth - 1] - 1;
2003 // Guarantees that the result can be sufficirntly
2004 // balanced. Longer ropes will probably still work,
2005 // but it's harder to make guarantees.
2008 typedef reverse_iterator<const_iterator> const_reverse_iterator;
2010 const_reverse_iterator
2012 { return const_reverse_iterator(end()); }
2014 const_reverse_iterator
2015 const_rbegin() const
2016 { return const_reverse_iterator(end()); }
2018 const_reverse_iterator
2020 { return const_reverse_iterator(begin()); }
2022 const_reverse_iterator
2024 { return const_reverse_iterator(begin()); }
2026 template<class _CharT2, class _Alloc2>
2027 friend rope<_CharT2, _Alloc2>
2028 operator+(const rope<_CharT2, _Alloc2>& __left,
2029 const rope<_CharT2, _Alloc2>& __right);
2031 template<class _CharT2, class _Alloc2>
2032 friend rope<_CharT2, _Alloc2>
2033 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2035 template<class _CharT2, class _Alloc2>
2036 friend rope<_CharT2, _Alloc2>
2037 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2038 // The symmetric cases are intentionally omitted, since they're presumed
2039 // to be less common, and we don't handle them as well.
2041 // The following should really be templatized.
2042 // The first argument should be an input iterator or
2043 // forward iterator with value_type _CharT.
2045 append(const _CharT* __iter, size_t __n)
2047 _RopeRep* __result =
2048 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
2049 _S_unref(this->_M_tree_ptr);
2050 this->_M_tree_ptr = __result;
2055 append(const _CharT* __c_string)
2057 size_t __len = _S_char_ptr_len(__c_string);
2058 append(__c_string, __len);
2063 append(const _CharT* __s, const _CharT* __e)
2065 _RopeRep* __result =
2066 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
2067 _S_unref(this->_M_tree_ptr);
2068 this->_M_tree_ptr = __result;
2073 append(const_iterator __s, const_iterator __e)
2075 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2077 __e._M_current_pos));
2078 _RopeRep* __result =
2079 _S_concat(this->_M_tree_ptr, (_RopeRep*)__appendee);
2080 _S_unref(this->_M_tree_ptr);
2081 this->_M_tree_ptr = __result;
2088 _RopeRep* __result =
2089 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
2090 _S_unref(this->_M_tree_ptr);
2091 this->_M_tree_ptr = __result;
2097 { return append(_CharT()); } // XXX why?
2100 append(const rope& __y)
2102 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2103 _S_unref(this->_M_tree_ptr);
2104 this->_M_tree_ptr = __result;
2109 append(size_t __n, _CharT __c)
2111 rope<_CharT,_Alloc> __last(__n, __c);
2112 return append(__last);
2118 _RopeRep* __tmp = this->_M_tree_ptr;
2119 this->_M_tree_ptr = __b._M_tree_ptr;
2120 __b._M_tree_ptr = __tmp;
2124 // Result is included in refcount.
2126 replace(_RopeRep* __old, size_t __pos1,
2127 size_t __pos2, _RopeRep* __r)
2134 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2135 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2139 __result = _S_concat(__left, __right);
2142 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2143 __result = _S_concat(__left_result, __right);
2150 insert(size_t __p, const rope& __r)
2152 _RopeRep* __result =
2153 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2154 _S_unref(this->_M_tree_ptr);
2155 this->_M_tree_ptr = __result;
2159 insert(size_t __p, size_t __n, _CharT __c)
2161 rope<_CharT,_Alloc> __r(__n,__c);
2166 insert(size_t __p, const _CharT* __i, size_t __n)
2168 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2169 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2171 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n));
2172 // _S_ destr_concat_char_iter should be safe here.
2173 // But as it stands it's probably not a win, since __left
2174 // is likely to have additional references.
2175 _RopeRep* __result = _S_concat(__left_result, __right);
2176 _S_unref(this->_M_tree_ptr);
2177 this->_M_tree_ptr = __result;
2181 insert(size_t __p, const _CharT* __c_string)
2182 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2185 insert(size_t __p, _CharT __c)
2186 { insert(__p, &__c, 1); }
2191 _CharT __c = _CharT();
2192 insert(__p, &__c, 1);
2196 insert(size_t __p, const _CharT* __i, const _CharT* __j)
2203 insert(size_t __p, const const_iterator& __i,
2204 const const_iterator& __j)
2211 insert(size_t __p, const iterator& __i,
2212 const iterator& __j)
2218 // (position, length) versions of replace operations:
2221 replace(size_t __p, size_t __n, const rope& __r)
2223 _RopeRep* __result =
2224 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2225 _S_unref(this->_M_tree_ptr);
2226 this->_M_tree_ptr = __result;
2230 replace(size_t __p, size_t __n,
2231 const _CharT* __i, size_t __i_len)
2233 rope __r(__i, __i_len);
2234 replace(__p, __n, __r);
2238 replace(size_t __p, size_t __n, _CharT __c)
2241 replace(__p, __n, __r);
2245 replace(size_t __p, size_t __n, const _CharT* __c_string)
2247 rope __r(__c_string);
2248 replace(__p, __n, __r);
2252 replace(size_t __p, size_t __n,
2253 const _CharT* __i, const _CharT* __j)
2256 replace(__p, __n, __r);
2260 replace(size_t __p, size_t __n,
2261 const const_iterator& __i, const const_iterator& __j)
2264 replace(__p, __n, __r);
2268 replace(size_t __p, size_t __n,
2269 const iterator& __i, const iterator& __j)
2272 replace(__p, __n, __r);
2275 // Single character variants:
2277 replace(size_t __p, _CharT __c)
2279 iterator __i(this, __p);
2284 replace(size_t __p, const rope& __r)
2285 { replace(__p, 1, __r); }
2288 replace(size_t __p, const _CharT* __i, size_t __i_len)
2289 { replace(__p, 1, __i, __i_len); }
2292 replace(size_t __p, const _CharT* __c_string)
2293 { replace(__p, 1, __c_string); }
2296 replace(size_t __p, const _CharT* __i, const _CharT* __j)
2297 { replace(__p, 1, __i, __j); }
2300 replace(size_t __p, const const_iterator& __i,
2301 const const_iterator& __j)
2302 { replace(__p, 1, __i, __j); }
2305 replace(size_t __p, const iterator& __i,
2306 const iterator& __j)
2307 { replace(__p, 1, __i, __j); }
2309 // Erase, (position, size) variant.
2311 erase(size_t __p, size_t __n)
2313 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2315 _S_unref(this->_M_tree_ptr);
2316 this->_M_tree_ptr = __result;
2319 // Erase, single character
2322 { erase(__p, __p + 1); }
2324 // Insert, iterator variants.
2326 insert(const iterator& __p, const rope& __r)
2328 insert(__p.index(), __r);
2333 insert(const iterator& __p, size_t __n, _CharT __c)
2335 insert(__p.index(), __n, __c);
2339 iterator insert(const iterator& __p, _CharT __c)
2341 insert(__p.index(), __c);
2346 insert(const iterator& __p )
2348 insert(__p.index());
2353 insert(const iterator& __p, const _CharT* c_string)
2355 insert(__p.index(), c_string);
2360 insert(const iterator& __p, const _CharT* __i, size_t __n)
2362 insert(__p.index(), __i, __n);
2367 insert(const iterator& __p, const _CharT* __i,
2370 insert(__p.index(), __i, __j);
2375 insert(const iterator& __p,
2376 const const_iterator& __i, const const_iterator& __j)
2378 insert(__p.index(), __i, __j);
2383 insert(const iterator& __p,
2384 const iterator& __i, const iterator& __j)
2386 insert(__p.index(), __i, __j);
2390 // Replace, range variants.
2392 replace(const iterator& __p, const iterator& __q, const rope& __r)
2393 { replace(__p.index(), __q.index() - __p.index(), __r); }
2396 replace(const iterator& __p, const iterator& __q, _CharT __c)
2397 { replace(__p.index(), __q.index() - __p.index(), __c); }
2400 replace(const iterator& __p, const iterator& __q,
2401 const _CharT* __c_string)
2402 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2405 replace(const iterator& __p, const iterator& __q,
2406 const _CharT* __i, size_t __n)
2407 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2410 replace(const iterator& __p, const iterator& __q,
2411 const _CharT* __i, const _CharT* __j)
2412 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2415 replace(const iterator& __p, const iterator& __q,
2416 const const_iterator& __i, const const_iterator& __j)
2417 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2420 replace(const iterator& __p, const iterator& __q,
2421 const iterator& __i, const iterator& __j)
2422 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2424 // Replace, iterator variants.
2426 replace(const iterator& __p, const rope& __r)
2427 { replace(__p.index(), __r); }
2430 replace(const iterator& __p, _CharT __c)
2431 { replace(__p.index(), __c); }
2434 replace(const iterator& __p, const _CharT* __c_string)
2435 { replace(__p.index(), __c_string); }
2438 replace(const iterator& __p, const _CharT* __i, size_t __n)
2439 { replace(__p.index(), __i, __n); }
2442 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2443 { replace(__p.index(), __i, __j); }
2446 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2447 { replace(__p.index(), __i, __j); }
2450 replace(const iterator& __p, iterator __i, iterator __j)
2451 { replace(__p.index(), __i, __j); }
2453 // Iterator and range variants of erase
2455 erase(const iterator& __p, const iterator& __q)
2457 size_t __p_index = __p.index();
2458 erase(__p_index, __q.index() - __p_index);
2459 return iterator(this, __p_index);
2463 erase(const iterator& __p)
2465 size_t __p_index = __p.index();
2466 erase(__p_index, 1);
2467 return iterator(this, __p_index);
2471 substr(size_t __start, size_t __len = 1) const
2473 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2479 substr(iterator __start, iterator __end) const
2481 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2487 substr(iterator __start) const
2489 size_t __pos = __start.index();
2490 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2495 substr(const_iterator __start, const_iterator __end) const
2497 // This might eventually take advantage of the cache in the
2499 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2504 rope<_CharT, _Alloc>
2505 substr(const_iterator __start)
2507 size_t __pos = __start.index();
2508 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2512 static const size_type npos;
2514 size_type find(_CharT __c, size_type __pos = 0) const;
2517 find(const _CharT* __s, size_type __pos = 0) const
2519 size_type __result_pos;
2520 const_iterator __result =
2521 std::search(const_begin() + __pos, const_end(),
2522 __s, __s + _S_char_ptr_len(__s));
2523 __result_pos = __result.index();
2524 #ifndef __STL_OLD_ROPE_SEMANTICS
2525 if (__result_pos == size())
2526 __result_pos = npos;
2528 return __result_pos;
2533 { return(iterator(this, 0)); }
2537 { return(iterator(this, size())); }
2539 typedef reverse_iterator<iterator> reverse_iterator;
2543 { return reverse_iterator(mutable_end()); }
2547 { return reverse_iterator(mutable_begin()); }
2550 mutable_reference_at(size_type __pos)
2551 { return reference(this, __pos); }
2555 operator[] (size_type __pos)
2556 { return _char_ref_proxy(this, __pos); }
2561 // if (__pos >= size()) throw out_of_range; // XXX
2562 return (*this)[__pos];
2565 void resize(size_type __n, _CharT __c) {}
2566 void resize(size_type __n) {}
2567 void reserve(size_type __res_arg = 0) {}
2571 { return max_size(); }
2573 // Stuff below this line is dangerous because it's error prone.
2574 // I would really like to get rid of it.
2575 // copy function with funny arg ordering.
2577 copy(_CharT* __buffer, size_type __n,
2578 size_type __pos = 0) const
2579 { return copy(__pos, __n, __buffer); }
2583 { return mutable_end(); }
2587 { return mutable_begin(); }
2591 { return mutable_rend(); }
2595 { return mutable_rbegin(); }
2600 { return const_end(); }
2604 { return const_begin(); }
2606 const_reverse_iterator
2608 { return const_rend(); }
2610 const_reverse_iterator
2612 { return const_rbegin(); }
2617 template <class _CharT, class _Alloc>
2618 const typename rope<_CharT, _Alloc>::size_type
2619 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2621 template <class _CharT, class _Alloc>
2622 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2623 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2624 { return (__x._M_current_pos == __y._M_current_pos
2625 && __x._M_root == __y._M_root); }
2627 template <class _CharT, class _Alloc>
2628 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2629 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2630 { return (__x._M_current_pos < __y._M_current_pos); }
2632 template <class _CharT, class _Alloc>
2633 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2634 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2635 { return !(__x == __y); }
2637 template <class _CharT, class _Alloc>
2638 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2639 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2640 { return __y < __x; }
2642 template <class _CharT, class _Alloc>
2644 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2645 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2646 { return !(__y < __x); }
2648 template <class _CharT, class _Alloc>
2650 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2651 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2652 { return !(__x < __y); }
2654 template <class _CharT, class _Alloc>
2656 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2657 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2658 { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
2660 template <class _CharT, class _Alloc>
2661 inline _Rope_const_iterator<_CharT, _Alloc>
2662 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2663 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2664 __x._M_current_pos - __n); }
2666 template <class _CharT, class _Alloc>
2667 inline _Rope_const_iterator<_CharT, _Alloc>
2668 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2669 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2670 __x._M_current_pos + __n); }
2672 template <class _CharT, class _Alloc>
2673 inline _Rope_const_iterator<_CharT, _Alloc>
2674 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT, _Alloc>& __x)
2675 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2676 __x._M_current_pos + __n); }
2678 template <class _CharT, class _Alloc>
2680 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2681 const _Rope_iterator<_CharT, _Alloc>& __y)
2682 {return (__x._M_current_pos == __y._M_current_pos
2683 && __x._M_root_rope == __y._M_root_rope); }
2685 template <class _CharT, class _Alloc>
2687 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2688 const _Rope_iterator<_CharT, _Alloc>& __y)
2689 { return (__x._M_current_pos < __y._M_current_pos); }
2691 template <class _CharT, class _Alloc>
2693 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2694 const _Rope_iterator<_CharT, _Alloc>& __y)
2695 { return !(__x == __y); }
2697 template <class _CharT, class _Alloc>
2699 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2700 const _Rope_iterator<_CharT, _Alloc>& __y)
2701 { return __y < __x; }
2703 template <class _CharT, class _Alloc>
2705 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2706 const _Rope_iterator<_CharT, _Alloc>& __y)
2707 { return !(__y < __x); }
2709 template <class _CharT, class _Alloc>
2711 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2712 const _Rope_iterator<_CharT, _Alloc>& __y)
2713 { return !(__x < __y); }
2715 template <class _CharT, class _Alloc>
2717 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2718 const _Rope_iterator<_CharT, _Alloc>& __y)
2719 { return ((ptrdiff_t)__x._M_current_pos
2720 - (ptrdiff_t)__y._M_current_pos); }
2722 template <class _CharT, class _Alloc>
2723 inline _Rope_iterator<_CharT, _Alloc>
2724 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2726 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2727 __x._M_current_pos - __n); }
2729 template <class _CharT, class _Alloc>
2730 inline _Rope_iterator<_CharT, _Alloc>
2731 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2732 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2733 __x._M_current_pos + __n); }
2735 template <class _CharT, class _Alloc>
2736 inline _Rope_iterator<_CharT,_Alloc>
2737 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2738 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2739 __x._M_current_pos + __n); }
2741 template <class _CharT, class _Alloc>
2742 inline rope<_CharT,_Alloc>
2743 operator+(const rope<_CharT, _Alloc>& __left,
2744 const rope<_CharT, _Alloc>& __right)
2746 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2747 _S_concat(__left._M_tree_ptr,
2748 __right._M_tree_ptr));
2749 // Inlining this should make it possible to keep __left and
2750 // __right in registers.
2753 template <class _CharT, class _Alloc>
2754 inline rope<_CharT, _Alloc>&
2755 operator+=(rope<_CharT, _Alloc>& __left,
2756 const rope<_CharT, _Alloc>& __right)
2758 __left.append(__right);
2762 template <class _CharT, class _Alloc>
2763 inline rope<_CharT, _Alloc>
2764 operator+(const rope<_CharT, _Alloc>& __left,
2765 const _CharT* __right)
2767 size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
2768 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2769 _S_concat_char_iter(__left._M_tree_ptr,
2773 template <class _CharT, class _Alloc>
2774 inline rope<_CharT, _Alloc>&
2775 operator+=(rope<_CharT, _Alloc>& __left,
2776 const _CharT* __right)
2778 __left.append(__right);
2782 template <class _CharT, class _Alloc>
2783 inline rope<_CharT, _Alloc>
2784 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2786 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2787 _S_concat_char_iter(__left._M_tree_ptr,
2791 template <class _CharT, class _Alloc>
2792 inline rope<_CharT, _Alloc>&
2793 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2795 __left.append(__right);
2799 template <class _CharT, class _Alloc>
2801 operator<(const rope<_CharT, _Alloc>& __left,
2802 const rope<_CharT, _Alloc>& __right)
2803 { return __left.compare(__right) < 0; }
2805 template <class _CharT, class _Alloc>
2807 operator==(const rope<_CharT, _Alloc>& __left,
2808 const rope<_CharT, _Alloc>& __right)
2809 { return __left.compare(__right) == 0; }
2811 template <class _CharT, class _Alloc>
2813 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2814 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2815 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2817 template <class _CharT, class _Alloc>
2819 operator!=(const rope<_CharT, _Alloc>& __x,
2820 const rope<_CharT, _Alloc>& __y)
2821 { return !(__x == __y); }
2823 template <class _CharT, class _Alloc>
2825 operator>(const rope<_CharT, _Alloc>& __x,
2826 const rope<_CharT, _Alloc>& __y)
2827 { return __y < __x; }
2829 template <class _CharT, class _Alloc>
2831 operator<=(const rope<_CharT, _Alloc>& __x,
2832 const rope<_CharT, _Alloc>& __y)
2833 { return !(__y < __x); }
2835 template <class _CharT, class _Alloc>
2837 operator>=(const rope<_CharT, _Alloc>& __x,
2838 const rope<_CharT, _Alloc>& __y)
2839 { return !(__x < __y); }
2841 template <class _CharT, class _Alloc>
2843 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2844 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2845 { return !(__x == __y); }
2847 template<class _CharT, class _Traits, class _Alloc>
2848 std::basic_ostream<_CharT, _Traits>&
2849 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2850 const rope<_CharT, _Alloc>& __r);
2852 typedef rope<char> crope;
2853 typedef rope<wchar_t> wrope;
2855 inline crope::reference
2856 __mutable_reference_at(crope& __c, size_t __i)
2857 { return __c.mutable_reference_at(__i); }
2859 inline wrope::reference
2860 __mutable_reference_at(wrope& __c, size_t __i)
2861 { return __c.mutable_reference_at(__i); }
2863 template <class _CharT, class _Alloc>
2865 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2868 // Hash functions should probably be revisited later:
2873 operator()(const crope& __str) const
2875 size_t __size = __str.size();
2879 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2888 operator()(const wrope& __str) const
2890 size_t __size = __str.size();
2894 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2898 } // namespace __gnu_cxx
2900 # include <ext/ropeimpl.h>