williamr@2: /*
williamr@4:  * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
williamr@4:  *
williamr@2:  * Copyright (c) 1996,1997
williamr@2:  * Silicon Graphics Computer Systems, Inc.
williamr@2:  *
williamr@2:  * Copyright (c) 1997
williamr@2:  * Moscow Center for SPARC Technology
williamr@2:  *
williamr@4:  * Copyright (c) 1999
williamr@2:  * Boris Fomitchev
williamr@2:  *
williamr@2:  * This material is provided "as is", with absolutely no warranty expressed
williamr@2:  * or implied. Any use is at your own risk.
williamr@2:  *
williamr@4:  * Permission to use or copy this software for any purpose is hereby granted
williamr@2:  * without fee, provided the above notices are retained on all copies.
williamr@2:  * Permission to modify the code and to distribute modified code is granted,
williamr@2:  * provided the above notices are retained, and a notice that the code was
williamr@2:  * modified is included with the above copyright notice.
williamr@2:  *
williamr@2:  */
williamr@2: 
williamr@2: /* NOTE: This is an internal header file, included by other STL headers.
williamr@2:  *   You should not attempt to use it directly.
williamr@2:  */
williamr@2: 
williamr@2: // rope<_CharT,_Alloc> is a sequence of _CharT.
williamr@2: // Ropes appear to be mutable, but update operations
williamr@2: // really copy enough of the data structure to leave the original
williamr@2: // valid.  Thus ropes can be logically copied by just copying
williamr@2: // a pointer value.
williamr@2: 
williamr@2: #ifndef _STLP_INTERNAL_ROPE_H
williamr@4: #define _STLP_INTERNAL_ROPE_H
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_ALGOBASE_H
williamr@2: #  include <stl/_algobase.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_IOSFWD
williamr@2: #  include <iosfwd>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_ALLOC_H
williamr@2: #  include <stl/_alloc.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_ITERATOR_H
williamr@2: #  include <stl/_iterator.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_ALGO_H
williamr@2: #  include <stl/_algo.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
williamr@4: #  include <stl/_function_base.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_NUMERIC_H
williamr@2: #  include <stl/_numeric.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_HASH_FUN_H
williamr@2: #  include <stl/_hash_fun.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_CHAR_TRAITS_H
williamr@4: #  include <stl/char_traits.h>
williamr@4: #endif
williamr@2: 
williamr@4: #ifndef _STLP_INTERNAL_THREADS_H
williamr@4: #  include <stl/_threads.h>
williamr@4: #endif
williamr@4: 
williamr@4: #ifdef _STLP_SGI_THREADS
williamr@4: #  include <mutex.h>
williamr@4: #endif
williamr@4: 
williamr@4: #ifndef _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE
williamr@4: #  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) (_Alloc_traits<_Tp,__atype>::create_allocator(__a))
williamr@4: #elif defined(__MRC__)||defined(__SC__)
williamr@4: #  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) __stl_alloc_create<_Tp,__atype>(__a,(_Tp*)0)
williamr@4: #else
williamr@4: #  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
williamr@4: #endif
williamr@2: 
williamr@2: _STLP_BEGIN_NAMESPACE
williamr@2: 
williamr@2: // First a lot of forward declarations.  The standard seems to require
williamr@2: // much stricter "declaration before use" than many of the implementations
williamr@2: // that preceded it.
williamr@2: template<class _CharT, _STLP_DEFAULT_ALLOCATOR_SELECT(_CharT) > class rope;
williamr@2: template<class _CharT, class _Alloc> struct _Rope_RopeConcatenation;
williamr@2: template<class _CharT, class _Alloc> struct _Rope_RopeRep;
williamr@2: template<class _CharT, class _Alloc> struct _Rope_RopeLeaf;
williamr@2: template<class _CharT, class _Alloc> struct _Rope_RopeFunction;
williamr@2: template<class _CharT, class _Alloc> struct _Rope_RopeSubstring;
williamr@2: template<class _CharT, class _Alloc> class _Rope_iterator;
williamr@2: template<class _CharT, class _Alloc> class _Rope_const_iterator;
williamr@2: template<class _CharT, class _Alloc> class _Rope_char_ref_proxy;
williamr@2: template<class _CharT, class _Alloc> class _Rope_char_ptr_proxy;
williamr@2: 
williamr@4: _STLP_MOVE_TO_PRIV_NAMESPACE
williamr@4: 
williamr@4: // Some helpers, so we can use the power algorithm on ropes.
williamr@2: // See below for why this isn't local to the implementation.
williamr@2: 
williamr@2: // This uses a nonstandard refcount convention.
williamr@2: // The result has refcount 0.
williamr@2: template<class _CharT, class _Alloc>
williamr@2: struct _Rope_Concat_fn
williamr@2:   : public binary_function<rope<_CharT,_Alloc>, rope<_CharT,_Alloc>,
williamr@4:                            rope<_CharT,_Alloc> > {
williamr@2:   rope<_CharT,_Alloc> operator() (const rope<_CharT,_Alloc>& __x,
williamr@2:                                   const rope<_CharT,_Alloc>& __y) {
williamr@2:     return __x + __y;
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline
williamr@2: rope<_CharT,_Alloc>
williamr@2: __identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
williamr@4: { return rope<_CharT,_Alloc>(); }
williamr@4: 
williamr@4: _STLP_MOVE_TO_STD_NAMESPACE
williamr@4: 
williamr@4: // Store an eos
williamr@4: template <class _CharT>
williamr@4: inline void _S_construct_null_aux(_CharT *__p, const __true_type&)
williamr@4: { *__p = 0; }
williamr@4: 
williamr@4: template <class _CharT>
williamr@4: inline void _S_construct_null_aux(_CharT *__p, const __false_type&)
williamr@4: { _STLP_STD::_Construct(__p); }
williamr@4: 
williamr@4: template <class _CharT>
williamr@4: inline void _S_construct_null(_CharT *__p) {
williamr@4:   typedef typename _IsIntegral<_CharT>::_Ret _Char_Is_Integral;
williamr@4:   _S_construct_null_aux(__p, _Char_Is_Integral());
williamr@2: }
williamr@2: 
williamr@2: // char_producers are logically functions that generate a section of
williamr@4: // a string.  These can be converted to ropes.  The resulting rope
williamr@2: // invokes the char_producer on demand.  This allows, for example,
williamr@2: // files to be viewed as ropes without reading the entire file.
williamr@2: template <class _CharT>
williamr@2: class char_producer {
williamr@2: public:
williamr@4:   virtual ~char_producer() {}
williamr@4:   virtual void operator()(size_t __start_pos, size_t __len,
williamr@2:                           _CharT* __buffer) = 0;
williamr@2:   // Buffer should really be an arbitrary output iterator.
williamr@2:   // That way we could flatten directly into an ostream, etc.
williamr@2:   // This is thoroughly impossible, since iterator types don't
williamr@2:   // have runtime descriptions.
williamr@2: };
williamr@2: 
williamr@2: // Sequence buffers:
williamr@2: //
williamr@2: // Sequence must provide an append operation that appends an
williamr@2: // array to the sequence.  Sequence buffers are useful only if
williamr@2: // appending an entire array is cheaper than appending element by element.
williamr@2: // This is true for many string representations.
williamr@2: // This should  perhaps inherit from ostream<sequence::value_type>
williamr@2: // and be implemented correspondingly, so that they can be used
williamr@2: // for formatted.  For the sake of portability, we don't do this yet.
williamr@2: //
williamr@2: // For now, sequence buffers behave as output iterators.  But they also
williamr@2: // behave a little like basic_ostringstream<sequence::value_type> and a
williamr@2: // little like containers.
williamr@2: 
williamr@2: template<class _Sequence
williamr@2: # if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
williamr@2:        defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
williamr@4:          , size_t _Buf_sz = 100
williamr@2: #   if defined(__sgi) && !defined(__GNUC__)
williamr@4: #   define __TYPEDEF_WORKAROUND
williamr@4:          ,class _V = typename _Sequence::value_type
williamr@2: #   endif /* __sgi */
williamr@2: # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@4:          >
williamr@2: // The 3rd parameter works around a common compiler bug.
williamr@2: class sequence_buffer : public iterator <output_iterator_tag, void, void, void, void> {
williamr@2: public:
williamr@4: # ifndef __TYPEDEF_WORKAROUND
williamr@2:   typedef typename _Sequence::value_type value_type;
williamr@2:   typedef sequence_buffer<_Sequence
williamr@2: # if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
williamr@2:        defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
williamr@2:   , _Buf_sz
williamr@2:   > _Self;
williamr@2: # else /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2:   > _Self;
williamr@4:   enum { _Buf_sz = 100};
williamr@2: # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2:   // # endif
williamr@4: # else /* __TYPEDEF_WORKAROUND */
williamr@2:   typedef _V value_type;
williamr@2:   typedef sequence_buffer<_Sequence, _Buf_sz, _V> _Self;
williamr@4: # endif /* __TYPEDEF_WORKAROUND */
williamr@2: protected:
williamr@2:   _Sequence* _M_prefix;
williamr@2:   value_type _M_buffer[_Buf_sz];
williamr@2:   size_t     _M_buf_count;
williamr@2: public:
williamr@2:   void flush() {
williamr@2:     _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
williamr@2:     _M_buf_count = 0;
williamr@2:   }
williamr@2:   ~sequence_buffer() { flush(); }
williamr@2:   sequence_buffer() : _M_prefix(0), _M_buf_count(0) {}
williamr@2:   sequence_buffer(const _Self& __x) {
williamr@2:     _M_prefix = __x._M_prefix;
williamr@2:     _M_buf_count = __x._M_buf_count;
williamr@2:     copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
williamr@2:   }
williamr@2:   sequence_buffer(_Self& __x) {
williamr@2:     __x.flush();
williamr@2:     _M_prefix = __x._M_prefix;
williamr@2:     _M_buf_count = 0;
williamr@2:   }
williamr@2:   sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) {}
williamr@2:   _Self& operator= (_Self& __x) {
williamr@2:     __x.flush();
williamr@2:     _M_prefix = __x._M_prefix;
williamr@2:     _M_buf_count = 0;
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator= (const _Self& __x) {
williamr@2:     _M_prefix = __x._M_prefix;
williamr@2:     _M_buf_count = __x._M_buf_count;
williamr@2:     copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
williamr@2:     return *this;
williamr@2:   }
williamr@4:   void push_back(value_type __x) {
williamr@2:     if (_M_buf_count < _Buf_sz) {
williamr@2:       _M_buffer[_M_buf_count] = __x;
williamr@2:       ++_M_buf_count;
williamr@2:     } else {
williamr@2:       flush();
williamr@2:       _M_buffer[0] = __x;
williamr@2:       _M_buf_count = 1;
williamr@2:     }
williamr@2:   }
williamr@4:   void append(const value_type *__s, size_t __len) {
williamr@2:     if (__len + _M_buf_count <= _Buf_sz) {
williamr@2:       size_t __i = _M_buf_count;
williamr@2:       size_t __j = 0;
williamr@2:       for (; __j < __len; __i++, __j++) {
williamr@2:         _M_buffer[__i] = __s[__j];
williamr@2:       }
williamr@2:       _M_buf_count += __len;
williamr@2:     } else if (0 == _M_buf_count) {
williamr@2:       _M_prefix->append(__s, __s + __len);
williamr@2:     } else {
williamr@2:       flush();
williamr@2:       append(__s, __len);
williamr@2:     }
williamr@2:   }
williamr@4:   _Self& write(const value_type *__s, size_t __len) {
williamr@2:     append(__s, __len);
williamr@2:     return *this;
williamr@2:   }
williamr@4:   _Self& put(value_type __x) {
williamr@2:     push_back(__x);
williamr@2:     return *this;
williamr@2:   }
williamr@4:   _Self& operator=(const value_type& __rhs) {
williamr@2:     push_back(__rhs);
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator*() { return *this; }
williamr@2:   _Self& operator++() { return *this; }
williamr@2:   _Self& operator++(int) { return *this; }
williamr@2: };
williamr@2: 
williamr@2: // The following should be treated as private, at least for now.
williamr@2: template<class _CharT>
williamr@2: class _Rope_char_consumer {
williamr@4: #if !defined (_STLP_MEMBER_TEMPLATES)
williamr@2: public:
williamr@4:   //Without member templates we have to use run-time parameterization.
williamr@2:   // The symmetry with char_producer is accidental and temporary.
williamr@4:   virtual ~_Rope_char_consumer() {}
williamr@2:   virtual bool operator()(const _CharT* __buffer, size_t __len) = 0;
williamr@4: #endif
williamr@2: };
williamr@2: 
williamr@2: //
williamr@2: // What follows should really be local to rope.  Unfortunately,
williamr@2: // that doesn't work, since it makes it impossible to define generic
williamr@2: // equality on rope iterators.  According to the draft standard, the
williamr@2: // template parameters for such an equality operator cannot be inferred
williamr@2: // from the occurence of a member class as a parameter.
williamr@2: // (SGI compilers in fact allow this, but the __result wouldn't be
williamr@2: // portable.)
williamr@2: // Similarly, some of the static member functions are member functions
williamr@2: // only to avoid polluting the global namespace, and to circumvent
williamr@2: // restrictions on type inference for template functions.
williamr@2: //
williamr@2: 
williamr@2: //
williamr@2: // The internal data structure for representing a rope.  This is
williamr@2: // private to the implementation.  A rope is really just a pointer
williamr@2: // to one of these.
williamr@2: //
williamr@2: // A few basic functions for manipulating this data structure
williamr@2: // are members of _RopeRep.  Most of the more complex algorithms
williamr@2: // are implemented as rope members.
williamr@2: //
williamr@2: // Some of the static member functions of _RopeRep have identically
williamr@2: // named functions in rope that simply invoke the _RopeRep versions.
williamr@2: //
williamr@2: 
williamr@2: template<class _CharT, class _Alloc>
williamr@2: struct _Rope_RopeRep
williamr@2:   : public _Refcount_Base
williamr@2: {
williamr@2:   typedef _Rope_RopeRep<_CharT, _Alloc> _Self;
williamr@2: public:
williamr@4:   //
williamr@4:   // GAB: 11/09/05
williamr@4:   //
williamr@4:   // "__ROPE_DEPTH_SIZE" is set to one more then the "__ROPE_MAX_DEPTH".
williamr@4:   // This was originally just an addition of "__ROPE_MAX_DEPTH + 1"
williamr@4:   // but this addition causes the sunpro compiler to complain about
williamr@4:   // multiple declarations during the initialization of "_S_min_len".
williamr@4:   // Changed to be a fixed value and the sunpro compiler appears to
williamr@4:   // be happy???
williamr@4:   //
williamr@2: #  define __ROPE_MAX_DEPTH  45
williamr@4: #  define __ROPE_DEPTH_SIZE 46 // __ROPE_MAX_DEPTH + 1
williamr@2:   enum { _S_max_rope_depth = __ROPE_MAX_DEPTH };
williamr@2:   enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
williamr@2:   // Apparently needed by VC++
williamr@2:   // The data fields of leaves are allocated with some
williamr@2:   // extra space, to accomodate future growth and for basic
williamr@2:   // character types, to hold a trailing eos character.
williamr@2:   enum { _S_alloc_granularity = 8 };
williamr@2: 
williamr@2:   _Tag _M_tag:8;
williamr@2:   bool _M_is_balanced:8;
williamr@2: 
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@4:   typedef typename _Alloc_traits<_CharT,_Alloc>::allocator_type allocator_type;
williamr@4: 
williamr@2:   allocator_type get_allocator() const { return allocator_type(_M_size);  }
williamr@2: 
williamr@2:   unsigned char _M_depth;
williamr@4:   _CharT* _STLP_VOLATILE _M_c_string;
williamr@4:   _STLP_PRIV _STLP_alloc_proxy<size_t, _CharT, allocator_type> _M_size;
williamr@2: 
williamr@2: # ifdef _STLP_NO_ARROW_OPERATOR
williamr@2:   _Rope_RopeRep() : _Refcount_Base(1), _M_size(allocator_type(), 0) {}
williamr@2: # endif
williamr@2: 
williamr@2:   /* Flattened version of string, if needed.  */
williamr@2:   /* typically 0.                             */
williamr@2:   /* If it's not 0, then the memory is owned  */
williamr@2:   /* by this node.                            */
williamr@2:   /* In the case of a leaf, this may point to */
williamr@2:   /* the same memory as the data field.       */
williamr@4:   _Rope_RopeRep(_Tag __t, unsigned char __d, bool __b, size_t _p_size,
williamr@2:                 allocator_type __a) :
williamr@2:     _Refcount_Base(1),
williamr@2:     _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0), _M_size(__a, _p_size)
williamr@2:   { }
williamr@2: 
williamr@4:   typedef typename _AreSameUnCVTypes<_CharT, char>::_Ret _IsChar;
williamr@4: # ifdef _STLP_HAS_WCHAR_T
williamr@4:   typedef typename _AreSameUnCVTypes<_CharT, wchar_t>::_Ret _IsWCharT;
williamr@4: # else
williamr@4:   typedef __false_type _IsWCharT;
williamr@4: # endif
williamr@4: 
williamr@4:   typedef typename _Lor2<_IsChar, _IsWCharT>::_Ret _IsBasicCharType;
williamr@4: 
williamr@4: #if 0
williamr@4:   /* Please tell why this code is necessary if you uncomment it.
williamr@4:    * Problem with it is that rope implementation expect that _S_rounded_up_size(n)
williamr@4:    * returns a size > n in order to store the terminating null charater. When
williamr@4:    * instanciation type is not a char or wchar_t this is not guaranty resulting in
williamr@4:    * memory overrun.
williamr@4:    */
williamr@4:   static size_t _S_rounded_up_size_aux(size_t __n, __true_type const& /*_IsBasicCharType*/) {
williamr@2:     // Allow slop for in-place expansion.
williamr@4:     return (__n + _S_alloc_granularity) & ~(_S_alloc_granularity - 1);
williamr@2:   }
williamr@2: 
williamr@4:   static size_t _S_rounded_up_size_aux(size_t __n, __false_type const& /*_IsBasicCharType*/) {
williamr@4:     // Allow slop for in-place expansion.
williamr@4:     return (__n + _S_alloc_granularity - 1) & ~(_S_alloc_granularity - 1);
williamr@4:   }
williamr@4: #endif
williamr@4:   // fbp : moved from RopeLeaf
williamr@4:   static size_t _S_rounded_up_size(size_t __n)
williamr@4:   //{ return _S_rounded_up_size_aux(__n, _IsBasicCharType()); }
williamr@4:   { return (__n + _S_alloc_granularity) & ~(_S_alloc_granularity - 1); }
williamr@4: 
williamr@4:   static void _S_free_string( _CharT* __s, size_t __len,
williamr@2:                              allocator_type __a) {
williamr@4:     _STLP_STD::_Destroy_Range(__s, __s + __len);
williamr@2:     //  This has to be a static member, so this gets a bit messy
williamr@4: #   ifndef _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE
williamr@4:     __a.deallocate(__s, _S_rounded_up_size(__len));    //*ty 03/24/2001 - restored not to use __stl_alloc_rebind() since it is not defined under _STLP_MEMBER_TEMPLATE_CLASSES
williamr@2: #   else
williamr@2:     __stl_alloc_rebind (__a, (_CharT*)0).deallocate(__s, _S_rounded_up_size(__len));
williamr@2: #   endif
williamr@2:   }
williamr@4: 
williamr@2:   // Deallocate data section of a leaf.
williamr@2:   // This shouldn't be a member function.
williamr@2:   // But its hard to do anything else at the
williamr@2:   // moment, because it's templatized w.r.t.
williamr@2:   // an allocator.
williamr@2:   // Does nothing if __GC is defined.
williamr@2:   void _M_free_c_string();
williamr@2:   void _M_free_tree();
williamr@2:   // Deallocate t. Assumes t is not 0.
williamr@4:   void _M_unref_nonnil() {
williamr@4:     if (_M_decr() == 0) _M_free_tree();
williamr@2:   }
williamr@4:   void _M_ref_nonnil() {
williamr@2:     _M_incr();
williamr@2:   }
williamr@4:   static void _S_unref(_Self* __t) {
williamr@2:     if (0 != __t) {
williamr@2:       __t->_M_unref_nonnil();
williamr@2:     }
williamr@2:   }
williamr@4:   static void _S_ref(_Self* __t) {
williamr@2:     if (0 != __t) __t->_M_incr();
williamr@2:   }
williamr@4:   //static void _S_free_if_unref(_Self* __t) {
williamr@4:   //  if (0 != __t && 0 == __t->_M_ref_count) __t->_M_free_tree();
williamr@4:   //}
williamr@4: };
williamr@2: 
williamr@2: template<class _CharT, class _Alloc>
williamr@2: struct _Rope_RopeLeaf : public _Rope_RopeRep<_CharT,_Alloc> {
williamr@2: public:
williamr@4:   _CharT* _M_data; /* Not necessarily 0 terminated. */
williamr@2:                                 /* The allocated size is         */
williamr@2:                                 /* _S_rounded_up_size(size), except */
williamr@2:                                 /* in the GC case, in which it   */
williamr@2:                                 /* doesn't matter.               */
williamr@4: private:
williamr@4:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@4:   typedef typename _RopeRep::_IsBasicCharType _IsBasicCharType;
williamr@4:   void _M_init(__true_type const& /*_IsBasicCharType*/) {
williamr@4:     this->_M_c_string = _M_data;
williamr@4:   }
williamr@4:   void _M_init(__false_type const& /*_IsBasicCharType*/) {}
williamr@4: 
williamr@4: public:
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@4:   typedef typename _RopeRep::allocator_type allocator_type;
williamr@4: 
williamr@4:   _Rope_RopeLeaf( _CharT* __d, size_t _p_size, allocator_type __a)
williamr@4:     : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_leaf, 0, true, _p_size, __a),
williamr@4:       _M_data(__d) {
williamr@2:     _STLP_ASSERT(_p_size > 0)
williamr@4:     _M_init(_IsBasicCharType());
williamr@2:   }
williamr@2: 
williamr@2: # ifdef _STLP_NO_ARROW_OPERATOR
williamr@2:   _Rope_RopeLeaf() {}
williamr@2:   _Rope_RopeLeaf(const _Rope_RopeLeaf<_CharT, _Alloc>& ) {}
williamr@2: # endif
williamr@4: 
williamr@2: // The constructor assumes that d has been allocated with
williamr@2:   // the proper allocator and the properly padded size.
williamr@2:   // In contrast, the destructor deallocates the data:
williamr@2:   ~_Rope_RopeLeaf() {
williamr@2:     if (_M_data != this->_M_c_string) {
williamr@2:       this->_M_free_c_string();
williamr@2:     }
williamr@4:     _RopeRep::_S_free_string(_M_data, this->_M_size._M_data, this->get_allocator());
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: template<class _CharT, class _Alloc>
williamr@4: struct _Rope_RopeConcatenation : public _Rope_RopeRep<_CharT, _Alloc> {
williamr@4: private:
williamr@4:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@4: 
williamr@2: public:
williamr@4:   _RopeRep* _M_left;
williamr@4:   _RopeRep* _M_right;
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@4:   typedef typename _RopeRep::allocator_type allocator_type;
williamr@4:   _Rope_RopeConcatenation(_RopeRep* __l, _RopeRep* __r, allocator_type __a)
williamr@4:     : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_concat,
williamr@4:                                    (max)(__l->_M_depth, __r->_M_depth) + 1, false,
williamr@4:                                    __l->_M_size._M_data + __r->_M_size._M_data, __a), _M_left(__l), _M_right(__r)
williamr@2:   {}
williamr@2: # ifdef _STLP_NO_ARROW_OPERATOR
williamr@2:   _Rope_RopeConcatenation() {}
williamr@2:   _Rope_RopeConcatenation(const _Rope_RopeConcatenation<_CharT, _Alloc>&) {}
williamr@2: # endif
williamr@2: 
williamr@2:   ~_Rope_RopeConcatenation() {
williamr@2:     this->_M_free_c_string();
williamr@2:     _M_left->_M_unref_nonnil();
williamr@2:     _M_right->_M_unref_nonnil();
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@4: template <class _CharT, class _Alloc>
williamr@4: struct _Rope_RopeFunction : public _Rope_RopeRep<_CharT, _Alloc> {
williamr@4: private:
williamr@4:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2: public:
williamr@2:   char_producer<_CharT>* _M_fn;
williamr@4:   /*
williamr@4:    * Char_producer is owned by the
williamr@4:    * rope and should be explicitly
williamr@4:    * deleted when the rope becomes
williamr@4:    * inaccessible.
williamr@4:    */
williamr@4:   bool _M_delete_when_done;
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2:   typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
williamr@2: # ifdef _STLP_NO_ARROW_OPERATOR
williamr@2:   _Rope_RopeFunction() {}
williamr@2:   _Rope_RopeFunction(const _Rope_RopeFunction<_CharT, _Alloc>& ) {}
williamr@2: # endif
williamr@2: 
williamr@2:   _Rope_RopeFunction(char_producer<_CharT>* __f, size_t _p_size,
williamr@2:                      bool __d, allocator_type __a)
williamr@4:     : _Rope_RopeRep<_CharT,_Alloc>(_RopeRep::_S_function, 0, true, _p_size, __a), _M_fn(__f)
williamr@2:     , _M_delete_when_done(__d)
williamr@4:   { _STLP_ASSERT(_p_size > 0) }
williamr@4: 
williamr@2:   ~_Rope_RopeFunction() {
williamr@2:     this->_M_free_c_string();
williamr@2:     if (_M_delete_when_done) {
williamr@2:       delete _M_fn;
williamr@2:     }
williamr@2:   }
williamr@2: };
williamr@4: 
williamr@4: /*
williamr@4:  * Substring results are usually represented using just
williamr@4:  * concatenation nodes.  But in the case of very long flat ropes
williamr@4:  * or ropes with a functional representation that isn't practical.
williamr@4:  * In that case, we represent the __result as a special case of
williamr@4:  * RopeFunction, whose char_producer points back to the rope itself.
williamr@4:  * In all cases except repeated substring operations and
williamr@4:  * deallocation, we treat the __result as a RopeFunction.
williamr@4:  */
williamr@2: template<class _CharT, class _Alloc>
williamr@4: struct _Rope_RopeSubstring : public char_producer<_CharT>, public _Rope_RopeFunction<_CharT,_Alloc> {
williamr@2: public:
williamr@2:   // XXX this whole class should be rewritten.
williamr@4:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@4:   _RopeRep *_M_base;      // not 0
williamr@2:   size_t _M_start;
williamr@4:   /* virtual */ void operator()(size_t __start_pos, size_t __req_len,
williamr@4:                                 _CharT* __buffer) {
williamr@4:     typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
williamr@4:     typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
williamr@4:     switch (_M_base->_M_tag) {
williamr@4:     case _RopeRep::_S_function:
williamr@4:     case _RopeRep::_S_substringfn:
williamr@2:       {
williamr@2:         char_producer<_CharT>* __fn =
williamr@4:           __STATIC_CAST(_RopeFunction*, _M_base)->_M_fn;
williamr@2:         _STLP_ASSERT(__start_pos + __req_len <= this->_M_size._M_data)
williamr@2:         _STLP_ASSERT(_M_start + this->_M_size._M_data <= _M_base->_M_size._M_data)
williamr@2:         (*__fn)(__start_pos + _M_start, __req_len, __buffer);
williamr@2:       }
williamr@2:       break;
williamr@4:     case _RopeRep::_S_leaf:
williamr@2:       {
williamr@4:         _CharT* __s =
williamr@4:           __STATIC_CAST(_RopeLeaf*, _M_base)->_M_data;
williamr@4:         _STLP_PRIV __ucopy_n(__s + __start_pos + _M_start, __req_len, __buffer);
williamr@2:       }
williamr@2:       break;
williamr@2:     default:
williamr@2:       _STLP_ASSERT(false)
williamr@2:         ;
williamr@2:     }
williamr@2:   }
williamr@2: 
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@4:   typedef typename _RopeRep::allocator_type allocator_type;
williamr@2: 
williamr@4:   _Rope_RopeSubstring(_RopeRep* __b, size_t __s, size_t __l, allocator_type __a)
williamr@2:     : _Rope_RopeFunction<_CharT,_Alloc>(this, __l, false, __a),
williamr@4:       _M_base(__b), _M_start(__s) {
williamr@2:     _STLP_ASSERT(__l > 0)
williamr@2:     _STLP_ASSERT(__s + __l <= __b->_M_size._M_data)
williamr@2:     _M_base->_M_ref_nonnil();
williamr@4:     this->_M_tag = _RopeRep::_S_substringfn;
williamr@2:   }
williamr@2:   virtual ~_Rope_RopeSubstring()
williamr@4:   { _M_base->_M_unref_nonnil(); }
williamr@2: };
williamr@2: 
williamr@4: /*
williamr@4:  * Self-destructing pointers to Rope_rep.
williamr@4:  * These are not conventional smart pointers.  Their
williamr@4:  * only purpose in life is to ensure that unref is called
williamr@4:  * on the pointer either at normal exit or if an exception
williamr@4:  * is raised.  It is the caller's responsibility to
williamr@4:  * adjust reference counts when these pointers are initialized
williamr@4:  * or assigned to.  (This convention significantly reduces
williamr@4:  * the number of potentially expensive reference count
williamr@4:  * updates.)
williamr@4:  */
williamr@2: template<class _CharT, class _Alloc>
williamr@2: struct _Rope_self_destruct_ptr {
williamr@2:   _Rope_RopeRep<_CharT,_Alloc>* _M_ptr;
williamr@4:   ~_Rope_self_destruct_ptr()
williamr@2:   { _Rope_RopeRep<_CharT,_Alloc>::_S_unref(_M_ptr); }
williamr@2: #   ifdef _STLP_USE_EXCEPTIONS
williamr@4:   _Rope_self_destruct_ptr() : _M_ptr(0) {}
williamr@2: #   else
williamr@4:   _Rope_self_destruct_ptr() {}
williamr@2: #   endif
williamr@2:   _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT,_Alloc>* __p) : _M_ptr(__p) {}
williamr@2:   _Rope_RopeRep<_CharT,_Alloc>& operator*() { return *_M_ptr; }
williamr@2:   _Rope_RopeRep<_CharT,_Alloc>* operator->() { return _M_ptr; }
williamr@2:   operator _Rope_RopeRep<_CharT,_Alloc>*() { return _M_ptr; }
williamr@4:   _Rope_self_destruct_ptr<_CharT, _Alloc>&
williamr@2:   operator= (_Rope_RopeRep<_CharT,_Alloc>* __x)
williamr@2:   { _M_ptr = __x; return *this; }
williamr@2: };
williamr@2: 
williamr@4: /*
williamr@4:  * Dereferencing a nonconst iterator has to return something
williamr@4:  * that behaves almost like a reference.  It's not possible to
williamr@4:  * return an actual reference since assignment requires extra
williamr@4:  * work.  And we would get into the same problems as with the
williamr@4:  * CD2 version of basic_string.
williamr@4:  */
williamr@2: template<class _CharT, class _Alloc>
williamr@2: class _Rope_char_ref_proxy {
williamr@2:   typedef _Rope_char_ref_proxy<_CharT, _Alloc> _Self;
williamr@2:   friend class rope<_CharT,_Alloc>;
williamr@2:   friend class _Rope_iterator<_CharT,_Alloc>;
williamr@2:   friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
williamr@2:   typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
williamr@2:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2:   typedef rope<_CharT,_Alloc> _My_rope;
williamr@2:   size_t _M_pos;
williamr@2:   _CharT _M_current;
williamr@2:   bool _M_current_valid;
williamr@2:   _My_rope* _M_root;     // The whole rope.
williamr@2: public:
williamr@2:   _Rope_char_ref_proxy(_My_rope* __r, size_t __p) :
williamr@2:     _M_pos(__p), _M_current_valid(false), _M_root(__r) {}
williamr@2:   _Rope_char_ref_proxy(const _Self& __x) :
williamr@2:     _M_pos(__x._M_pos), _M_current_valid(false), _M_root(__x._M_root) {}
williamr@2:   // Don't preserve cache if the reference can outlive the
williamr@2:   // expression.  We claim that's not possible without calling
williamr@2:   // a copy constructor or generating reference to a proxy
williamr@2:   // reference.  We declare the latter to have undefined semantics.
williamr@4:   _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
williamr@4:     : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
williamr@2:   inline operator _CharT () const;
williamr@2:   _Self& operator= (_CharT __c);
williamr@2:   _Rope_char_ptr_proxy<_CharT, _Alloc> operator& () const;
williamr@2:   _Self& operator= (const _Self& __c) {
williamr@4:     return operator=((_CharT)__c);
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
williamr@2: template<class _CharT, class __Alloc>
williamr@2: inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
williamr@2:                  _Rope_char_ref_proxy <_CharT, __Alloc > __b) {
williamr@2:   _CharT __tmp = __a;
williamr@2:   __a = __b;
williamr@2:   __b = __tmp;
williamr@2: }
williamr@2: #else
williamr@2: // There is no really acceptable way to handle this.  The default
williamr@2: // definition of swap doesn't work for proxy references.
williamr@2: // It can't really be made to work, even with ugly hacks, since
williamr@2: // the only unusual operation it uses is the copy constructor, which
williamr@2: // is needed for other purposes.  We provide a macro for
williamr@2: // full specializations, and instantiate the most common case.
williamr@2: # define _ROPE_SWAP_SPECIALIZATION(_CharT, __Alloc) \
williamr@2:     inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a, \
williamr@2:                      _Rope_char_ref_proxy <_CharT, __Alloc > __b) { \
williamr@2:         _CharT __tmp = __a; \
williamr@2:         __a = __b; \
williamr@2:         __b = __tmp; \
williamr@2:     }
williamr@2: 
williamr@2: _ROPE_SWAP_SPECIALIZATION(char,_STLP_DEFAULT_ALLOCATOR(char) )
williamr@2: 
williamr@4: # ifndef _STLP_NO_WCHAR_T
williamr@4: _ROPE_SWAP_SPECIALIZATION(wchar_t,_STLP_DEFAULT_ALLOCATOR(wchar_t) )
williamr@4: # endif
williamr@4: 
williamr@2: #endif /* !_STLP_FUNCTION_TMPL_PARTIAL_ORDER */
williamr@2: 
williamr@4: template<class _CharT, class _Alloc>
williamr@2: class _Rope_char_ptr_proxy {
williamr@2:   // XXX this class should be rewritten.
williamr@2: public:
williamr@2:   typedef _Rope_char_ptr_proxy<_CharT, _Alloc> _Self;
williamr@2:   friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
williamr@2:   size_t _M_pos;
williamr@2:   rope<_CharT,_Alloc>* _M_root;     // The whole rope.
williamr@2: 
williamr@4:   _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
williamr@2:     : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
williamr@2:   _Rope_char_ptr_proxy(const _Self& __x)
williamr@2:     : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
williamr@2:   _Rope_char_ptr_proxy() {}
williamr@2:   _Rope_char_ptr_proxy(_CharT* __x) : _M_pos(0), _M_root(0) {
williamr@2:     _STLP_ASSERT(0 == __x)
williamr@2:   }
williamr@4:   _Self& operator= (const _Self& __x) {
williamr@2:     _M_pos = __x._M_pos;
williamr@2:     _M_root = __x._M_root;
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Rope_char_ref_proxy<_CharT,_Alloc> operator*() const {
williamr@2:     return _Rope_char_ref_proxy<_CharT,_Alloc>(_M_root, _M_pos);
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: 
williamr@4: /*
williamr@4:  * Rope iterators:
williamr@4:  * Unlike in the C version, we cache only part of the stack
williamr@4:  * for rope iterators, since they must be efficiently copyable.
williamr@4:  * When we run out of cache, we have to reconstruct the iterator
williamr@4:  * value.
williamr@4:  * Pointers from iterators are not included in reference counts.
williamr@4:  * Iterators are assumed to be thread private.  Ropes can
williamr@4:  * be shared.
williamr@4:  */
williamr@2: template<class _CharT, class _Alloc>
williamr@2: class _Rope_iterator_base
williamr@2: /*   : public random_access_iterator<_CharT, ptrdiff_t>  */
williamr@2: {
williamr@2:   friend class rope<_CharT,_Alloc>;
williamr@2:   typedef _Rope_iterator_base<_CharT, _Alloc> _Self;
williamr@4:   typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcat;
williamr@2: public:
williamr@2:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@4: 
williamr@4:   enum { _S_path_cache_len = 4 }; // Must be <= 9 because of _M_path_direction.
williamr@2:   enum { _S_iterator_buf_len = 15 };
williamr@2:   size_t _M_current_pos;
williamr@4:   // The whole rope.
williamr@4:   _RopeRep* _M_root;
williamr@4:   // Starting position for current leaf
williamr@4:   size_t _M_leaf_pos;
williamr@4:   // Buffer possibly containing current char.
williamr@4:   _CharT* _M_buf_start;
williamr@4:   // Pointer to current char in buffer, != 0 ==> buffer valid.
williamr@4:   _CharT* _M_buf_ptr;
williamr@2:   // One past __last valid char in buffer.
williamr@4:   _CharT* _M_buf_end;
williamr@4: 
williamr@2:   // What follows is the path cache.  We go out of our
williamr@2:   // way to make this compact.
williamr@2:   // Path_end contains the bottom section of the path from
williamr@2:   // the root to the current leaf.
williamr@4:   struct {
williamr@4: #  if defined (__BORLANDC__) && (__BORLANDC__ < 0x560)
williamr@4:     _RopeRep const*_M_data[4];
williamr@4: #  else
williamr@4:     _RopeRep const*_M_data[_S_path_cache_len];
williamr@4: #  endif
williamr@4:   } _M_path_end;
williamr@4:   // Last valid __pos in path_end;
williamr@4:   // _M_path_end[0] ... _M_path_end[_M_leaf_index-1]
williamr@2:   // point to concatenation nodes.
williamr@4:   int _M_leaf_index;
williamr@4:   // (_M_path_directions >> __i) & 1 is 1
williamr@4:   // if we got from _M_path_end[leaf_index - __i - 1]
williamr@2:   // to _M_path_end[leaf_index - __i] by going to the
williamr@2:   // __right. Assumes path_cache_len <= 9.
williamr@4:   unsigned char _M_path_directions;
williamr@2:   // Short buffer for surrounding chars.
williamr@4:   // This is useful primarily for
williamr@2:   // RopeFunctions.  We put the buffer
williamr@2:   // here to avoid locking in the
williamr@2:   // multithreaded case.
williamr@2:   // The cached path is generally assumed to be valid
williamr@2:   // only if the buffer is valid.
williamr@4:   struct {
williamr@4: #  if defined (__BORLANDC__) && (__BORLANDC__ < 0x560)
williamr@4:     _CharT _M_data[15];
williamr@4: #  else
williamr@4:     _CharT _M_data[_S_iterator_buf_len];
williamr@4: #  endif
williamr@4:   } _M_tmp_buf;
williamr@4: 
williamr@4:   // Set buffer contents given path cache.
williamr@2:   static void _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@4:   // Set buffer contents and path cache.
williamr@2:   static void _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@4:   // As above, but assumes path cache is valid for previous posn.
williamr@2:   static void _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@2:   _Rope_iterator_base() {}
williamr@2:   _Rope_iterator_base(_RopeRep* __root, size_t __pos)
williamr@2:     : _M_current_pos(__pos),_M_root(__root),  _M_buf_ptr(0) {}
williamr@2:   void _M_incr(size_t __n);
williamr@2:   void _M_decr(size_t __n);
williamr@2: public:
williamr@2:   size_t index() const { return _M_current_pos; }
williamr@4: private:
williamr@4:   void _M_copy_buf(const _Self& __x) {
williamr@4:     _M_tmp_buf = __x._M_tmp_buf;
williamr@4:     if (__x._M_buf_start == __x._M_tmp_buf._M_data) {
williamr@4:       _M_buf_start = _M_tmp_buf._M_data;
williamr@4:       _M_buf_end = _M_buf_start + (__x._M_buf_end - __x._M_buf_start);
williamr@4:       _M_buf_ptr = _M_buf_start + (__x._M_buf_ptr - __x._M_buf_start);
williamr@2:     } else {
williamr@4:       _M_buf_end = __x._M_buf_end;
williamr@2:     }
williamr@2:   }
williamr@4: 
williamr@4: public:
williamr@4:   _Rope_iterator_base(const _Self& __x) : 
williamr@4:       _M_current_pos(__x._M_current_pos),
williamr@4:       _M_root(__x._M_root),
williamr@4:       _M_leaf_pos( __x._M_leaf_pos ),
williamr@4:       _M_buf_start(__x._M_buf_start),
williamr@4:       _M_buf_ptr(__x._M_buf_ptr),
williamr@4:       _M_path_end(__x._M_path_end),
williamr@4:       _M_leaf_index(__x._M_leaf_index),
williamr@4:       _M_path_directions(__x._M_path_directions)
williamr@4:       {
williamr@4:         if (0 != __x._M_buf_ptr) {
williamr@4:           _M_copy_buf(__x);
williamr@4:         }
williamr@4:       }
williamr@4:   _Self& operator = (const _Self& __x)
williamr@4:       {
williamr@4:         _M_current_pos = __x._M_current_pos;
williamr@4:         _M_root = __x._M_root;
williamr@4:         _M_buf_start = __x._M_buf_start;
williamr@4:         _M_buf_ptr = __x._M_buf_ptr;
williamr@4:         _M_path_end = __x._M_path_end;
williamr@4:         _M_leaf_index = __x._M_leaf_index;
williamr@4:         _M_path_directions = __x._M_path_directions;
williamr@4:         _M_leaf_pos = __x._M_leaf_pos;
williamr@4:         if (0 != __x._M_buf_ptr) {
williamr@4:           _M_copy_buf(__x);
williamr@4:         }
williamr@4:         return *this;
williamr@4:       }
williamr@2: };
williamr@2: 
williamr@2: template<class _CharT, class _Alloc> class _Rope_iterator;
williamr@2: 
williamr@2: template<class _CharT, class _Alloc>
williamr@2: class _Rope_const_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
williamr@2:   friend class rope<_CharT,_Alloc>;
williamr@2:   typedef  _Rope_const_iterator<_CharT, _Alloc> _Self;
williamr@2:   typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
williamr@2:   //  protected:
williamr@2: public:
williamr@2: #   ifndef _STLP_HAS_NO_NAMESPACES
williamr@2:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2:   // The one from the base class may not be directly visible.
williamr@2: #   endif
williamr@2:   _Rope_const_iterator(const _RopeRep* __root, size_t __pos):
williamr@4:     _Rope_iterator_base<_CharT,_Alloc>(__CONST_CAST(_RopeRep*,__root), __pos)
williamr@2:     // Only nonconst iterators modify root ref count
williamr@2:   {}
williamr@2: public:
williamr@2:   typedef _CharT reference;   // Really a value.  Returning a reference
williamr@4:                               // Would be a mess, since it would have
williamr@4:                               // to be included in refcount.
williamr@2:   typedef const _CharT* pointer;
williamr@2:   typedef _CharT value_type;
williamr@2:   typedef ptrdiff_t difference_type;
williamr@2:   typedef random_access_iterator_tag iterator_category;
williamr@2: 
williamr@2: public:
williamr@4:   _Rope_const_iterator() {}
williamr@2:   _Rope_const_iterator(const _Self& __x) :
williamr@2:     _Rope_iterator_base<_CharT,_Alloc>(__x) { }
williamr@4:   _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x):
williamr@2:     _Rope_iterator_base<_CharT,_Alloc>(__x) {}
williamr@2:   _Rope_const_iterator(const rope<_CharT,_Alloc>& __r, size_t __pos) :
williamr@2:     _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr._M_data, __pos) {}
williamr@2:   _Self& operator= (const _Self& __x) {
williamr@4:     _Base::operator=(__x);
williamr@4:     return *this;
williamr@2:   }
williamr@2:   reference operator*() {
williamr@4:     if (0 == this->_M_buf_ptr)
williamr@4: #if !defined (__DMC__)
williamr@4:       _Rope_iterator_base<_CharT, _Alloc>::_S_setcache(*this);
williamr@4: #else
williamr@4:     { _Rope_iterator_base<_CharT, _Alloc>* __x = this; _S_setcache(*__x); }
williamr@4: #endif
williamr@2:     return *(this->_M_buf_ptr);
williamr@2:   }
williamr@2:   _Self& operator++() {
williamr@4:     _CharT* __next;
williamr@2:     if (0 != this->_M_buf_ptr && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end) {
williamr@2:       this->_M_buf_ptr = __next;
williamr@2:       ++this->_M_current_pos;
williamr@2:     } else {
williamr@2:       this->_M_incr(1);
williamr@2:     }
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator+=(ptrdiff_t __n) {
williamr@2:     if (__n >= 0) {
williamr@2:       this->_M_incr(__n);
williamr@2:     } else {
williamr@2:       this->_M_decr(-__n);
williamr@2:     }
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator--() {
williamr@2:     this->_M_decr(1);
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator-=(ptrdiff_t __n) {
williamr@2:     if (__n >= 0) {
williamr@2:       this->_M_decr(__n);
williamr@2:     } else {
williamr@2:       this->_M_incr(-__n);
williamr@2:     }
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self operator++(int) {
williamr@2:     size_t __old_pos = this->_M_current_pos;
williamr@2:     this->_M_incr(1);
williamr@2:     return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
williamr@2:     // This makes a subsequent dereference expensive.
williamr@2:     // Perhaps we should instead copy the iterator
williamr@2:     // if it has a valid cache?
williamr@2:   }
williamr@2:   _Self operator--(int) {
williamr@2:     size_t __old_pos = this->_M_current_pos;
williamr@2:     this->_M_decr(1);
williamr@2:     return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
williamr@2:   }
williamr@2:   inline reference operator[](size_t __n);
williamr@2: };
williamr@2: 
williamr@2: template<class _CharT, class _Alloc>
williamr@2: class _Rope_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
williamr@2:   friend class rope<_CharT,_Alloc>;
williamr@2:   typedef _Rope_iterator<_CharT, _Alloc> _Self;
williamr@2:   typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
williamr@2:   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@4: 
williamr@2: public:
williamr@2:   rope<_CharT,_Alloc>* _M_root_rope;
williamr@2:   // root is treated as a cached version of this,
williamr@2:   // and is used to detect changes to the underlying
williamr@2:   // rope.
williamr@2:   // Root is included in the reference count.
williamr@2:   // This is necessary so that we can detect changes reliably.
williamr@2:   // Unfortunately, it requires careful bookkeeping for the
williamr@2:   // nonGC case.
williamr@2:   _Rope_iterator(rope<_CharT,_Alloc>* __r, size_t __pos);
williamr@4: 
williamr@2:   void _M_check();
williamr@2: public:
williamr@2:   typedef _Rope_char_ref_proxy<_CharT,_Alloc>  reference;
williamr@2:   typedef _Rope_char_ref_proxy<_CharT,_Alloc>* pointer;
williamr@2:   typedef _CharT value_type;
williamr@2:   typedef ptrdiff_t difference_type;
williamr@2:   typedef random_access_iterator_tag iterator_category;
williamr@2: public:
williamr@4:   ~_Rope_iterator() {  //*TY 5/6/00 - added dtor to balance reference count
williamr@2:     _RopeRep::_S_unref(this->_M_root);
williamr@2:   }
williamr@4: 
williamr@2:   rope<_CharT,_Alloc>& container() { return *_M_root_rope; }
williamr@2:   _Rope_iterator() {
williamr@2:     this->_M_root = 0;  // Needed for reference counting.
williamr@4:   }
williamr@2:   _Rope_iterator(const  _Self& __x) :
williamr@2:     _Rope_iterator_base<_CharT,_Alloc>(__x) {
williamr@2:     _M_root_rope = __x._M_root_rope;
williamr@2:     _RopeRep::_S_ref(this->_M_root);
williamr@2:   }
williamr@2:   _Rope_iterator(rope<_CharT,_Alloc>& __r, size_t __pos);
williamr@2:   _Self& operator= (const  _Self& __x) {
williamr@2:     _RopeRep* __old = this->_M_root;
williamr@2:     _RopeRep::_S_ref(__x._M_root);
williamr@4:     _Base::operator=(__x);
williamr@4:     _M_root_rope = __x._M_root_rope;
williamr@2:     _RopeRep::_S_unref(__old);
williamr@4:     return *this;
williamr@2:   }
williamr@2:   reference operator*() {
williamr@2:     _M_check();
williamr@2:     if (0 == this->_M_buf_ptr) {
williamr@4:       return reference(_M_root_rope, this->_M_current_pos);
williamr@2:     } else {
williamr@4:       return reference(_M_root_rope, this->_M_current_pos, *(this->_M_buf_ptr));
williamr@2:     }
williamr@2:   }
williamr@2:   _Self& operator++() {
williamr@2:     this->_M_incr(1);
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator+=(ptrdiff_t __n) {
williamr@2:     if (__n >= 0) {
williamr@2:       this->_M_incr(__n);
williamr@2:     } else {
williamr@2:       this->_M_decr(-__n);
williamr@2:     }
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator--() {
williamr@2:     this->_M_decr(1);
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self& operator-=(ptrdiff_t __n) {
williamr@2:     if (__n >= 0) {
williamr@2:       this->_M_decr(__n);
williamr@2:     } else {
williamr@2:       this->_M_incr(-__n);
williamr@2:     }
williamr@2:     return *this;
williamr@2:   }
williamr@2:   _Self operator++(int) {
williamr@2:     size_t __old_pos = this->_M_current_pos;
williamr@2:     this->_M_incr(1);
williamr@4:     return _Self(_M_root_rope, __old_pos);
williamr@2:   }
williamr@2:   _Self operator--(int) {
williamr@2:     size_t __old_pos = this->_M_current_pos;
williamr@2:     this->_M_decr(1);
williamr@4:     return _Self(_M_root_rope, __old_pos);
williamr@2:   }
williamr@2:   reference operator[](ptrdiff_t __n) {
williamr@4:     return reference(_M_root_rope, this->_M_current_pos + __n);
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@2: # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline random_access_iterator_tag
williamr@2: iterator_category(const _Rope_iterator<_CharT,_Alloc>&) {  return random_access_iterator_tag();}
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _CharT* value_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline ptrdiff_t* distance_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline random_access_iterator_tag
williamr@2: iterator_category(const _Rope_const_iterator<_CharT,_Alloc>&) { return random_access_iterator_tag(); }
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _CharT* value_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline ptrdiff_t* distance_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
williamr@4: #endif /* _STLP_USE_OLD_HP_ITERATOR_QUERIES */
williamr@4: 
williamr@4: template <class _CharT, class _Alloc, class _CharConsumer>
williamr@4: bool _S_apply_to_pieces(_CharConsumer& __c,
williamr@4:                         _Rope_RopeRep<_CharT, _Alloc> *__r,
williamr@4:                         size_t __begin, size_t __end);
williamr@4:                         // begin and end are assumed to be in range.
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: class rope
williamr@4: #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
williamr@4:            : public __stlport_class<rope<_CharT, _Alloc> >
williamr@4: #endif
williamr@4: {
williamr@2:   typedef rope<_CharT,_Alloc> _Self;
williamr@2: public:
williamr@2:   typedef _CharT value_type;
williamr@2:   typedef ptrdiff_t difference_type;
williamr@2:   typedef size_t size_type;
williamr@2:   typedef _CharT const_reference;
williamr@2:   typedef const _CharT* const_pointer;
williamr@2:   typedef _Rope_iterator<_CharT,_Alloc> iterator;
williamr@2:   typedef _Rope_const_iterator<_CharT,_Alloc> const_iterator;
williamr@2:   typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
williamr@2:   typedef _Rope_char_ptr_proxy<_CharT,_Alloc> pointer;
williamr@4: 
williamr@2:   friend class _Rope_iterator<_CharT,_Alloc>;
williamr@2:   friend class _Rope_const_iterator<_CharT,_Alloc>;
williamr@2:   friend struct _Rope_RopeRep<_CharT,_Alloc>;
williamr@2:   friend class _Rope_iterator_base<_CharT,_Alloc>;
williamr@2:   friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
williamr@2:   friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
williamr@2:   friend struct _Rope_RopeSubstring<_CharT,_Alloc>;
williamr@2: 
williamr@2:   _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
williamr@4: 
williamr@2: protected:
williamr@4:   typedef _CharT* _Cstrptr;
williamr@4: 
williamr@2:   static _CharT _S_empty_c_str[1];
williamr@4: 
williamr@2:   enum { _S_copy_max = 23 };
williamr@2:   // For strings shorter than _S_copy_max, we copy to
williamr@2:   // concatenate.
williamr@4: 
williamr@4:   typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
williamr@4:   typedef typename _RopeRep::_IsBasicCharType _IsBasicCharType;
williamr@4: 
williamr@2: public:
williamr@2:   _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2:   typedef typename _Alloc_traits<_CharT,_Alloc>::allocator_type  allocator_type;
williamr@4: 
williamr@2: public:
williamr@2:   // The only data member of a rope:
williamr@4:   _STLP_PRIV _STLP_alloc_proxy<_RopeRep*, _CharT, allocator_type> _M_tree_ptr;
williamr@2: 
williamr@4: public:
williamr@4:   allocator_type get_allocator() const { return allocator_type(_M_tree_ptr); }
williamr@4: 
williamr@4: public:
williamr@2:   typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcatenation;
williamr@2:   typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
williamr@2:   typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
williamr@2:   typedef _Rope_RopeSubstring<_CharT,_Alloc> _RopeSubstring;
williamr@2: 
williamr@2:   // Retrieve a character at the indicated position.
williamr@2:   static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
williamr@2: 
williamr@2:   // Obtain a pointer to the character at the indicated position.
williamr@2:   // The pointer can be used to change the character.
williamr@2:   // If such a pointer cannot be produced, as is frequently the
williamr@2:   // case, 0 is returned instead.
williamr@2:   // (Returns nonzero only if all nodes in the path have a refcount
williamr@2:   // of 1.)
williamr@2:   static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
williamr@2: 
williamr@4:   static void _S_unref(_RopeRep* __t) {
williamr@2:     _RopeRep::_S_unref(__t);
williamr@2:   }
williamr@4:   static void _S_ref(_RopeRep* __t) {
williamr@2:     _RopeRep::_S_ref(__t);
williamr@2:   }
williamr@2: 
williamr@2:   typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
williamr@2: 
williamr@2:   // _Result is counted in refcount.
williamr@2:   static _RopeRep* _S_substring(_RopeRep* __base,
williamr@2:                                 size_t __start, size_t __endp1);
williamr@2: 
williamr@2:   static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
williamr@2:                                        const _CharT* __iter, size_t __slen);
williamr@2:   // Concatenate rope and char ptr, copying __s.
williamr@2:   // Should really take an arbitrary iterator.
williamr@2:   // Result is counted in refcount.
williamr@2:   static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
williamr@4:                                              const _CharT* __iter, size_t __slen);
williamr@2:     // As above, but one reference to __r is about to be
williamr@2:     // destroyed.  Thus the pieces may be recycled if all
williamr@2:     // relevent reference counts are 1.
williamr@2: 
williamr@2:   // General concatenation on _RopeRep.  _Result
williamr@2:   // has refcount of 1.  Adjusts argument refcounts.
williamr@4:   static _RopeRep* _S_concat_rep(_RopeRep* __left, _RopeRep* __right);
williamr@2: 
williamr@2: public:
williamr@4: #if defined (_STLP_MEMBER_TEMPLATES)
williamr@4:   template <class _CharConsumer>
williamr@4: #else
williamr@4:   typedef _Rope_char_consumer<_CharT> _CharConsumer;
williamr@4: #endif
williamr@4:   void apply_to_pieces(size_t __begin, size_t __end,
williamr@4:                        _CharConsumer& __c) const
williamr@4:   { _S_apply_to_pieces(__c, _M_tree_ptr._M_data, __begin, __end); }
williamr@2: 
williamr@2: protected:
williamr@2: 
williamr@4:   static size_t _S_rounded_up_size(size_t __n)
williamr@4:   { return _RopeRep::_S_rounded_up_size(__n); }
williamr@2: 
williamr@2:   // Allocate and construct a RopeLeaf using the supplied allocator
williamr@2:   // Takes ownership of s instead of copying.
williamr@4:   static _RopeLeaf* _S_new_RopeLeaf(_CharT *__s,
williamr@4:                                     size_t _p_size, allocator_type __a) {
williamr@4:     _RopeLeaf* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
williamr@4:                                                 _RopeLeaf).allocate(1);
williamr@2:     _STLP_TRY {
williamr@2:       _STLP_PLACEMENT_NEW(__space) _RopeLeaf(__s, _p_size, __a);
williamr@2:     }
williamr@4:    _STLP_UNWIND(_STLP_CREATE_ALLOCATOR(allocator_type,__a,
williamr@4:                                        _RopeLeaf).deallocate(__space, 1))
williamr@4:     return __space;
williamr@2:   }
williamr@2: 
williamr@4:   static _RopeConcatenation* _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
williamr@4:                                                       allocator_type __a) {
williamr@4:    _RopeConcatenation* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
williamr@4:                                                         _RopeConcatenation).allocate(1);
williamr@2:     return _STLP_PLACEMENT_NEW(__space) _RopeConcatenation(__left, __right, __a);
williamr@2:   }
williamr@2: 
williamr@2:   static _RopeFunction* _S_new_RopeFunction(char_producer<_CharT>* __f,
williamr@4:                                             size_t _p_size, bool __d, allocator_type __a) {
williamr@4:    _RopeFunction* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
williamr@4:                                                    _RopeFunction).allocate(1);
williamr@2:     return _STLP_PLACEMENT_NEW(__space) _RopeFunction(__f, _p_size, __d, __a);
williamr@2:   }
williamr@2: 
williamr@4:   static _RopeSubstring* _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
williamr@4:                                               size_t __l, allocator_type __a) {
williamr@4:    _RopeSubstring* __space = _STLP_CREATE_ALLOCATOR(allocator_type, __a,
williamr@4:                                                     _RopeSubstring).allocate(1);
williamr@2:     return _STLP_PLACEMENT_NEW(__space) _RopeSubstring(__b, __s, __l, __a);
williamr@2:   }
williamr@2: 
williamr@2:   static
williamr@2:   _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
williamr@4:                                                size_t _p_size, allocator_type __a) {
williamr@2:     if (0 == _p_size) return 0;
williamr@2: 
williamr@2:    _CharT* __buf = _STLP_CREATE_ALLOCATOR(allocator_type,__a, _CharT).allocate(_S_rounded_up_size(_p_size));
williamr@2: 
williamr@4:     _STLP_PRIV __ucopy_n(__s, _p_size, __buf);
williamr@4:     _S_construct_null(__buf + _p_size);
williamr@2: 
williamr@2:     _STLP_TRY {
williamr@2:       return _S_new_RopeLeaf(__buf, _p_size, __a);
williamr@2:     }
williamr@2:     _STLP_UNWIND(_RopeRep::_S_free_string(__buf, _p_size, __a))
williamr@4:     _STLP_RET_AFTER_THROW(0)
williamr@2:   }
williamr@4: 
williamr@2: 
williamr@2:   // Concatenation of nonempty strings.
williamr@2:   // Always builds a concatenation node.
williamr@2:   // Rebalances if the result is too deep.
williamr@2:   // Result has refcount 1.
williamr@2:   // Does not increment left and right ref counts even though
williamr@2:   // they are referenced.
williamr@2:   static _RopeRep*
williamr@2:   _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
williamr@2: 
williamr@2:   // Concatenation helper functions
williamr@2:   static _RopeLeaf*
williamr@2:   _S_leaf_concat_char_iter(_RopeLeaf* __r,
williamr@2:                            const _CharT* __iter, size_t __slen);
williamr@2:   // Concatenate by copying leaf.
williamr@2:   // should take an arbitrary iterator
williamr@2:   // result has refcount 1.
williamr@2:   static _RopeLeaf* _S_destr_leaf_concat_char_iter
williamr@2:   (_RopeLeaf* __r, const _CharT* __iter, size_t __slen);
williamr@2:   // A version that potentially clobbers __r if __r->_M_ref_count == 1.
williamr@2: 
williamr@2: 
williamr@2:   // A helper function for exponentiating strings.
williamr@2:   // This uses a nonstandard refcount convention.
williamr@2:   // The result has refcount 0.
williamr@4:   typedef _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
williamr@4: #if !defined (__GNUC__) || (__GNUC__ < 3)
williamr@4:   friend _Concat_fn;
williamr@4: #else
williamr@4:   friend struct _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc>;
williamr@4: #endif
williamr@2: 
williamr@2: public:
williamr@2:   static size_t _S_char_ptr_len(const _CharT* __s) {
williamr@4:     return char_traits<_CharT>::length(__s);
williamr@2:   }
williamr@2: 
williamr@2: public: /* for operators */
williamr@2:   rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
williamr@2:     : _M_tree_ptr(__a, __t) { }
williamr@2: private:
williamr@2:   // Copy __r to the _CharT buffer.
williamr@2:   // Returns __buffer + __r->_M_size._M_data.
williamr@2:   // Assumes that buffer is uninitialized.
williamr@2:   static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
williamr@2: 
williamr@2:   // Again, with explicit starting position and length.
williamr@2:   // Assumes that buffer is uninitialized.
williamr@2:   static _CharT* _S_flatten(_RopeRep* __r,
williamr@2:                             size_t __start, size_t __len,
williamr@2:                             _CharT* __buffer);
williamr@2: 
williamr@2:   // fbp : HP aCC prohibits access to protected min_len from within static methods ( ?? )
williamr@2: public:
williamr@4:   static const unsigned long _S_min_len[__ROPE_DEPTH_SIZE];
williamr@2: protected:
williamr@2:   static bool _S_is_balanced(_RopeRep* __r)
williamr@2:   { return (__r->_M_size._M_data >= _S_min_len[__r->_M_depth]); }
williamr@2: 
williamr@4:   static bool _S_is_almost_balanced(_RopeRep* __r) {
williamr@4:     return (__r->_M_depth == 0 ||
williamr@4:             __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 1]);
williamr@4:   }
williamr@2: 
williamr@4:   static bool _S_is_roughly_balanced(_RopeRep* __r) {
williamr@4:     return (__r->_M_depth <= 1 ||
williamr@4:             __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 2]);
williamr@4:   }
williamr@2: 
williamr@2:   // Assumes the result is not empty.
williamr@2:   static _RopeRep* _S_concat_and_set_balanced(_RopeRep* __left,
williamr@4:                                               _RopeRep* __right) {
williamr@2:     _RopeRep* __result = _S_concat_rep(__left, __right);
williamr@2:     if (_S_is_balanced(__result)) __result->_M_is_balanced = true;
williamr@2:     return __result;
williamr@2:   }
williamr@2: 
williamr@2:   // The basic rebalancing operation.  Logically copies the
williamr@2:   // rope.  The result has refcount of 1.  The client will
williamr@2:   // usually decrement the reference count of __r.
williamr@2:   // The result is within height 2 of balanced by the above
williamr@2:   // definition.
williamr@2:   static _RopeRep* _S_balance(_RopeRep* __r);
williamr@2: 
williamr@2:   // Add all unbalanced subtrees to the forest of balanceed trees.
williamr@2:   // Used only by balance.
williamr@2:   static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
williamr@4: 
williamr@2:   // Add __r to forest, assuming __r is already balanced.
williamr@2:   static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
williamr@2: 
williamr@4: #ifdef _STLP_DEBUG
williamr@2:   // Print to stdout, exposing structure
williamr@2:   static void _S_dump(_RopeRep* __r, int __indent = 0);
williamr@4: #endif
williamr@2: 
williamr@2:   // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
williamr@2:   static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
williamr@2: 
williamr@4:   void _STLP_FUNCTION_THROWS _M_throw_out_of_range() const;
williamr@4: 
williamr@4:   void _M_reset(_RopeRep* __r) {
williamr@4:     //if (__r != _M_tree_ptr._M_data) {
williamr@4:       _S_unref(_M_tree_ptr._M_data);
williamr@4:       _M_tree_ptr._M_data = __r;
williamr@4:     //}
williamr@4:   }
williamr@4: 
williamr@2: public:
williamr@2:   bool empty() const { return 0 == _M_tree_ptr._M_data; }
williamr@2: 
williamr@2:   // Comparison member function.  This is public only for those
williamr@2:   // clients that need a ternary comparison.  Others
williamr@2:   // should use the comparison operators below.
williamr@2:   int compare(const _Self& __y) const {
williamr@2:     return _S_compare(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data);
williamr@2:   }
williamr@2: 
williamr@2:   rope(const _CharT* __s, const allocator_type& __a = allocator_type())
williamr@4:     : _M_tree_ptr(__a, _S_RopeLeaf_from_unowned_char_ptr(__s, _S_char_ptr_len(__s),__a))
williamr@4:   {}
williamr@2: 
williamr@2:   rope(const _CharT* __s, size_t __len,
williamr@2:        const allocator_type& __a = allocator_type())
williamr@4:     : _M_tree_ptr(__a, (_S_RopeLeaf_from_unowned_char_ptr(__s, __len, __a)))
williamr@4:   {}
williamr@2: 
williamr@2:   // Should perhaps be templatized with respect to the iterator type
williamr@2:   // and use Sequence_buffer.  (It should perhaps use sequence_buffer
williamr@2:   // even now.)
williamr@2:   rope(const _CharT *__s, const _CharT *__e,
williamr@2:        const allocator_type& __a = allocator_type())
williamr@4:     : _M_tree_ptr(__a, _S_RopeLeaf_from_unowned_char_ptr(__s, __e - __s, __a))
williamr@4:   {}
williamr@2: 
williamr@2:   rope(const const_iterator& __s, const const_iterator& __e,
williamr@2:        const allocator_type& __a = allocator_type())
williamr@2:     : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
williamr@2:                                     __e._M_current_pos))
williamr@4:   {}
williamr@2: 
williamr@2:   rope(const iterator& __s, const iterator& __e,
williamr@2:        const allocator_type& __a = allocator_type())
williamr@2:     : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
williamr@2:                                     __e._M_current_pos))
williamr@4:   {}
williamr@2: 
williamr@2:   rope(_CharT __c, const allocator_type& __a = allocator_type())
williamr@4:     : _M_tree_ptr(__a, (_RopeRep*)0) {
williamr@2:     _CharT* __buf = _M_tree_ptr.allocate(_S_rounded_up_size(1));
williamr@2: 
williamr@4:     _Copy_Construct(__buf, __c);
williamr@4:     _S_construct_null(__buf + 1);
williamr@4: 
williamr@2:     _STLP_TRY {
williamr@2:       _M_tree_ptr._M_data = _S_new_RopeLeaf(__buf, 1, __a);
williamr@2:     }
williamr@2:     _STLP_UNWIND(_RopeRep::_S_free_string(__buf, 1, __a))
williamr@4:   }
williamr@2: 
williamr@4:   rope(size_t __n, _CharT __c,
williamr@2:        const allocator_type& __a = allocator_type()):
williamr@2:     _M_tree_ptr(__a, (_RopeRep*)0) {
williamr@4:     if (0 == __n)
williamr@4:       return;
williamr@4: 
williamr@2:     rope<_CharT,_Alloc> __result;
williamr@2: # define  __exponentiate_threshold size_t(32)
williamr@2:     _RopeRep* __remainder;
williamr@2:     rope<_CharT,_Alloc> __remainder_rope;
williamr@4: 
williamr@2:     // gcc-2.7.2 bugs
williamr@4:     typedef _STLP_PRIV _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
williamr@4: 
williamr@2:     size_t __exponent = __n / __exponentiate_threshold;
williamr@2:     size_t __rest = __n % __exponentiate_threshold;
williamr@2:     if (0 == __rest) {
williamr@2:       __remainder = 0;
williamr@2:     } else {
williamr@2:       _CharT* __rest_buffer = _M_tree_ptr.allocate(_S_rounded_up_size(__rest));
williamr@2:       uninitialized_fill_n(__rest_buffer, __rest, __c);
williamr@4:       _S_construct_null(__rest_buffer + __rest);
williamr@2:       _STLP_TRY {
williamr@4:         __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
williamr@2:       }
williamr@2:       _STLP_UNWIND(_RopeRep::_S_free_string(__rest_buffer, __rest, __a))
williamr@4:     }
williamr@2:     __remainder_rope._M_tree_ptr._M_data = __remainder;
williamr@2:     if (__exponent != 0) {
williamr@4:       _CharT* __base_buffer = _M_tree_ptr.allocate(_S_rounded_up_size(__exponentiate_threshold));
williamr@2:       _RopeLeaf* __base_leaf;
williamr@2:       rope<_CharT,_Alloc> __base_rope;
williamr@2:       uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
williamr@4:       _S_construct_null(__base_buffer + __exponentiate_threshold);
williamr@2:       _STLP_TRY {
williamr@4:         __base_leaf = _S_new_RopeLeaf(__base_buffer,
williamr@2:                                       __exponentiate_threshold, __a);
williamr@2:       }
williamr@4:       _STLP_UNWIND(_RopeRep::_S_free_string(__base_buffer,
williamr@2:                                             __exponentiate_threshold, __a))
williamr@4:       __base_rope._M_tree_ptr._M_data = __base_leaf;
williamr@2:       if (1 == __exponent) {
williamr@4:         __result = __base_rope;
williamr@4:         // One each for base_rope and __result
williamr@4:         //_STLP_ASSERT(2 == __result._M_tree_ptr._M_data->_M_ref_count)
williamr@2:       } else {
williamr@4:         __result = _STLP_PRIV __power(__base_rope, __exponent, _Concat_fn());
williamr@2:       }
williamr@2:       if (0 != __remainder) {
williamr@4:         __result += __remainder_rope;
williamr@2:       }
williamr@2:     } else {
williamr@2:       __result = __remainder_rope;
williamr@2:     }
williamr@2:     _M_tree_ptr._M_data = __result._M_tree_ptr._M_data;
williamr@2:     _M_tree_ptr._M_data->_M_ref_nonnil();
williamr@2: # undef __exponentiate_threshold
williamr@2:   }
williamr@2: 
williamr@2:   rope(const allocator_type& __a = allocator_type())
williamr@2:     : _M_tree_ptr(__a, (_RopeRep*)0) {}
williamr@2: 
williamr@2:   // Construct a rope from a function that can compute its members
williamr@2:   rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
williamr@2:        const allocator_type& __a = allocator_type())
williamr@4:     : _M_tree_ptr(__a, (_RopeRep*)0) {
williamr@2:     _M_tree_ptr._M_data = (0 == __len) ?
williamr@2:       0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
williamr@2:   }
williamr@2: 
williamr@2:   rope(const _Self& __x)
williamr@4:     : _M_tree_ptr(__x._M_tree_ptr, __x._M_tree_ptr._M_data) {
williamr@2:     _S_ref(_M_tree_ptr._M_data);
williamr@2:   }
williamr@2: 
williamr@4:   rope(__move_source<_Self> __src)
williamr@4:     : _M_tree_ptr(__src.get()._M_tree_ptr, __src.get()._M_tree_ptr._M_data) {
williamr@4:     __src.get()._M_tree_ptr._M_data = 0;
williamr@4:   }
williamr@4: 
williamr@4:   ~rope() {
williamr@2:     _S_unref(_M_tree_ptr._M_data);
williamr@2:   }
williamr@2: 
williamr@4:   _Self& operator=(const _Self& __x) {
williamr@2:     _STLP_ASSERT(get_allocator() == __x.get_allocator())
williamr@4:     _S_ref(__x._M_tree_ptr._M_data);
williamr@4:     _M_reset(__x._M_tree_ptr._M_data);
williamr@4:     return *this;
williamr@2:   }
williamr@4: 
williamr@4:   void clear() {
williamr@2:     _S_unref(_M_tree_ptr._M_data);
williamr@2:     _M_tree_ptr._M_data = 0;
williamr@2:   }
williamr@4:   void push_back(_CharT __x) {
williamr@4:     _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, &__x, 1));
williamr@2:   }
williamr@2: 
williamr@4:   void pop_back() {
williamr@2:     _RopeRep* __old = _M_tree_ptr._M_data;
williamr@4:     _M_tree_ptr._M_data =
williamr@2:       _S_substring(_M_tree_ptr._M_data, 0, _M_tree_ptr._M_data->_M_size._M_data - 1);
williamr@2:     _S_unref(__old);
williamr@2:   }
williamr@2: 
williamr@4:   _CharT back() const {
williamr@2:     return _S_fetch(_M_tree_ptr._M_data, _M_tree_ptr._M_data->_M_size._M_data - 1);
williamr@2:   }
williamr@2: 
williamr@4:   void push_front(_CharT __x) {
williamr@2:     _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2:     _RopeRep* __left =
williamr@4:       _S_RopeLeaf_from_unowned_char_ptr(&__x, 1, _M_tree_ptr);
williamr@2:     _STLP_TRY {
williamr@2:       _M_tree_ptr._M_data = _S_concat_rep(__left, _M_tree_ptr._M_data);
williamr@2:       _S_unref(__old);
williamr@2:       _S_unref(__left);
williamr@2:     }
williamr@2:     _STLP_UNWIND(_S_unref(__left))
williamr@4:   }
williamr@2: 
williamr@4:   void pop_front() {
williamr@2:     _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2:     _M_tree_ptr._M_data = _S_substring(_M_tree_ptr._M_data, 1, _M_tree_ptr._M_data->_M_size._M_data);
williamr@2:     _S_unref(__old);
williamr@2:   }
williamr@2: 
williamr@4:   _CharT front() const {
williamr@2:     return _S_fetch(_M_tree_ptr._M_data, 0);
williamr@2:   }
williamr@2: 
williamr@4:   void balance() {
williamr@2:     _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2:     _M_tree_ptr._M_data = _S_balance(_M_tree_ptr._M_data);
williamr@2:     _S_unref(__old);
williamr@2:   }
williamr@2: 
williamr@2:   void copy(_CharT* __buffer) const {
williamr@4:     _STLP_STD::_Destroy_Range(__buffer, __buffer + size());
williamr@2:     _S_flatten(_M_tree_ptr._M_data, __buffer);
williamr@2:   }
williamr@2: 
williamr@4:   /*
williamr@4:    * This is the copy function from the standard, but
williamr@4:    * with the arguments reordered to make it consistent with the
williamr@4:    * rest of the interface.
williamr@4:    * Note that this guaranteed not to compile if the draft standard
williamr@4:    * order is assumed.
williamr@4:    */
williamr@4:   size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const {
williamr@2:     size_t _p_size = size();
williamr@2:     size_t __len = (__pos + __n > _p_size? _p_size - __pos : __n);
williamr@2: 
williamr@4:     _STLP_STD::_Destroy_Range(__buffer, __buffer + __len);
williamr@2:     _S_flatten(_M_tree_ptr._M_data, __pos, __len, __buffer);
williamr@2:     return __len;
williamr@2:   }
williamr@2: 
williamr@4: # ifdef _STLP_DEBUG
williamr@2:   // Print to stdout, exposing structure.  May be useful for
williamr@2:   // performance debugging.
williamr@2:   void dump() {
williamr@2:     _S_dump(_M_tree_ptr._M_data);
williamr@2:   }
williamr@4: # endif
williamr@2: 
williamr@2:   // Convert to 0 terminated string in new allocated memory.
williamr@2:   // Embedded 0s in the input do not terminate the copy.
williamr@2:   const _CharT* c_str() const;
williamr@2: 
williamr@4:   // As above, but also use the flattened representation as the
williamr@2:   // the new rope representation.
williamr@2:   const _CharT* replace_with_c_str();
williamr@2: 
williamr@2:   // Reclaim memory for the c_str generated flattened string.
williamr@2:   // Intentionally undocumented, since it's hard to say when this
williamr@2:   // is safe for multiple threads.
williamr@2:   void delete_c_str () {
williamr@2:     if (0 == _M_tree_ptr._M_data) return;
williamr@4:     if (_RopeRep::_S_leaf == _M_tree_ptr._M_data->_M_tag &&
williamr@4:         ((_RopeLeaf*)_M_tree_ptr._M_data)->_M_data ==
williamr@2:         _M_tree_ptr._M_data->_M_c_string) {
williamr@2:       // Representation shared
williamr@2:       return;
williamr@2:     }
williamr@2:     _M_tree_ptr._M_data->_M_free_c_string();
williamr@2:     _M_tree_ptr._M_data->_M_c_string = 0;
williamr@2:   }
williamr@2: 
williamr@2:   _CharT operator[] (size_type __pos) const {
williamr@2:     return _S_fetch(_M_tree_ptr._M_data, __pos);
williamr@2:   }
williamr@2: 
williamr@2:   _CharT at(size_type __pos) const {
williamr@4:     if (__pos >= size()) _M_throw_out_of_range();
williamr@2:     return (*this)[__pos];
williamr@2:   }
williamr@2: 
williamr@2:   const_iterator begin() const {
williamr@2:     return(const_iterator(_M_tree_ptr._M_data, 0));
williamr@2:   }
williamr@2: 
williamr@2:   // An easy way to get a const iterator from a non-const container.
williamr@2:   const_iterator const_begin() const {
williamr@2:     return(const_iterator(_M_tree_ptr._M_data, 0));
williamr@2:   }
williamr@2: 
williamr@2:   const_iterator end() const {
williamr@2:     return(const_iterator(_M_tree_ptr._M_data, size()));
williamr@2:   }
williamr@2: 
williamr@2:   const_iterator const_end() const {
williamr@2:     return(const_iterator(_M_tree_ptr._M_data, size()));
williamr@2:   }
williamr@2: 
williamr@4:   size_type size() const {
williamr@2:     return(0 == _M_tree_ptr._M_data? 0 : _M_tree_ptr._M_data->_M_size._M_data);
williamr@2:   }
williamr@2: 
williamr@2:   size_type length() const {
williamr@2:     return size();
williamr@2:   }
williamr@2: 
williamr@2:   size_type max_size() const {
williamr@2:     return _S_min_len[__ROPE_MAX_DEPTH-1] - 1;
williamr@4:     //  Guarantees that the result can be sufficiently
williamr@2:     //  balanced.  Longer ropes will probably still work,
williamr@2:     //  but it's harder to make guarantees.
williamr@2:   }
williamr@2: 
williamr@2:   const_reverse_iterator rbegin() const {
williamr@2:     return const_reverse_iterator(end());
williamr@2:   }
williamr@2: 
williamr@2:   const_reverse_iterator const_rbegin() const {
williamr@2:     return const_reverse_iterator(end());
williamr@2:   }
williamr@2: 
williamr@2:   const_reverse_iterator rend() const {
williamr@2:     return const_reverse_iterator(begin());
williamr@2:   }
williamr@2: 
williamr@2:   const_reverse_iterator const_rend() const {
williamr@2:     return const_reverse_iterator(begin());
williamr@2:   }
williamr@2:   // The symmetric cases are intentionally omitted, since they're presumed
williamr@2:   // to be less common, and we don't handle them as well.
williamr@2: 
williamr@2:   // The following should really be templatized.
williamr@2:   // The first argument should be an input iterator or
williamr@2:   // forward iterator with value_type _CharT.
williamr@2:   _Self& append(const _CharT* __iter, size_t __n) {
williamr@4:     _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __iter, __n));
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append(const _CharT* __c_string) {
williamr@2:     size_t __len = _S_char_ptr_len(__c_string);
williamr@2:     append(__c_string, __len);
williamr@4:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append(const _CharT* __s, const _CharT* __e) {
williamr@4:     _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __s, __e - __s));
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append(const_iterator __s, const_iterator __e) {
williamr@2:     _STLP_ASSERT(__s._M_root == __e._M_root)
williamr@2:     _STLP_ASSERT(get_allocator() == __s._M_root->get_allocator())
williamr@4:     _Self_destruct_ptr __appendee(_S_substring(__s._M_root, __s._M_current_pos, __e._M_current_pos));
williamr@4:     _M_reset(_S_concat_rep(_M_tree_ptr._M_data, (_RopeRep*)__appendee));
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append(_CharT __c) {
williamr@4:     _M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, &__c, 1));
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append() { return append(_CharT()); }  // XXX why?
williamr@2: 
williamr@2:   _Self& append(const _Self& __y) {
williamr@2:     _STLP_ASSERT(__y.get_allocator() == get_allocator())
williamr@4:     _M_reset(_S_concat_rep(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data));
williamr@2:     return *this;
williamr@2:   }
williamr@2: 
williamr@2:   _Self& append(size_t __n, _CharT __c) {
williamr@2:     rope<_CharT,_Alloc> __last(__n, __c);
williamr@2:     return append(__last);
williamr@2:   }
williamr@2: 
williamr@2:   void swap(_Self& __b) {
williamr@4:     _M_tree_ptr.swap(__b._M_tree_ptr);
williamr@2:   }
williamr@2: 
williamr@2: protected:
williamr@2:   // Result is included in refcount.
williamr@2:   static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
williamr@2:                            size_t __pos2, _RopeRep* __r) {
williamr@2:     if (0 == __old) { _S_ref(__r); return __r; }
williamr@4:     _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
williamr@4:     _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size._M_data));
williamr@4:     _STLP_MPWFIX_TRY  //*TY 06/01/2000 -
williamr@2:     _RopeRep* __result;
williamr@2: 
williamr@2:     if (0 == __r) {
williamr@2:       __result = _S_concat_rep(__left, __right);
williamr@2:     } else {
williamr@2:       _STLP_ASSERT(__old->get_allocator() == __r->get_allocator())
williamr@2:       _Self_destruct_ptr __left_result(_S_concat_rep(__left, __r));
williamr@2:       __result = _S_concat_rep(__left_result, __right);
williamr@2:     }
williamr@2:     return __result;
williamr@4:     _STLP_MPWFIX_CATCH  //*TY 06/01/2000 -
williamr@2:   }
williamr@2: 
williamr@2: public:
williamr@2:   void insert(size_t __p, const _Self& __r) {
williamr@4:     if (__p > size()) _M_throw_out_of_range();
williamr@2:     _STLP_ASSERT(get_allocator() == __r.get_allocator())
williamr@4:     _M_reset(replace(_M_tree_ptr._M_data, __p, __p, __r._M_tree_ptr._M_data));
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, size_t __n, _CharT __c) {
williamr@2:     rope<_CharT,_Alloc> __r(__n,__c);
williamr@2:     insert(__p, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, const _CharT* __i, size_t __n) {
williamr@4:     if (__p > size()) _M_throw_out_of_range();
williamr@2:     _Self_destruct_ptr __left(_S_substring(_M_tree_ptr._M_data, 0, __p));
williamr@2:     _Self_destruct_ptr __right(_S_substring(_M_tree_ptr._M_data, __p, size()));
williamr@2:     _Self_destruct_ptr __left_result(
williamr@2:                                      _S_concat_char_iter(__left, __i, __n));
williamr@2:     // _S_ destr_concat_char_iter should be safe here.
williamr@2:     // But as it stands it's probably not a win, since __left
williamr@2:     // is likely to have additional references.
williamr@4:     _M_reset(_S_concat_rep(__left_result, __right));
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, const _CharT* __c_string) {
williamr@2:     insert(__p, __c_string, _S_char_ptr_len(__c_string));
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, _CharT __c) {
williamr@2:     insert(__p, &__c, 1);
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p) {
williamr@2:     _CharT __c = _CharT();
williamr@2:     insert(__p, &__c, 1);
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     insert(__p, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, const const_iterator& __i,
williamr@4:                           const const_iterator& __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     insert(__p, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void insert(size_t __p, const iterator& __i,
williamr@4:                           const iterator& __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     insert(__p, __r);
williamr@2:   }
williamr@2: 
williamr@2:   // (position, length) versions of replace operations:
williamr@2:   void replace(size_t __p, size_t __n, const _Self& __r) {
williamr@4:     if (__p > size()) _M_throw_out_of_range();
williamr@4:     _M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, __r._M_tree_ptr._M_data));
williamr@2:   }
williamr@2: 
williamr@4:   void replace(size_t __p, size_t __n,
williamr@2:                const _CharT* __i, size_t __i_len) {
williamr@2:     _Self __r(__i, __i_len);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, size_t __n, _CharT __c) {
williamr@2:     _Self __r(__c);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, size_t __n, const _CharT* __c_string) {
williamr@2:     _Self __r(__c_string);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@4:   void replace(size_t __p, size_t __n,
williamr@2:                const _CharT* __i, const _CharT* __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, size_t __n,
williamr@2:                const const_iterator& __i, const const_iterator& __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, size_t __n,
williamr@2:                const iterator& __i, const iterator& __j) {
williamr@2:     _Self __r(__i, __j);
williamr@2:     replace(__p, __n, __r);
williamr@2:   }
williamr@2: 
williamr@2:   // Single character variants:
williamr@2:   void replace(size_t __p, _CharT __c) {
williamr@4:     if (__p > size()) _M_throw_out_of_range();
williamr@2:     iterator __i(this, __p);
williamr@2:     *__i = __c;
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const _Self& __r) {
williamr@2:     replace(__p, 1, __r);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const _CharT* __i, size_t __i_len) {
williamr@2:     replace(__p, 1, __i, __i_len);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const _CharT* __c_string) {
williamr@2:     replace(__p, 1, __c_string);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
williamr@2:     replace(__p, 1, __i, __j);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const const_iterator& __i,
williamr@4:                            const const_iterator& __j) {
williamr@2:     replace(__p, 1, __i, __j);
williamr@2:   }
williamr@2: 
williamr@2:   void replace(size_t __p, const iterator& __i,
williamr@4:                            const iterator& __j) {
williamr@2:     replace(__p, 1, __i, __j);
williamr@2:   }
williamr@2: 
williamr@2:   // Erase, (position, size) variant.
williamr@2:   void erase(size_t __p, size_t __n) {
williamr@4:     if (__p > size()) _M_throw_out_of_range();
williamr@4:     _M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, 0));
williamr@2:   }
williamr@2: 
williamr@2:   // Erase, single character
williamr@2:   void erase(size_t __p) {
williamr@2:     erase(__p, __p + 1);
williamr@2:   }
williamr@2: 
williamr@4:   // Insert, iterator variants.
williamr@2:   iterator insert(const iterator& __p, const _Self& __r)
williamr@2:   { insert(__p.index(), __r); return __p; }
williamr@2:   iterator insert(const iterator& __p, size_t __n, _CharT __c)
williamr@2:   { insert(__p.index(), __n, __c); return __p; }
williamr@4:   iterator insert(const iterator& __p, _CharT __c)
williamr@2:   { insert(__p.index(), __c); return __p; }
williamr@4:   iterator insert(const iterator& __p )
williamr@2:   { insert(__p.index()); return __p; }
williamr@4:   iterator insert(const iterator& __p, const _CharT* c_string)
williamr@2:   { insert(__p.index(), c_string); return __p; }
williamr@2:   iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
williamr@2:   { insert(__p.index(), __i, __n); return __p; }
williamr@4:   iterator insert(const iterator& __p, const _CharT* __i,
williamr@2:                   const _CharT* __j)
williamr@2:   { insert(__p.index(), __i, __j);  return __p; }
williamr@2:   iterator insert(const iterator& __p,
williamr@2:                   const const_iterator& __i, const const_iterator& __j)
williamr@2:   { insert(__p.index(), __i, __j); return __p; }
williamr@2:   iterator insert(const iterator& __p,
williamr@2:                   const iterator& __i, const iterator& __j)
williamr@2:   { insert(__p.index(), __i, __j); return __p; }
williamr@2: 
williamr@2:   // Replace, range variants.
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const _Self& __r)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __r); }
williamr@2:   void replace(const iterator& __p, const iterator& __q, _CharT __c)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __c); }
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const _CharT* __c_string)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __c_string); }
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const _CharT* __i, size_t __n)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const _CharT* __i, const _CharT* __j)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const const_iterator& __i, const const_iterator& __j)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2:   void replace(const iterator& __p, const iterator& __q,
williamr@2:                const iterator& __i, const iterator& __j)
williamr@2:   { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2: 
williamr@2:   // Replace, iterator variants.
williamr@2:   void replace(const iterator& __p, const _Self& __r)
williamr@2:   { replace(__p.index(), __r); }
williamr@2:   void replace(const iterator& __p, _CharT __c)
williamr@2:   { replace(__p.index(), __c); }
williamr@2:   void replace(const iterator& __p, const _CharT* __c_string)
williamr@2:   { replace(__p.index(), __c_string); }
williamr@2:   void replace(const iterator& __p, const _CharT* __i, size_t __n)
williamr@2:   { replace(__p.index(), __i, __n); }
williamr@2:   void replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
williamr@2:   { replace(__p.index(), __i, __j); }
williamr@4:   void replace(const iterator& __p, const_iterator __i,
williamr@2:                const_iterator __j)
williamr@2:   { replace(__p.index(), __i, __j); }
williamr@2:   void replace(const iterator& __p, iterator __i, iterator __j)
williamr@2:   { replace(__p.index(), __i, __j); }
williamr@2: 
williamr@2:   // Iterator and range variants of erase
williamr@2:   iterator erase(const iterator& __p, const iterator& __q) {
williamr@2:     size_t __p_index = __p.index();
williamr@2:     erase(__p_index, __q.index() - __p_index);
williamr@2:     return iterator(this, __p_index);
williamr@2:   }
williamr@2:   iterator erase(const iterator& __p) {
williamr@2:     size_t __p_index = __p.index();
williamr@2:     erase(__p_index, 1);
williamr@2:     return iterator(this, __p_index);
williamr@2:   }
williamr@2: 
williamr@2:   _Self substr(size_t __start, size_t __len = 1) const {
williamr@4:     if (__start > size()) _M_throw_out_of_range();
williamr@4:     return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start, __start + __len));
williamr@2:   }
williamr@2: 
williamr@2:   _Self substr(iterator __start, iterator __end) const {
williamr@4:     return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
williamr@2:   }
williamr@4: 
williamr@2:   _Self substr(iterator __start) const {
williamr@2:     size_t __pos = __start.index();
williamr@4:     return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
williamr@2:   }
williamr@4: 
williamr@2:   _Self substr(const_iterator __start, const_iterator __end) const {
williamr@2:     // This might eventually take advantage of the cache in the
williamr@2:     // iterator.
williamr@4:     return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
williamr@2:   }
williamr@2: 
williamr@2:   rope<_CharT,_Alloc> substr(const_iterator __start) {
williamr@2:     size_t __pos = __start.index();
williamr@4:     return rope<_CharT,_Alloc>(_S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
williamr@2:   }
williamr@2: 
williamr@4: #include <stl/_string_npos.h>
williamr@2: 
williamr@4:   size_type find(const _Self& __s, size_type __pos = 0) const {
williamr@4:     if (__pos >= size())
williamr@4: # ifndef _STLP_OLD_ROPE_SEMANTICS
williamr@4:       return npos;
williamr@4: # else
williamr@4:       return size();
williamr@4: # endif
williamr@2: 
williamr@4:     size_type __result_pos;
williamr@4:     const_iterator __result = search(const_begin() + (ptrdiff_t)__pos, const_end(), __s.begin(), __s.end() );
williamr@4:     __result_pos = __result.index();
williamr@4: # ifndef _STLP_OLD_ROPE_SEMANTICS
williamr@4:     if (__result_pos == size()) __result_pos = npos;
williamr@4: # endif
williamr@4:     return __result_pos;
williamr@4:   }
williamr@2:   size_type find(_CharT __c, size_type __pos = 0) const;
williamr@2:   size_type find(const _CharT* __s, size_type __pos = 0) const {
williamr@2:     size_type __result_pos;
williamr@2:     const_iterator __result = search(const_begin() + (ptrdiff_t)__pos, const_end(),
williamr@2:                                      __s, __s + _S_char_ptr_len(__s));
williamr@2:     __result_pos = __result.index();
williamr@4: # ifndef _STLP_OLD_ROPE_SEMANTICS
williamr@2:     if (__result_pos == size()) __result_pos = npos;
williamr@4: # endif
williamr@2:     return __result_pos;
williamr@2:   }
williamr@2: 
williamr@2:   iterator mutable_begin() {
williamr@2:     return(iterator(this, 0));
williamr@2:   }
williamr@2: 
williamr@2:   iterator mutable_end() {
williamr@2:     return(iterator(this, size()));
williamr@2:   }
williamr@2: 
williamr@2:   reverse_iterator mutable_rbegin() {
williamr@2:     return reverse_iterator(mutable_end());
williamr@2:   }
williamr@2: 
williamr@2:   reverse_iterator mutable_rend() {
williamr@2:     return reverse_iterator(mutable_begin());
williamr@2:   }
williamr@2: 
williamr@2:   reference mutable_reference_at(size_type __pos) {
williamr@2:     return reference(this, __pos);
williamr@2:   }
williamr@2: 
williamr@4: # ifdef __STD_STUFF
williamr@2:   reference operator[] (size_type __pos) {
williamr@2:     return reference(this, __pos);
williamr@2:   }
williamr@2: 
williamr@2:   reference at(size_type __pos) {
williamr@4:     if (__pos >= size()) _M_throw_out_of_range();
williamr@2:     return (*this)[__pos];
williamr@2:   }
williamr@2: 
williamr@2:   void resize(size_type, _CharT) {}
williamr@2:   void resize(size_type) {}
williamr@2:   void reserve(size_type = 0) {}
williamr@2:   size_type capacity() const {
williamr@2:     return max_size();
williamr@2:   }
williamr@2: 
williamr@2:   // Stuff below this line is dangerous because it's error prone.
williamr@2:   // I would really like to get rid of it.
williamr@2:   // copy function with funny arg ordering.
williamr@4:   size_type copy(_CharT* __buffer, size_type __n,
williamr@2:                  size_type __pos = 0) const {
williamr@2:     return copy(__pos, __n, __buffer);
williamr@2:   }
williamr@2: 
williamr@2:   iterator end() { return mutable_end(); }
williamr@2: 
williamr@2:   iterator begin() { return mutable_begin(); }
williamr@2: 
williamr@2:   reverse_iterator rend() { return mutable_rend(); }
williamr@2: 
williamr@2:   reverse_iterator rbegin() { return mutable_rbegin(); }
williamr@2: 
williamr@4: # else
williamr@2: 
williamr@2:   const_iterator end() { return const_end(); }
williamr@2: 
williamr@2:   const_iterator begin() { return const_begin(); }
williamr@2: 
williamr@2:   const_reverse_iterator rend() { return const_rend(); }
williamr@4: 
williamr@2:   const_reverse_iterator rbegin() { return const_rbegin(); }
williamr@2: 
williamr@4: # endif
williamr@4: }; //class rope
williamr@2: 
williamr@4: #if !defined (_STLP_STATIC_CONST_INIT_BUG)
williamr@4: #  if defined (__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 96)
williamr@4: template <class _CharT, class _Alloc>
williamr@4: const size_t rope<_CharT, _Alloc>::npos = ~(size_t) 0;
williamr@4: #  endif
williamr@4: #endif
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline _CharT
williamr@2: _Rope_const_iterator< _CharT, _Alloc>::operator[](size_t __n)
williamr@4: { return rope<_CharT,_Alloc>::_S_fetch(this->_M_root, this->_M_current_pos + __n); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator== (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2:                         const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@4:   return (__x._M_current_pos == __y._M_current_pos &&
williamr@2:           __x._M_root == __y._M_root);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator< (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                        const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return (__x._M_current_pos < __y._M_current_pos); }
williamr@2: 
williamr@2: #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator!= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__x == __y); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator> (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                        const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return __y < __x; }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator<= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__y < __x); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator>= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__x < __y); }
williamr@2: 
williamr@2: #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline ptrdiff_t operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@4:                            const _Rope_const_iterator<_CharT,_Alloc>& __y)
williamr@4: { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
williamr@2: 
williamr@4: #if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000  // dwa 8/21/97  - "ambiguous access to overloaded function" bug.
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_const_iterator<_CharT,_Alloc>
williamr@4: operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n)
williamr@4: { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos - __n); }
williamr@2: # endif
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_const_iterator<_CharT,_Alloc>
williamr@4: operator+(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n)
williamr@4: { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos + __n); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_const_iterator<_CharT,_Alloc>
williamr@4: operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT,_Alloc>& __x)
williamr@4: { return _Rope_const_iterator<_CharT,_Alloc>(__x._M_root, __x._M_current_pos + __n); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator== (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2:                         const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@4:   return (__x._M_current_pos == __y._M_current_pos &&
williamr@2:           __x._M_root_rope == __y._M_root_rope);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator< (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                        const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return (__x._M_current_pos < __y._M_current_pos); }
williamr@2: 
williamr@4: #if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator!= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__x == __y); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator> (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                        const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return __y < __x; }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator<= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__y < __x); }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator>= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                         const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return !(__x < __y); }
williamr@2: #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline ptrdiff_t operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@4:                            const _Rope_iterator<_CharT,_Alloc>& __y)
williamr@4: { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
williamr@2: 
williamr@4: #if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000  // dwa 8/21/97  - "ambiguous access to overloaded function" bug.
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_iterator<_CharT,_Alloc>
williamr@2: operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2:           ptrdiff_t __n) {
williamr@4:   return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos - __n);
williamr@2: }
williamr@2: # endif
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_iterator<_CharT,_Alloc>
williamr@2: operator+(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2:           ptrdiff_t __n) {
williamr@4:   return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos + __n);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline _Rope_iterator<_CharT,_Alloc>
williamr@2: operator+(ptrdiff_t __n, const _Rope_iterator<_CharT,_Alloc>& __x) {
williamr@4:   return _Rope_iterator<_CharT,_Alloc>(__x._M_root_rope, __x._M_current_pos + __n);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>
williamr@2: operator+ (const rope<_CharT,_Alloc>& __left,
williamr@4:            const rope<_CharT,_Alloc>& __right) {
williamr@2:   _STLP_ASSERT(__left.get_allocator() == __right.get_allocator())
williamr@2:   return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_rep(__left._M_tree_ptr._M_data, __right._M_tree_ptr._M_data));
williamr@4:   // Inlining this should make it possible to keep __left and __right in registers.
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>&
williamr@4: operator+= (rope<_CharT,_Alloc>& __left,
williamr@4:             const rope<_CharT,_Alloc>& __right) {
williamr@2:   __left.append(__right);
williamr@2:   return __left;
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>
williamr@2: operator+ (const rope<_CharT,_Alloc>& __left,
williamr@2:            const _CharT* __right) {
williamr@2:   size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
williamr@4:   return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_char_iter(__left._M_tree_ptr._M_data, __right, __rlen));
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>&
williamr@2: operator+= (rope<_CharT,_Alloc>& __left,
williamr@2:             const _CharT* __right) {
williamr@2:   __left.append(__right);
williamr@2:   return __left;
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>
williamr@4: operator+ (const rope<_CharT,_Alloc>& __left, _CharT __right) {
williamr@4:   return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_char_iter(__left._M_tree_ptr._M_data, &__right, 1));
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline rope<_CharT,_Alloc>&
williamr@4: operator+= (rope<_CharT,_Alloc>& __left, _CharT __right) {
williamr@2:   __left.append(__right);
williamr@2:   return __left;
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@4: operator< (const rope<_CharT,_Alloc>& __left,
williamr@2:            const rope<_CharT,_Alloc>& __right) {
williamr@2:   return __left.compare(__right) < 0;
williamr@2: }
williamr@4: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@4: operator== (const rope<_CharT,_Alloc>& __left,
williamr@2:             const rope<_CharT,_Alloc>& __right) {
williamr@2:   return __left.compare(__right) == 0;
williamr@2: }
williamr@2: 
williamr@2: #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@2: operator!= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2:   return !(__x == __y);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@2: operator> (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2:   return __y < __x;
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@2: operator<= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2:   return !(__y < __x);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool
williamr@2: operator>= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2:   return !(__x < __y);
williamr@2: }
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator!= (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
williamr@2:                         const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
williamr@2:   return !(__x == __y);
williamr@2: }
williamr@2: 
williamr@2: #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@2: inline bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
williamr@2:                         const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
williamr@2:   return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root);
williamr@2: }
williamr@2: 
williamr@4: #if !defined (_STLP_USE_NO_IOSTREAMS)
williamr@2: template<class _CharT, class _Traits, class _Alloc>
williamr@4: basic_ostream<_CharT, _Traits>& operator<< (basic_ostream<_CharT, _Traits>& __o,
williamr@2:                                             const rope<_CharT, _Alloc>& __r);
williamr@2: #endif
williamr@4: 
williamr@2: typedef rope<char, _STLP_DEFAULT_ALLOCATOR(char) > crope;
williamr@4: #if defined (_STLP_HAS_WCHAR_T)
williamr@2: typedef rope<wchar_t, _STLP_DEFAULT_ALLOCATOR(wchar_t) > wrope;
williamr@4: #endif
williamr@2: 
williamr@2: inline crope::reference __mutable_reference_at(crope& __c, size_t __i)
williamr@4: { return __c.mutable_reference_at(__i); }
williamr@2: 
williamr@4: #if defined (_STLP_HAS_WCHAR_T)
williamr@2: inline wrope::reference __mutable_reference_at(wrope& __c, size_t __i)
williamr@4: { return __c.mutable_reference_at(__i); }
williamr@4: #endif
williamr@2: 
williamr@4: #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline void swap(rope<_CharT,_Alloc>& __x, rope<_CharT,_Alloc>& __y)
williamr@4: { __x.swap(__y); }
williamr@2: #else
williamr@2: 
williamr@2: inline void swap(crope& __x, crope& __y) { __x.swap(__y); }
williamr@4: # ifdef _STLP_HAS_WCHAR_T  // dwa 8/21/97
williamr@2: inline void swap(wrope& __x, wrope& __y) { __x.swap(__y); }
williamr@2: # endif
williamr@2: 
williamr@2: #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
williamr@2: 
williamr@2: 
williamr@2: // Hash functions should probably be revisited later:
williamr@4: _STLP_TEMPLATE_NULL struct hash<crope> {
williamr@4:   size_t operator()(const crope& __str) const {
williamr@2:     size_t _p_size = __str.size();
williamr@2: 
williamr@2:     if (0 == _p_size) return 0;
williamr@2:     return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
williamr@2:   }
williamr@2: };
williamr@2: 
williamr@4: #if defined (_STLP_HAS_WCHAR_T)  // dwa 8/21/97
williamr@4: _STLP_TEMPLATE_NULL struct hash<wrope> {
williamr@4:   size_t operator()(const wrope& __str) const {
williamr@2:     size_t _p_size = __str.size();
williamr@2: 
williamr@2:     if (0 == _p_size) return 0;
williamr@2:     return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
williamr@2:   }
williamr@2: };
williamr@2: #endif
williamr@2: 
williamr@4: #if (!defined (_STLP_MSVC) || (_STLP_MSVC >= 1310))
williamr@2: // I couldn't get this to work with VC++
williamr@2: template<class _CharT,class _Alloc>
williamr@4: #  if defined (__DMC__) && !defined (__PUT_STATIC_DATA_MEMBERS_HERE)
williamr@4: extern
williamr@4: #  endif
williamr@4: void _Rope_rotate(_Rope_iterator<_CharT, _Alloc> __first,
williamr@4:                   _Rope_iterator<_CharT, _Alloc> __middle,
williamr@4:                   _Rope_iterator<_CharT, _Alloc> __last);
williamr@2: 
williamr@4: inline void rotate(_Rope_iterator<char, _STLP_DEFAULT_ALLOCATOR(char) > __first,
williamr@4:                    _Rope_iterator<char, _STLP_DEFAULT_ALLOCATOR(char) > __middle,
williamr@4:                    _Rope_iterator<char, _STLP_DEFAULT_ALLOCATOR(char) > __last)
williamr@4: { _Rope_rotate(__first, __middle, __last); }
williamr@2: #endif
williamr@2: 
williamr@2: template <class _CharT, class _Alloc>
williamr@4: inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const {
williamr@2:   if (_M_current_valid) {
williamr@4:     return _M_current;
williamr@2:   } else {
williamr@2:     return _My_rope::_S_fetch(_M_root->_M_tree_ptr._M_data, _M_pos);
williamr@2:   }
williamr@2: }
williamr@4: 
williamr@4: #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
williamr@4: template <class _CharT, class _Alloc>
williamr@4: struct __move_traits<rope<_CharT, _Alloc> > {
williamr@4:   typedef __stlp_movable implemented;
williamr@4:   //Completness depends on the allocator:
williamr@4:   typedef typename __move_traits<_Alloc>::complete complete;
williamr@4: };
williamr@4: #endif
williamr@4: 
williamr@2: _STLP_END_NAMESPACE
williamr@2: 
williamr@4: #if !defined (_STLP_LINK_TIME_INSTANTIATION)
williamr@2: #  include <stl/_rope.c>
williamr@4: #endif
williamr@2: 
williamr@4: #endif /* _STLP_INTERNAL_ROPE_H */
williamr@2: 
williamr@2: // Local Variables:
williamr@2: // mode:C++
williamr@2: // End: