stl_rope.h

00001 /*
00002  * Copyright (c) 1997-1998
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Permission to use, copy, modify, distribute and sell this software
00006  * and its documentation for any purpose is hereby granted without fee,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.  Silicon Graphics makes no
00010  * representations about the suitability of this software for any
00011  * purpose.  It is provided "as is" without express or implied warranty.
00012  */
00013 
00014 /* NOTE: This is an internal header file, included by other STL headers.
00015  *   You should not attempt to use it directly.
00016  */
00017 
00018 // rope<_CharT,_Alloc> is a sequence of _CharT.
00019 // Ropes appear to be mutable, but update operations
00020 // really copy enough of the data structure to leave the original
00021 // valid.  Thus ropes can be logically copied by just copying
00022 // a pointer value.
00023 
00024 #ifndef __SGI_STL_INTERNAL_ROPE_H
00025 # define __SGI_STL_INTERNAL_ROPE_H
00026 
00027 # ifdef __GC
00028 #   define __GC_CONST const
00029 # else
00030 #   include <stl_threads.h>
00031 #   define __GC_CONST   // constant except for deallocation
00032 # endif
00033 #ifndef UNDER_CE
00034 # ifdef __STL_SGI_THREADS
00035 #    include <mutex.h>
00036 # endif
00037 #endif //UNDER_CE
00038 
00039 __STL_BEGIN_NAMESPACE
00040 
00041 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00042 #pragma set woff 1174
00043 #endif
00044 
00045 // The _S_eos function is used for those functions that
00046 // convert to/from C-like strings to detect the end of the string.
00047 
00048 // The end-of-C-string character.
00049 // This is what the draft standard says it should be.
00050 template <class _CharT>
00051 inline _CharT _S_eos(_CharT*) { return _CharT(); }
00052 
00053 // Test for basic character types.
00054 // For basic character types leaves having a trailing eos.
00055 template <class _CharT>
00056 inline bool _S_is_basic_char_type(_CharT*) { return false; }
00057 template <class _CharT>
00058 inline bool _S_is_one_byte_char_type(_CharT*) { return false; }
00059 
00060 inline bool _S_is_basic_char_type(char*) { return true; }
00061 inline bool _S_is_one_byte_char_type(char*) { return true; }
00062 inline bool _S_is_basic_char_type(wchar_t*) { return true; }
00063 
00064 // Store an eos iff _CharT is a basic character type.
00065 // Do not reference _S_eos if it isn't.
00066 template <class _CharT>
00067 inline void _S_cond_store_eos(_CharT&) {}
00068 
00069 inline void _S_cond_store_eos(char& __c) { __c = 0; }
00070 inline void _S_cond_store_eos(wchar_t& __c) { __c = 0; }
00071 
00072 // char_producers are logically functions that generate a section of
00073 // a string.  These can be convereted to ropes.  The resulting rope
00074 // invokes the char_producer on demand.  This allows, for example,
00075 // files to be viewed as ropes without reading the entire file.
00076 template <class _CharT>
00077 class char_producer {
00078     public:
00079         virtual ~char_producer() {};
00080         virtual void operator()(size_t __start_pos, size_t __len, 
00081                                 _CharT* __buffer) = 0;
00082         // Buffer should really be an arbitrary output iterator.
00083         // That way we could flatten directly into an ostream, etc.
00084         // This is thoroughly impossible, since iterator types don't
00085         // have runtime descriptions.
00086 };
00087 
00088 // Sequence buffers:
00089 //
00090 // Sequence must provide an append operation that appends an
00091 // array to the sequence.  Sequence buffers are useful only if
00092 // appending an entire array is cheaper than appending element by element.
00093 // This is true for many string representations.
00094 // This should  perhaps inherit from ostream<sequence::value_type>
00095 // and be implemented correspondingly, so that they can be used
00096 // for formatted.  For the sake of portability, we don't do this yet.
00097 //
00098 // For now, sequence buffers behave as output iterators.  But they also
00099 // behave a little like basic_ostringstream<sequence::value_type> and a
00100 // little like containers.
00101 
00102 template<class _Sequence, size_t _Buf_sz = 100
00103 #   if defined(__sgi) && !defined(__GNUC__)
00104 #        define __TYPEDEF_WORKAROUND
00105          ,class _V = typename _Sequence::value_type
00106 #   endif
00107         >
00108 // The 3rd parameter works around a common compiler bug.
00109 class sequence_buffer : public output_iterator {
00110     public:
00111 #       ifndef __TYPEDEF_WORKAROUND
00112             typedef typename _Sequence::value_type value_type;
00113 #       else
00114             typedef _V value_type;
00115 #       endif
00116     protected:
00117         _Sequence* _M_prefix;
00118         value_type _M_buffer[_Buf_sz];
00119         size_t     _M_buf_count;
00120     public:
00121         void flush() {
00122             _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
00123             _M_buf_count = 0;
00124         }
00125         ~sequence_buffer() { flush(); }
00126         sequence_buffer() : _M_prefix(0), _M_buf_count(0) {}
00127         sequence_buffer(const sequence_buffer& __x) {
00128             _M_prefix = __x._M_prefix;
00129             _M_buf_count = __x._M_buf_count;
00130             copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
00131         }
00132         sequence_buffer(sequence_buffer& __x) {
00133             __x.flush();
00134             _M_prefix = __x._M_prefix;
00135             _M_buf_count = 0;
00136         }
00137         sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) {}
00138         sequence_buffer& operator= (sequence_buffer& __x) {
00139             __x.flush();
00140             _M_prefix = __x._M_prefix;
00141             _M_buf_count = 0;
00142             return *this;
00143         }
00144         sequence_buffer& operator= (const sequence_buffer& __x) {
00145             _M_prefix = __x._M_prefix;
00146             _M_buf_count = __x._M_buf_count;
00147             copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
00148             return *this;
00149         }
00150         void push_back(value_type __x)
00151         {
00152             if (_M_buf_count < _Buf_sz) {
00153                 _M_buffer[_M_buf_count] = __x;
00154                 ++_M_buf_count;
00155             } else {
00156                 flush();
00157                 _M_buffer[0] = __x;
00158                 _M_buf_count = 1;
00159             }
00160         }
00161         void append(value_type* __s, size_t __len)
00162         {
00163             if (__len + _M_buf_count <= _Buf_sz) {
00164                 size_t __i = _M_buf_count;
00165                 size_t __j = 0;
00166                 for (; __j < __len; __i++, __j++) {
00167                     _M_buffer[__i] = __s[__j];
00168                 }
00169                 _M_buf_count += __len;
00170             } else if (0 == _M_buf_count) {
00171                 _M_prefix->append(__s, __s + __len);
00172             } else {
00173                 flush();
00174                 append(__s, __len);
00175             }
00176         }
00177         sequence_buffer& write(value_type* __s, size_t __len)
00178         {
00179             append(__s, __len);
00180             return *this;
00181         }
00182         sequence_buffer& put(value_type __x)
00183         {
00184             push_back(__x);
00185             return *this;
00186         }
00187         sequence_buffer& operator=(const value_type& __rhs)
00188         {
00189             push_back(__rhs);
00190             return *this;
00191         }
00192         sequence_buffer& operator*() { return *this; }
00193         sequence_buffer& operator++() { return *this; }
00194         sequence_buffer& operator++(int) { return *this; }
00195 };
00196 
00197 // The following should be treated as private, at least for now.
00198 template<class _CharT>
00199 class _Rope_char_consumer {
00200     public:
00201         // If we had member templates, these should not be virtual.
00202         // For now we need to use run-time parametrization where
00203         // compile-time would do.  Hence this should all be private
00204         // for now.
00205         // The symmetry with char_producer is accidental and temporary.
00206         virtual ~_Rope_char_consumer() {};
00207         virtual bool operator()(const _CharT* __buffer, size_t __len) = 0;
00208 };
00209 
00210 // First a lot of forward declarations.  The standard seems to require
00211 // much stricter "declaration before use" than many of the implementations
00212 // that preceded it.
00213 template<class _CharT, class _Alloc=__STL_DEFAULT_ALLOCATOR(_CharT)> class rope;
00214 template<class _CharT, class _Alloc> struct _Rope_RopeConcatenation;
00215 template<class _CharT, class _Alloc> struct _Rope_RopeLeaf;
00216 template<class _CharT, class _Alloc> struct _Rope_RopeFunction;
00217 template<class _CharT, class _Alloc> struct _Rope_RopeSubstring;
00218 template<class _CharT, class _Alloc> class _Rope_iterator;
00219 template<class _CharT, class _Alloc> class _Rope_const_iterator;
00220 template<class _CharT, class _Alloc> class _Rope_char_ref_proxy;
00221 template<class _CharT, class _Alloc> class _Rope_char_ptr_proxy;
00222 
00223 template<class _CharT, class _Alloc>
00224 bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
00225                  const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y);
00226 
00227 template<class _CharT, class _Alloc>
00228 _Rope_const_iterator<_CharT,_Alloc> operator-
00229         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
00230          ptrdiff_t __n);
00231 
00232 template<class _CharT, class _Alloc>
00233 _Rope_const_iterator<_CharT,_Alloc> operator+
00234         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
00235          ptrdiff_t __n);
00236 
00237 template<class _CharT, class _Alloc>
00238 _Rope_const_iterator<_CharT,_Alloc> operator+
00239         (ptrdiff_t __n,
00240          const _Rope_const_iterator<_CharT,_Alloc>& __x);
00241 
00242 template<class _CharT, class _Alloc>
00243 bool operator== 
00244         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
00245          const _Rope_const_iterator<_CharT,_Alloc>& __y);
00246 
00247 template<class _CharT, class _Alloc>
00248 bool operator< 
00249         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
00250          const _Rope_const_iterator<_CharT,_Alloc>& __y);
00251 
00252 template<class _CharT, class _Alloc>
00253 ptrdiff_t operator- 
00254         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
00255          const _Rope_const_iterator<_CharT,_Alloc>& __y);
00256 
00257 template<class _CharT, class _Alloc>
00258 _Rope_iterator<_CharT,_Alloc> operator-
00259         (const _Rope_iterator<_CharT,_Alloc>& __x,
00260          ptrdiff_t __n);
00261 
00262 template<class _CharT, class _Alloc>
00263 _Rope_iterator<_CharT,_Alloc> operator+
00264         (const _Rope_iterator<_CharT,_Alloc>& __x,
00265          ptrdiff_t __n);
00266 
00267 template<class _CharT, class _Alloc>
00268 _Rope_iterator<_CharT,_Alloc> operator+
00269         (ptrdiff_t __n,
00270          const _Rope_iterator<_CharT,_Alloc>& __x);
00271 
00272 template<class _CharT, class _Alloc>
00273 bool operator== 
00274         (const _Rope_iterator<_CharT,_Alloc>& __x,
00275          const _Rope_iterator<_CharT,_Alloc>& __y);
00276 
00277 template<class _CharT, class _Alloc>
00278 bool operator< 
00279         (const _Rope_iterator<_CharT,_Alloc>& __x,
00280          const _Rope_iterator<_CharT,_Alloc>& __y);
00281 
00282 template<class _CharT, class _Alloc>
00283 ptrdiff_t operator- 
00284         (const _Rope_iterator<_CharT,_Alloc>& __x,
00285          const _Rope_iterator<_CharT,_Alloc>& __y);
00286 
00287 template<class _CharT, class _Alloc>
00288 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
00289                                const rope<_CharT,_Alloc>& __right);
00290         
00291 template<class _CharT, class _Alloc>
00292 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
00293                                const _CharT* __right);
00294         
00295 template<class _CharT, class _Alloc>
00296 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
00297                                _CharT __right);
00298         
00299 // Some helpers, so we can use power on ropes.
00300 // See below for why this isn't local to the implementation.
00301 
00302 // This uses a nonstandard refcount convention.
00303 // The result has refcount 0.
00304 template<class _CharT, class _Alloc>
00305 struct _Rope_Concat_fn
00306        : public binary_function<rope<_CharT,_Alloc>, rope<_CharT,_Alloc>,
00307                                      rope<_CharT,_Alloc> > {
00308         rope<_CharT,_Alloc> operator() (const rope<_CharT,_Alloc>& __x,
00309                                 const rope<_CharT,_Alloc>& __y) {
00310                     return __x + __y;
00311         }
00312 };
00313 
00314 template <class _CharT, class _Alloc>
00315 inline
00316 rope<_CharT,_Alloc>
00317 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
00318 {
00319     return rope<_CharT,_Alloc>();
00320 }
00321 
00322 
00323 //
00324 // What follows should really be local to rope.  Unfortunately,
00325 // that doesn't work, since it makes it impossible to define generic
00326 // equality on rope iterators.  According to the draft standard, the
00327 // template parameters for such an equality operator cannot be inferred
00328 // from the occurence of a member class as a parameter.
00329 // (SGI compilers in fact allow this, but the __result wouldn't be
00330 // portable.)
00331 // Similarly, some of the static member functions are member functions
00332 // only to avoid polluting the global namespace, and to circumvent
00333 // restrictions on type inference for template functions.
00334 //
00335 
00336 //
00337 // The internal data structure for representing a rope.  This is
00338 // private to the implementation.  A rope is really just a pointer
00339 // to one of these.
00340 //
00341 // A few basic functions for manipulating this data structure
00342 // are members of _RopeRep.  Most of the more complex algorithms
00343 // are implemented as rope members.
00344 //
00345 // Some of the static member functions of _RopeRep have identically
00346 // named functions in rope that simply invoke the _RopeRep versions.
00347 //
00348 // A macro to introduce various allocation and deallocation functions
00349 // These need to be defined differently depending on whether or not
00350 // we are using standard conforming allocators, and whether the allocator
00351 // instances have real state.  Thus this macro is invoked repeatedly
00352 // with different definitions of __ROPE_DEFINE_ALLOC.
00353 // __ROPE_DEFINE_ALLOC(type,name) defines 
00354 //   type * name_allocate(size_t) and
00355 //   void name_deallocate(tipe *, size_t)
00356 // Both functions may or may not be static.
00357 
00358 #define __ROPE_DEFINE_ALLOCS(__a) \
00359         __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
00360         typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
00361         __ROPE_DEFINE_ALLOC(__C,_C) \
00362         typedef _Rope_RopeLeaf<_CharT,__a> __L; \
00363         __ROPE_DEFINE_ALLOC(__L,_L) \
00364         typedef _Rope_RopeFunction<_CharT,__a> __F; \
00365         __ROPE_DEFINE_ALLOC(__F,_F) \
00366         typedef _Rope_RopeSubstring<_CharT,__a> __S; \
00367         __ROPE_DEFINE_ALLOC(__S,_S)
00368 
00369 //  Internal rope nodes potentially store a copy of the allocator
00370 //  instance used to allocate them.  This is mostly redundant.
00371 //  But the alternative would be to pass allocator instances around
00372 //  in some form to nearly all internal functions, since any pointer
00373 //  assignment may result in a zero reference count and thus require
00374 //  deallocation.
00375 //  The _Rope_rep_base class encapsulates
00376 //  the differences between SGI-style allocators and standard-conforming
00377 //  allocators.
00378 
00379 #ifdef __STL_USE_STD_ALLOCATORS
00380 
00381 #define __STATIC_IF_SGI_ALLOC  /* not static */
00382 
00383 // Base class for ordinary allocators.
00384 template <class _CharT, class _Allocator, bool _IsStatic>
00385 class _Rope_rep_alloc_base {
00386 public:
00387   typedef typename _Alloc_traits<_CharT,_Allocator>::allocator_type
00388           allocator_type;
00389   allocator_type get_allocator() const { return _M_data_allocator; }
00390   _Rope_rep_alloc_base(size_t __size, const allocator_type& __a)
00391         : _M_size(__size), _M_data_allocator(__a) {}
00392   size_t _M_size;       // This is here only to avoid wasting space
00393                 // for an otherwise empty base class.
00394 
00395   
00396 protected:
00397     allocator_type _M_data_allocator;
00398 
00399 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
00400         typedef typename \
00401           _Alloc_traits<_Tp,_Allocator>::allocator_type __name##Allocator; \
00402         /*static*/ _Tp * __name##_allocate(size_t __n) \
00403           { return __name##Allocator(_M_data_allocator).allocate(__n); } \
00404         void __name##_deallocate(_Tp* __p, size_t __n) \
00405           { __name##Allocator(_M_data_allocator).deallocate(__p, __n); }
00406   __ROPE_DEFINE_ALLOCS(_Allocator);
00407 # undef __ROPE_DEFINE_ALLOC
00408 };
00409 
00410 // Specialization for allocators that have the property that we don't
00411 //  actually have to store an allocator object.  
00412 template <class _CharT, class _Allocator>
00413 class _Rope_rep_alloc_base<_CharT,_Allocator,true> {
00414 public:
00415   typedef typename _Alloc_traits<_CharT,_Allocator>::allocator_type
00416           allocator_type;
00417   allocator_type get_allocator() const { return allocator_type(); }
00418   _Rope_rep_alloc_base(size_t __size, const allocator_type&)
00419                 : _M_size(__size) {}
00420   size_t _M_size;
00421   
00422 protected:
00423 
00424 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
00425         typedef typename \
00426           _Alloc_traits<_Tp,_Allocator>::_Alloc_type __name##Alloc; \
00427         typedef typename \
00428           _Alloc_traits<_Tp,_Allocator>::allocator_type __name##Allocator; \
00429         static _Tp* __name##_allocate(size_t __n) \
00430                 { return __name##Alloc::allocate(__n); } \
00431         void __name##_deallocate(_Tp *__p, size_t __n) \
00432                 { __name##Alloc::deallocate(__p, __n); }
00433   __ROPE_DEFINE_ALLOCS(_Allocator);
00434 # undef __ROPE_DEFINE_ALLOC
00435 };
00436 
00437 template <class _CharT, class _Alloc>
00438 struct _Rope_rep_base
00439   : public _Rope_rep_alloc_base<_CharT,_Alloc,
00440                                 _Alloc_traits<_CharT,_Alloc>::_S_instanceless>
00441 {
00442   typedef _Rope_rep_alloc_base<_CharT,_Alloc,
00443                                _Alloc_traits<_CharT,_Alloc>::_S_instanceless>
00444           _Base;
00445   typedef typename _Base::allocator_type allocator_type;
00446   _Rope_rep_base(size_t __size, const allocator_type& __a)
00447     : _Base(__size, __a) {}
00448 };    
00449 
00450 #else /* !__STL_USE_STD_ALLOCATORS */
00451 
00452 #define __STATIC_IF_SGI_ALLOC static
00453 
00454 template <class _CharT, class _Alloc> 
00455 class _Rope_rep_base {
00456 public:
00457   typedef _Alloc allocator_type;
00458   static allocator_type get_allocator() { return allocator_type(); }
00459   _Rope_rep_base(size_t __size, const allocator_type&) : _M_size(__size) {}
00460   size_t _M_size;
00461 
00462 protected:
00463 
00464 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
00465         typedef simple_alloc<_Tp, _Alloc> __name##Alloc; \
00466         static _Tp* __name##_allocate(size_t __n) \
00467                 { return __name##Alloc::allocate(__n); } \
00468         static void __name##_deallocate(_Tp* __p, size_t __n) \
00469                 { __name##Alloc::deallocate(__p, __n); }
00470   __ROPE_DEFINE_ALLOCS(_Alloc);
00471 # undef __ROPE_DEFINE_ALLOC
00472 };
00473 
00474 #endif /* __STL_USE_STD_ALLOCATORS */
00475 
00476 
00477 template<class _CharT, class _Alloc>
00478 struct _Rope_RopeRep : public _Rope_rep_base<_CharT,_Alloc>
00479 # ifndef __GC
00480     , _Refcount_Base
00481 # endif
00482 {
00483     public:
00484     enum { _S_max_rope_depth = 45 };
00485     enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
00486     _Tag _M_tag:8;
00487     bool _M_is_balanced:8;
00488     unsigned char _M_depth;
00489     __GC_CONST _CharT* _M_c_string;
00490                         /* Flattened version of string, if needed.  */
00491                         /* typically 0.                             */
00492                         /* If it's not 0, then the memory is owned  */
00493                         /* by this node.                            */
00494                         /* In the case of a leaf, this may point to */
00495                         /* the same memory as the data field.       */
00496     typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
00497                         allocator_type;
00498     _Rope_RopeRep(_Tag __t, int __d, bool __b, size_t __size,
00499                   allocator_type __a)
00500         : _Rope_rep_base<_CharT,_Alloc>(__size, __a),
00501 #         ifndef __GC
00502           _Refcount_Base(1),
00503 #         endif
00504           _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
00505     { }
00506 #   ifdef __GC
00507         void _M_incr () {}
00508 #   endif
00509 #   ifdef __STL_USE_STD_ALLOCATORS
00510         static void _S_free_string(__GC_CONST _CharT*, size_t __len,
00511                                    allocator_type __a);
00512 #       define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
00513 #   else
00514         static void _S_free_string(__GC_CONST _CharT*, size_t __len);
00515 #       define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l);
00516 #   endif
00517                         // Deallocate data section of a leaf.
00518                         // This shouldn't be a member function.
00519                         // But its hard to do anything else at the
00520                         // moment, because it's templatized w.r.t.
00521                         // an allocator.
00522                         // Does nothing if __GC is defined.
00523 #   ifndef __GC
00524           void _M_free_c_string();
00525           void _M_free_tree();
00526                         // Deallocate t. Assumes t is not 0.
00527           void _M_unref_nonnil()
00528           {
00529               if (0 == _M_decr()) _M_free_tree();
00530           }
00531           void _M_ref_nonnil()
00532           {
00533               _M_incr();
00534           }
00535           static void _S_unref(_Rope_RopeRep* __t)
00536           {
00537               if (0 != __t) {
00538                   __t->_M_unref_nonnil();
00539               }
00540           }
00541           static void _S_ref(_Rope_RopeRep* __t)
00542           {
00543               if (0 != __t) __t->_M_incr();
00544           }
00545           static void _S_free_if_unref(_Rope_RopeRep* __t)
00546           {
00547               if (0 != __t && 0 == __t->_M_ref_count) __t->_M_free_tree();
00548           }
00549 #   else /* __GC */
00550           void _M_unref_nonnil() {}
00551           void _M_ref_nonnil() {}
00552           static void _S_unref(_Rope_RopeRep*) {}
00553           static void _S_ref(_Rope_RopeRep*) {}
00554           static void _S_free_if_unref(_Rope_RopeRep*) {}
00555 #   endif
00556 
00557 };
00558 
00559 template<class _CharT, class _Alloc>
00560 struct _Rope_RopeLeaf : public _Rope_RopeRep<_CharT,_Alloc> {
00561   public:
00562     // Apparently needed by VC++
00563     // The data fields of leaves are allocated with some
00564     // extra space, to accomodate future growth and for basic
00565     // character types, to hold a trailing eos character.
00566     enum { _S_alloc_granularity = 8 };
00567     static size_t _S_rounded_up_size(size_t __n) {
00568         size_t __size_with_eos;
00569              
00570         if (_S_is_basic_char_type((_CharT*)0)) {
00571             __size_with_eos = __n + 1;
00572         } else {
00573             __size_with_eos = __n;
00574         }
00575 #       ifdef __GC
00576            return __size_with_eos;
00577 #       else
00578            // Allow slop for in-place expansion.
00579            return (__size_with_eos + _S_alloc_granularity-1)
00580                         &~ (_S_alloc_granularity-1);
00581 #       endif
00582     }
00583     __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
00584                                 /* The allocated size is         */
00585                                 /* _S_rounded_up_size(size), except */
00586                                 /* in the GC case, in which it   */
00587                                 /* doesn't matter.               */
00588     typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
00589                         allocator_type;
00590     _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size, allocator_type __a)
00591         : _Rope_RopeRep<_CharT,_Alloc>(_S_leaf, 0, true, __size, __a),
00592           _M_data(__d)
00593         {
00594         __stl_assert(__size > 0);
00595         if (_S_is_basic_char_type((_CharT *)0)) {
00596             // already eos terminated.
00597             _M_c_string = __d;
00598         }
00599     }
00600         // The constructor assumes that d has been allocated with
00601         // the proper allocator and the properly padded size.
00602         // In contrast, the destructor deallocates the data:
00603 # ifndef __GC
00604     ~_Rope_RopeLeaf() {
00605         if (_M_data != _M_c_string) {
00606             _M_free_c_string();
00607         }
00608         __STL_FREE_STRING(_M_data, _M_size, get_allocator());
00609     }
00610 # endif
00611 };
00612 
00613 template<class _CharT, class _Alloc>
00614 struct _Rope_RopeConcatenation : public _Rope_RopeRep<_CharT,_Alloc> {
00615   public:
00616     _Rope_RopeRep<_CharT,_Alloc>* _M_left;
00617     _Rope_RopeRep<_CharT,_Alloc>* _M_right;
00618     typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
00619                         allocator_type;
00620     _Rope_RopeConcatenation(_Rope_RopeRep<_CharT,_Alloc>* __l,
00621                              _Rope_RopeRep<_CharT,_Alloc>* __r,
00622                              allocator_type __a)
00623 
00624       : _Rope_RopeRep<_CharT,_Alloc>(_S_concat,
00625                                      max(__l->_M_depth, __r->_M_depth) + 1,
00626                                      false,
00627                                      __l->_M_size + __r->_M_size, __a),
00628         _M_left(__l), _M_right(__r)
00629       {}
00630 # ifndef __GC
00631     ~_Rope_RopeConcatenation() {
00632         _M_free_c_string();
00633         _M_left->_M_unref_nonnil();
00634         _M_right->_M_unref_nonnil();
00635     }
00636 # endif
00637 };
00638 
00639 template<class _CharT, class _Alloc>
00640 struct _Rope_RopeFunction : public _Rope_RopeRep<_CharT,_Alloc> {
00641   public:
00642     char_producer<_CharT>* _M_fn;
00643 #   ifndef __GC
00644       bool _M_delete_when_done; // Char_producer is owned by the
00645                                 // rope and should be explicitly
00646                                 // deleted when the rope becomes
00647                                 // inaccessible.
00648 #   else
00649       // In the GC case, we either register the rope for
00650       // finalization, or not.  Thus the field is unnecessary;
00651       // the information is stored in the collector data structures.
00652       // We do need a finalization procedure to be invoked by the
00653       // collector.
00654       static void _S_fn_finalization_proc(void * __tree, void *) {
00655         delete ((_Rope_RopeFunction *)__tree) -> _M_fn;
00656       }
00657 #   endif
00658     typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
00659                                         allocator_type;
00660     _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
00661                         bool __d, allocator_type __a)
00662       : _Rope_RopeRep<_CharT,_Alloc>(_S_function, 0, true, __size, __a)
00663       , _M_fn(__f)
00664 #       ifndef __GC
00665       , _M_delete_when_done(__d)
00666 #       endif
00667     {
00668         __stl_assert(__size > 0);
00669 #       ifdef __GC
00670             if (__d) {
00671                 GC_REGISTER_FINALIZER(
00672                   this, _Rope_RopeFunction::_S_fn_finalization_proc, 0, 0, 0);
00673             }
00674 #       endif
00675     }
00676 # ifndef __GC
00677     ~_Rope_RopeFunction() {
00678           _M_free_c_string();
00679           if (_M_delete_when_done) {
00680               delete _M_fn;
00681           }
00682     }
00683 # endif
00684 };
00685 // Substring results are usually represented using just
00686 // concatenation nodes.  But in the case of very long flat ropes
00687 // or ropes with a functional representation that isn't practical.
00688 // In that case, we represent the __result as a special case of
00689 // RopeFunction, whose char_producer points back to the rope itself.
00690 // In all cases except repeated substring operations and
00691 // deallocation, we treat the __result as a RopeFunction.
00692 template<class _CharT, class _Alloc>
00693 struct _Rope_RopeSubstring : public _Rope_RopeFunction<_CharT,_Alloc>,
00694                              public char_producer<_CharT> {
00695   public:
00696     // XXX this whole class should be rewritten.
00697     _Rope_RopeRep<_CharT,_Alloc>* _M_base;      // not 0
00698     size_t _M_start;
00699     virtual void operator()(size_t __start_pos, size_t __req_len,
00700                             _CharT* __buffer) {
00701         switch(_M_base->_M_tag) {
00702             case _S_function:
00703             case _S_substringfn:
00704               {
00705                 char_producer<_CharT>* __fn =
00706                         ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
00707                 __stl_assert(__start_pos + __req_len <= _M_size);
00708                 __stl_assert(_M_start + _M_size <= _M_base->_M_size);
00709                 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
00710               }
00711               break;
00712             case _S_leaf:
00713               {
00714                 __GC_CONST _CharT* __s =
00715                         ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
00716                 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
00717                                      __buffer);
00718               }
00719               break;
00720             default:
00721               __stl_assert(false);
00722         }
00723     }
00724     typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
00725         allocator_type;
00726     _Rope_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
00727                           size_t __l, allocator_type __a)
00728       : _Rope_RopeFunction<_CharT,_Alloc>(this, __l, false, __a),
00729         char_producer<_CharT>(),
00730         _M_base(__b),
00731         _M_start(__s)
00732     {
00733         __stl_assert(__l > 0);
00734         __stl_assert(__s + __l <= __b->_M_size);
00735 #       ifndef __GC
00736             _M_base->_M_ref_nonnil();
00737 #       endif
00738         _M_tag = _S_substringfn;
00739     }
00740     virtual ~_Rope_RopeSubstring()
00741       { 
00742 #       ifndef __GC
00743           _M_base->_M_unref_nonnil();
00744           // _M_free_c_string();  -- done by parent class
00745 #       endif
00746       }
00747 };
00748 
00749 
00750 // Self-destructing pointers to Rope_rep.
00751 // These are not conventional smart pointers.  Their
00752 // only purpose in life is to ensure that unref is called
00753 // on the pointer either at normal exit or if an exception
00754 // is raised.  It is the caller's responsibility to
00755 // adjust reference counts when these pointers are initialized
00756 // or assigned to.  (This convention significantly reduces
00757 // the number of potentially expensive reference count
00758 // updates.)
00759 #ifndef __GC
00760   template<class _CharT, class _Alloc>
00761   struct _Rope_self_destruct_ptr {
00762     _Rope_RopeRep<_CharT,_Alloc>* _M_ptr;
00763     ~_Rope_self_destruct_ptr() 
00764       { _Rope_RopeRep<_CharT,_Alloc>::_S_unref(_M_ptr); }
00765 #   ifdef __STL_USE_EXCEPTIONS
00766         _Rope_self_destruct_ptr() : _M_ptr(0) {};
00767 #   else
00768         _Rope_self_destruct_ptr() {};
00769 #   endif
00770     _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT,_Alloc>* __p) : _M_ptr(__p) {}
00771     _Rope_RopeRep<_CharT,_Alloc>& operator*() { return *_M_ptr; }
00772     _Rope_RopeRep<_CharT,_Alloc>* operator->() { return _M_ptr; }
00773     operator _Rope_RopeRep<_CharT,_Alloc>*() { return _M_ptr; }
00774     _Rope_self_destruct_ptr& operator= (_Rope_RopeRep<_CharT,_Alloc>* __x)
00775         { _M_ptr = __x; return *this; }
00776   };
00777 #endif
00778 
00779 // Dereferencing a nonconst iterator has to return something
00780 // that behaves almost like a reference.  It's not possible to
00781 // return an actual reference since assignment requires extra
00782 // work.  And we would get into the same problems as with the
00783 // CD2 version of basic_string.
00784 template<class _CharT, class _Alloc>
00785 class _Rope_char_ref_proxy {
00786     friend class rope<_CharT,_Alloc>;
00787     friend class _Rope_iterator<_CharT,_Alloc>;
00788     friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
00789 #   ifdef __GC
00790         typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
00791 #   else
00792         typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
00793 #   endif
00794     typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
00795     typedef rope<_CharT,_Alloc> _My_rope;
00796     size_t _M_pos;
00797     _CharT _M_current;
00798     bool _M_current_valid;
00799     _My_rope* _M_root;     // The whole rope.
00800   public:
00801     _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
00802       :  _M_pos(__p), _M_current_valid(false), _M_root(__r) {}
00803     _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
00804       : _M_pos(__x._M_pos), _M_current_valid(false), _M_root(__x._M_root) {}
00805         // Don't preserve cache if the reference can outlive the
00806         // expression.  We claim that's not possible without calling
00807         // a copy constructor or generating reference to a proxy
00808         // reference.  We declare the latter to have undefined semantics.
00809     _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
00810       : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
00811     inline operator _CharT () const;
00812     _Rope_char_ref_proxy& operator= (_CharT __c);
00813     _Rope_char_ptr_proxy<_CharT,_Alloc> operator& () const;
00814     _Rope_char_ref_proxy& operator= (const _Rope_char_ref_proxy& __c) {
00815         return operator=((_CharT)__c); 
00816     }
00817 };
00818 
00819 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00820     template<class _CharT, class __Alloc>
00821     inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
00822                      _Rope_char_ref_proxy <_CharT, __Alloc > __b) {
00823         _CharT __tmp = __a;
00824         __a = __b;
00825         __b = __tmp;
00826     }
00827 #else
00828 // There is no really acceptable way to handle this.  The default
00829 // definition of swap doesn't work for proxy references.
00830 // It can't really be made to work, even with ugly hacks, since
00831 // the only unusual operation it uses is the copy constructor, which
00832 // is needed for other purposes.  We provide a macro for
00833 // full specializations, and instantiate the most common case.
00834 # define _ROPE_SWAP_SPECIALIZATION(_CharT, __Alloc) \
00835     inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a, \
00836                      _Rope_char_ref_proxy <_CharT, __Alloc > __b) { \
00837         _CharT __tmp = __a; \
00838         __a = __b; \
00839         __b = __tmp; \
00840     }
00841 
00842 _ROPE_SWAP_SPECIALIZATION(char,__STL_DEFAULT_ALLOCATOR(char))
00843 
00844 #endif /* !__STL_FUNCTION_TMPL_PARTIAL_ORDER */
00845 
00846 template<class _CharT, class _Alloc>
00847 class _Rope_char_ptr_proxy {
00848     // XXX this class should be rewritten.
00849     friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
00850     size_t _M_pos;
00851     rope<_CharT,_Alloc>* _M_root;     // The whole rope.
00852   public:
00853     _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x) 
00854       : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
00855     _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
00856       : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
00857     _Rope_char_ptr_proxy() {}
00858     _Rope_char_ptr_proxy(_CharT* __x) : _M_root(0), _M_pos(0) {
00859         __stl_assert(0 == __x);
00860     }
00861     _Rope_char_ptr_proxy& 
00862     operator= (const _Rope_char_ptr_proxy& __x) {
00863         _M_pos = __x._M_pos;
00864         _M_root = __x._M_root;
00865         return *this;
00866     }
00867 #ifdef __STL_MEMBER_TEMPLATES
00868     template<class _CharT2, class _Alloc2>
00869     friend bool operator== (const _Rope_char_ptr_proxy<_CharT2,_Alloc2>& __x,
00870                             const _Rope_char_ptr_proxy<_CharT2,_Alloc2>& __y);
00871 #else
00872     friend bool operator==  __STL_NULL_TMPL_ARGS
00873                 (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
00874                  const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y);
00875 #endif
00876     _Rope_char_ref_proxy<_CharT,_Alloc> operator*() const {
00877         return _Rope_char_ref_proxy<_CharT,_Alloc>(_M_root, _M_pos);
00878     }
00879 };
00880 
00881 
00882 // Rope iterators:
00883 // Unlike in the C version, we cache only part of the stack
00884 // for rope iterators, since they must be efficiently copyable.
00885 // When we run out of cache, we have to reconstruct the iterator
00886 // value.
00887 // Pointers from iterators are not included in reference counts.
00888 // Iterators are assumed to be thread private.  Ropes can
00889 // be shared.
00890 
00891 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00892 #pragma set woff 1375
00893 #endif
00894 
00895 template<class _CharT, class _Alloc>
00896 class _Rope_iterator_base
00897   : public random_access_iterator<_CharT, ptrdiff_t> {
00898     friend class rope<_CharT,_Alloc>;
00899   public:
00900     typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
00901     typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
00902         // Borland doesnt want this to be protected.
00903   protected:
00904     enum { _S_path_cache_len = 4 }; // Must be <= 9.
00905     enum { _S_iterator_buf_len = 15 };
00906     size_t _M_current_pos;
00907     _RopeRep* _M_root;     // The whole rope.
00908     size_t _M_leaf_pos;    // Starting position for current leaf
00909     __GC_CONST _CharT* _M_buf_start;
00910                         // Buffer possibly
00911                         // containing current char.
00912     __GC_CONST _CharT* _M_buf_ptr;
00913                         // Pointer to current char in buffer.
00914                         // != 0 ==> buffer valid.
00915     __GC_CONST _CharT* _M_buf_end;
00916                         // One past __last valid char in buffer.
00917     // What follows is the path cache.  We go out of our
00918     // way to make this compact.
00919     // Path_end contains the bottom section of the path from
00920     // the root to the current leaf.
00921     const _RopeRep* _M_path_end[_S_path_cache_len];
00922     int _M_leaf_index;     // Last valid __pos in path_end;
00923                         // _M_path_end[0] ... _M_path_end[leaf_index-1]
00924                         // point to concatenation nodes.
00925     unsigned char _M_path_directions;
00926                           // (path_directions >> __i) & 1 is 1
00927                           // iff we got from _M_path_end[leaf_index - __i - 1]
00928                           // to _M_path_end[leaf_index - __i] by going to the
00929                           // __right. Assumes path_cache_len <= 9.
00930     _CharT _M_tmp_buf[_S_iterator_buf_len];
00931                         // Short buffer for surrounding chars.
00932                         // This is useful primarily for 
00933                         // RopeFunctions.  We put the buffer
00934                         // here to avoid locking in the
00935                         // multithreaded case.
00936     // The cached path is generally assumed to be valid
00937     // only if the buffer is valid.
00938     static void _S_setbuf(_Rope_iterator_base& __x);
00939                                         // Set buffer contents given
00940                                         // path cache.
00941     static void _S_setcache(_Rope_iterator_base& __x);
00942                                         // Set buffer contents and
00943                                         // path cache.
00944     static void _S_setcache_for_incr(_Rope_iterator_base& __x);
00945                                         // As above, but assumes path
00946                                         // cache is valid for previous posn.
00947     _Rope_iterator_base() {}
00948     _Rope_iterator_base(_RopeRep* __root, size_t __pos)
00949       : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) {}
00950     void _M_incr(size_t __n);
00951     void _M_decr(size_t __n);
00952   public:
00953     size_t index() const { return _M_current_pos; }
00954     _Rope_iterator_base(const _Rope_iterator_base& __x) {
00955         if (0 != __x._M_buf_ptr) {
00956             *this = __x;
00957         } else {
00958             _M_current_pos = __x._M_current_pos;
00959             _M_root = __x._M_root;
00960             _M_buf_ptr = 0;
00961         }
00962     }
00963 };
00964 
00965 template<class _CharT, class _Alloc> class _Rope_iterator;
00966 
00967 template<class _CharT, class _Alloc>
00968 class _Rope_const_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
00969     friend class rope<_CharT,_Alloc>;
00970   protected:
00971 #   ifdef __STL_HAS_NAMESPACES
00972       typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
00973       // The one from the base class may not be directly visible.
00974 #   endif
00975     _Rope_const_iterator(const _RopeRep* __root, size_t __pos):
00976                    _Rope_iterator_base<_CharT,_Alloc>(
00977                      const_cast<_RopeRep*>(__root), __pos)
00978                    // Only nonconst iterators modify root ref count
00979     {}
00980   public:
00981     typedef _CharT reference;   // Really a value.  Returning a reference
00982                                 // Would be a mess, since it would have
00983                                 // to be included in refcount.
00984     typedef const _CharT* pointer;
00985 
00986   public:
00987     _Rope_const_iterator() {};
00988     _Rope_const_iterator(const _Rope_const_iterator& __x) :
00989                                 _Rope_iterator_base<_CharT,_Alloc>(__x) { }
00990     _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
00991     _Rope_const_iterator(const rope<_CharT,_Alloc>& __r, size_t __pos) :
00992         _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) {}
00993     _Rope_const_iterator& operator= (const _Rope_const_iterator& __x) {
00994         if (0 != __x._M_buf_ptr) {
00995             *(static_cast<_Rope_iterator_base<_CharT,_Alloc>*>(this)) = __x;
00996         } else {
00997             _M_current_pos = __x._M_current_pos;
00998             _M_root = __x._M_root;
00999             _M_buf_ptr = 0;
01000         }
01001         return(*this);
01002     }
01003     reference operator*() {
01004         if (0 == _M_buf_ptr) _S_setcache(*this);
01005         return *_M_buf_ptr;
01006     }
01007     _Rope_const_iterator& operator++() {
01008         __GC_CONST _CharT* __next;
01009         if (0 != _M_buf_ptr && (__next = _M_buf_ptr + 1) < _M_buf_end) {
01010             _M_buf_ptr = __next;
01011             ++_M_current_pos;
01012         } else {
01013             _M_incr(1);
01014         }
01015         return *this;
01016     }
01017     _Rope_const_iterator& operator+=(ptrdiff_t __n) {
01018         if (__n >= 0) {
01019             _M_incr(__n);
01020         } else {
01021             _M_decr(-__n);
01022         }
01023         return *this;
01024     }
01025     _Rope_const_iterator& operator--() {
01026         _M_decr(1);
01027         return *this;
01028     }
01029     _Rope_const_iterator& operator-=(ptrdiff_t __n) {
01030         if (__n >= 0) {
01031             _M_decr(__n);
01032         } else {
01033             _M_incr(-__n);
01034         }
01035         return *this;
01036     }
01037     _Rope_const_iterator operator++(int) {
01038         size_t __old_pos = _M_current_pos;
01039         _M_incr(1);
01040         return _Rope_const_iterator<_CharT,_Alloc>(_M_root, __old_pos);
01041         // This makes a subsequent dereference expensive.
01042         // Perhaps we should instead copy the iterator
01043         // if it has a valid cache?
01044     }
01045     _Rope_const_iterator operator--(int) {
01046         size_t __old_pos = _M_current_pos;
01047         _M_decr(1);
01048         return _Rope_const_iterator<_CharT,_Alloc>(_M_root, __old_pos);
01049     }
01050 #if defined(__STL_MEMBER_TEMPLATES) && defined(__STL_FUNCTION_TMPL_PARTIAL_ORDER)
01051     template<class _CharT2, class _Alloc2>
01052     friend _Rope_const_iterator<_CharT2,_Alloc2> operator-
01053         (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
01054          ptrdiff_t __n);
01055     template<class _CharT2, class _Alloc2>
01056     friend _Rope_const_iterator<_CharT2,_Alloc2> operator+
01057         (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
01058          ptrdiff_t __n);
01059     template<class _CharT2, class _Alloc2>
01060     friend _Rope_const_iterator<_CharT2,_Alloc2> operator+
01061         (ptrdiff_t __n,
01062          const _Rope_const_iterator<_CharT2,_Alloc2>& __x);
01063 #else
01064     friend _Rope_const_iterator<_CharT,_Alloc> operator- __STL_NULL_TMPL_ARGS
01065         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
01066          ptrdiff_t __n);
01067     friend _Rope_const_iterator<_CharT,_Alloc> operator+ __STL_NULL_TMPL_ARGS
01068         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
01069          ptrdiff_t __n);
01070     friend _Rope_const_iterator<_CharT,_Alloc> operator+ __STL_NULL_TMPL_ARGS
01071         (ptrdiff_t __n,
01072          const _Rope_const_iterator<_CharT,_Alloc>& __x);
01073 #endif
01074 
01075     reference operator[](size_t __n) {
01076         return rope<_CharT,_Alloc>::_S_fetch(_M_root, _M_current_pos + __n);
01077     }
01078 
01079 #if defined(__STL_MEMBER_TEMPLATES) && defined(__STL_FUNCTION_TMPL_PARTIAL_ORDER)
01080     template<class _CharT2, class _Alloc2>
01081     friend bool operator==
01082         (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
01083          const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
01084     template<class _CharT2, class _Alloc2>
01085     friend bool operator< 
01086         (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
01087          const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
01088     template<class _CharT2, class _Alloc2>
01089     friend ptrdiff_t operator-
01090         (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
01091          const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
01092 #else
01093     friend bool operator== __STL_NULL_TMPL_ARGS
01094         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
01095          const _Rope_const_iterator<_CharT,_Alloc>& __y);
01096     friend bool operator< __STL_NULL_TMPL_ARGS
01097         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
01098          const _Rope_const_iterator<_CharT,_Alloc>& __y);
01099     friend ptrdiff_t operator- __STL_NULL_TMPL_ARGS
01100         (const _Rope_const_iterator<_CharT,_Alloc>& __x,
01101          const _Rope_const_iterator<_CharT,_Alloc>& __y);
01102 #endif
01103 };
01104 
01105 template<class _CharT, class _Alloc>
01106 class _Rope_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
01107     friend class rope<_CharT,_Alloc>;
01108   protected:
01109     rope<_CharT,_Alloc>* _M_root_rope;
01110         // root is treated as a cached version of this,
01111         // and is used to detect changes to the underlying
01112         // rope.
01113         // Root is included in the reference count.
01114         // This is necessary so that we can detect changes reliably.
01115         // Unfortunately, it requires careful bookkeeping for the
01116         // nonGC case.
01117     _Rope_iterator(rope<_CharT,_Alloc>* __r, size_t __pos)
01118       : _Rope_iterator_base<_CharT,_Alloc>(__r->_M_tree_ptr, __pos),
01119         _M_root_rope(__r) 
01120        { _RopeRep::_S_ref(_M_root); if (!(__r -> empty()))_S_setcache(*this); }
01121 
01122     void _M_check();
01123   public:
01124     typedef _Rope_char_ref_proxy<_CharT,_Alloc>  reference;
01125     typedef _Rope_char_ref_proxy<_CharT,_Alloc>* pointer;
01126 
01127   public:
01128     rope<_CharT,_Alloc>& container() { return *_M_root_rope; }
01129     _Rope_iterator() {
01130         _M_root = 0;  // Needed for reference counting.
01131     };
01132     _Rope_iterator(const _Rope_iterator& __x) :
01133         _Rope_iterator_base<_CharT,_Alloc>(__x) {
01134         _M_root_rope = __x._M_root_rope;
01135         _RopeRep::_S_ref(_M_root);
01136     }
01137     _Rope_iterator(rope<_CharT,_Alloc>& __r, size_t __pos);
01138     ~_Rope_iterator() {
01139         _RopeRep::_S_unref(_M_root);
01140     }
01141     _Rope_iterator& operator= (const _Rope_iterator& __x) {
01142         _RopeRep* __old = _M_root;
01143 
01144         _RopeRep::_S_ref(__x._M_root);
01145         if (0 != __x._M_buf_ptr) {
01146             _M_root_rope = __x._M_root_rope;
01147             *(static_cast<_Rope_iterator_base<_CharT,_Alloc>*>(this)) = __x;
01148         } else {
01149             _M_current_pos = __x._M_current_pos;
01150             _M_root = __x._M_root;
01151             _M_root_rope = __x._M_root_rope;
01152             _M_buf_ptr = 0;
01153         }
01154         _RopeRep::_S_unref(__old);
01155         return(*this);
01156     }
01157     reference operator*() {
01158         _M_check();
01159         if (0 == _M_buf_ptr) {
01160             return _Rope_char_ref_proxy<_CharT,_Alloc>(
01161                _M_root_rope, _M_current_pos);
01162         } else {
01163             return _Rope_char_ref_proxy<_CharT,_Alloc>(
01164                _M_root_rope, _M_current_pos, *_M_buf_ptr);
01165         }
01166     }
01167     _Rope_iterator& operator++() {
01168         _M_incr(1);
01169         return *this;
01170     }
01171     _Rope_iterator& operator+=(ptrdiff_t __n) {
01172         if (__n >= 0) {
01173             _M_incr(__n);
01174         } else {
01175             _M_decr(-__n);
01176         }
01177         return *this;
01178     }
01179     _Rope_iterator& operator--() {
01180         _M_decr(1);
01181         return *this;
01182     }
01183     _Rope_iterator& operator-=(ptrdiff_t __n) {
01184         if (__n >= 0) {
01185             _M_decr(__n);
01186         } else {
01187             _M_incr(-__n);
01188         }
01189         return *this;
01190     }
01191     _Rope_iterator operator++(int) {
01192         size_t __old_pos = _M_current_pos;
01193         _M_incr(1);
01194         return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
01195     }
01196     _Rope_iterator operator--(int) {
01197         size_t __old_pos = _M_current_pos;
01198         _M_decr(1);
01199         return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
01200     }
01201     reference operator[](ptrdiff_t __n) {
01202         return _Rope_char_ref_proxy<_CharT,_Alloc>(
01203           _M_root_rope, _M_current_pos + __n);
01204     }
01205 
01206 #if defined(__STL_MEMBER_TEMPLATES) && defined(__STL_FUNCTION_TMPL_PARTIAL_ORDER)
01207     template<class _CharT2, class _Alloc2>
01208     friend bool operator==
01209         (const _Rope_iterator<_CharT2,_Alloc2>& __x,
01210          const _Rope_iterator<_CharT2,_Alloc2>& __y);
01211     template<class _CharT2, class _Alloc2>
01212     friend bool operator<
01213         (const _Rope_iterator<_CharT2,_Alloc2>& __x,
01214          const _Rope_iterator<_CharT2,_Alloc2>& __y);
01215     template<class _CharT2, class _Alloc2>
01216     friend ptrdiff_t operator-
01217         (const _Rope_iterator<_CharT2,_Alloc2>& __x,
01218          const _Rope_iterator<_CharT2,_Alloc2>& __y);
01219     template<class _CharT2, class _Alloc2>
01220     friend _Rope_iterator<_CharT2,_Alloc2> operator-
01221         (const _Rope_iterator<_CharT2,_Alloc2>& __x,
01222          ptrdiff_t __n);
01223     template<class _CharT2, class _Alloc2>
01224     friend _Rope_iterator<_CharT2,_Alloc2> operator+
01225         (const _Rope_iterator<_CharT2,_Alloc2>& __x,
01226          ptrdiff_t __n);
01227     template<class _CharT2, class _Alloc2>
01228     friend _Rope_iterator<_CharT2,_Alloc2> operator+
01229         (ptrdiff_t __n,
01230          const _Rope_iterator<_CharT2,_Alloc2>& __x);
01231 #else
01232     friend bool operator== __STL_NULL_TMPL_ARGS
01233         (const _Rope_iterator<_CharT,_Alloc>& __x,
01234          const _Rope_iterator<_CharT,_Alloc>& __y);
01235     friend bool operator< __STL_NULL_TMPL_ARGS
01236         (const _Rope_iterator<_CharT,_Alloc>& __x,
01237          const _Rope_iterator<_CharT,_Alloc>& __y);
01238     friend ptrdiff_t operator- __STL_NULL_TMPL_ARGS
01239         (const _Rope_iterator<_CharT,_Alloc>& __x,
01240          const _Rope_iterator<_CharT,_Alloc>& __y);
01241     friend _Rope_iterator<_CharT,_Alloc> operator- __STL_NULL_TMPL_ARGS
01242         (const _Rope_iterator<_CharT,_Alloc>& __x,
01243          ptrdiff_t __n);
01244     friend _Rope_iterator<_CharT,_Alloc> operator+ __STL_NULL_TMPL_ARGS
01245         (const _Rope_iterator<_CharT,_Alloc>& __x,
01246          ptrdiff_t __n);
01247     friend _Rope_iterator<_CharT,_Alloc> operator+ __STL_NULL_TMPL_ARGS
01248         (ptrdiff_t __n,
01249          const _Rope_iterator<_CharT,_Alloc>& __x);
01250 #endif
01251 };
01252 
01253 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
01254 #pragma reset woff 1375
01255 #endif
01256 
01257 //  The rope base class encapsulates
01258 //  the differences between SGI-style allocators and standard-conforming
01259 //  allocators.
01260 
01261 #ifdef __STL_USE_STD_ALLOCATORS
01262 
01263 // Base class for ordinary allocators.
01264 template <class _CharT, class _Allocator, bool _IsStatic>
01265 class _Rope_alloc_base {
01266 public:
01267   typedef _Rope_RopeRep<_CharT,_Allocator> _RopeRep;
01268   typedef typename _Alloc_traits<_CharT,_Allocator>::allocator_type
01269           allocator_type;
01270   allocator_type get_allocator() const { return _M_data_allocator; }
01271   _Rope_alloc_base(_RopeRep *__t, const allocator_type& __a)
01272         : _M_tree_ptr(__t), _M_data_allocator(__a) {}
01273   _Rope_alloc_base(const allocator_type& __a)
01274         : _M_data_allocator(__a) {}
01275   
01276 protected:
01277   // The only data members of a rope:
01278     allocator_type _M_data_allocator;
01279     _RopeRep* _M_tree_ptr;
01280 
01281 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
01282         typedef typename \
01283           _Alloc_traits<_Tp,_Allocator>::allocator_type __name##Allocator; \
01284         _Tp* __name##_allocate(size_t __n) const \
01285           { return __name##Allocator(_M_data_allocator).allocate(__n); } \
01286         void __name##_deallocate(_Tp *__p, size_t __n) const \
01287                 { __name##Allocator(_M_data_allocator).deallocate(__p, __n); }
01288   __ROPE_DEFINE_ALLOCS(_Allocator)
01289 # undef __ROPE_DEFINE_ALLOC
01290 };
01291 
01292 // Specialization for allocators that have the property that we don't
01293 //  actually have to store an allocator object.  
01294 template <class _CharT, class _Allocator>
01295 class _Rope_alloc_base<_CharT,_Allocator,true> {
01296 public:
01297   typedef _Rope_RopeRep<_CharT,_Allocator> _RopeRep;
01298   typedef typename _Alloc_traits<_CharT,_Allocator>::allocator_type
01299           allocator_type;
01300   allocator_type get_allocator() const { return allocator_type(); }
01301   _Rope_alloc_base(_RopeRep *__t, const allocator_type&)
01302                 : _M_tree_ptr(__t) {}
01303   _Rope_alloc_base(const allocator_type&) {}
01304   
01305 protected:
01306   // The only data member of a rope:
01307     _RopeRep *_M_tree_ptr;
01308 
01309 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
01310         typedef typename \
01311           _Alloc_traits<_Tp,_Allocator>::_Alloc_type __name##Alloc; \
01312         typedef typename \
01313           _Alloc_traits<_Tp,_Allocator>::allocator_type __name##Allocator; \
01314         static _Tp* __name##_allocate(size_t __n) \
01315           { return __name##Alloc::allocate(__n); } \
01316         static void __name##_deallocate(_Tp *__p, size_t __n) \
01317           { __name##Alloc::deallocate(__p, __n); }
01318   __ROPE_DEFINE_ALLOCS(_Allocator)
01319 # undef __ROPE_DEFINE_ALLOC
01320 };
01321 
01322 template <class _CharT, class _Alloc>
01323 struct _Rope_base 
01324   : public _Rope_alloc_base<_CharT,_Alloc,
01325                             _Alloc_traits<_CharT,_Alloc>::_S_instanceless>
01326 {
01327   typedef _Rope_alloc_base<_CharT,_Alloc,
01328                             _Alloc_traits<_CharT,_Alloc>::_S_instanceless>
01329           _Base;
01330   typedef typename _Base::allocator_type allocator_type;
01331   typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
01332         // The one in _Base may not be visible due to template rules.
01333   _Rope_base(_RopeRep* __t, const allocator_type& __a) : _Base(__t, __a) {}
01334   _Rope_base(const allocator_type& __a) : _Base(__a) {}
01335 };    
01336 
01337 #else /* !__STL_USE_STD_ALLOCATORS */
01338 
01339 template <class _CharT, class _Alloc> 
01340 class _Rope_base {
01341 public:
01342   typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
01343   typedef _Alloc allocator_type;
01344   static allocator_type get_allocator() { return allocator_type(); }
01345   _Rope_base(_RopeRep * __t, const allocator_type&) : _M_tree_ptr(__t) {}
01346   _Rope_base(const allocator_type&) {}
01347 
01348 protected:
01349   // The only data member of a rope:
01350     _RopeRep* _M_tree_ptr;
01351 
01352 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
01353         typedef simple_alloc<_Tp, _Alloc> __name##Alloc; \
01354         static _Tp* __name##_allocate(size_t __n) \
01355                 { return __name##Alloc::allocate(__n); } \
01356         static void __name##_deallocate(_Tp *__p, size_t __n) \
01357                 { __name##Alloc::deallocate(__p, __n); }
01358   __ROPE_DEFINE_ALLOCS(_Alloc)
01359 # undef __ROPE_DEFINE_ALLOC
01360 };
01361 
01362 #endif /* __STL_USE_STD_ALLOCATORS */
01363 
01364 
01365 template <class _CharT, class _Alloc>
01366 class rope : public _Rope_base<_CharT,_Alloc> {
01367     public:
01368         typedef _CharT value_type;
01369         typedef ptrdiff_t difference_type;
01370         typedef size_t size_type;
01371         typedef _CharT const_reference;
01372         typedef const _CharT* const_pointer;
01373         typedef _Rope_iterator<_CharT,_Alloc> iterator;
01374         typedef _Rope_const_iterator<_CharT,_Alloc> const_iterator;
01375         typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
01376         typedef _Rope_char_ptr_proxy<_CharT,_Alloc> pointer;
01377 
01378         friend class _Rope_iterator<_CharT,_Alloc>;
01379         friend class _Rope_const_iterator<_CharT,_Alloc>;
01380         friend struct _Rope_RopeRep<_CharT,_Alloc>;
01381         friend class _Rope_iterator_base<_CharT,_Alloc>;
01382         friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
01383         friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
01384         friend struct _Rope_RopeSubstring<_CharT,_Alloc>;
01385 
01386     protected:
01387         typedef _Rope_base<_CharT,_Alloc> _Base;
01388         typedef typename _Base::allocator_type allocator_type;
01389 #       ifdef __STL_USE_NAMESPACES
01390           using _Base::_M_tree_ptr;
01391 #       endif
01392         typedef __GC_CONST _CharT* _Cstrptr;
01393 
01394         static _CharT _S_empty_c_str[1];
01395 
01396         static bool _S_is0(_CharT __c) { return __c == _S_eos((_CharT*)0); }
01397         enum { _S_copy_max = 23 };
01398                 // For strings shorter than _S_copy_max, we copy to
01399                 // concatenate.
01400 
01401         typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
01402         typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcatenation;
01403         typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
01404         typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
01405         typedef _Rope_RopeSubstring<_CharT,_Alloc> _RopeSubstring;
01406 
01407         // Retrieve a character at the indicated position.
01408         static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
01409 
01410 #       ifndef __GC
01411             // Obtain a pointer to the character at the indicated position.
01412             // The pointer can be used to change the character.
01413             // If such a pointer cannot be produced, as is frequently the
01414             // case, 0 is returned instead.
01415             // (Returns nonzero only if all nodes in the path have a refcount
01416             // of 1.)
01417             static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
01418 #       endif
01419 
01420         static bool _S_apply_to_pieces(
01421                                 // should be template parameter
01422                                 _Rope_char_consumer<_CharT>& __c,
01423                                 const _RopeRep* __r,
01424                                 size_t __begin, size_t __end);
01425                                 // begin and end are assumed to be in range.
01426 
01427 #       ifndef __GC
01428           static void _S_unref(_RopeRep* __t)
01429           {
01430               _RopeRep::_S_unref(__t);
01431           }
01432           static void _S_ref(_RopeRep* __t)
01433           {
01434               _RopeRep::_S_ref(__t);
01435           }
01436 #       else /* __GC */
01437           static void _S_unref(_RopeRep*) {}
01438           static void _S_ref(_RopeRep*) {}
01439 #       endif
01440 
01441 
01442 #       ifdef __GC
01443             typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
01444 #       else
01445             typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
01446 #       endif
01447 
01448         // _Result is counted in refcount.
01449         static _RopeRep* _S_substring(_RopeRep* __base,
01450                                     size_t __start, size_t __endp1);
01451 
01452         static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
01453                                           const _CharT* __iter, size_t __slen);
01454                 // Concatenate rope and char ptr, copying __s.
01455                 // Should really take an arbitrary iterator.
01456                 // Result is counted in refcount.
01457         static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
01458                                           const _CharT* __iter, size_t __slen)
01459                 // As above, but one reference to __r is about to be
01460                 // destroyed.  Thus the pieces may be recycled if all
01461                 // relevent reference counts are 1.
01462 #           ifdef __GC
01463                 // We can't really do anything since refcounts are unavailable.
01464                 { return _S_concat_char_iter(__r, __iter, __slen); }
01465 #           else
01466                 ;
01467 #           endif
01468 
01469         static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
01470                 // General concatenation on _RopeRep.  _Result
01471                 // has refcount of 1.  Adjusts argument refcounts.
01472 
01473    public:
01474         void apply_to_pieces( size_t __begin, size_t __end,
01475                               _Rope_char_consumer<_CharT>& __c) const {
01476             _S_apply_to_pieces(__c, _M_tree_ptr, __begin, __end);
01477         }
01478 
01479 
01480    protected:
01481 
01482         static size_t _S_rounded_up_size(size_t __n) {
01483             return _RopeLeaf::_S_rounded_up_size(__n);
01484         }
01485 
01486         static size_t _S_allocated_capacity(size_t __n) {
01487             if (_S_is_basic_char_type((_CharT*)0)) {
01488                 return _S_rounded_up_size(__n) - 1;
01489             } else {
01490                 return _S_rounded_up_size(__n);
01491             }
01492         }
01493                 
01494         // Allocate and construct a RopeLeaf using the supplied allocator
01495         // Takes ownership of s instead of copying.
01496         static _RopeLeaf* _S_new_RopeLeaf(__GC_CONST _CharT *__s,
01497                                           size_t __size, allocator_type __a)
01498         {
01499 #           ifdef __STL_USE_STD_ALLOCATORS
01500               _RopeLeaf* __space = _LAllocator(__a).allocate(1);
01501 #           else
01502               _RopeLeaf* __space = _L_allocate(1);
01503 #           endif
01504             return new(__space) _RopeLeaf(__s, __size, __a);
01505         }
01506 
01507         static _RopeConcatenation* _S_new_RopeConcatenation(
01508                         _RopeRep* __left, _RopeRep* __right,
01509                         allocator_type __a)
01510         {
01511 #           ifdef __STL_USE_STD_ALLOCATORS
01512               _RopeConcatenation* __space = _CAllocator(__a).allocate(1);
01513 #           else
01514               _RopeConcatenation* __space = _C_allocate(1);
01515 #           endif
01516             return new(__space) _RopeConcatenation(__left, __right, __a);
01517         }
01518 
01519         static _RopeFunction* _S_new_RopeFunction(char_producer<_CharT>* __f,
01520                 size_t __size, bool __d, allocator_type __a)
01521         {
01522 #           ifdef __STL_USE_STD_ALLOCATORS
01523               _RopeFunction* __space = _FAllocator(__a).allocate(1);
01524 #           else
01525               _RopeFunction* __space = _F_allocate(1);
01526 #           endif
01527             return new(__space) _RopeFunction(__f, __size, __d, __a);
01528         }
01529 
01530         static _RopeSubstring* _S_new_RopeSubstring(
01531                 _Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
01532                 size_t __l, allocator_type __a)
01533         {
01534 #           ifdef __STL_USE_STD_ALLOCATORS
01535               _RopeSubstring* __space = _SAllocator(__a).allocate(1);
01536 #           else
01537               _RopeSubstring* __space = _S_allocate(1);
01538 #           endif
01539             return new(__space) _RopeSubstring(__b, __s, __l, __a);
01540         }
01541 
01542 #       ifdef __STL_USE_STD_ALLOCATORS
01543           static
01544           _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
01545                        size_t __size, allocator_type __a)
01546 #         define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
01547                 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)     
01548 #       else
01549           static
01550           _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr2(const _CharT* __s,
01551                                                         size_t __size)
01552 #         define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
01553                _S_RopeLeaf_from_unowned_char_ptr2(__s, __size)
01554 #       endif
01555         {
01556             if (0 == __size) return 0;
01557 #           ifdef __STL_USE_STD_ALLOCATORS
01558               _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
01559 #           else
01560               _CharT* __buf = _Data_allocate(_S_rounded_up_size(__size));
01561               allocator_type __a = allocator_type();
01562 #           endif
01563 
01564             uninitialized_copy_n(__s, __size, __buf);
01565             _S_cond_store_eos(__buf[__size]);
01566             __STL_TRY {
01567               return _S_new_RopeLeaf(__buf, __size, __a);
01568             }
01569             __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__buf, __size, __a))
01570         }
01571             
01572 
01573         // Concatenation of nonempty strings.
01574         // Always builds a concatenation node.
01575         // Rebalances if the result is too deep.
01576         // Result has refcount 1.
01577         // Does not increment left and right ref counts even though
01578         // they are referenced.
01579         static _RopeRep*
01580         _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
01581 
01582         // Concatenation helper functions
01583         static _RopeLeaf*
01584         _S_leaf_concat_char_iter(_RopeLeaf* __r,
01585                                  const _CharT* __iter, size_t __slen);
01586                 // Concatenate by copying leaf.
01587                 // should take an arbitrary iterator
01588                 // result has refcount 1.
01589 #       ifndef __GC
01590           static _RopeLeaf* _S_destr_leaf_concat_char_iter
01591                         (_RopeLeaf* __r, const _CharT* __iter, size_t __slen);
01592           // A version that potentially clobbers __r if __r->_M_ref_count == 1.
01593 #       endif
01594 
01595         private:
01596 
01597         static size_t _S_char_ptr_len(const _CharT* __s);
01598                         // slightly generalized strlen
01599 
01600         rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
01601           : _Base(__t,__a) { }
01602 
01603 
01604         // Copy __r to the _CharT buffer.
01605         // Returns __buffer + __r->_M_size.
01606         // Assumes that buffer is uninitialized.
01607         static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
01608 
01609         // Again, with explicit starting position and length.
01610         // Assumes that buffer is uninitialized.
01611         static _CharT* _S_flatten(_RopeRep* __r,
01612                                   size_t __start, size_t __len,
01613                                   _CharT* __buffer);
01614 
01615         static const unsigned long 
01616           _S_min_len[_RopeRep::_S_max_rope_depth + 1];
01617 
01618         static bool _S_is_balanced(_RopeRep* __r)
01619                 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
01620 
01621         static bool _S_is_almost_balanced(_RopeRep* __r)
01622                 { return (__r->_M_depth == 0 ||
01623                           __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
01624 
01625         static bool _S_is_roughly_balanced(_RopeRep* __r)
01626                 { return (__r->_M_depth <= 1 ||
01627                           __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
01628 
01629         // Assumes the result is not empty.
01630         static _RopeRep* _S_concat_and_set_balanced(_RopeRep* __left,
01631                                                      _RopeRep* __right)
01632         {
01633             _RopeRep* __result = _S_concat(__left, __right);
01634             if (_S_is_balanced(__result)) __result->_M_is_balanced = true;
01635             return __result;
01636         }
01637 
01638         // The basic rebalancing operation.  Logically copies the
01639         // rope.  The result has refcount of 1.  The client will
01640         // usually decrement the reference count of __r.
01641         // The result is within height 2 of balanced by the above
01642         // definition.
01643         static _RopeRep* _S_balance(_RopeRep* __r);
01644 
01645         // Add all unbalanced subtrees to the forest of balanceed trees.
01646         // Used only by balance.
01647         static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
01648         
01649         // Add __r to forest, assuming __r is already balanced.
01650         static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
01651 
01652         // Print to stdout, exposing structure
01653         static void _S_dump(_RopeRep* __r, int __indent = 0);
01654 
01655         // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
01656         static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
01657 
01658    public:
01659         bool empty() const { return 0 == _M_tree_ptr; }
01660 
01661         // Comparison member function.  This is public only for those
01662         // clients that need a ternary comparison.  Others
01663         // should use the comparison operators below.
01664         int compare(const rope& __y) const {
01665             return _S_compare(_M_tree_ptr, __y._M_tree_ptr);
01666         }
01667 
01668         rope(const _CharT* __s, const allocator_type& __a = allocator_type())
01669         : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
01670                                                  __a),__a)
01671         { }
01672 
01673         rope(const _CharT* __s, size_t __len,
01674              const allocator_type& __a = allocator_type())
01675         : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
01676         { }
01677 
01678         // Should perhaps be templatized with respect to the iterator type
01679         // and use Sequence_buffer.  (It should perhaps use sequence_buffer
01680         // even now.)
01681         rope(const _CharT *__s, const _CharT *__e,
01682              const allocator_type& __a = allocator_type())
01683         : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
01684         { }
01685 
01686         rope(const const_iterator& __s, const const_iterator& __e,
01687              const allocator_type& __a = allocator_type())
01688         : _Base(_S_substring(__s._M_root, __s._M_current_pos,
01689                              __e._M_current_pos), __a)
01690         { }
01691 
01692         rope(const iterator& __s, const iterator& __e,
01693              const allocator_type& __a = allocator_type())
01694         : _Base(_S_substring(__s._M_root, __s._M_current_pos,
01695                              __e._M_current_pos), __a)
01696         { }
01697 
01698         rope(_CharT __c, const allocator_type& __a = allocator_type())
01699         : _Base(__a)
01700         {
01701             _CharT* __buf = _Data_allocate(_S_rounded_up_size(1));
01702 
01703             construct(__buf, __c);
01704             __STL_TRY {
01705                 _M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a);
01706             }
01707             __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__buf, 1, __a))
01708         }
01709 
01710         rope(size_t __n, _CharT __c,
01711              const allocator_type& __a = allocator_type());
01712 
01713         rope(const allocator_type& __a = allocator_type())
01714         : _Base(0, __a) {}
01715 
01716         // Construct a rope from a function that can compute its members
01717         rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
01718              const allocator_type& __a = allocator_type())
01719             : _Base(__a)
01720         {
01721             _M_tree_ptr = (0 == __len) ?
01722                0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
01723         }
01724 
01725         rope(const rope& __x, const allocator_type& __a = allocator_type())
01726         : _Base(__x._M_tree_ptr, __a)
01727         {
01728             _S_ref(_M_tree_ptr);
01729         }
01730 
01731         ~rope()
01732         {
01733             _S_unref(_M_tree_ptr);
01734         }
01735 
01736         rope& operator=(const rope& __x)
01737         {
01738             _RopeRep* __old = _M_tree_ptr;
01739 #           ifdef __STL_USE_STD_ALLOCATORS
01740               __stl_assert(get_allocator() == __x.get_allocator());
01741 #           endif
01742             _M_tree_ptr = __x._M_tree_ptr;
01743             _S_ref(_M_tree_ptr);
01744             _S_unref(__old);
01745             return(*this);
01746         }
01747 
01748         void clear()
01749         {
01750             _S_unref(_M_tree_ptr);
01751             _M_tree_ptr = 0;
01752         }
01753 
01754         void push_back(_CharT __x)
01755         {
01756             _RopeRep* __old = _M_tree_ptr;
01757             _M_tree_ptr = _S_destr_concat_char_iter(_M_tree_ptr, &__x, 1);
01758             _S_unref(__old);
01759         }
01760 
01761         void pop_back()
01762         {
01763             _RopeRep* __old = _M_tree_ptr;
01764             _M_tree_ptr = 
01765               _S_substring(_M_tree_ptr, 0, _M_tree_ptr->_M_size - 1);
01766             _S_unref(__old);
01767         }
01768 
01769         _CharT back() const
01770         {
01771             return _S_fetch(_M_tree_ptr, _M_tree_ptr->_M_size - 1);
01772         }
01773 
01774         void push_front(_CharT __x)
01775         {
01776             _RopeRep* __old = _M_tree_ptr;
01777             _RopeRep* __left =
01778               __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, get_allocator());
01779             __STL_TRY {
01780               _M_tree_ptr = _S_concat(__left, _M_tree_ptr);
01781               _S_unref(__old);
01782               _S_unref(__left);
01783             }
01784             __STL_UNWIND(_S_unref(__left))
01785         }
01786 
01787         void pop_front()
01788         {
01789             _RopeRep* __old = _M_tree_ptr;
01790             _M_tree_ptr = _S_substring(_M_tree_ptr, 1, _M_tree_ptr->_M_size);
01791             _S_unref(__old);
01792         }
01793 
01794         _CharT front() const
01795         {
01796             return _S_fetch(_M_tree_ptr, 0);
01797         }
01798 
01799         void balance()
01800         {
01801             _RopeRep* __old = _M_tree_ptr;
01802             _M_tree_ptr = _S_balance(_M_tree_ptr);
01803             _S_unref(__old);
01804         }
01805 
01806         void copy(_CharT* __buffer) const {
01807             destroy(__buffer, __buffer + size());
01808             _S_flatten(_M_tree_ptr, __buffer);
01809         }
01810 
01811         // This is the copy function from the standard, but
01812         // with the arguments reordered to make it consistent with the
01813         // rest of the interface.
01814         // Note that this guaranteed not to compile if the draft standard
01815         // order is assumed.
01816         size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const 
01817         {
01818             size_t __size = size();
01819             size_t __len = (__pos + __n > __size? __size - __pos : __n);
01820 
01821             destroy(__buffer, __buffer + __len);
01822             _S_flatten(_M_tree_ptr, __pos, __len, __buffer);
01823             return __len;
01824         }
01825 
01826         // Print to stdout, exposing structure.  May be useful for
01827         // performance debugging.
01828         void dump() {
01829             _S_dump(_M_tree_ptr);
01830         }
01831 
01832         // Convert to 0 terminated string in new allocated memory.
01833         // Embedded 0s in the input do not terminate the copy.
01834         const _CharT* c_str() const;
01835 
01836         // As above, but lso use the flattened representation as the
01837         // the new rope representation.
01838         const _CharT* replace_with_c_str();
01839 
01840         // Reclaim memory for the c_str generated flattened string.
01841         // Intentionally undocumented, since it's hard to say when this
01842         // is safe for multiple threads.
01843         void delete_c_str () {
01844             if (0 == _M_tree_ptr) return;
01845             if (_RopeRep::_S_leaf == _M_tree_ptr->_M_tag && 
01846                 ((_RopeLeaf*)_M_tree_ptr)->_M_data == 
01847                       _M_tree_ptr->_M_c_string) {
01848                 // Representation shared
01849                 return;
01850             }
01851 #           ifndef __GC
01852               _M_tree_ptr->_M_free_c_string();
01853 #           endif
01854             _M_tree_ptr->_M_c_string = 0;
01855         }
01856 
01857         _CharT operator[] (size_type __pos) const {
01858             return _S_fetch(_M_tree_ptr, __pos);
01859         }
01860 
01861         _CharT at(size_type __pos) const {
01862            // if (__pos >= size()) throw out_of_range;  // XXX
01863            return (*this)[__pos];
01864         }
01865 
01866         const_iterator begin() const {
01867             return(const_iterator(_M_tree_ptr, 0));
01868         }
01869 
01870         // An easy way to get a const iterator from a non-const container.
01871         const_iterator const_begin() const {
01872             return(const_iterator(_M_tree_ptr, 0));
01873         }
01874 
01875         const_iterator end() const {
01876             return(const_iterator(_M_tree_ptr, size()));
01877         }
01878 
01879         const_iterator const_end() const {
01880             return(const_iterator(_M_tree_ptr, size()));
01881         }
01882 
01883         size_type size() const { 
01884             return(0 == _M_tree_ptr? 0 : _M_tree_ptr->_M_size);
01885         }
01886 
01887         size_type length() const {
01888             return size();
01889         }
01890 
01891         size_type max_size() const {
01892             return _S_min_len[_RopeRep::_S_max_rope_depth-1] - 1;
01893             //  Guarantees that the result can be sufficirntly
01894             //  balanced.  Longer ropes will probably still work,
01895             //  but it's harder to make guarantees.
01896         }
01897 
01898 #     ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
01899         typedef reverse_iterator<const_iterator> const_reverse_iterator;
01900 #     else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
01901         typedef reverse_iterator<const_iterator, value_type, const_reference,
01902                                  difference_type>  const_reverse_iterator;
01903 #     endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 
01904 
01905         const_reverse_iterator rbegin() const {
01906             return const_reverse_iterator(end());
01907         }
01908 
01909         const_reverse_iterator const_rbegin() const {
01910             return const_reverse_iterator(end());
01911         }
01912 
01913         const_reverse_iterator rend() const {
01914             return const_reverse_iterator(begin());
01915         }
01916 
01917         const_reverse_iterator const_rend() const {
01918             return const_reverse_iterator(begin());
01919         }
01920 
01921 #if defined(__STL_MEMBER_TEMPLATES) && defined(__STL_FUNCTION_TMPL_PARTIAL_ORDER)
01922         template<class _CharT2, class _Alloc2>
01923         friend rope<_CharT2,_Alloc2>
01924         operator+ (const rope<_CharT2,_Alloc2>& __left,
01925                    const rope<_CharT2,_Alloc2>& __right);
01926         
01927         template<class _CharT2, class _Alloc2>
01928         friend rope<_CharT2,_Alloc2>
01929         operator+ (const rope<_CharT2,_Alloc2>& __left,
01930                    const _CharT2* __right);
01931         
01932         template<class _CharT2, class _Alloc2>
01933         friend rope<_CharT2,_Alloc2>
01934         operator+ (const rope<_CharT2,_Alloc2>& __left, _CharT2 __right);
01935 #else
01936         friend rope<_CharT,_Alloc> __STD_QUALIFIER
01937         operator+ __STL_NULL_TMPL_ARGS (const rope<_CharT,_Alloc>& __left,
01938                                         const rope<_CharT,_Alloc>& __right);
01939         
01940         friend rope<_CharT,_Alloc> __STD_QUALIFIER
01941         operator+ __STL_NULL_TMPL_ARGS (const rope<_CharT,_Alloc>& __left,
01942                                         const _CharT* __right);
01943         
01944         friend rope<_CharT,_Alloc> __STD_QUALIFIER
01945         operator+ __STL_NULL_TMPL_ARGS (const rope<_CharT,_Alloc>& __left,
01946                                         _CharT __right);
01947 #endif        
01948         // The symmetric cases are intentionally omitted, since they're presumed
01949         // to be less common, and we don't handle them as well.
01950 
01951         // The following should really be templatized.
01952         // The first argument should be an input iterator or
01953         // forward iterator with value_type _CharT.
01954         rope& append(const _CharT* __iter, size_t __n) {
01955             _RopeRep* __result = 
01956               _S_destr_concat_char_iter(_M_tree_ptr, __iter, __n);
01957             _S_unref(_M_tree_ptr);
01958             _M_tree_ptr = __result;
01959             return *this;
01960         }
01961 
01962         rope& append(const _CharT* __c_string) {
01963             size_t __len = _S_char_ptr_len(__c_string);
01964             append(__c_string, __len);
01965             return(*this);
01966         }
01967 
01968         rope& append(const _CharT* __s, const _CharT* __e) {
01969             _RopeRep* __result =
01970                 _S_destr_concat_char_iter(_M_tree_ptr, __s, __e - __s);
01971             _S_unref(_M_tree_ptr);
01972             _M_tree_ptr = __result;
01973             return *this;
01974         }
01975 
01976         rope& append(const_iterator __s, const_iterator __e) {
01977             __stl_assert(__s._M_root == __e._M_root);
01978 #           ifdef __STL_USE_STD_ALLOCATORS
01979                 __stl_assert(get_allocator() == __s._M_root->get_allocator());
01980 #           endif
01981             _Self_destruct_ptr __appendee(_S_substring(
01982               __s._M_root, __s._M_current_pos, __e._M_current_pos));
01983             _RopeRep* __result = 
01984               _S_concat(_M_tree_ptr, (_RopeRep*)__appendee);
01985             _S_unref(_M_tree_ptr);
01986             _M_tree_ptr = __result;
01987             return *this;
01988         }
01989 
01990         rope& append(_CharT __c) {
01991             _RopeRep* __result = 
01992               _S_destr_concat_char_iter(_M_tree_ptr, &__c, 1);
01993             _S_unref(_M_tree_ptr);
01994             _M_tree_ptr = __result;
01995             return *this;
01996         }
01997 
01998         rope& append() { return append(_CharT()); }  // XXX why?
01999 
02000         rope& append(const rope& __y) {
02001 #           ifdef __STL_USE_STD_ALLOCATORS
02002               __stl_assert(__y.get_allocator() == get_allocator());
02003 #           endif
02004             _RopeRep* __result = _S_concat(_M_tree_ptr, __y._M_tree_ptr);
02005             _S_unref(_M_tree_ptr);
02006             _M_tree_ptr = __result;
02007             return *this;
02008         }
02009 
02010         rope& append(size_t __n, _CharT __c) {
02011             rope<_CharT,_Alloc> __last(__n, __c);
02012             return append(__last);
02013         }
02014 
02015         void swap(rope& __b) {
02016 #           ifdef __STL_USE_STD_ALLOCATORS
02017                 __stl_assert(get_allocator() == __b.get_allocator());
02018 #           endif
02019             _RopeRep* __tmp = _M_tree_ptr;
02020             _M_tree_ptr = __b._M_tree_ptr;
02021             __b._M_tree_ptr = __tmp;
02022         }
02023 
02024 
02025     protected:
02026         // Result is included in refcount.
02027         static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
02028                                   size_t __pos2, _RopeRep* __r) {
02029             if (0 == __old) { _S_ref(__r); return __r; }
02030             _Self_destruct_ptr __left(
02031               _S_substring(__old, 0, __pos1));
02032             _Self_destruct_ptr __right(
02033               _S_substring(__old, __pos2, __old->_M_size));
02034             _RopeRep* __result;
02035 
02036 #           ifdef __STL_USE_STD_ALLOCATORS
02037                 __stl_assert(__old->get_allocator() == __r->get_allocator());
02038 #           endif
02039             if (0 == __r) {
02040                 __result = _S_concat(__left, __right);
02041             } else {
02042                 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
02043                 __result = _S_concat(__left_result, __right);
02044             }
02045             return __result;
02046         }
02047 
02048     public:
02049         void insert(size_t __p, const rope& __r) {
02050             _RopeRep* __result = 
02051               replace(_M_tree_ptr, __p, __p, __r._M_tree_ptr);
02052 #           ifdef __STL_USE_STD_ALLOCATORS
02053                 __stl_assert(get_allocator() == __r.get_allocator());
02054 #           endif
02055             _S_unref(_M_tree_ptr);
02056             _M_tree_ptr = __result;
02057         }
02058 
02059         void insert(size_t __p, size_t __n, _CharT __c) {
02060             rope<_CharT,_Alloc> __r(__n,__c);
02061             insert(__p, __r);
02062         }
02063 
02064         void insert(size_t __p, const _CharT* __i, size_t __n) {
02065             _Self_destruct_ptr __left(_S_substring(_M_tree_ptr, 0, __p));
02066             _Self_destruct_ptr __right(_S_substring(_M_tree_ptr, __p, size()));
02067             _Self_destruct_ptr __left_result(
02068               _S_concat_char_iter(__left, __i, __n));
02069                 // _S_ destr_concat_char_iter should be safe here.
02070                 // But as it stands it's probably not a win, since __left
02071                 // is likely to have additional references.
02072             _RopeRep* __result = _S_concat(__left_result, __right);
02073             _S_unref(_M_tree_ptr);
02074             _M_tree_ptr = __result;
02075         }
02076 
02077         void insert(size_t __p, const _CharT* __c_string) {
02078             insert(__p, __c_string, _S_char_ptr_len(__c_string));
02079         }
02080 
02081         void insert(size_t __p, _CharT __c) {
02082             insert(__p, &__c, 1);
02083         }
02084 
02085         void insert(size_t __p) {
02086             _CharT __c = _CharT();
02087             insert(__p, &__c, 1);
02088         }
02089 
02090         void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
02091             rope __r(__i, __j);
02092             insert(__p, __r);
02093         }
02094 
02095         void insert(size_t __p, const const_iterator& __i,
02096                               const const_iterator& __j) {
02097             rope __r(__i, __j);
02098             insert(__p, __r);
02099         }
02100 
02101         void insert(size_t __p, const iterator& __i,
02102                               const iterator& __j) {
02103             rope __r(__i, __j);
02104             insert(__p, __r);
02105         }
02106 
02107         // (position, length) versions of replace operations:
02108 
02109         void replace(size_t __p, size_t __n, const rope& __r) {
02110             _RopeRep* __result = 
02111               replace(_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
02112             _S_unref(_M_tree_ptr);
02113             _M_tree_ptr = __result;
02114         }
02115 
02116         void replace(size_t __p, size_t __n, 
02117                      const _CharT* __i, size_t __i_len) {
02118             rope __r(__i, __i_len);
02119             replace(__p, __n, __r);
02120         }
02121 
02122         void replace(size_t __p, size_t __n, _CharT __c) {
02123             rope __r(__c);
02124             replace(__p, __n, __r);
02125         }
02126 
02127         void replace(size_t __p, size_t __n, const _CharT* __c_string) {
02128             rope __r(__c_string);
02129             replace(__p, __n, __r);
02130         }
02131 
02132         void replace(size_t __p, size_t __n, 
02133                      const _CharT* __i, const _CharT* __j) {
02134             rope __r(__i, __j);
02135             replace(__p, __n, __r);
02136         }
02137 
02138         void replace(size_t __p, size_t __n,
02139                      const const_iterator& __i, const const_iterator& __j) {
02140             rope __r(__i, __j);
02141             replace(__p, __n, __r);
02142         }
02143 
02144         void replace(size_t __p, size_t __n,
02145                      const iterator& __i, const iterator& __j) {
02146             rope __r(__i, __j);
02147             replace(__p, __n, __r);
02148         }
02149 
02150         // Single character variants:
02151         void replace(size_t __p, _CharT __c) {
02152             iterator __i(this, __p);
02153             *__i = __c;
02154         }
02155 
02156         void replace(size_t __p, const rope& __r) {
02157             replace(__p, 1, __r);
02158         }
02159 
02160         void replace(size_t __p, const _CharT* __i, size_t __i_len) {
02161             replace(__p, 1, __i, __i_len);
02162         }
02163 
02164         void replace(size_t __p, const _CharT* __c_string) {
02165             replace(__p, 1, __c_string);
02166         }
02167 
02168         void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
02169             replace(__p, 1, __i, __j);
02170         }
02171 
02172         void replace(size_t __p, const const_iterator& __i,
02173                                const const_iterator& __j) {
02174             replace(__p, 1, __i, __j);
02175         }
02176 
02177         void replace(size_t __p, const iterator& __i,
02178                                const iterator& __j) {
02179             replace(__p, 1, __i, __j);
02180         }
02181 
02182         // Erase, (position, size) variant.
02183         void erase(size_t __p, size_t __n) {
02184             _RopeRep* __result = replace(_M_tree_ptr, __p, __p + __n, 0);
02185             _S_unref(_M_tree_ptr);
02186             _M_tree_ptr = __result;
02187         }
02188 
02189         // Erase, single character
02190         void erase(size_t __p) {
02191             erase(__p, __p + 1);
02192         }
02193 
02194         // Insert, iterator variants.  
02195         iterator insert(const iterator& __p, const rope& __r)
02196                 { insert(__p.index(), __r); return __p; }
02197         iterator insert(const iterator& __p, size_t __n, _CharT __c)
02198                 { insert(__p.index(), __n, __c); return __p; }
02199         iterator insert(const iterator& __p, _CharT __c) 
02200                 { insert(__p.index(), __c); return __p; }
02201         iterator insert(const iterator& __p ) 
02202                 { insert(__p.index()); return __p; }
02203         iterator insert(const iterator& __p, const _CharT* c_string) 
02204                 { insert(__p.index(), c_string); return __p; }
02205         iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
02206                 { insert(__p.index(), __i, __n); return __p; }
02207         iterator insert(const iterator& __p, const _CharT* __i, 
02208                         const _CharT* __j)
02209                 { insert(__p.index(), __i, __j);  return __p; }
02210         iterator insert(const iterator& __p,
02211                         const const_iterator& __i, const const_iterator& __j)
02212                 { insert(__p.index(), __i, __j); return __p; }
02213         iterator insert(const iterator& __p,
02214                         const iterator& __i, const iterator& __j)
02215                 { insert(__p.index(), __i, __j); return __p; }
02216 
02217         // Replace, range variants.
02218         void replace(const iterator& __p, const iterator& __q,
02219                      const rope& __r)
02220                 { replace(__p.index(), __q.index() - __p.index(), __r); }
02221         void replace(const iterator& __p, const iterator& __q, _CharT __c)
02222                 { replace(__p.index(), __q.index() - __p.index(), __c); }
02223         void replace(const iterator& __p, const iterator& __q,
02224                      const _CharT* __c_string)
02225                 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
02226         void replace(const iterator& __p, const iterator& __q,
02227                      const _CharT* __i, size_t __n)
02228                 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
02229         void replace(const iterator& __p, const iterator& __q,
02230                      const _CharT* __i, const _CharT* __j)
02231                 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
02232         void replace(const iterator& __p, const iterator& __q,
02233                      const const_iterator& __i, const const_iterator& __j)
02234                 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
02235         void replace(const iterator& __p, const iterator& __q,
02236                      const iterator& __i, const iterator& __j)
02237                 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
02238 
02239         // Replace, iterator variants.
02240         void replace(const iterator& __p, const rope& __r)
02241                 { replace(__p.index(), __r); }
02242         void replace(const iterator& __p, _CharT __c)
02243                 { replace(__p.index(), __c); }
02244         void replace(const iterator& __p, const _CharT* __c_string)
02245                 { replace(__p.index(), __c_string); }
02246         void replace(const iterator& __p, const _CharT* __i, size_t __n)
02247                 { replace(__p.index(), __i, __n); }
02248         void replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
02249                 { replace(__p.index(), __i, __j); }
02250         void replace(const iterator& __p, const_iterator __i, 
02251                      const_iterator __j)
02252                 { replace(__p.index(), __i, __j); }
02253         void replace(const iterator& __p, iterator __i, iterator __j)
02254                 { replace(__p.index(), __i, __j); }
02255 
02256         // Iterator and range variants of erase
02257         iterator erase(const iterator& __p, const iterator& __q) {
02258             size_t __p_index = __p.index();
02259             erase(__p_index, __q.index() - __p_index);
02260             return iterator(this, __p_index);
02261         }
02262         iterator erase(const iterator& __p) {
02263             size_t __p_index = __p.index();
02264             erase(__p_index, 1);
02265             return iterator(this, __p_index);
02266         }
02267 
02268         rope substr(size_t __start, size_t __len = 1) const {
02269             return rope<_CharT,_Alloc>(
02270                         _S_substring(_M_tree_ptr, __start, __start + __len));
02271         }
02272 
02273         rope substr(iterator __start, iterator __end) const {
02274             return rope<_CharT,_Alloc>(
02275                 _S_substring(_M_tree_ptr, __start.index(), __end.index()));
02276         }
02277         
02278         rope substr(iterator __start) const {
02279             size_t __pos = __start.index();
02280             return rope<_CharT,_Alloc>(
02281                         _S_substring(_M_tree_ptr, __pos, __pos + 1));
02282         }
02283         
02284         rope substr(const_iterator __start, const_iterator __end) const {
02285             // This might eventually take advantage of the cache in the
02286             // iterator.
02287             return rope<_CharT,_Alloc>(
02288               _S_substring(_M_tree_ptr, __start.index(), __end.index()));
02289         }
02290 
02291         rope<_CharT,_Alloc> substr(const_iterator __start) {
02292             size_t __pos = __start.index();
02293             return rope<_CharT,_Alloc>(
02294               _S_substring(_M_tree_ptr, __pos, __pos + 1));
02295         }
02296 
02297         static const size_type npos;
02298 
02299         size_type find(_CharT __c, size_type __pos = 0) const;
02300         size_type find(const _CharT* __s, size_type __pos = 0) const {
02301             size_type __result_pos;
02302             const_iterator __result = search(const_begin() + __pos, const_end(),
02303                                            __s, __s + _S_char_ptr_len(__s));
02304             __result_pos = __result.index();
02305 #           ifndef __STL_OLD_ROPE_SEMANTICS
02306                 if (__result_pos == size()) __result_pos = npos;
02307 #           endif
02308             return __result_pos;
02309         }
02310 
02311         iterator mutable_begin() {
02312             return(iterator(this, 0));
02313         }
02314 
02315         iterator mutable_end() {
02316             return(iterator(this, size()));
02317         }
02318 
02319 #     ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
02320         typedef reverse_iterator<iterator> reverse_iterator;
02321 #     else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
02322         typedef reverse_iterator<iterator, value_type, reference,
02323                                  difference_type>  reverse_iterator;
02324 #     endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 
02325 
02326         reverse_iterator mutable_rbegin() {
02327             return reverse_iterator(mutable_end());
02328         }
02329 
02330         reverse_iterator mutable_rend() {
02331             return reverse_iterator(mutable_begin());
02332         }
02333 
02334         reference mutable_reference_at(size_type __pos) {
02335             return reference(this, __pos);
02336         }
02337 
02338 #       ifdef __STD_STUFF
02339             reference operator[] (size_type __pos) {
02340                 return _char_ref_proxy(this, __pos);
02341             }
02342 
02343             reference at(size_type __pos) {
02344                 // if (__pos >= size()) throw out_of_range;  // XXX
02345                 return (*this)[__pos];
02346             }
02347 
02348             void resize(size_type __n, _CharT __c) {}
02349             void resize(size_type __n) {}
02350             void reserve(size_type __res_arg = 0) {}
02351             size_type capacity() const {
02352                 return max_size();
02353             }
02354 
02355           // Stuff below this line is dangerous because it's error prone.
02356           // I would really like to get rid of it.
02357             // copy function with funny arg ordering.
02358               size_type copy(_CharT* __buffer, size_type __n, 
02359                              size_type __pos = 0) const {
02360                 return copy(__pos, __n, __buffer);
02361               }
02362 
02363             iterator end() { return mutable_end(); }
02364 
02365             iterator begin() { return mutable_begin(); }
02366 
02367             reverse_iterator rend() { return mutable_rend(); }
02368 
02369             reverse_iterator rbegin() { return mutable_rbegin(); }
02370 
02371 #       else
02372 
02373             const_iterator end() { return const_end(); }
02374 
02375             const_iterator begin() { return const_begin(); }
02376 
02377             const_reverse_iterator rend() { return const_rend(); }
02378   
02379             const_reverse_iterator rbegin() { return const_rbegin(); }
02380 
02381 #       endif
02382         
02383 };
02384 
02385 template <class _CharT, class _Alloc>
02386 const rope<_CharT, _Alloc>::size_type rope<_CharT, _Alloc>::npos =
02387                         (size_type)(-1);
02388 
02389 template <class _CharT, class _Alloc>
02390 inline bool operator== (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02391                         const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02392   return (__x._M_current_pos == __y._M_current_pos && 
02393           __x._M_root == __y._M_root);
02394 }
02395 
02396 template <class _CharT, class _Alloc>
02397 inline bool operator< (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02398                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02399   return (__x._M_current_pos < __y._M_current_pos);
02400 }
02401 
02402 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
02403 
02404 template <class _CharT, class _Alloc>
02405 inline bool operator!= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02406                         const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02407   return !(__x == __y);
02408 }
02409 
02410 template <class _CharT, class _Alloc>
02411 inline bool operator> (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02412                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02413   return __y < __x;
02414 }
02415 
02416 template <class _CharT, class _Alloc>
02417 inline bool operator<= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02418                         const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02419   return !(__y < __x);
02420 }
02421 
02422 template <class _CharT, class _Alloc>
02423 inline bool operator>= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
02424                         const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02425   return !(__x < __y);
02426 }
02427 
02428 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
02429 
02430 template <class _CharT, class _Alloc>
02431 inline ptrdiff_t operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x,
02432                            const _Rope_const_iterator<_CharT,_Alloc>& __y) {
02433   return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
02434 }
02435 
02436 template <class _CharT, class _Alloc>
02437 inline _Rope_const_iterator<_CharT,_Alloc>
02438 operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
02439   return _Rope_const_iterator<_CharT,_Alloc>(
02440             __x._M_root, __x._M_current_pos - __n);
02441 }
02442 
02443 template <class _CharT, class _Alloc>
02444 inline _Rope_const_iterator<_CharT,_Alloc>
02445 operator+(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
02446   return _Rope_const_iterator<_CharT,_Alloc>(
02447            __x._M_root, __x._M_current_pos + __n);
02448 }
02449 
02450 template <class _CharT, class _Alloc>
02451 inline _Rope_const_iterator<_CharT,_Alloc>
02452 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT,_Alloc>& __x) {
02453   return _Rope_const_iterator<_CharT,_Alloc>(
02454            __x._M_root, __x._M_current_pos + __n);
02455 }
02456 
02457 template <class _CharT, class _Alloc>
02458 inline bool operator== (const _Rope_iterator<_CharT,_Alloc>& __x,
02459                         const _Rope_iterator<_CharT,_Alloc>& __y) {
02460   return (__x._M_current_pos == __y._M_current_pos && 
02461           __x._M_root_rope == __y._M_root_rope);
02462 }
02463 
02464 template <class _CharT, class _Alloc>
02465 inline bool operator< (const _Rope_iterator<_CharT,_Alloc>& __x,
02466                        const _Rope_iterator<_CharT,_Alloc>& __y) {
02467   return (__x._M_current_pos < __y._M_current_pos);
02468 }
02469 
02470 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
02471 
02472 template <class _CharT, class _Alloc>
02473 inline bool operator!= (const _Rope_iterator<_CharT,_Alloc>& __x,
02474                         const _Rope_iterator<_CharT,_Alloc>& __y) {
02475   return !(__x == __y);
02476 }
02477 
02478 template <class _CharT, class _Alloc>
02479 inline bool operator> (const _Rope_iterator<_CharT,_Alloc>& __x,
02480                        const _Rope_iterator<_CharT,_Alloc>& __y) {
02481   return __y < __x;
02482 }
02483 
02484 template <class _CharT, class _Alloc>
02485 inline bool operator<= (const _Rope_iterator<_CharT,_Alloc>& __x,
02486                         const _Rope_iterator<_CharT,_Alloc>& __y) {
02487   return !(__y < __x);
02488 }
02489 
02490 template <class _CharT, class _Alloc>
02491 inline bool operator>= (const _Rope_iterator<_CharT,_Alloc>& __x,
02492                         const _Rope_iterator<_CharT,_Alloc>& __y) {
02493   return !(__x < __y);
02494 }
02495 
02496 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
02497 
02498 template <class _CharT, class _Alloc>
02499 inline ptrdiff_t operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
02500                            const _Rope_iterator<_CharT,_Alloc>& __y) {
02501   return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
02502 }
02503 
02504 template <class _CharT, class _Alloc>
02505 inline _Rope_iterator<_CharT,_Alloc>
02506 operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
02507           ptrdiff_t __n) {
02508   return _Rope_iterator<_CharT,_Alloc>(
02509     __x._M_root_rope, __x._M_current_pos - __n);
02510 }
02511 
02512 template <class _CharT, class _Alloc>
02513 inline _Rope_iterator<_CharT,_Alloc>
02514 operator+(const _Rope_iterator<_CharT,_Alloc>& __x,
02515           ptrdiff_t __n) {
02516   return _Rope_iterator<_CharT,_Alloc>(
02517     __x._M_root_rope, __x._M_current_pos + __n);
02518 }
02519 
02520 template <class _CharT, class _Alloc>
02521 inline _Rope_iterator<_CharT,_Alloc>
02522 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT,_Alloc>& __x) {
02523   return _Rope_iterator<_CharT,_Alloc>(
02524     __x._M_root_rope, __x._M_current_pos + __n);
02525 }
02526 
02527 template <class _CharT, class _Alloc>
02528 inline
02529 rope<_CharT,_Alloc>
02530 operator+ (const rope<_CharT,_Alloc>& __left,
02531            const rope<_CharT,_Alloc>& __right)
02532 {
02533 #   ifdef __STL_USE_STD_ALLOCATORS
02534         __stl_assert(__left.get_allocator() == __right.get_allocator());
02535 #   endif
02536     return rope<_CharT,_Alloc>(
02537       rope<_CharT,_Alloc>::_S_concat(__left._M_tree_ptr, __right._M_tree_ptr));
02538     // Inlining this should make it possible to keep __left and
02539     // __right in registers.
02540 }
02541 
02542 template <class _CharT, class _Alloc>
02543 inline
02544 rope<_CharT,_Alloc>&
02545 operator+= (rope<_CharT,_Alloc>& __left, 
02546       const rope<_CharT,_Alloc>& __right)
02547 {
02548     __left.append(__right);
02549     return __left;
02550 }
02551 
02552 template <class _CharT, class _Alloc>
02553 inline
02554 rope<_CharT,_Alloc>
02555 operator+ (const rope<_CharT,_Alloc>& __left,
02556            const _CharT* __right) {
02557     size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
02558     return rope<_CharT,_Alloc>(
02559       rope<_CharT,_Alloc>::_S_concat_char_iter(
02560         __left._M_tree_ptr, __right, __rlen)); 
02561 }
02562 
02563 template <class _CharT, class _Alloc>
02564 inline
02565 rope<_CharT,_Alloc>&
02566 operator+= (rope<_CharT,_Alloc>& __left,
02567             const _CharT* __right) {
02568     __left.append(__right);
02569     return __left;
02570 }
02571 
02572 template <class _CharT, class _Alloc>
02573 inline
02574 rope<_CharT,_Alloc>
02575 operator+ (const rope<_CharT,_Alloc>& __left, _CharT __right) {
02576     return rope<_CharT,_Alloc>(
02577       rope<_CharT,_Alloc>::_S_concat_char_iter(
02578         __left._M_tree_ptr, &__right, 1));
02579 }
02580 
02581 template <class _CharT, class _Alloc>
02582 inline
02583 rope<_CharT,_Alloc>&
02584 operator+= (rope<_CharT,_Alloc>& __left, _CharT __right) {
02585     __left.append(__right);
02586     return __left;
02587 }
02588 
02589 template <class _CharT, class _Alloc>
02590 bool
02591 operator< (const rope<_CharT,_Alloc>& __left, 
02592            const rope<_CharT,_Alloc>& __right) {
02593     return __left.compare(__right) < 0;
02594 }
02595         
02596 template <class _CharT, class _Alloc>
02597 bool
02598 operator== (const rope<_CharT,_Alloc>& __left, 
02599             const rope<_CharT,_Alloc>& __right) {
02600     return __left.compare(__right) == 0;
02601 }
02602 
02603 template <class _CharT, class _Alloc>
02604 inline bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
02605                         const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
02606         return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root);
02607 }
02608 
02609 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
02610 
02611 template <class _CharT, class _Alloc>
02612 inline bool
02613 operator!= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
02614   return !(__x == __y);
02615 }
02616 
02617 template <class _CharT, class _Alloc>
02618 inline bool
02619 operator> (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
02620   return __y < __x;
02621 }
02622 
02623 template <class _CharT, class _Alloc>
02624 inline bool
02625 operator<= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
02626   return !(__y < __x);
02627 }
02628 
02629 template <class _CharT, class _Alloc>
02630 inline bool
02631 operator>= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
02632   return !(__x < __y);
02633 }
02634 
02635 template <class _CharT, class _Alloc>
02636 inline bool operator!= (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
02637                         const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
02638   return !(__x == __y);
02639 }
02640 
02641 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
02642 
02643 #ifndef UNDER_CE
02644 #ifdef __STL_USE_NEW_IOSTREAMS
02645   template<class _CharT, class _Traits, class _Alloc>
02646   basic_ostream<_CharT, _Traits>& operator<<
02647                                         (basic_ostream<_CharT, _Traits>& __o,
02648                                          const rope<_CharT, _Alloc>& __r);
02649 #else
02650   template<class _CharT, class _Alloc>
02651   ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r);
02652 #endif
02653 #endif //UNDER_CE
02654         
02655 typedef rope<char> crope;
02656 typedef rope<wchar_t> wrope;
02657 
02658 inline crope::reference __mutable_reference_at(crope& __c, size_t __i)
02659 {
02660     return __c.mutable_reference_at(__i);
02661 }
02662 
02663 inline wrope::reference __mutable_reference_at(wrope& __c, size_t __i)
02664 {
02665     return __c.mutable_reference_at(__i);
02666 }
02667 
02668 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
02669 
02670 template <class _CharT, class _Alloc>
02671 inline void swap(rope<_CharT,_Alloc>& __x, rope<_CharT,_Alloc>& __y) {
02672   __x.swap(__y);
02673 }
02674 
02675 #else
02676 
02677 inline void swap(crope __x, crope __y) { __x.swap(__y); }
02678 inline void swap(wrope __x, wrope __y) { __x.swap(__y); }
02679 
02680 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
02681 
02682 // Hash functions should probably be revisited later:
02683 __STL_TEMPLATE_NULL struct hash<crope>
02684 {
02685   size_t operator()(const crope& __str) const
02686   {
02687     size_t __size = __str.size();
02688 
02689     if (0 == __size) return 0;
02690     return 13*__str[0] + 5*__str[__size - 1] + __size;
02691   }
02692 };
02693 
02694 
02695 __STL_TEMPLATE_NULL struct hash<wrope>
02696 {
02697   size_t operator()(const wrope& __str) const
02698   {
02699     size_t __size = __str.size();
02700 
02701     if (0 == __size) return 0;
02702     return 13*__str[0] + 5*__str[__size - 1] + __size;
02703   }
02704 };
02705 
02706 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
02707 #pragma reset woff 1174
02708 #endif
02709 
02710 __STL_END_NAMESPACE
02711 
02712 # include <ropeimpl.h>
02713 
02714 # endif /* __SGI_STL_INTERNAL_ROPE_H */
02715 
02716 // Local Variables:
02717 // mode:C++
02718 // End:

Generated on Mon Jun 5 10:20:44 2006 for Intelligence.kdevelop by  doxygen 1.4.6