epoc32/include/stdapis/stlportv5/stl/_rope.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/stlport/stl/_rope.h@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/*
williamr@2
     2
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
williamr@2
     3
 * Copyright (c) 1996,1997
williamr@2
     4
 * Silicon Graphics Computer Systems, Inc.
williamr@2
     5
 *
williamr@2
     6
 * Copyright (c) 1997
williamr@2
     7
 * Moscow Center for SPARC Technology
williamr@2
     8
 *
williamr@2
     9
 * Copyright (c) 1999 
williamr@2
    10
 * Boris Fomitchev
williamr@2
    11
 *
williamr@2
    12
 * This material is provided "as is", with absolutely no warranty expressed
williamr@2
    13
 * or implied. Any use is at your own risk.
williamr@2
    14
 *
williamr@2
    15
 * Permission to use or copy this software for any purpose is hereby granted 
williamr@2
    16
 * without fee, provided the above notices are retained on all copies.
williamr@2
    17
 * Permission to modify the code and to distribute modified code is granted,
williamr@2
    18
 * provided the above notices are retained, and a notice that the code was
williamr@2
    19
 * modified is included with the above copyright notice.
williamr@2
    20
 *
williamr@2
    21
 */
williamr@2
    22
williamr@2
    23
/* NOTE: This is an internal header file, included by other STL headers.
williamr@2
    24
 *   You should not attempt to use it directly.
williamr@2
    25
 */
williamr@2
    26
williamr@2
    27
// rope<_CharT,_Alloc> is a sequence of _CharT.
williamr@2
    28
// Ropes appear to be mutable, but update operations
williamr@2
    29
// really copy enough of the data structure to leave the original
williamr@2
    30
// valid.  Thus ropes can be logically copied by just copying
williamr@2
    31
// a pointer value.
williamr@2
    32
williamr@2
    33
#ifndef _STLP_INTERNAL_ROPE_H
williamr@2
    34
# define _STLP_INTERNAL_ROPE_H
williamr@2
    35
williamr@2
    36
# ifndef _STLP_INTERNAL_ALGOBASE_H
williamr@2
    37
#  include <stl/_algobase.h>
williamr@2
    38
# endif
williamr@2
    39
williamr@2
    40
# ifndef _STLP_IOSFWD
williamr@2
    41
#  include <iosfwd>
williamr@2
    42
# endif
williamr@2
    43
williamr@2
    44
# ifndef _STLP_INTERNAL_ALLOC_H
williamr@2
    45
#  include <stl/_alloc.h>
williamr@2
    46
# endif
williamr@2
    47
williamr@2
    48
# ifndef _STLP_INTERNAL_ITERATOR_H
williamr@2
    49
#  include <stl/_iterator.h>
williamr@2
    50
# endif
williamr@2
    51
williamr@2
    52
# ifndef _STLP_INTERNAL_ALGO_H
williamr@2
    53
#  include <stl/_algo.h>
williamr@2
    54
# endif
williamr@2
    55
williamr@2
    56
# ifndef _STLP_INTERNAL_FUNCTION_H
williamr@2
    57
#  include <stl/_function.h>
williamr@2
    58
# endif
williamr@2
    59
williamr@2
    60
# ifndef _STLP_INTERNAL_NUMERIC_H
williamr@2
    61
#  include <stl/_numeric.h>
williamr@2
    62
# endif
williamr@2
    63
williamr@2
    64
# ifndef _STLP_INTERNAL_HASH_FUN_H
williamr@2
    65
#  include <stl/_hash_fun.h>
williamr@2
    66
# endif
williamr@2
    67
williamr@2
    68
# ifdef __GC
williamr@2
    69
#   define __GC_CONST const
williamr@2
    70
# else
williamr@2
    71
# include <stl/_threads.h>
williamr@2
    72
#   define __GC_CONST   // constant except for deallocation
williamr@2
    73
# endif
williamr@2
    74
# ifdef _STLP_SGI_THREADS
williamr@2
    75
#    include <mutex.h>
williamr@2
    76
# endif
williamr@2
    77
williamr@2
    78
#ifdef _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM 
williamr@2
    79
#  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) (_Alloc_traits<_Tp,__atype>::create_allocator(__a)) 
williamr@2
    80
#elif defined(__MRC__)||defined(__SC__) 
williamr@2
    81
#  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) __stl_alloc_create<_Tp,__atype>(__a,(_Tp*)0) 
williamr@2
    82
#else 
williamr@2
    83
#  define _STLP_CREATE_ALLOCATOR(__atype,__a, _Tp) __stl_alloc_create(__a,(_Tp*)0) 
williamr@2
    84
#endif 
williamr@2
    85
williamr@2
    86
_STLP_BEGIN_NAMESPACE
williamr@2
    87
williamr@2
    88
// First a lot of forward declarations.  The standard seems to require
williamr@2
    89
// much stricter "declaration before use" than many of the implementations
williamr@2
    90
// that preceded it.
williamr@2
    91
template<class _CharT, _STLP_DEFAULT_ALLOCATOR_SELECT(_CharT) > class rope;
williamr@2
    92
template<class _CharT, class _Alloc> struct _Rope_RopeConcatenation;
williamr@2
    93
template<class _CharT, class _Alloc> struct _Rope_RopeRep;
williamr@2
    94
template<class _CharT, class _Alloc> struct _Rope_RopeLeaf;
williamr@2
    95
template<class _CharT, class _Alloc> struct _Rope_RopeFunction;
williamr@2
    96
template<class _CharT, class _Alloc> struct _Rope_RopeSubstring;
williamr@2
    97
template<class _CharT, class _Alloc> class _Rope_iterator;
williamr@2
    98
template<class _CharT, class _Alloc> class _Rope_const_iterator;
williamr@2
    99
template<class _CharT, class _Alloc> class _Rope_char_ref_proxy;
williamr@2
   100
template<class _CharT, class _Alloc> class _Rope_char_ptr_proxy;
williamr@2
   101
williamr@2
   102
// Some helpers, so we can use power on ropes.
williamr@2
   103
// See below for why this isn't local to the implementation.
williamr@2
   104
williamr@2
   105
// This uses a nonstandard refcount convention.
williamr@2
   106
// The result has refcount 0.
williamr@2
   107
template<class _CharT, class _Alloc>
williamr@2
   108
struct _Rope_Concat_fn
williamr@2
   109
  : public binary_function<rope<_CharT,_Alloc>, rope<_CharT,_Alloc>,
williamr@2
   110
  rope<_CharT,_Alloc> > {
williamr@2
   111
  rope<_CharT,_Alloc> operator() (const rope<_CharT,_Alloc>& __x,
williamr@2
   112
                                  const rope<_CharT,_Alloc>& __y) {
williamr@2
   113
    return __x + __y;
williamr@2
   114
  }
williamr@2
   115
};
williamr@2
   116
williamr@2
   117
template <class _CharT, class _Alloc>
williamr@2
   118
inline
williamr@2
   119
rope<_CharT,_Alloc>
williamr@2
   120
__identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
williamr@2
   121
{
williamr@2
   122
  return rope<_CharT,_Alloc>();
williamr@2
   123
}
williamr@2
   124
williamr@2
   125
// The _S_eos function is used for those functions that
williamr@2
   126
// convert to/from C-like strings to detect the end of the string.
williamr@2
   127
williamr@2
   128
// The end-of-C-string character.
williamr@2
   129
// This is what the draft standard says it should be.
williamr@2
   130
template <class _CharT>
williamr@2
   131
inline _CharT _S_eos(_CharT*) { return _CharT(); }
williamr@2
   132
williamr@2
   133
// fbp : some compilers fail to zero-initialize builtins ;(
williamr@2
   134
inline const char _S_eos(const char*) { return 0; }
williamr@2
   135
# ifdef _STLP_HAS_WCHAR_T
williamr@2
   136
inline const wchar_t _S_eos(const wchar_t*) { return 0; }
williamr@2
   137
# endif
williamr@2
   138
williamr@2
   139
// Test for basic character types.
williamr@2
   140
// For basic character types leaves having a trailing eos.
williamr@2
   141
template <class _CharT>
williamr@2
   142
inline bool _S_is_basic_char_type(_CharT*) { return false; }
williamr@2
   143
template <class _CharT>
williamr@2
   144
inline bool _S_is_one_byte_char_type(_CharT*) { return false; }
williamr@2
   145
williamr@2
   146
inline bool _S_is_basic_char_type(char*) { return true; }
williamr@2
   147
inline bool _S_is_one_byte_char_type(char*) { return true; }
williamr@2
   148
# ifdef _STLP_HAS_WCHAR_T
williamr@2
   149
inline bool _S_is_basic_char_type(wchar_t*) { return true; }
williamr@2
   150
# endif
williamr@2
   151
williamr@2
   152
// Store an eos iff _CharT is a basic character type.
williamr@2
   153
// Do not reference _S_eos if it isn't.
williamr@2
   154
template <class _CharT>
williamr@2
   155
inline void _S_cond_store_eos(_CharT&) {}
williamr@2
   156
williamr@2
   157
inline void _S_cond_store_eos(char& __c) { __c = 0; }
williamr@2
   158
# ifdef _STLP_HAS_WCHAR_T
williamr@2
   159
inline void _S_cond_store_eos(wchar_t& __c) { __c = 0; }
williamr@2
   160
# endif
williamr@2
   161
williamr@2
   162
// char_producers are logically functions that generate a section of
williamr@2
   163
// a string.  These can be convereted to ropes.  The resulting rope
williamr@2
   164
// invokes the char_producer on demand.  This allows, for example,
williamr@2
   165
// files to be viewed as ropes without reading the entire file.
williamr@2
   166
template <class _CharT>
williamr@2
   167
class char_producer {
williamr@2
   168
public:
williamr@2
   169
  virtual ~char_producer() {};
williamr@2
   170
  virtual void operator()(size_t __start_pos, size_t __len, 
williamr@2
   171
                          _CharT* __buffer) = 0;
williamr@2
   172
  // Buffer should really be an arbitrary output iterator.
williamr@2
   173
  // That way we could flatten directly into an ostream, etc.
williamr@2
   174
  // This is thoroughly impossible, since iterator types don't
williamr@2
   175
  // have runtime descriptions.
williamr@2
   176
};
williamr@2
   177
williamr@2
   178
// Sequence buffers:
williamr@2
   179
//
williamr@2
   180
// Sequence must provide an append operation that appends an
williamr@2
   181
// array to the sequence.  Sequence buffers are useful only if
williamr@2
   182
// appending an entire array is cheaper than appending element by element.
williamr@2
   183
// This is true for many string representations.
williamr@2
   184
// This should  perhaps inherit from ostream<sequence::value_type>
williamr@2
   185
// and be implemented correspondingly, so that they can be used
williamr@2
   186
// for formatted.  For the sake of portability, we don't do this yet.
williamr@2
   187
//
williamr@2
   188
// For now, sequence buffers behave as output iterators.  But they also
williamr@2
   189
// behave a little like basic_ostringstream<sequence::value_type> and a
williamr@2
   190
// little like containers.
williamr@2
   191
williamr@2
   192
template<class _Sequence
williamr@2
   193
# if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
williamr@2
   194
       defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
williamr@2
   195
, size_t _Buf_sz = 100
williamr@2
   196
#   if defined(__sgi) && !defined(__GNUC__)
williamr@2
   197
#	 define __TYPEDEF_WORKAROUND
williamr@2
   198
,class _V = typename _Sequence::value_type
williamr@2
   199
#   endif /* __sgi */
williamr@2
   200
# endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2
   201
>
williamr@2
   202
// The 3rd parameter works around a common compiler bug.
williamr@2
   203
class sequence_buffer : public iterator <output_iterator_tag, void, void, void, void> {
williamr@2
   204
public:
williamr@2
   205
#       ifndef __TYPEDEF_WORKAROUND
williamr@2
   206
  typedef typename _Sequence::value_type value_type;
williamr@2
   207
  typedef sequence_buffer<_Sequence
williamr@2
   208
# if !(defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) || \
williamr@2
   209
       defined ( _STLP_NO_DEFAULT_NON_TYPE_PARAM ))
williamr@2
   210
  , _Buf_sz
williamr@2
   211
  > _Self;
williamr@2
   212
# else /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2
   213
  > _Self;
williamr@2
   214
  enum { _Buf_sz = 100}; 
williamr@2
   215
# endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2
   216
  // # endif
williamr@2
   217
#	else /* __TYPEDEF_WORKAROUND */
williamr@2
   218
  typedef _V value_type;
williamr@2
   219
  typedef sequence_buffer<_Sequence, _Buf_sz, _V> _Self;
williamr@2
   220
#	endif /* __TYPEDEF_WORKAROUND */
williamr@2
   221
protected:
williamr@2
   222
  _Sequence* _M_prefix;
williamr@2
   223
  value_type _M_buffer[_Buf_sz];
williamr@2
   224
  size_t     _M_buf_count;
williamr@2
   225
public:
williamr@2
   226
  void flush() {
williamr@2
   227
    _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
williamr@2
   228
    _M_buf_count = 0;
williamr@2
   229
  }
williamr@2
   230
  ~sequence_buffer() { flush(); }
williamr@2
   231
  sequence_buffer() : _M_prefix(0), _M_buf_count(0) {}
williamr@2
   232
  sequence_buffer(const _Self& __x) {
williamr@2
   233
    _M_prefix = __x._M_prefix;
williamr@2
   234
    _M_buf_count = __x._M_buf_count;
williamr@2
   235
    copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
williamr@2
   236
  }
williamr@2
   237
  sequence_buffer(_Self& __x) {
williamr@2
   238
    __x.flush();
williamr@2
   239
    _M_prefix = __x._M_prefix;
williamr@2
   240
    _M_buf_count = 0;
williamr@2
   241
  }
williamr@2
   242
  sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) {}
williamr@2
   243
  _Self& operator= (_Self& __x) {
williamr@2
   244
    __x.flush();
williamr@2
   245
    _M_prefix = __x._M_prefix;
williamr@2
   246
    _M_buf_count = 0;
williamr@2
   247
    return *this;
williamr@2
   248
  }
williamr@2
   249
  _Self& operator= (const _Self& __x) {
williamr@2
   250
    _M_prefix = __x._M_prefix;
williamr@2
   251
    _M_buf_count = __x._M_buf_count;
williamr@2
   252
    copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
williamr@2
   253
    return *this;
williamr@2
   254
  }
williamr@2
   255
  void push_back(value_type __x)
williamr@2
   256
  {
williamr@2
   257
    if (_M_buf_count < _Buf_sz) {
williamr@2
   258
      _M_buffer[_M_buf_count] = __x;
williamr@2
   259
      ++_M_buf_count;
williamr@2
   260
    } else {
williamr@2
   261
      flush();
williamr@2
   262
      _M_buffer[0] = __x;
williamr@2
   263
      _M_buf_count = 1;
williamr@2
   264
    }
williamr@2
   265
  }
williamr@2
   266
  void append(value_type* __s, size_t __len)
williamr@2
   267
  {
williamr@2
   268
    if (__len + _M_buf_count <= _Buf_sz) {
williamr@2
   269
      size_t __i = _M_buf_count;
williamr@2
   270
      size_t __j = 0;
williamr@2
   271
      for (; __j < __len; __i++, __j++) {
williamr@2
   272
        _M_buffer[__i] = __s[__j];
williamr@2
   273
      }
williamr@2
   274
      _M_buf_count += __len;
williamr@2
   275
    } else if (0 == _M_buf_count) {
williamr@2
   276
      _M_prefix->append(__s, __s + __len);
williamr@2
   277
    } else {
williamr@2
   278
      flush();
williamr@2
   279
      append(__s, __len);
williamr@2
   280
    }
williamr@2
   281
  }
williamr@2
   282
  _Self& write(value_type* __s, size_t __len)
williamr@2
   283
  {
williamr@2
   284
    append(__s, __len);
williamr@2
   285
    return *this;
williamr@2
   286
  }
williamr@2
   287
  _Self& put(value_type __x)
williamr@2
   288
  {
williamr@2
   289
    push_back(__x);
williamr@2
   290
    return *this;
williamr@2
   291
  }
williamr@2
   292
  _Self& operator=(const value_type& __rhs)
williamr@2
   293
  {
williamr@2
   294
    push_back(__rhs);
williamr@2
   295
    return *this;
williamr@2
   296
  }
williamr@2
   297
  _Self& operator*() { return *this; }
williamr@2
   298
  _Self& operator++() { return *this; }
williamr@2
   299
  _Self& operator++(int) { return *this; }
williamr@2
   300
};
williamr@2
   301
williamr@2
   302
// The following should be treated as private, at least for now.
williamr@2
   303
template<class _CharT>
williamr@2
   304
class _Rope_char_consumer {
williamr@2
   305
public:
williamr@2
   306
  // If we had member templates, these should not be virtual.
williamr@2
   307
  // For now we need to use run-time parametrization where
williamr@2
   308
  // compile-time would do.  _Hence this should all be private
williamr@2
   309
  // for now.
williamr@2
   310
  // The symmetry with char_producer is accidental and temporary.
williamr@2
   311
  virtual ~_Rope_char_consumer() {};
williamr@2
   312
  virtual bool operator()(const _CharT* __buffer, size_t __len) = 0;
williamr@2
   313
};
williamr@2
   314
williamr@2
   315
//
williamr@2
   316
// What follows should really be local to rope.  Unfortunately,
williamr@2
   317
// that doesn't work, since it makes it impossible to define generic
williamr@2
   318
// equality on rope iterators.  According to the draft standard, the
williamr@2
   319
// template parameters for such an equality operator cannot be inferred
williamr@2
   320
// from the occurence of a member class as a parameter.
williamr@2
   321
// (SGI compilers in fact allow this, but the __result wouldn't be
williamr@2
   322
// portable.)
williamr@2
   323
// Similarly, some of the static member functions are member functions
williamr@2
   324
// only to avoid polluting the global namespace, and to circumvent
williamr@2
   325
// restrictions on type inference for template functions.
williamr@2
   326
//
williamr@2
   327
williamr@2
   328
//
williamr@2
   329
// The internal data structure for representing a rope.  This is
williamr@2
   330
// private to the implementation.  A rope is really just a pointer
williamr@2
   331
// to one of these.
williamr@2
   332
//
williamr@2
   333
// A few basic functions for manipulating this data structure
williamr@2
   334
// are members of _RopeRep.  Most of the more complex algorithms
williamr@2
   335
// are implemented as rope members.
williamr@2
   336
//
williamr@2
   337
// Some of the static member functions of _RopeRep have identically
williamr@2
   338
// named functions in rope that simply invoke the _RopeRep versions.
williamr@2
   339
//
williamr@2
   340
// A macro to introduce various allocation and deallocation functions
williamr@2
   341
// These need to be defined differently depending on whether or not
williamr@2
   342
// we are using standard conforming allocators, and whether the allocator
williamr@2
   343
// instances have real state.  Thus this macro is invoked repeatedly
williamr@2
   344
// with different definitions of __ROPE_DEFINE_ALLOC.
williamr@2
   345
williamr@2
   346
#if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
williamr@2
   347
# define __ROPE_DEFINE_ALLOC(_Tp, __name, _M_proxy) \
williamr@2
   348
        typedef typename \
williamr@2
   349
          _Alloc_traits<_Tp,_Alloc>::allocator_type __name##Allocator;
williamr@2
   350
williamr@2
   351
#define __ROPE_DEFINE_ALLOCS(__a, _M_proxy) \
williamr@2
   352
        __ROPE_DEFINE_ALLOC(_CharT,_Data, _M_proxy) /* character data */ \
williamr@2
   353
        typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
williamr@2
   354
        __ROPE_DEFINE_ALLOC(__C,_C, _M_proxy) \
williamr@2
   355
        typedef _Rope_RopeLeaf<_CharT,__a> __L; \
williamr@2
   356
        __ROPE_DEFINE_ALLOC(__L,_L, _M_proxy) \
williamr@2
   357
        typedef _Rope_RopeFunction<_CharT,__a> __F; \
williamr@2
   358
        __ROPE_DEFINE_ALLOC(__F,_F, _M_proxy) \
williamr@2
   359
        typedef _Rope_RopeSubstring<_CharT,__a> __S; \
williamr@2
   360
        __ROPE_DEFINE_ALLOC(__S,_S,_M_proxy)
williamr@2
   361
#else
williamr@2
   362
#define __ROPE_DEFINE_ALLOC(_Tp, __name, _M_proxy) 
williamr@2
   363
#define __ROPE_DEFINE_ALLOCS(__a, _M_proxy)
williamr@2
   364
#endif
williamr@2
   365
williamr@2
   366
williamr@2
   367
template<class _CharT, class _Alloc>
williamr@2
   368
struct _Rope_RopeRep
williamr@2
   369
# ifndef __GC
williamr@2
   370
  : public _Refcount_Base
williamr@2
   371
# endif
williamr@2
   372
{
williamr@2
   373
  typedef _Rope_RopeRep<_CharT, _Alloc> _Self;
williamr@2
   374
public:
williamr@2
   375
#  define __ROPE_MAX_DEPTH  45
williamr@2
   376
#  define __ROPE_DEPTH_SIZE 46
williamr@2
   377
  enum { _S_max_rope_depth = __ROPE_MAX_DEPTH };
williamr@2
   378
  enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
williamr@2
   379
  // Apparently needed by VC++
williamr@2
   380
  // The data fields of leaves are allocated with some
williamr@2
   381
  // extra space, to accomodate future growth and for basic
williamr@2
   382
  // character types, to hold a trailing eos character.
williamr@2
   383
  enum { _S_alloc_granularity = 8 };
williamr@2
   384
williamr@2
   385
  
williamr@2
   386
  _Tag _M_tag:8;
williamr@2
   387
  bool _M_is_balanced:8;
williamr@2
   388
williamr@2
   389
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
   390
  typedef typename _Alloc_traits<_CharT,_Alloc>::allocator_type
williamr@2
   391
  allocator_type;
williamr@2
   392
  
williamr@2
   393
  allocator_type get_allocator() const { return allocator_type(_M_size);  }
williamr@2
   394
williamr@2
   395
  unsigned char _M_depth;
williamr@2
   396
  __GC_CONST _CharT* _M_c_string;
williamr@2
   397
  _STLP_alloc_proxy<size_t, _CharT, allocator_type> _M_size;
williamr@2
   398
williamr@2
   399
# ifdef _STLP_NO_ARROW_OPERATOR
williamr@2
   400
  _Rope_RopeRep() : _Refcount_Base(1), _M_size(allocator_type(), 0) {}
williamr@2
   401
# endif
williamr@2
   402
williamr@2
   403
  /* Flattened version of string, if needed.  */
williamr@2
   404
  /* typically 0.                             */
williamr@2
   405
  /* If it's not 0, then the memory is owned  */
williamr@2
   406
  /* by this node.                            */
williamr@2
   407
  /* In the case of a leaf, this may point to */
williamr@2
   408
  /* the same memory as the data field.       */
williamr@2
   409
  _Rope_RopeRep(_Tag __t, int __d, bool __b, size_t _p_size,
williamr@2
   410
                allocator_type __a) :
williamr@2
   411
#         ifndef __GC
williamr@2
   412
    _Refcount_Base(1),
williamr@2
   413
#	  endif
williamr@2
   414
    _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0), _M_size(__a, _p_size)
williamr@2
   415
  { }
williamr@2
   416
#   ifdef __GC
williamr@2
   417
  void _M_incr () {}
williamr@2
   418
#   endif
williamr@2
   419
williamr@2
   420
  // fbp : moved from RopeLeaf
williamr@2
   421
  static size_t _S_rounded_up_size(size_t __n) {
williamr@2
   422
    size_t __size_with_eos;
williamr@2
   423
    
williamr@2
   424
    if (_S_is_basic_char_type((_CharT*)0)) {
williamr@2
   425
      __size_with_eos = __n + 1;
williamr@2
   426
    } else {
williamr@2
   427
      __size_with_eos = __n;
williamr@2
   428
    }
williamr@2
   429
#       ifdef __GC
williamr@2
   430
    return __size_with_eos;
williamr@2
   431
#       else
williamr@2
   432
    // Allow slop for in-place expansion.
williamr@2
   433
    return (__size_with_eos + _S_alloc_granularity-1)
williamr@2
   434
      &~ (_S_alloc_granularity-1);
williamr@2
   435
#       endif
williamr@2
   436
  }
williamr@2
   437
williamr@2
   438
  static void _S_free_string(__GC_CONST _CharT* __s, size_t __len,
williamr@2
   439
                             allocator_type __a) {
williamr@2
   440
williamr@2
   441
    if (!_S_is_basic_char_type((_CharT*)0)) {
williamr@2
   442
      _STLP_STD::_Destroy(__s, __s + __len);
williamr@2
   443
    }
williamr@2
   444
    //  This has to be a static member, so this gets a bit messy
williamr@2
   445
#   ifdef _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM
williamr@2
   446
    __a.deallocate(__s, _S_rounded_up_size(__len));		//*ty 03/24/2001 - restored not to use __stl_alloc_rebind() since it is not defined under _STLP_MEMBER_TEMPLATE_CLASSES
williamr@2
   447
#   else
williamr@2
   448
    __stl_alloc_rebind (__a, (_CharT*)0).deallocate(__s, _S_rounded_up_size(__len));
williamr@2
   449
#   endif
williamr@2
   450
  }
williamr@2
   451
  
williamr@2
   452
  // Deallocate data section of a leaf.
williamr@2
   453
  // This shouldn't be a member function.
williamr@2
   454
  // But its hard to do anything else at the
williamr@2
   455
  // moment, because it's templatized w.r.t.
williamr@2
   456
  // an allocator.
williamr@2
   457
  // Does nothing if __GC is defined.
williamr@2
   458
#   ifndef __GC
williamr@2
   459
  void _M_free_c_string();
williamr@2
   460
  void _M_free_tree();
williamr@2
   461
  // Deallocate t. Assumes t is not 0.
williamr@2
   462
  void _M_unref_nonnil()
williamr@2
   463
  {
williamr@2
   464
    _M_decr(); if (!_M_ref_count) _M_free_tree();
williamr@2
   465
  }
williamr@2
   466
  void _M_ref_nonnil()
williamr@2
   467
  {
williamr@2
   468
    _M_incr();
williamr@2
   469
  }
williamr@2
   470
  static void _S_unref(_Self* __t)
williamr@2
   471
  {
williamr@2
   472
    if (0 != __t) {
williamr@2
   473
      __t->_M_unref_nonnil();
williamr@2
   474
    }
williamr@2
   475
  }
williamr@2
   476
  static void _S_ref(_Self* __t)
williamr@2
   477
  {
williamr@2
   478
    if (0 != __t) __t->_M_incr();
williamr@2
   479
  }
williamr@2
   480
  static void _S_free_if_unref(_Self* __t)
williamr@2
   481
  {
williamr@2
   482
    if (0 != __t && 0 == __t->_M_ref_count) __t->_M_free_tree();
williamr@2
   483
  }
williamr@2
   484
#   else /* __GC */
williamr@2
   485
  void _M_unref_nonnil() {}
williamr@2
   486
  void _M_ref_nonnil() {}
williamr@2
   487
  static void _S_unref(_Self*) {}
williamr@2
   488
  static void _S_ref(_Self*) {}
williamr@2
   489
  static void _S_free_if_unref(_Self*) {}
williamr@2
   490
#   endif
williamr@2
   491
williamr@2
   492
  __ROPE_DEFINE_ALLOCS(_Alloc, _M_size)
williamr@2
   493
    };
williamr@2
   494
williamr@2
   495
template<class _CharT, class _Alloc>
williamr@2
   496
struct _Rope_RopeLeaf : public _Rope_RopeRep<_CharT,_Alloc> {
williamr@2
   497
public:
williamr@2
   498
  __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
williamr@2
   499
                                /* The allocated size is         */
williamr@2
   500
                                /* _S_rounded_up_size(size), except */
williamr@2
   501
                                /* in the GC case, in which it   */
williamr@2
   502
                                /* doesn't matter.               */
williamr@2
   503
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
   504
  typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
williamr@2
   505
  _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t _p_size, allocator_type __a)
williamr@2
   506
    : _Rope_RopeRep<_CharT,_Alloc>(_Rope_RopeRep<_CharT,_Alloc>::_S_leaf, 0, true, _p_size, __a), 
williamr@2
   507
    _M_data(__d)
williamr@2
   508
  {
williamr@2
   509
    _STLP_ASSERT(_p_size > 0)
williamr@2
   510
    if (_S_is_basic_char_type((_CharT *)0)) {
williamr@2
   511
      // already eos terminated.
williamr@2
   512
      this->_M_c_string = __d;
williamr@2
   513
    }
williamr@2
   514
  }
williamr@2
   515
williamr@2
   516
# ifdef _STLP_NO_ARROW_OPERATOR
williamr@2
   517
  _Rope_RopeLeaf() {}
williamr@2
   518
  _Rope_RopeLeaf(const _Rope_RopeLeaf<_CharT, _Alloc>& ) {}
williamr@2
   519
# endif
williamr@2
   520
  
williamr@2
   521
// The constructor assumes that d has been allocated with
williamr@2
   522
  // the proper allocator and the properly padded size.
williamr@2
   523
  // In contrast, the destructor deallocates the data:
williamr@2
   524
# ifndef __GC
williamr@2
   525
  ~_Rope_RopeLeaf() {
williamr@2
   526
    if (_M_data != this->_M_c_string) {
williamr@2
   527
      this->_M_free_c_string();
williamr@2
   528
    }
williamr@2
   529
    _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_M_data, this->_M_size._M_data, this->get_allocator());
williamr@2
   530
  }
williamr@2
   531
# endif
williamr@2
   532
};
williamr@2
   533
williamr@2
   534
template<class _CharT, class _Alloc>
williamr@2
   535
struct _Rope_RopeConcatenation : public _Rope_RopeRep<_CharT,_Alloc> {
williamr@2
   536
public:
williamr@2
   537
  _Rope_RopeRep<_CharT,_Alloc>* _M_left;
williamr@2
   538
  _Rope_RopeRep<_CharT,_Alloc>* _M_right;
williamr@2
   539
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
   540
  typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
williamr@2
   541
  _Rope_RopeConcatenation(_Rope_RopeRep<_CharT,_Alloc>* __l,
williamr@2
   542
                          _Rope_RopeRep<_CharT,_Alloc>* __r,
williamr@2
   543
                          allocator_type __a)
williamr@2
   544
    :   _Rope_RopeRep<_CharT,_Alloc>(
williamr@2
   545
                                     _Rope_RopeRep<_CharT,_Alloc>::_S_concat, 
williamr@2
   546
				     (max)(__l->_M_depth, __r->_M_depth) + 1, false,
williamr@2
   547
                                     __l->_M_size._M_data + __r->_M_size._M_data, __a), _M_left(__l), _M_right(__r)
williamr@2
   548
  {}
williamr@2
   549
# ifdef _STLP_NO_ARROW_OPERATOR
williamr@2
   550
  _Rope_RopeConcatenation() {}
williamr@2
   551
  _Rope_RopeConcatenation(const _Rope_RopeConcatenation<_CharT, _Alloc>&) {}
williamr@2
   552
# endif
williamr@2
   553
williamr@2
   554
# ifndef __GC
williamr@2
   555
  ~_Rope_RopeConcatenation() {
williamr@2
   556
    this->_M_free_c_string();
williamr@2
   557
    _M_left->_M_unref_nonnil();
williamr@2
   558
    _M_right->_M_unref_nonnil();
williamr@2
   559
  }
williamr@2
   560
# endif
williamr@2
   561
};
williamr@2
   562
williamr@2
   563
template<class _CharT, class _Alloc>
williamr@2
   564
struct _Rope_RopeFunction : public _Rope_RopeRep<_CharT,_Alloc> {
williamr@2
   565
public:
williamr@2
   566
  char_producer<_CharT>* _M_fn;
williamr@2
   567
#   ifndef __GC
williamr@2
   568
  bool _M_delete_when_done; // Char_producer is owned by the
williamr@2
   569
                                // rope and should be explicitly
williamr@2
   570
                                // deleted when the rope becomes
williamr@2
   571
                                // inaccessible.
williamr@2
   572
#   else
williamr@2
   573
  // In the GC case, we either register the rope for
williamr@2
   574
  // finalization, or not.  Thus the field is unnecessary;
williamr@2
   575
  // the information is stored in the collector data structures.
williamr@2
   576
  // We do need a finalization procedure to be invoked by the
williamr@2
   577
  // collector.
williamr@2
   578
  static void _S_fn_finalization_proc(void * __tree, void *) {
williamr@2
   579
    delete ((_Rope_RopeFunction *)__tree) -> _M_fn;
williamr@2
   580
  }
williamr@2
   581
#   endif
williamr@2
   582
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
   583
  typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
williamr@2
   584
# ifdef _STLP_NO_ARROW_OPERATOR
williamr@2
   585
  _Rope_RopeFunction() {}
williamr@2
   586
  _Rope_RopeFunction(const _Rope_RopeFunction<_CharT, _Alloc>& ) {}
williamr@2
   587
# endif
williamr@2
   588
williamr@2
   589
  _Rope_RopeFunction(char_producer<_CharT>* __f, size_t _p_size,
williamr@2
   590
                     bool __d, allocator_type __a)
williamr@2
   591
    :
williamr@2
   592
    _Rope_RopeRep<_CharT,_Alloc>(_Rope_RopeRep<_CharT,_Alloc>::_S_function, 0, true, _p_size, __a),
williamr@2
   593
    _M_fn(__f)
williamr@2
   594
#       ifndef __GC
williamr@2
   595
    , _M_delete_when_done(__d)
williamr@2
   596
#       endif
williamr@2
   597
  {
williamr@2
   598
    _STLP_ASSERT(_p_size > 0)
williamr@2
   599
#       ifdef __GC
williamr@2
   600
    if (__d) {
williamr@2
   601
      GC_REGISTER_FINALIZER(
williamr@2
   602
                            this, _Rope_RopeFunction::_S_fn_finalization_proc, 0, 0, 0);
williamr@2
   603
    }
williamr@2
   604
#       endif
williamr@2
   605
  }
williamr@2
   606
# ifndef __GC
williamr@2
   607
  ~_Rope_RopeFunction() {
williamr@2
   608
    this->_M_free_c_string();
williamr@2
   609
    if (_M_delete_when_done) {
williamr@2
   610
      delete _M_fn;
williamr@2
   611
    }
williamr@2
   612
  }
williamr@2
   613
# endif
williamr@2
   614
};
williamr@2
   615
// Substring results are usually represented using just
williamr@2
   616
// concatenation nodes.  But in the case of very long flat ropes
williamr@2
   617
// or ropes with a functional representation that isn't practical.
williamr@2
   618
// In that case, we represent the __result as a special case of
williamr@2
   619
// RopeFunction, whose char_producer points back to the rope itself.
williamr@2
   620
// In all cases except repeated substring operations and
williamr@2
   621
// deallocation, we treat the __result as a RopeFunction.
williamr@2
   622
template<class _CharT, class _Alloc>
williamr@2
   623
# if  ( defined (__IBMCPP__) && (__IBMCPP__ == 500) )  // JFA 10-Aug-2000 for some reason xlC cares about the order
williamr@2
   624
struct _Rope_RopeSubstring : public char_producer<_CharT> , public _Rope_RopeFunction<_CharT,_Alloc>
williamr@2
   625
# else
williamr@2
   626
struct _Rope_RopeSubstring : public _Rope_RopeFunction<_CharT,_Alloc>,
williamr@2
   627
                             public char_producer<_CharT>
williamr@2
   628
# endif
williamr@2
   629
{
williamr@2
   630
public:
williamr@2
   631
  // XXX this whole class should be rewritten.
williamr@2
   632
  typedef _Rope_RopeRep<_CharT,_Alloc> _Base;
williamr@2
   633
  _Rope_RopeRep<_CharT,_Alloc>* _M_base;      // not 0
williamr@2
   634
  size_t _M_start;
williamr@2
   635
  virtual void operator()(size_t __start_pos, size_t __req_len,
williamr@2
   636
                          _CharT* __buffer) {
williamr@2
   637
    switch(_M_base->_M_tag) {
williamr@2
   638
    case _Base::_S_function:
williamr@2
   639
    case _Base::_S_substringfn:
williamr@2
   640
      {
williamr@2
   641
        char_producer<_CharT>* __fn =
williamr@2
   642
          ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
williamr@2
   643
        _STLP_ASSERT(__start_pos + __req_len <= this->_M_size._M_data)
williamr@2
   644
        _STLP_ASSERT(_M_start + this->_M_size._M_data <= _M_base->_M_size._M_data)
williamr@2
   645
        (*__fn)(__start_pos + _M_start, __req_len, __buffer);
williamr@2
   646
      }
williamr@2
   647
      break;
williamr@2
   648
    case _Base::_S_leaf:
williamr@2
   649
      {
williamr@2
   650
        __GC_CONST _CharT* __s =
williamr@2
   651
          ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
williamr@2
   652
        uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
williamr@2
   653
                             __buffer);
williamr@2
   654
      }
williamr@2
   655
      break;
williamr@2
   656
    default:
williamr@2
   657
      _STLP_ASSERT(false)
williamr@2
   658
        ;
williamr@2
   659
    }
williamr@2
   660
  }
williamr@2
   661
williamr@2
   662
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
   663
  typedef typename _Rope_RopeRep<_CharT,_Alloc>::allocator_type allocator_type;
williamr@2
   664
williamr@2
   665
  _Rope_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
williamr@2
   666
                      size_t __l, allocator_type __a)
williamr@2
   667
    : _Rope_RopeFunction<_CharT,_Alloc>(this, __l, false, __a),
williamr@2
   668
	_M_base(__b),
williamr@2
   669
    _M_start(__s)
williamr@2
   670
       
williamr@2
   671
  {
williamr@2
   672
    _STLP_ASSERT(__l > 0)
williamr@2
   673
    _STLP_ASSERT(__s + __l <= __b->_M_size._M_data)
williamr@2
   674
#       ifndef __GC
williamr@2
   675
    _M_base->_M_ref_nonnil();
williamr@2
   676
#       endif
williamr@2
   677
    this->_M_tag = _Base::_S_substringfn;
williamr@2
   678
  }
williamr@2
   679
  virtual ~_Rope_RopeSubstring()
williamr@2
   680
  { 
williamr@2
   681
#       ifndef __GC
williamr@2
   682
    _M_base->_M_unref_nonnil();
williamr@2
   683
#       endif
williamr@2
   684
  }
williamr@2
   685
};
williamr@2
   686
williamr@2
   687
// Self-destructing pointers to Rope_rep.
williamr@2
   688
// These are not conventional smart pointers.  Their
williamr@2
   689
// only purpose in life is to ensure that unref is called
williamr@2
   690
// on the pointer either at normal exit or if an exception
williamr@2
   691
// is raised.  It is the caller's responsibility to
williamr@2
   692
// adjust reference counts when these pointers are initialized
williamr@2
   693
// or assigned to.  (This convention significantly reduces
williamr@2
   694
// the number of potentially expensive reference count
williamr@2
   695
// updates.)
williamr@2
   696
#ifndef __GC
williamr@2
   697
template<class _CharT, class _Alloc>
williamr@2
   698
struct _Rope_self_destruct_ptr {
williamr@2
   699
  _Rope_RopeRep<_CharT,_Alloc>* _M_ptr;
williamr@2
   700
  ~_Rope_self_destruct_ptr() 
williamr@2
   701
  { _Rope_RopeRep<_CharT,_Alloc>::_S_unref(_M_ptr); }
williamr@2
   702
#   ifdef _STLP_USE_EXCEPTIONS
williamr@2
   703
  _Rope_self_destruct_ptr() : _M_ptr(0) {};
williamr@2
   704
#   else
williamr@2
   705
  _Rope_self_destruct_ptr() {};
williamr@2
   706
#   endif
williamr@2
   707
  _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT,_Alloc>* __p) : _M_ptr(__p) {}
williamr@2
   708
  _Rope_RopeRep<_CharT,_Alloc>& operator*() { return *_M_ptr; }
williamr@2
   709
  _Rope_RopeRep<_CharT,_Alloc>* operator->() { return _M_ptr; }
williamr@2
   710
  operator _Rope_RopeRep<_CharT,_Alloc>*() { return _M_ptr; }
williamr@2
   711
  _Rope_self_destruct_ptr<_CharT, _Alloc>& 
williamr@2
   712
  operator= (_Rope_RopeRep<_CharT,_Alloc>* __x)
williamr@2
   713
  { _M_ptr = __x; return *this; }
williamr@2
   714
};
williamr@2
   715
#endif
williamr@2
   716
williamr@2
   717
// Dereferencing a nonconst iterator has to return something
williamr@2
   718
// that behaves almost like a reference.  It's not possible to
williamr@2
   719
// return an actual reference since assignment requires extra
williamr@2
   720
// work.  And we would get into the same problems as with the
williamr@2
   721
// CD2 version of basic_string.
williamr@2
   722
template<class _CharT, class _Alloc>
williamr@2
   723
class _Rope_char_ref_proxy {
williamr@2
   724
  typedef _Rope_char_ref_proxy<_CharT, _Alloc> _Self;
williamr@2
   725
  friend class rope<_CharT,_Alloc>;
williamr@2
   726
  friend class _Rope_iterator<_CharT,_Alloc>;
williamr@2
   727
  friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
williamr@2
   728
#   ifdef __GC
williamr@2
   729
  typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
williamr@2
   730
#   else
williamr@2
   731
  typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
williamr@2
   732
#   endif
williamr@2
   733
  typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2
   734
  typedef rope<_CharT,_Alloc> _My_rope;
williamr@2
   735
  size_t _M_pos;
williamr@2
   736
  _CharT _M_current;
williamr@2
   737
  bool _M_current_valid;
williamr@2
   738
  _My_rope* _M_root;     // The whole rope.
williamr@2
   739
public:
williamr@2
   740
  _Rope_char_ref_proxy(_My_rope* __r, size_t __p) :
williamr@2
   741
    _M_pos(__p), _M_current_valid(false), _M_root(__r) {}
williamr@2
   742
  _Rope_char_ref_proxy(const _Self& __x) :
williamr@2
   743
    _M_pos(__x._M_pos), _M_current_valid(false), _M_root(__x._M_root) {}
williamr@2
   744
  // Don't preserve cache if the reference can outlive the
williamr@2
   745
  // expression.  We claim that's not possible without calling
williamr@2
   746
  // a copy constructor or generating reference to a proxy
williamr@2
   747
  // reference.  We declare the latter to have undefined semantics.
williamr@2
   748
  _Rope_char_ref_proxy(_My_rope* __r, size_t __p,
williamr@2
   749
                       _CharT __c) :
williamr@2
   750
    _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
williamr@2
   751
  inline operator _CharT () const;
williamr@2
   752
  _Self& operator= (_CharT __c);
williamr@2
   753
  _Rope_char_ptr_proxy<_CharT, _Alloc> operator& () const;
williamr@2
   754
  _Self& operator= (const _Self& __c) {
williamr@2
   755
    return operator=((_CharT)__c); 
williamr@2
   756
  }
williamr@2
   757
};
williamr@2
   758
williamr@2
   759
#ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
williamr@2
   760
template<class _CharT, class __Alloc>
williamr@2
   761
inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
williamr@2
   762
                 _Rope_char_ref_proxy <_CharT, __Alloc > __b) {
williamr@2
   763
  _CharT __tmp = __a;
williamr@2
   764
  __a = __b;
williamr@2
   765
  __b = __tmp;
williamr@2
   766
}
williamr@2
   767
#else
williamr@2
   768
// There is no really acceptable way to handle this.  The default
williamr@2
   769
// definition of swap doesn't work for proxy references.
williamr@2
   770
// It can't really be made to work, even with ugly hacks, since
williamr@2
   771
// the only unusual operation it uses is the copy constructor, which
williamr@2
   772
// is needed for other purposes.  We provide a macro for
williamr@2
   773
// full specializations, and instantiate the most common case.
williamr@2
   774
# define _ROPE_SWAP_SPECIALIZATION(_CharT, __Alloc) \
williamr@2
   775
    inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a, \
williamr@2
   776
                     _Rope_char_ref_proxy <_CharT, __Alloc > __b) { \
williamr@2
   777
        _CharT __tmp = __a; \
williamr@2
   778
        __a = __b; \
williamr@2
   779
        __b = __tmp; \
williamr@2
   780
    }
williamr@2
   781
williamr@2
   782
_ROPE_SWAP_SPECIALIZATION(char,_STLP_DEFAULT_ALLOCATOR(char) )
williamr@2
   783
williamr@2
   784
#endif /* !_STLP_FUNCTION_TMPL_PARTIAL_ORDER */
williamr@2
   785
williamr@2
   786
  template<class _CharT, class _Alloc>
williamr@2
   787
class _Rope_char_ptr_proxy {
williamr@2
   788
  // XXX this class should be rewritten.
williamr@2
   789
public:
williamr@2
   790
  typedef _Rope_char_ptr_proxy<_CharT, _Alloc> _Self;
williamr@2
   791
  friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
williamr@2
   792
  size_t _M_pos;
williamr@2
   793
  rope<_CharT,_Alloc>* _M_root;     // The whole rope.
williamr@2
   794
williamr@2
   795
  _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x) 
williamr@2
   796
    : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
williamr@2
   797
  _Rope_char_ptr_proxy(const _Self& __x)
williamr@2
   798
    : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
williamr@2
   799
  _Rope_char_ptr_proxy() {}
williamr@2
   800
  _Rope_char_ptr_proxy(_CharT* __x) : _M_pos(0), _M_root(0) {
williamr@2
   801
    _STLP_ASSERT(0 == __x)
williamr@2
   802
  }
williamr@2
   803
  _Self& 
williamr@2
   804
  operator= (const _Self& __x) {
williamr@2
   805
    _M_pos = __x._M_pos;
williamr@2
   806
    _M_root = __x._M_root;
williamr@2
   807
    return *this;
williamr@2
   808
  }
williamr@2
   809
williamr@2
   810
  _Rope_char_ref_proxy<_CharT,_Alloc> operator*() const {
williamr@2
   811
    return _Rope_char_ref_proxy<_CharT,_Alloc>(_M_root, _M_pos);
williamr@2
   812
  }
williamr@2
   813
};
williamr@2
   814
williamr@2
   815
williamr@2
   816
// Rope iterators:
williamr@2
   817
// Unlike in the C version, we cache only part of the stack
williamr@2
   818
// for rope iterators, since they must be efficiently copyable.
williamr@2
   819
// When we run out of cache, we have to reconstruct the iterator
williamr@2
   820
// value.
williamr@2
   821
// Pointers from iterators are not included in reference counts.
williamr@2
   822
// Iterators are assumed to be thread private.  Ropes can
williamr@2
   823
// be shared.
williamr@2
   824
williamr@2
   825
template<class _CharT, class _Alloc>
williamr@2
   826
class _Rope_iterator_base
williamr@2
   827
/*   : public random_access_iterator<_CharT, ptrdiff_t>  */
williamr@2
   828
{
williamr@2
   829
  friend class rope<_CharT,_Alloc>;
williamr@2
   830
  typedef _Rope_iterator_base<_CharT, _Alloc> _Self;
williamr@2
   831
public:
williamr@2
   832
  typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2
   833
  // Borland doesnt want this to be protected.
williamr@2
   834
  //  protected:
williamr@2
   835
  enum { _S_path_cache_len = 4 }; // Must be <= 9.
williamr@2
   836
  enum { _S_iterator_buf_len = 15 };
williamr@2
   837
  size_t _M_current_pos;
williamr@2
   838
  _RopeRep* _M_root;     // The whole rope.
williamr@2
   839
  size_t _M_leaf_pos;    // Starting position for current leaf
williamr@2
   840
  __GC_CONST _CharT* _M_buf_start;
williamr@2
   841
  // Buffer possibly
williamr@2
   842
  // containing current char.
williamr@2
   843
  __GC_CONST _CharT* _M_buf_ptr;
williamr@2
   844
  // Pointer to current char in buffer.
williamr@2
   845
  // != 0 ==> buffer valid.
williamr@2
   846
  __GC_CONST _CharT* _M_buf_end;
williamr@2
   847
  // One past __last valid char in buffer.
williamr@2
   848
  // What follows is the path cache.  We go out of our
williamr@2
   849
  // way to make this compact.
williamr@2
   850
  // Path_end contains the bottom section of the path from
williamr@2
   851
  // the root to the current leaf.
williamr@2
   852
  const _RopeRep* _M_path_end[_S_path_cache_len];
williamr@2
   853
  int _M_leaf_index;     // Last valid __pos in path_end;
williamr@2
   854
  // _M_path_end[0] ... _M_path_end[leaf_index-1]
williamr@2
   855
  // point to concatenation nodes.
williamr@2
   856
  unsigned char _M_path_directions;
williamr@2
   857
  // (path_directions >> __i) & 1 is 1
williamr@2
   858
  // iff we got from _M_path_end[leaf_index - __i - 1]
williamr@2
   859
  // to _M_path_end[leaf_index - __i] by going to the
williamr@2
   860
  // __right. Assumes path_cache_len <= 9.
williamr@2
   861
  _CharT _M_tmp_buf[_S_iterator_buf_len];
williamr@2
   862
  // Short buffer for surrounding chars.
williamr@2
   863
  // This is useful primarily for 
williamr@2
   864
  // RopeFunctions.  We put the buffer
williamr@2
   865
  // here to avoid locking in the
williamr@2
   866
  // multithreaded case.
williamr@2
   867
  // The cached path is generally assumed to be valid
williamr@2
   868
  // only if the buffer is valid.
williamr@2
   869
  static void _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@2
   870
  // Set buffer contents given
williamr@2
   871
  // path cache.
williamr@2
   872
  static void _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@2
   873
  // Set buffer contents and
williamr@2
   874
  // path cache.
williamr@2
   875
  static void _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x);
williamr@2
   876
  // As above, but assumes path
williamr@2
   877
  // cache is valid for previous posn.
williamr@2
   878
  _Rope_iterator_base() {}
williamr@2
   879
  _Rope_iterator_base(_RopeRep* __root, size_t __pos)
williamr@2
   880
    : _M_current_pos(__pos),_M_root(__root),  _M_buf_ptr(0) {}
williamr@2
   881
  void _M_incr(size_t __n);
williamr@2
   882
  void _M_decr(size_t __n);
williamr@2
   883
public:
williamr@2
   884
  size_t index() const { return _M_current_pos; }
williamr@2
   885
  _Rope_iterator_base(const _Self& __x) {
williamr@2
   886
    if (0 != __x._M_buf_ptr) {
williamr@2
   887
      *this = __x;
williamr@2
   888
    } else {
williamr@2
   889
      _M_current_pos = __x._M_current_pos;
williamr@2
   890
      _M_root = __x._M_root;
williamr@2
   891
      _M_buf_ptr = 0;
williamr@2
   892
    }
williamr@2
   893
  }
williamr@2
   894
};
williamr@2
   895
williamr@2
   896
template<class _CharT, class _Alloc> class _Rope_iterator;
williamr@2
   897
williamr@2
   898
template<class _CharT, class _Alloc>
williamr@2
   899
class _Rope_const_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
williamr@2
   900
  friend class rope<_CharT,_Alloc>;
williamr@2
   901
  typedef  _Rope_const_iterator<_CharT, _Alloc> _Self;
williamr@2
   902
  typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
williamr@2
   903
  //  protected:
williamr@2
   904
public:
williamr@2
   905
#   ifndef _STLP_HAS_NO_NAMESPACES
williamr@2
   906
  typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2
   907
  // The one from the base class may not be directly visible.
williamr@2
   908
#   endif
williamr@2
   909
  _Rope_const_iterator(const _RopeRep* __root, size_t __pos):
williamr@2
   910
    _Rope_iterator_base<_CharT,_Alloc>(
williamr@2
   911
                                       __CONST_CAST(_RopeRep*,__root), __pos)
williamr@2
   912
    // Only nonconst iterators modify root ref count
williamr@2
   913
  {}
williamr@2
   914
public:
williamr@2
   915
  typedef _CharT reference;   // Really a value.  Returning a reference
williamr@2
   916
                                // Would be a mess, since it would have
williamr@2
   917
                                // to be included in refcount.
williamr@2
   918
  typedef const _CharT* pointer;
williamr@2
   919
  typedef _CharT value_type;
williamr@2
   920
  typedef ptrdiff_t difference_type;
williamr@2
   921
  typedef random_access_iterator_tag iterator_category;
williamr@2
   922
williamr@2
   923
public:
williamr@2
   924
  _Rope_const_iterator() {};
williamr@2
   925
  _Rope_const_iterator(const _Self& __x) :
williamr@2
   926
    _Rope_iterator_base<_CharT,_Alloc>(__x) { }
williamr@2
   927
  _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x): 
williamr@2
   928
    _Rope_iterator_base<_CharT,_Alloc>(__x) {}
williamr@2
   929
  _Rope_const_iterator(const rope<_CharT,_Alloc>& __r, size_t __pos) :
williamr@2
   930
    _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr._M_data, __pos) {}
williamr@2
   931
  _Self& operator= (const _Self& __x) {
williamr@2
   932
    if (0 != __x._M_buf_ptr) {
williamr@2
   933
      *(__STATIC_CAST(_Base*,this)) = __x;
williamr@2
   934
    } else {
williamr@2
   935
      this->_M_current_pos = __x._M_current_pos;
williamr@2
   936
      this->_M_root = __x._M_root;
williamr@2
   937
      this->_M_buf_ptr = 0;
williamr@2
   938
    }
williamr@2
   939
    return(*this);
williamr@2
   940
  }
williamr@2
   941
  reference operator*() {
williamr@2
   942
    if (0 == this->_M_buf_ptr) _S_setcache(*this);
williamr@2
   943
    return *(this->_M_buf_ptr);
williamr@2
   944
  }
williamr@2
   945
  _Self& operator++() {
williamr@2
   946
    __GC_CONST _CharT* __next;
williamr@2
   947
    if (0 != this->_M_buf_ptr && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end) {
williamr@2
   948
      this->_M_buf_ptr = __next;
williamr@2
   949
      ++this->_M_current_pos;
williamr@2
   950
    } else {
williamr@2
   951
      this->_M_incr(1);
williamr@2
   952
    }
williamr@2
   953
    return *this;
williamr@2
   954
  }
williamr@2
   955
  _Self& operator+=(ptrdiff_t __n) {
williamr@2
   956
    if (__n >= 0) {
williamr@2
   957
      this->_M_incr(__n);
williamr@2
   958
    } else {
williamr@2
   959
      this->_M_decr(-__n);
williamr@2
   960
    }
williamr@2
   961
    return *this;
williamr@2
   962
  }
williamr@2
   963
  _Self& operator--() {
williamr@2
   964
    this->_M_decr(1);
williamr@2
   965
    return *this;
williamr@2
   966
  }
williamr@2
   967
  _Self& operator-=(ptrdiff_t __n) {
williamr@2
   968
    if (__n >= 0) {
williamr@2
   969
      this->_M_decr(__n);
williamr@2
   970
    } else {
williamr@2
   971
      this->_M_incr(-__n);
williamr@2
   972
    }
williamr@2
   973
    return *this;
williamr@2
   974
  }
williamr@2
   975
  _Self operator++(int) {
williamr@2
   976
    size_t __old_pos = this->_M_current_pos;
williamr@2
   977
    this->_M_incr(1);
williamr@2
   978
    return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
williamr@2
   979
    // This makes a subsequent dereference expensive.
williamr@2
   980
    // Perhaps we should instead copy the iterator
williamr@2
   981
    // if it has a valid cache?
williamr@2
   982
  }
williamr@2
   983
  _Self operator--(int) {
williamr@2
   984
    size_t __old_pos = this->_M_current_pos;
williamr@2
   985
    this->_M_decr(1);
williamr@2
   986
    return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
williamr@2
   987
  }
williamr@2
   988
  inline reference operator[](size_t __n);
williamr@2
   989
};
williamr@2
   990
williamr@2
   991
template<class _CharT, class _Alloc>
williamr@2
   992
class _Rope_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
williamr@2
   993
  friend class rope<_CharT,_Alloc>;
williamr@2
   994
  typedef _Rope_iterator<_CharT, _Alloc> _Self;
williamr@2
   995
  typedef _Rope_iterator_base<_CharT,_Alloc> _Base;
williamr@2
   996
  typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
williamr@2
   997
  //  protected:
williamr@2
   998
public:
williamr@2
   999
  rope<_CharT,_Alloc>* _M_root_rope;
williamr@2
  1000
  // root is treated as a cached version of this,
williamr@2
  1001
  // and is used to detect changes to the underlying
williamr@2
  1002
  // rope.
williamr@2
  1003
  // Root is included in the reference count.
williamr@2
  1004
  // This is necessary so that we can detect changes reliably.
williamr@2
  1005
  // Unfortunately, it requires careful bookkeeping for the
williamr@2
  1006
  // nonGC case.
williamr@2
  1007
  _Rope_iterator(rope<_CharT,_Alloc>* __r, size_t __pos);
williamr@2
  1008
  
williamr@2
  1009
  void _M_check();
williamr@2
  1010
public:
williamr@2
  1011
  typedef _Rope_char_ref_proxy<_CharT,_Alloc>  reference;
williamr@2
  1012
  typedef _Rope_char_ref_proxy<_CharT,_Alloc>* pointer;
williamr@2
  1013
  typedef _CharT value_type;
williamr@2
  1014
  typedef ptrdiff_t difference_type;
williamr@2
  1015
  typedef random_access_iterator_tag iterator_category;
williamr@2
  1016
public:
williamr@2
  1017
  ~_Rope_iterator() 		//*TY 5/6/00 - added dtor to balance reference count
williamr@2
  1018
  {
williamr@2
  1019
    _RopeRep::_S_unref(this->_M_root);
williamr@2
  1020
  }
williamr@2
  1021
  
williamr@2
  1022
  rope<_CharT,_Alloc>& container() { return *_M_root_rope; }
williamr@2
  1023
  _Rope_iterator() {
williamr@2
  1024
    this->_M_root = 0;  // Needed for reference counting.
williamr@2
  1025
  };
williamr@2
  1026
  _Rope_iterator(const  _Self& __x) :
williamr@2
  1027
    _Rope_iterator_base<_CharT,_Alloc>(__x) {
williamr@2
  1028
    _M_root_rope = __x._M_root_rope;
williamr@2
  1029
    _RopeRep::_S_ref(this->_M_root);
williamr@2
  1030
  }
williamr@2
  1031
  _Rope_iterator(rope<_CharT,_Alloc>& __r, size_t __pos);
williamr@2
  1032
  _Self& operator= (const  _Self& __x) {
williamr@2
  1033
    _RopeRep* __old = this->_M_root;
williamr@2
  1034
    
williamr@2
  1035
    _RopeRep::_S_ref(__x._M_root);
williamr@2
  1036
    if (0 != __x._M_buf_ptr) {
williamr@2
  1037
      _M_root_rope = __x._M_root_rope;
williamr@2
  1038
      *(__STATIC_CAST(_Base*,this)) = __x;
williamr@2
  1039
    } else {
williamr@2
  1040
      this->_M_current_pos = __x._M_current_pos;
williamr@2
  1041
      this->_M_root = __x._M_root;
williamr@2
  1042
      _M_root_rope = __x._M_root_rope;
williamr@2
  1043
      this->_M_buf_ptr = 0;
williamr@2
  1044
    }
williamr@2
  1045
    _RopeRep::_S_unref(__old);
williamr@2
  1046
    return(*this);
williamr@2
  1047
  }
williamr@2
  1048
  reference operator*() {
williamr@2
  1049
    _M_check();
williamr@2
  1050
    if (0 == this->_M_buf_ptr) {
williamr@2
  1051
      return _Rope_char_ref_proxy<_CharT,_Alloc>(
williamr@2
  1052
                                                 _M_root_rope, this->_M_current_pos);
williamr@2
  1053
    } else {
williamr@2
  1054
      return _Rope_char_ref_proxy<_CharT,_Alloc>(
williamr@2
  1055
                                                 _M_root_rope, this->_M_current_pos, *(this->_M_buf_ptr));
williamr@2
  1056
    }
williamr@2
  1057
  }
williamr@2
  1058
  _Self& operator++() {
williamr@2
  1059
    this->_M_incr(1);
williamr@2
  1060
    return *this;
williamr@2
  1061
  }
williamr@2
  1062
  _Self& operator+=(ptrdiff_t __n) {
williamr@2
  1063
    if (__n >= 0) {
williamr@2
  1064
      this->_M_incr(__n);
williamr@2
  1065
    } else {
williamr@2
  1066
      this->_M_decr(-__n);
williamr@2
  1067
    }
williamr@2
  1068
    return *this;
williamr@2
  1069
  }
williamr@2
  1070
  _Self& operator--() {
williamr@2
  1071
    this->_M_decr(1);
williamr@2
  1072
    return *this;
williamr@2
  1073
  }
williamr@2
  1074
  _Self& operator-=(ptrdiff_t __n) {
williamr@2
  1075
    if (__n >= 0) {
williamr@2
  1076
      this->_M_decr(__n);
williamr@2
  1077
    } else {
williamr@2
  1078
      this->_M_incr(-__n);
williamr@2
  1079
    }
williamr@2
  1080
    return *this;
williamr@2
  1081
  }
williamr@2
  1082
  _Self operator++(int) {
williamr@2
  1083
    size_t __old_pos = this->_M_current_pos;
williamr@2
  1084
    this->_M_incr(1);
williamr@2
  1085
    return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
williamr@2
  1086
  }
williamr@2
  1087
  _Self operator--(int) {
williamr@2
  1088
    size_t __old_pos = this->_M_current_pos;
williamr@2
  1089
    this->_M_decr(1);
williamr@2
  1090
    return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
williamr@2
  1091
  }
williamr@2
  1092
  reference operator[](ptrdiff_t __n) {
williamr@2
  1093
    return _Rope_char_ref_proxy<_CharT,_Alloc>(
williamr@2
  1094
                                               _M_root_rope, this->_M_current_pos + __n);
williamr@2
  1095
  }
williamr@2
  1096
};
williamr@2
  1097
williamr@2
  1098
# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
williamr@2
  1099
template <class _CharT, class _Alloc>
williamr@2
  1100
inline random_access_iterator_tag
williamr@2
  1101
iterator_category(const _Rope_iterator<_CharT,_Alloc>&) {  return random_access_iterator_tag();}
williamr@2
  1102
template <class _CharT, class _Alloc>
williamr@2
  1103
inline _CharT* value_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2
  1104
template <class _CharT, class _Alloc>
williamr@2
  1105
inline ptrdiff_t* distance_type(const _Rope_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2
  1106
template <class _CharT, class _Alloc>
williamr@2
  1107
inline random_access_iterator_tag
williamr@2
  1108
iterator_category(const _Rope_const_iterator<_CharT,_Alloc>&) { return random_access_iterator_tag(); }
williamr@2
  1109
template <class _CharT, class _Alloc>
williamr@2
  1110
inline _CharT* value_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2
  1111
template <class _CharT, class _Alloc>
williamr@2
  1112
inline ptrdiff_t* distance_type(const _Rope_const_iterator<_CharT,_Alloc>&) { return 0; }
williamr@2
  1113
#endif
williamr@2
  1114
williamr@2
  1115
template <class _CharT, class _Alloc>
williamr@2
  1116
class rope {
williamr@2
  1117
  typedef rope<_CharT,_Alloc> _Self;
williamr@2
  1118
public:
williamr@2
  1119
  typedef _CharT value_type;
williamr@2
  1120
  typedef ptrdiff_t difference_type;
williamr@2
  1121
  typedef size_t size_type;
williamr@2
  1122
  typedef _CharT const_reference;
williamr@2
  1123
  typedef const _CharT* const_pointer;
williamr@2
  1124
  typedef _Rope_iterator<_CharT,_Alloc> iterator;
williamr@2
  1125
  typedef _Rope_const_iterator<_CharT,_Alloc> const_iterator;
williamr@2
  1126
  typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
williamr@2
  1127
  typedef _Rope_char_ptr_proxy<_CharT,_Alloc> pointer;
williamr@2
  1128
  
williamr@2
  1129
  friend class _Rope_iterator<_CharT,_Alloc>;
williamr@2
  1130
  friend class _Rope_const_iterator<_CharT,_Alloc>;
williamr@2
  1131
  friend struct _Rope_RopeRep<_CharT,_Alloc>;
williamr@2
  1132
  friend class _Rope_iterator_base<_CharT,_Alloc>;
williamr@2
  1133
  friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
williamr@2
  1134
  friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
williamr@2
  1135
  friend struct _Rope_RopeSubstring<_CharT,_Alloc>;
williamr@2
  1136
williamr@2
  1137
  _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
williamr@2
  1138
  
williamr@2
  1139
protected:
williamr@2
  1140
  typedef __GC_CONST _CharT* _Cstrptr;
williamr@2
  1141
  
williamr@2
  1142
  static _CharT _S_empty_c_str[1];
williamr@2
  1143
  
williamr@2
  1144
  static bool _S_is0(_CharT __c) { return __c == _S_eos((_CharT*)0); }
williamr@2
  1145
  enum { _S_copy_max = 23 };
williamr@2
  1146
  // For strings shorter than _S_copy_max, we copy to
williamr@2
  1147
  // concatenate.
williamr@2
  1148
  
williamr@2
  1149
public:
williamr@2
  1150
  typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
williamr@2
  1151
  _STLP_FORCE_ALLOCATORS(_CharT, _Alloc)
williamr@2
  1152
  typedef typename _Alloc_traits<_CharT,_Alloc>::allocator_type  allocator_type;
williamr@2
  1153
  allocator_type get_allocator() const { return allocator_type(_M_tree_ptr); }
williamr@2
  1154
public:
williamr@2
  1155
  // The only data member of a rope:
williamr@2
  1156
  _STLP_alloc_proxy<_RopeRep*, _CharT, allocator_type> _M_tree_ptr;
williamr@2
  1157
williamr@2
  1158
  typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcatenation;
williamr@2
  1159
  typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
williamr@2
  1160
  typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
williamr@2
  1161
  typedef _Rope_RopeSubstring<_CharT,_Alloc> _RopeSubstring;
williamr@2
  1162
williamr@2
  1163
williamr@2
  1164
williamr@2
  1165
  // Retrieve a character at the indicated position.
williamr@2
  1166
  static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
williamr@2
  1167
williamr@2
  1168
#       ifndef __GC
williamr@2
  1169
  // Obtain a pointer to the character at the indicated position.
williamr@2
  1170
  // The pointer can be used to change the character.
williamr@2
  1171
  // If such a pointer cannot be produced, as is frequently the
williamr@2
  1172
  // case, 0 is returned instead.
williamr@2
  1173
  // (Returns nonzero only if all nodes in the path have a refcount
williamr@2
  1174
  // of 1.)
williamr@2
  1175
  static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
williamr@2
  1176
#       endif
williamr@2
  1177
williamr@2
  1178
  static bool _S_apply_to_pieces(
williamr@2
  1179
                                // should be template parameter
williamr@2
  1180
                                 _Rope_char_consumer<_CharT>& __c,
williamr@2
  1181
                                 const _RopeRep* __r,
williamr@2
  1182
                                 size_t __begin, size_t __end);
williamr@2
  1183
                                // begin and end are assumed to be in range.
williamr@2
  1184
williamr@2
  1185
#       ifndef __GC
williamr@2
  1186
  static void _S_unref(_RopeRep* __t)
williamr@2
  1187
  {
williamr@2
  1188
    _RopeRep::_S_unref(__t);
williamr@2
  1189
  }
williamr@2
  1190
  static void _S_ref(_RopeRep* __t)
williamr@2
  1191
  {
williamr@2
  1192
    _RopeRep::_S_ref(__t);
williamr@2
  1193
  }
williamr@2
  1194
#       else /* __GC */
williamr@2
  1195
  static void _S_unref(_RopeRep*) {}
williamr@2
  1196
  static void _S_ref(_RopeRep*) {}
williamr@2
  1197
#       endif
williamr@2
  1198
williamr@2
  1199
williamr@2
  1200
#       ifdef __GC
williamr@2
  1201
  typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
williamr@2
  1202
#       else
williamr@2
  1203
  typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
williamr@2
  1204
#       endif
williamr@2
  1205
williamr@2
  1206
  // _Result is counted in refcount.
williamr@2
  1207
  static _RopeRep* _S_substring(_RopeRep* __base,
williamr@2
  1208
                                size_t __start, size_t __endp1);
williamr@2
  1209
williamr@2
  1210
  static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
williamr@2
  1211
                                       const _CharT* __iter, size_t __slen);
williamr@2
  1212
  // Concatenate rope and char ptr, copying __s.
williamr@2
  1213
  // Should really take an arbitrary iterator.
williamr@2
  1214
  // Result is counted in refcount.
williamr@2
  1215
  static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
williamr@2
  1216
                                             const _CharT* __iter, size_t __slen)
williamr@2
  1217
    // As above, but one reference to __r is about to be
williamr@2
  1218
    // destroyed.  Thus the pieces may be recycled if all
williamr@2
  1219
    // relevent reference counts are 1.
williamr@2
  1220
#           ifdef __GC
williamr@2
  1221
    // We can't really do anything since refcounts are unavailable.
williamr@2
  1222
  { return _S_concat_char_iter(__r, __iter, __slen); }
williamr@2
  1223
#           else
williamr@2
  1224
  ;
williamr@2
  1225
#           endif
williamr@2
  1226
williamr@2
  1227
  static _RopeRep* _S_concat_rep(_RopeRep* __left, _RopeRep* __right);
williamr@2
  1228
  // General concatenation on _RopeRep.  _Result
williamr@2
  1229
  // has refcount of 1.  Adjusts argument refcounts.
williamr@2
  1230
williamr@2
  1231
public:
williamr@2
  1232
  void apply_to_pieces( size_t __begin, size_t __end,
williamr@2
  1233
                        _Rope_char_consumer<_CharT>& __c) const {
williamr@2
  1234
    _S_apply_to_pieces(__c, _M_tree_ptr._M_data, __begin, __end);
williamr@2
  1235
  }
williamr@2
  1236
williamr@2
  1237
williamr@2
  1238
protected:
williamr@2
  1239
williamr@2
  1240
  static size_t _S_rounded_up_size(size_t __n) {
williamr@2
  1241
    return _RopeRep::_S_rounded_up_size(__n);
williamr@2
  1242
  }
williamr@2
  1243
williamr@2
  1244
  static size_t _S_allocated_capacity(size_t __n) {
williamr@2
  1245
    if (_S_is_basic_char_type((_CharT*)0)) {
williamr@2
  1246
      return _S_rounded_up_size(__n) - 1;
williamr@2
  1247
    } else {
williamr@2
  1248
      return _S_rounded_up_size(__n);
williamr@2
  1249
    }
williamr@2
  1250
  }
williamr@2
  1251
                
williamr@2
  1252
  // Allocate and construct a RopeLeaf using the supplied allocator
williamr@2
  1253
  // Takes ownership of s instead of copying.
williamr@2
  1254
  static _RopeLeaf* _S_new_RopeLeaf(__GC_CONST _CharT *__s,
williamr@2
  1255
                                    size_t _p_size, allocator_type __a)
williamr@2
  1256
  {
williamr@2
  1257
   _RopeLeaf* __space = _STLP_CREATE_ALLOCATOR(allocator_type,__a, _RopeLeaf).allocate(1,(const void*)0);
williamr@2
  1258
    _STLP_TRY {
williamr@2
  1259
      _STLP_PLACEMENT_NEW(__space) _RopeLeaf(__s, _p_size, __a);
williamr@2
  1260
    }
williamr@2
  1261
   _STLP_UNWIND(_STLP_CREATE_ALLOCATOR(allocator_type,__a, 
williamr@2
  1262
                                   _RopeLeaf).deallocate(__space, 1))
williamr@2
  1263
	  return __space;
williamr@2
  1264
  }
williamr@2
  1265
williamr@2
  1266
  static _RopeConcatenation* _S_new_RopeConcatenation(
williamr@2
  1267
                                                      _RopeRep* __left, _RopeRep* __right,
williamr@2
  1268
                                                      allocator_type __a)
williamr@2
  1269
  {
williamr@2
  1270
   _RopeConcatenation* __space = _STLP_CREATE_ALLOCATOR(allocator_type,__a,
williamr@2
  1271
                                                    _RopeConcatenation).allocate(1,(const void*)0);
williamr@2
  1272
    return _STLP_PLACEMENT_NEW(__space) _RopeConcatenation(__left, __right, __a);
williamr@2
  1273
  }
williamr@2
  1274
williamr@2
  1275
  static _RopeFunction* _S_new_RopeFunction(char_producer<_CharT>* __f,
williamr@2
  1276
                                            size_t _p_size, bool __d, allocator_type __a)
williamr@2
  1277
  {
williamr@2
  1278
   _RopeFunction* __space = _STLP_CREATE_ALLOCATOR(allocator_type,__a, 
williamr@2
  1279
                                               _RopeFunction).allocate(1,(const void*)0);
williamr@2
  1280
    return _STLP_PLACEMENT_NEW(__space) _RopeFunction(__f, _p_size, __d, __a);
williamr@2
  1281
  }
williamr@2
  1282
williamr@2
  1283
  static _RopeSubstring* _S_new_RopeSubstring(
williamr@2
  1284
                                              _Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
williamr@2
  1285
                                              size_t __l, allocator_type __a)
williamr@2
  1286
  {
williamr@2
  1287
   _RopeSubstring* __space = _STLP_CREATE_ALLOCATOR(allocator_type,__a, 
williamr@2
  1288
                                                _RopeSubstring).allocate(1,(const void*)0);
williamr@2
  1289
    return _STLP_PLACEMENT_NEW(__space) _RopeSubstring(__b, __s, __l, __a);
williamr@2
  1290
  }
williamr@2
  1291
williamr@2
  1292
#         define _STLP_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _p_size, __a) \
williamr@2
  1293
                _S_RopeLeaf_from_unowned_char_ptr(__s, _p_size, __a)     
williamr@2
  1294
williamr@2
  1295
  static
williamr@2
  1296
  _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
williamr@2
  1297
                                               size_t _p_size, allocator_type __a)
williamr@2
  1298
  {
williamr@2
  1299
    if (0 == _p_size) return 0;
williamr@2
  1300
williamr@2
  1301
   _CharT* __buf = _STLP_CREATE_ALLOCATOR(allocator_type,__a, _CharT).allocate(_S_rounded_up_size(_p_size));
williamr@2
  1302
williamr@2
  1303
    uninitialized_copy_n(__s, _p_size, __buf);
williamr@2
  1304
    _S_cond_store_eos(__buf[_p_size]);
williamr@2
  1305
williamr@2
  1306
    _STLP_TRY {
williamr@2
  1307
      return _S_new_RopeLeaf(__buf, _p_size, __a);
williamr@2
  1308
    }
williamr@2
  1309
    _STLP_UNWIND(_RopeRep::_S_free_string(__buf, _p_size, __a))
williamr@2
  1310
            
williamr@2
  1311
# if defined (_STLP_THROW_RETURN_BUG)
williamr@2
  1312
      return 0;
williamr@2
  1313
# endif
williamr@2
  1314
  }
williamr@2
  1315
            
williamr@2
  1316
williamr@2
  1317
  // Concatenation of nonempty strings.
williamr@2
  1318
  // Always builds a concatenation node.
williamr@2
  1319
  // Rebalances if the result is too deep.
williamr@2
  1320
  // Result has refcount 1.
williamr@2
  1321
  // Does not increment left and right ref counts even though
williamr@2
  1322
  // they are referenced.
williamr@2
  1323
  static _RopeRep*
williamr@2
  1324
  _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
williamr@2
  1325
williamr@2
  1326
  // Concatenation helper functions
williamr@2
  1327
  static _RopeLeaf*
williamr@2
  1328
  _S_leaf_concat_char_iter(_RopeLeaf* __r,
williamr@2
  1329
                           const _CharT* __iter, size_t __slen);
williamr@2
  1330
  // Concatenate by copying leaf.
williamr@2
  1331
  // should take an arbitrary iterator
williamr@2
  1332
  // result has refcount 1.
williamr@2
  1333
#       ifndef __GC
williamr@2
  1334
  static _RopeLeaf* _S_destr_leaf_concat_char_iter
williamr@2
  1335
  (_RopeLeaf* __r, const _CharT* __iter, size_t __slen);
williamr@2
  1336
  // A version that potentially clobbers __r if __r->_M_ref_count == 1.
williamr@2
  1337
#       endif
williamr@2
  1338
williamr@2
  1339
williamr@2
  1340
  // A helper function for exponentiating strings.
williamr@2
  1341
  // This uses a nonstandard refcount convention.
williamr@2
  1342
  // The result has refcount 0.
williamr@2
  1343
  friend struct _Rope_Concat_fn<_CharT,_Alloc>;
williamr@2
  1344
  typedef _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
williamr@2
  1345
williamr@2
  1346
public:
williamr@2
  1347
  static size_t _S_char_ptr_len(const _CharT* __s) {
williamr@2
  1348
    const _CharT* __p = __s;
williamr@2
  1349
	  
williamr@2
  1350
    while (!_S_is0(*__p)) { ++__p; }
williamr@2
  1351
    return (__p - __s);
williamr@2
  1352
  }
williamr@2
  1353
williamr@2
  1354
public: /* for operators */
williamr@2
  1355
  rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
williamr@2
  1356
    : _M_tree_ptr(__a, __t) { }
williamr@2
  1357
private:
williamr@2
  1358
  // Copy __r to the _CharT buffer.
williamr@2
  1359
  // Returns __buffer + __r->_M_size._M_data.
williamr@2
  1360
  // Assumes that buffer is uninitialized.
williamr@2
  1361
  static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
williamr@2
  1362
williamr@2
  1363
  // Again, with explicit starting position and length.
williamr@2
  1364
  // Assumes that buffer is uninitialized.
williamr@2
  1365
  static _CharT* _S_flatten(_RopeRep* __r,
williamr@2
  1366
                            size_t __start, size_t __len,
williamr@2
  1367
                            _CharT* __buffer);
williamr@2
  1368
williamr@2
  1369
  // fbp : HP aCC prohibits access to protected min_len from within static methods ( ?? )
williamr@2
  1370
public:
williamr@2
  1371
  static const unsigned long _S_min_len[46];
williamr@2
  1372
protected:
williamr@2
  1373
  static bool _S_is_balanced(_RopeRep* __r)
williamr@2
  1374
  { return (__r->_M_size._M_data >= _S_min_len[__r->_M_depth]); }
williamr@2
  1375
williamr@2
  1376
  static bool _S_is_almost_balanced(_RopeRep* __r)
williamr@2
  1377
  { return (__r->_M_depth == 0 ||
williamr@2
  1378
            __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 1]); }
williamr@2
  1379
williamr@2
  1380
  static bool _S_is_roughly_balanced(_RopeRep* __r)
williamr@2
  1381
  { return (__r->_M_depth <= 1 ||
williamr@2
  1382
            __r->_M_size._M_data >= _S_min_len[__r->_M_depth - 2]); }
williamr@2
  1383
williamr@2
  1384
  // Assumes the result is not empty.
williamr@2
  1385
  static _RopeRep* _S_concat_and_set_balanced(_RopeRep* __left,
williamr@2
  1386
                                              _RopeRep* __right)
williamr@2
  1387
  {
williamr@2
  1388
    _RopeRep* __result = _S_concat_rep(__left, __right);
williamr@2
  1389
    if (_S_is_balanced(__result)) __result->_M_is_balanced = true;
williamr@2
  1390
    return __result;
williamr@2
  1391
  }
williamr@2
  1392
williamr@2
  1393
  // The basic rebalancing operation.  Logically copies the
williamr@2
  1394
  // rope.  The result has refcount of 1.  The client will
williamr@2
  1395
  // usually decrement the reference count of __r.
williamr@2
  1396
  // The result is within height 2 of balanced by the above
williamr@2
  1397
  // definition.
williamr@2
  1398
  static _RopeRep* _S_balance(_RopeRep* __r);
williamr@2
  1399
williamr@2
  1400
  // Add all unbalanced subtrees to the forest of balanceed trees.
williamr@2
  1401
  // Used only by balance.
williamr@2
  1402
  static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
williamr@2
  1403
        
williamr@2
  1404
  // Add __r to forest, assuming __r is already balanced.
williamr@2
  1405
  static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
williamr@2
  1406
williamr@2
  1407
  // Print to stdout, exposing structure
williamr@2
  1408
  static void _S_dump(_RopeRep* __r, int __indent = 0);
williamr@2
  1409
williamr@2
  1410
  // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
williamr@2
  1411
  static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
williamr@2
  1412
williamr@2
  1413
public:
williamr@2
  1414
  bool empty() const { return 0 == _M_tree_ptr._M_data; }
williamr@2
  1415
williamr@2
  1416
  // Comparison member function.  This is public only for those
williamr@2
  1417
  // clients that need a ternary comparison.  Others
williamr@2
  1418
  // should use the comparison operators below.
williamr@2
  1419
  int compare(const _Self& __y) const {
williamr@2
  1420
    return _S_compare(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data);
williamr@2
  1421
  }
williamr@2
  1422
williamr@2
  1423
  rope(const _CharT* __s, const allocator_type& __a = allocator_type())
williamr@2
  1424
    : _M_tree_ptr(__a, _STLP_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),__a))
williamr@2
  1425
  { }
williamr@2
  1426
williamr@2
  1427
  rope(const _CharT* __s, size_t __len,
williamr@2
  1428
       const allocator_type& __a = allocator_type())
williamr@2
  1429
    : _M_tree_ptr(__a, (_STLP_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a)))
williamr@2
  1430
  { }
williamr@2
  1431
williamr@2
  1432
  // Should perhaps be templatized with respect to the iterator type
williamr@2
  1433
  // and use Sequence_buffer.  (It should perhaps use sequence_buffer
williamr@2
  1434
  // even now.)
williamr@2
  1435
  rope(const _CharT *__s, const _CharT *__e,
williamr@2
  1436
       const allocator_type& __a = allocator_type())
williamr@2
  1437
    : _M_tree_ptr(__a, _STLP_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a))
williamr@2
  1438
  { }
williamr@2
  1439
williamr@2
  1440
  rope(const const_iterator& __s, const const_iterator& __e,
williamr@2
  1441
       const allocator_type& __a = allocator_type())
williamr@2
  1442
    : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
williamr@2
  1443
                                    __e._M_current_pos))
williamr@2
  1444
  { }
williamr@2
  1445
williamr@2
  1446
  rope(const iterator& __s, const iterator& __e,
williamr@2
  1447
       const allocator_type& __a = allocator_type())
williamr@2
  1448
    : _M_tree_ptr(__a, _S_substring(__s._M_root, __s._M_current_pos,
williamr@2
  1449
                                    __e._M_current_pos))
williamr@2
  1450
  { }
williamr@2
  1451
williamr@2
  1452
  rope(_CharT __c, const allocator_type& __a = allocator_type())
williamr@2
  1453
    : _M_tree_ptr(__a, (_RopeRep*)0)
williamr@2
  1454
  {
williamr@2
  1455
    _CharT* __buf = _M_tree_ptr.allocate(_S_rounded_up_size(1));
williamr@2
  1456
williamr@2
  1457
    _Construct(__buf, __c);
williamr@2
  1458
    _STLP_TRY {
williamr@2
  1459
      _M_tree_ptr._M_data = _S_new_RopeLeaf(__buf, 1, __a);
williamr@2
  1460
    }
williamr@2
  1461
    _STLP_UNWIND(_RopeRep::_S_free_string(__buf, 1, __a))
williamr@2
  1462
      }
williamr@2
  1463
williamr@2
  1464
  rope(size_t __n, _CharT __c,     
williamr@2
  1465
       const allocator_type& __a = allocator_type()):
williamr@2
  1466
    _M_tree_ptr(__a, (_RopeRep*)0) {
williamr@2
  1467
    rope<_CharT,_Alloc> __result;
williamr@2
  1468
# define  __exponentiate_threshold size_t(32)
williamr@2
  1469
    _RopeRep* __remainder;
williamr@2
  1470
    rope<_CharT,_Alloc> __remainder_rope;
williamr@2
  1471
	    
williamr@2
  1472
    // gcc-2.7.2 bugs
williamr@2
  1473
    typedef _Rope_Concat_fn<_CharT,_Alloc> _Concat_fn;
williamr@2
  1474
	    
williamr@2
  1475
    if (0 == __n)
williamr@2
  1476
      return;
williamr@2
  1477
	    
williamr@2
  1478
    size_t __exponent = __n / __exponentiate_threshold;
williamr@2
  1479
    size_t __rest = __n % __exponentiate_threshold;
williamr@2
  1480
    if (0 == __rest) {
williamr@2
  1481
      __remainder = 0;
williamr@2
  1482
    } else {
williamr@2
  1483
      _CharT* __rest_buffer = _M_tree_ptr.allocate(_S_rounded_up_size(__rest));
williamr@2
  1484
      uninitialized_fill_n(__rest_buffer, __rest, __c);
williamr@2
  1485
      _S_cond_store_eos(__rest_buffer[__rest]);
williamr@2
  1486
      _STLP_TRY {
williamr@2
  1487
		__remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
williamr@2
  1488
      }
williamr@2
  1489
      _STLP_UNWIND(_RopeRep::_S_free_string(__rest_buffer, __rest, __a))
williamr@2
  1490
		}
williamr@2
  1491
    __remainder_rope._M_tree_ptr._M_data = __remainder;
williamr@2
  1492
    if (__exponent != 0) {
williamr@2
  1493
      _CharT* __base_buffer =
williamr@2
  1494
		_M_tree_ptr.allocate(_S_rounded_up_size(__exponentiate_threshold));
williamr@2
  1495
      _RopeLeaf* __base_leaf;
williamr@2
  1496
      rope<_CharT,_Alloc> __base_rope;
williamr@2
  1497
      uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
williamr@2
  1498
      _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
williamr@2
  1499
      _STLP_TRY {
williamr@2
  1500
		__base_leaf = _S_new_RopeLeaf(__base_buffer,
williamr@2
  1501
                                      __exponentiate_threshold, __a);
williamr@2
  1502
      }
williamr@2
  1503
      _STLP_UNWIND(_RopeRep::_S_free_string(__base_buffer, 
williamr@2
  1504
                                            __exponentiate_threshold, __a))
williamr@2
  1505
		__base_rope._M_tree_ptr._M_data = __base_leaf;
williamr@2
  1506
      if (1 == __exponent) {
williamr@2
  1507
		__result = __base_rope;
williamr@2
  1508
#         ifndef __GC
williamr@2
  1509
		_STLP_ASSERT(2 == __result._M_tree_ptr._M_data->_M_ref_count)
williamr@2
  1510
		// One each for base_rope and __result
williamr@2
  1511
#         endif
williamr@2
  1512
      } else {
williamr@2
  1513
		__result = power(__base_rope, __exponent, _Concat_fn());
williamr@2
  1514
      }
williamr@2
  1515
      if (0 != __remainder) {
williamr@2
  1516
		__result += __remainder_rope;
williamr@2
  1517
      }
williamr@2
  1518
    } else {
williamr@2
  1519
      __result = __remainder_rope;
williamr@2
  1520
    }
williamr@2
  1521
    _M_tree_ptr._M_data = __result._M_tree_ptr._M_data;
williamr@2
  1522
    _M_tree_ptr._M_data->_M_ref_nonnil();
williamr@2
  1523
# undef __exponentiate_threshold
williamr@2
  1524
  }
williamr@2
  1525
williamr@2
  1526
  rope(const allocator_type& __a = allocator_type())
williamr@2
  1527
    : _M_tree_ptr(__a, (_RopeRep*)0) {}
williamr@2
  1528
williamr@2
  1529
  // Construct a rope from a function that can compute its members
williamr@2
  1530
  rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
williamr@2
  1531
       const allocator_type& __a = allocator_type())
williamr@2
  1532
    : _M_tree_ptr(__a, (_RopeRep*)0)
williamr@2
  1533
  {
williamr@2
  1534
    _M_tree_ptr._M_data = (0 == __len) ?
williamr@2
  1535
      0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
williamr@2
  1536
  }
williamr@2
  1537
williamr@2
  1538
  rope(const _Self& __x)
williamr@2
  1539
    : _M_tree_ptr(__x.get_allocator(), __x._M_tree_ptr._M_data)
williamr@2
  1540
  {
williamr@2
  1541
    _S_ref(_M_tree_ptr._M_data);
williamr@2
  1542
  }
williamr@2
  1543
williamr@2
  1544
  ~rope()
williamr@2
  1545
  {
williamr@2
  1546
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1547
  }
williamr@2
  1548
williamr@2
  1549
  _Self& operator=(const _Self& __x)
williamr@2
  1550
  {
williamr@2
  1551
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1552
    _STLP_ASSERT(get_allocator() == __x.get_allocator())
williamr@2
  1553
    _M_tree_ptr._M_data = __x._M_tree_ptr._M_data;
williamr@2
  1554
    _S_ref(_M_tree_ptr._M_data);
williamr@2
  1555
    _S_unref(__old);
williamr@2
  1556
    return(*this);
williamr@2
  1557
  }
williamr@2
  1558
  void clear()
williamr@2
  1559
  {
williamr@2
  1560
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1561
    _M_tree_ptr._M_data = 0;
williamr@2
  1562
  }
williamr@2
  1563
  void push_back(_CharT __x)
williamr@2
  1564
  {
williamr@2
  1565
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1566
    _M_tree_ptr._M_data = _S_destr_concat_char_iter(_M_tree_ptr._M_data, &__x, 1);
williamr@2
  1567
    _S_unref(__old);
williamr@2
  1568
  }
williamr@2
  1569
williamr@2
  1570
  void pop_back()
williamr@2
  1571
  {
williamr@2
  1572
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1573
    _M_tree_ptr._M_data = 
williamr@2
  1574
      _S_substring(_M_tree_ptr._M_data, 0, _M_tree_ptr._M_data->_M_size._M_data - 1);
williamr@2
  1575
    _S_unref(__old);
williamr@2
  1576
  }
williamr@2
  1577
williamr@2
  1578
  _CharT back() const
williamr@2
  1579
  {
williamr@2
  1580
    return _S_fetch(_M_tree_ptr._M_data, _M_tree_ptr._M_data->_M_size._M_data - 1);
williamr@2
  1581
  }
williamr@2
  1582
williamr@2
  1583
  void push_front(_CharT __x)
williamr@2
  1584
  {
williamr@2
  1585
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1586
    _RopeRep* __left =
williamr@2
  1587
      _STLP_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, get_allocator());
williamr@2
  1588
    _STLP_TRY {
williamr@2
  1589
      _M_tree_ptr._M_data = _S_concat_rep(__left, _M_tree_ptr._M_data);
williamr@2
  1590
      _S_unref(__old);
williamr@2
  1591
      _S_unref(__left);
williamr@2
  1592
    }
williamr@2
  1593
    _STLP_UNWIND(_S_unref(__left))
williamr@2
  1594
      }
williamr@2
  1595
williamr@2
  1596
  void pop_front()
williamr@2
  1597
  {
williamr@2
  1598
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1599
    _M_tree_ptr._M_data = _S_substring(_M_tree_ptr._M_data, 1, _M_tree_ptr._M_data->_M_size._M_data);
williamr@2
  1600
    _S_unref(__old);
williamr@2
  1601
  }
williamr@2
  1602
williamr@2
  1603
  _CharT front() const
williamr@2
  1604
  {
williamr@2
  1605
    return _S_fetch(_M_tree_ptr._M_data, 0);
williamr@2
  1606
  }
williamr@2
  1607
williamr@2
  1608
  void balance()
williamr@2
  1609
  {
williamr@2
  1610
    _RopeRep* __old = _M_tree_ptr._M_data;
williamr@2
  1611
    _M_tree_ptr._M_data = _S_balance(_M_tree_ptr._M_data);
williamr@2
  1612
    _S_unref(__old);
williamr@2
  1613
  }
williamr@2
  1614
williamr@2
  1615
  void copy(_CharT* __buffer) const {
williamr@2
  1616
    _STLP_STD::_Destroy(__buffer, __buffer + size());
williamr@2
  1617
    _S_flatten(_M_tree_ptr._M_data, __buffer);
williamr@2
  1618
  }
williamr@2
  1619
williamr@2
  1620
  // This is the copy function from the standard, but
williamr@2
  1621
  // with the arguments reordered to make it consistent with the
williamr@2
  1622
  // rest of the interface.
williamr@2
  1623
  // Note that this guaranteed not to compile if the draft standard
williamr@2
  1624
  // order is assumed.
williamr@2
  1625
  size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const 
williamr@2
  1626
  {
williamr@2
  1627
    size_t _p_size = size();
williamr@2
  1628
    size_t __len = (__pos + __n > _p_size? _p_size - __pos : __n);
williamr@2
  1629
williamr@2
  1630
    _STLP_STD::_Destroy(__buffer, __buffer + __len);
williamr@2
  1631
    _S_flatten(_M_tree_ptr._M_data, __pos, __len, __buffer);
williamr@2
  1632
    return __len;
williamr@2
  1633
  }
williamr@2
  1634
williamr@2
  1635
  // Print to stdout, exposing structure.  May be useful for
williamr@2
  1636
  // performance debugging.
williamr@2
  1637
  void dump() {
williamr@2
  1638
    _S_dump(_M_tree_ptr._M_data);
williamr@2
  1639
  }
williamr@2
  1640
williamr@2
  1641
  // Convert to 0 terminated string in new allocated memory.
williamr@2
  1642
  // Embedded 0s in the input do not terminate the copy.
williamr@2
  1643
  const _CharT* c_str() const;
williamr@2
  1644
williamr@2
  1645
  // As above, but lso use the flattened representation as the
williamr@2
  1646
  // the new rope representation.
williamr@2
  1647
  const _CharT* replace_with_c_str();
williamr@2
  1648
williamr@2
  1649
  // Reclaim memory for the c_str generated flattened string.
williamr@2
  1650
  // Intentionally undocumented, since it's hard to say when this
williamr@2
  1651
  // is safe for multiple threads.
williamr@2
  1652
  void delete_c_str () {
williamr@2
  1653
    if (0 == _M_tree_ptr._M_data) return;
williamr@2
  1654
    if (_RopeRep::_S_leaf == _M_tree_ptr._M_data->_M_tag && 
williamr@2
  1655
        ((_RopeLeaf*)_M_tree_ptr._M_data)->_M_data == 
williamr@2
  1656
        _M_tree_ptr._M_data->_M_c_string) {
williamr@2
  1657
      // Representation shared
williamr@2
  1658
      return;
williamr@2
  1659
    }
williamr@2
  1660
#           ifndef __GC
williamr@2
  1661
    _M_tree_ptr._M_data->_M_free_c_string();
williamr@2
  1662
#           endif
williamr@2
  1663
    _M_tree_ptr._M_data->_M_c_string = 0;
williamr@2
  1664
  }
williamr@2
  1665
williamr@2
  1666
  _CharT operator[] (size_type __pos) const {
williamr@2
  1667
    return _S_fetch(_M_tree_ptr._M_data, __pos);
williamr@2
  1668
  }
williamr@2
  1669
williamr@2
  1670
  _CharT at(size_type __pos) const {
williamr@2
  1671
    // if (__pos >= size()) throw out_of_range;  // XXX
williamr@2
  1672
    return (*this)[__pos];
williamr@2
  1673
  }
williamr@2
  1674
williamr@2
  1675
  const_iterator begin() const {
williamr@2
  1676
    return(const_iterator(_M_tree_ptr._M_data, 0));
williamr@2
  1677
  }
williamr@2
  1678
williamr@2
  1679
  // An easy way to get a const iterator from a non-const container.
williamr@2
  1680
  const_iterator const_begin() const {
williamr@2
  1681
    return(const_iterator(_M_tree_ptr._M_data, 0));
williamr@2
  1682
  }
williamr@2
  1683
williamr@2
  1684
  const_iterator end() const {
williamr@2
  1685
    return(const_iterator(_M_tree_ptr._M_data, size()));
williamr@2
  1686
  }
williamr@2
  1687
williamr@2
  1688
  const_iterator const_end() const {
williamr@2
  1689
    return(const_iterator(_M_tree_ptr._M_data, size()));
williamr@2
  1690
  }
williamr@2
  1691
williamr@2
  1692
  size_type size() const { 
williamr@2
  1693
    return(0 == _M_tree_ptr._M_data? 0 : _M_tree_ptr._M_data->_M_size._M_data);
williamr@2
  1694
  }
williamr@2
  1695
williamr@2
  1696
  size_type length() const {
williamr@2
  1697
    return size();
williamr@2
  1698
  }
williamr@2
  1699
williamr@2
  1700
  size_type max_size() const {
williamr@2
  1701
    return _S_min_len[__ROPE_MAX_DEPTH-1] - 1;
williamr@2
  1702
    //  Guarantees that the result can be sufficirntly
williamr@2
  1703
    //  balanced.  Longer ropes will probably still work,
williamr@2
  1704
    //  but it's harder to make guarantees.
williamr@2
  1705
  }
williamr@2
  1706
williamr@2
  1707
  const_reverse_iterator rbegin() const {
williamr@2
  1708
    return const_reverse_iterator(end());
williamr@2
  1709
  }
williamr@2
  1710
williamr@2
  1711
  const_reverse_iterator const_rbegin() const {
williamr@2
  1712
    return const_reverse_iterator(end());
williamr@2
  1713
  }
williamr@2
  1714
williamr@2
  1715
  const_reverse_iterator rend() const {
williamr@2
  1716
    return const_reverse_iterator(begin());
williamr@2
  1717
  }
williamr@2
  1718
williamr@2
  1719
  const_reverse_iterator const_rend() const {
williamr@2
  1720
    return const_reverse_iterator(begin());
williamr@2
  1721
  }
williamr@2
  1722
  // The symmetric cases are intentionally omitted, since they're presumed
williamr@2
  1723
  // to be less common, and we don't handle them as well.
williamr@2
  1724
williamr@2
  1725
  // The following should really be templatized.
williamr@2
  1726
  // The first argument should be an input iterator or
williamr@2
  1727
  // forward iterator with value_type _CharT.
williamr@2
  1728
  _Self& append(const _CharT* __iter, size_t __n) {
williamr@2
  1729
    _RopeRep* __result = 
williamr@2
  1730
      _S_destr_concat_char_iter(_M_tree_ptr._M_data, __iter, __n);
williamr@2
  1731
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1732
    _M_tree_ptr._M_data = __result;
williamr@2
  1733
    return *this;
williamr@2
  1734
  }
williamr@2
  1735
williamr@2
  1736
  _Self& append(const _CharT* __c_string) {
williamr@2
  1737
    size_t __len = _S_char_ptr_len(__c_string);
williamr@2
  1738
    append(__c_string, __len);
williamr@2
  1739
    return(*this);
williamr@2
  1740
  }
williamr@2
  1741
williamr@2
  1742
  _Self& append(const _CharT* __s, const _CharT* __e) {
williamr@2
  1743
    _RopeRep* __result =
williamr@2
  1744
      _S_destr_concat_char_iter(_M_tree_ptr._M_data, __s, __e - __s);
williamr@2
  1745
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1746
    _M_tree_ptr._M_data = __result;
williamr@2
  1747
    return *this;
williamr@2
  1748
  }
williamr@2
  1749
williamr@2
  1750
  _Self& append(const_iterator __s, const_iterator __e) {
williamr@2
  1751
    _STLP_ASSERT(__s._M_root == __e._M_root)
williamr@2
  1752
    _STLP_ASSERT(get_allocator() == __s._M_root->get_allocator())
williamr@2
  1753
    _Self_destruct_ptr __appendee(_S_substring(
williamr@2
  1754
                                               __s._M_root, __s._M_current_pos, __e._M_current_pos));
williamr@2
  1755
    _RopeRep* __result = 
williamr@2
  1756
      _S_concat_rep(_M_tree_ptr._M_data, (_RopeRep*)__appendee);
williamr@2
  1757
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1758
    _M_tree_ptr._M_data = __result;
williamr@2
  1759
    return *this;
williamr@2
  1760
  }
williamr@2
  1761
williamr@2
  1762
  _Self& append(_CharT __c) {
williamr@2
  1763
    _RopeRep* __result = 
williamr@2
  1764
      _S_destr_concat_char_iter(_M_tree_ptr._M_data, &__c, 1);
williamr@2
  1765
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1766
    _M_tree_ptr._M_data = __result;
williamr@2
  1767
    return *this;
williamr@2
  1768
  }
williamr@2
  1769
williamr@2
  1770
  _Self& append() { return append(_CharT()); }  // XXX why?
williamr@2
  1771
williamr@2
  1772
  _Self& append(const _Self& __y) {
williamr@2
  1773
    _STLP_ASSERT(__y.get_allocator() == get_allocator())
williamr@2
  1774
    _RopeRep* __result = _S_concat_rep(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data);
williamr@2
  1775
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1776
    _M_tree_ptr._M_data = __result;
williamr@2
  1777
    return *this;
williamr@2
  1778
  }
williamr@2
  1779
williamr@2
  1780
  _Self& append(size_t __n, _CharT __c) {
williamr@2
  1781
    rope<_CharT,_Alloc> __last(__n, __c);
williamr@2
  1782
    return append(__last);
williamr@2
  1783
  }
williamr@2
  1784
williamr@2
  1785
  void swap(_Self& __b) {
williamr@2
  1786
    _STLP_ASSERT(get_allocator() == __b.get_allocator())
williamr@2
  1787
    _RopeRep* __tmp = _M_tree_ptr._M_data;
williamr@2
  1788
    _M_tree_ptr._M_data = __b._M_tree_ptr._M_data;
williamr@2
  1789
    __b._M_tree_ptr._M_data = __tmp;
williamr@2
  1790
  }
williamr@2
  1791
williamr@2
  1792
williamr@2
  1793
protected:
williamr@2
  1794
  // Result is included in refcount.
williamr@2
  1795
  static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
williamr@2
  1796
                           size_t __pos2, _RopeRep* __r) {
williamr@2
  1797
    if (0 == __old) { _S_ref(__r); return __r; }
williamr@2
  1798
    _Self_destruct_ptr __left(
williamr@2
  1799
                              _S_substring(__old, 0, __pos1));
williamr@2
  1800
    _Self_destruct_ptr __right(
williamr@2
  1801
                               _S_substring(__old, __pos2, __old->_M_size._M_data));
williamr@2
  1802
	_STLP_MPWFIX_TRY	//*TY 06/01/2000 - 
williamr@2
  1803
    _RopeRep* __result;
williamr@2
  1804
williamr@2
  1805
    if (0 == __r) {
williamr@2
  1806
      __result = _S_concat_rep(__left, __right);
williamr@2
  1807
    } else {
williamr@2
  1808
      _STLP_ASSERT(__old->get_allocator() == __r->get_allocator())
williamr@2
  1809
      _Self_destruct_ptr __left_result(_S_concat_rep(__left, __r));
williamr@2
  1810
      __result = _S_concat_rep(__left_result, __right);
williamr@2
  1811
    }
williamr@2
  1812
    return __result;
williamr@2
  1813
	_STLP_MPWFIX_CATCH	//*TY 06/01/2000 - 
williamr@2
  1814
  }
williamr@2
  1815
williamr@2
  1816
public:
williamr@2
  1817
  void insert(size_t __p, const _Self& __r) {
williamr@2
  1818
    _RopeRep* __result = 
williamr@2
  1819
      replace(_M_tree_ptr._M_data, __p, __p, __r._M_tree_ptr._M_data);
williamr@2
  1820
    _STLP_ASSERT(get_allocator() == __r.get_allocator())
williamr@2
  1821
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1822
    _M_tree_ptr._M_data = __result;
williamr@2
  1823
  }
williamr@2
  1824
williamr@2
  1825
  void insert(size_t __p, size_t __n, _CharT __c) {
williamr@2
  1826
    rope<_CharT,_Alloc> __r(__n,__c);
williamr@2
  1827
    insert(__p, __r);
williamr@2
  1828
  }
williamr@2
  1829
williamr@2
  1830
  void insert(size_t __p, const _CharT* __i, size_t __n) {
williamr@2
  1831
    _Self_destruct_ptr __left(_S_substring(_M_tree_ptr._M_data, 0, __p));
williamr@2
  1832
    _Self_destruct_ptr __right(_S_substring(_M_tree_ptr._M_data, __p, size()));
williamr@2
  1833
    _Self_destruct_ptr __left_result(
williamr@2
  1834
                                     _S_concat_char_iter(__left, __i, __n));
williamr@2
  1835
    // _S_ destr_concat_char_iter should be safe here.
williamr@2
  1836
    // But as it stands it's probably not a win, since __left
williamr@2
  1837
    // is likely to have additional references.
williamr@2
  1838
    _RopeRep* __result = _S_concat_rep(__left_result, __right);
williamr@2
  1839
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1840
    _M_tree_ptr._M_data = __result;
williamr@2
  1841
  }
williamr@2
  1842
williamr@2
  1843
  void insert(size_t __p, const _CharT* __c_string) {
williamr@2
  1844
    insert(__p, __c_string, _S_char_ptr_len(__c_string));
williamr@2
  1845
  }
williamr@2
  1846
williamr@2
  1847
  void insert(size_t __p, _CharT __c) {
williamr@2
  1848
    insert(__p, &__c, 1);
williamr@2
  1849
  }
williamr@2
  1850
williamr@2
  1851
  void insert(size_t __p) {
williamr@2
  1852
    _CharT __c = _CharT();
williamr@2
  1853
    insert(__p, &__c, 1);
williamr@2
  1854
  }
williamr@2
  1855
williamr@2
  1856
  void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
williamr@2
  1857
    _Self __r(__i, __j);
williamr@2
  1858
    insert(__p, __r);
williamr@2
  1859
  }
williamr@2
  1860
williamr@2
  1861
  void insert(size_t __p, const const_iterator& __i,
williamr@2
  1862
              const const_iterator& __j) {
williamr@2
  1863
    _Self __r(__i, __j);
williamr@2
  1864
    insert(__p, __r);
williamr@2
  1865
  }
williamr@2
  1866
williamr@2
  1867
  void insert(size_t __p, const iterator& __i,
williamr@2
  1868
              const iterator& __j) {
williamr@2
  1869
    _Self __r(__i, __j);
williamr@2
  1870
    insert(__p, __r);
williamr@2
  1871
  }
williamr@2
  1872
williamr@2
  1873
  // (position, length) versions of replace operations:
williamr@2
  1874
williamr@2
  1875
  void replace(size_t __p, size_t __n, const _Self& __r) {
williamr@2
  1876
    _RopeRep* __result = 
williamr@2
  1877
      replace(_M_tree_ptr._M_data, __p, __p + __n, __r._M_tree_ptr._M_data);
williamr@2
  1878
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1879
    _M_tree_ptr._M_data = __result;
williamr@2
  1880
  }
williamr@2
  1881
williamr@2
  1882
  void replace(size_t __p, size_t __n, 
williamr@2
  1883
               const _CharT* __i, size_t __i_len) {
williamr@2
  1884
    _Self __r(__i, __i_len);
williamr@2
  1885
    replace(__p, __n, __r);
williamr@2
  1886
  }
williamr@2
  1887
williamr@2
  1888
  void replace(size_t __p, size_t __n, _CharT __c) {
williamr@2
  1889
    _Self __r(__c);
williamr@2
  1890
    replace(__p, __n, __r);
williamr@2
  1891
  }
williamr@2
  1892
williamr@2
  1893
  void replace(size_t __p, size_t __n, const _CharT* __c_string) {
williamr@2
  1894
    _Self __r(__c_string);
williamr@2
  1895
    replace(__p, __n, __r);
williamr@2
  1896
  }
williamr@2
  1897
williamr@2
  1898
  void replace(size_t __p, size_t __n, 
williamr@2
  1899
               const _CharT* __i, const _CharT* __j) {
williamr@2
  1900
    _Self __r(__i, __j);
williamr@2
  1901
    replace(__p, __n, __r);
williamr@2
  1902
  }
williamr@2
  1903
williamr@2
  1904
  void replace(size_t __p, size_t __n,
williamr@2
  1905
               const const_iterator& __i, const const_iterator& __j) {
williamr@2
  1906
    _Self __r(__i, __j);
williamr@2
  1907
    replace(__p, __n, __r);
williamr@2
  1908
  }
williamr@2
  1909
williamr@2
  1910
  void replace(size_t __p, size_t __n,
williamr@2
  1911
               const iterator& __i, const iterator& __j) {
williamr@2
  1912
    _Self __r(__i, __j);
williamr@2
  1913
    replace(__p, __n, __r);
williamr@2
  1914
  }
williamr@2
  1915
williamr@2
  1916
  // Single character variants:
williamr@2
  1917
  void replace(size_t __p, _CharT __c) {
williamr@2
  1918
    iterator __i(this, __p);
williamr@2
  1919
    *__i = __c;
williamr@2
  1920
  }
williamr@2
  1921
williamr@2
  1922
  void replace(size_t __p, const _Self& __r) {
williamr@2
  1923
    replace(__p, 1, __r);
williamr@2
  1924
  }
williamr@2
  1925
williamr@2
  1926
  void replace(size_t __p, const _CharT* __i, size_t __i_len) {
williamr@2
  1927
    replace(__p, 1, __i, __i_len);
williamr@2
  1928
  }
williamr@2
  1929
williamr@2
  1930
  void replace(size_t __p, const _CharT* __c_string) {
williamr@2
  1931
    replace(__p, 1, __c_string);
williamr@2
  1932
  }
williamr@2
  1933
williamr@2
  1934
  void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
williamr@2
  1935
    replace(__p, 1, __i, __j);
williamr@2
  1936
  }
williamr@2
  1937
williamr@2
  1938
  void replace(size_t __p, const const_iterator& __i,
williamr@2
  1939
               const const_iterator& __j) {
williamr@2
  1940
    replace(__p, 1, __i, __j);
williamr@2
  1941
  }
williamr@2
  1942
williamr@2
  1943
  void replace(size_t __p, const iterator& __i,
williamr@2
  1944
               const iterator& __j) {
williamr@2
  1945
    replace(__p, 1, __i, __j);
williamr@2
  1946
  }
williamr@2
  1947
williamr@2
  1948
  // Erase, (position, size) variant.
williamr@2
  1949
  void erase(size_t __p, size_t __n) {
williamr@2
  1950
    _RopeRep* __result = replace(_M_tree_ptr._M_data, __p, __p + __n, 0);
williamr@2
  1951
    _S_unref(_M_tree_ptr._M_data);
williamr@2
  1952
    _M_tree_ptr._M_data = __result;
williamr@2
  1953
  }
williamr@2
  1954
williamr@2
  1955
  // Erase, single character
williamr@2
  1956
  void erase(size_t __p) {
williamr@2
  1957
    erase(__p, __p + 1);
williamr@2
  1958
  }
williamr@2
  1959
williamr@2
  1960
  // Insert, iterator variants.  
williamr@2
  1961
  iterator insert(const iterator& __p, const _Self& __r)
williamr@2
  1962
  { insert(__p.index(), __r); return __p; }
williamr@2
  1963
  iterator insert(const iterator& __p, size_t __n, _CharT __c)
williamr@2
  1964
  { insert(__p.index(), __n, __c); return __p; }
williamr@2
  1965
  iterator insert(const iterator& __p, _CharT __c) 
williamr@2
  1966
  { insert(__p.index(), __c); return __p; }
williamr@2
  1967
  iterator insert(const iterator& __p ) 
williamr@2
  1968
  { insert(__p.index()); return __p; }
williamr@2
  1969
  iterator insert(const iterator& __p, const _CharT* c_string) 
williamr@2
  1970
  { insert(__p.index(), c_string); return __p; }
williamr@2
  1971
  iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
williamr@2
  1972
  { insert(__p.index(), __i, __n); return __p; }
williamr@2
  1973
  iterator insert(const iterator& __p, const _CharT* __i, 
williamr@2
  1974
                  const _CharT* __j)
williamr@2
  1975
  { insert(__p.index(), __i, __j);  return __p; }
williamr@2
  1976
  iterator insert(const iterator& __p,
williamr@2
  1977
                  const const_iterator& __i, const const_iterator& __j)
williamr@2
  1978
  { insert(__p.index(), __i, __j); return __p; }
williamr@2
  1979
  iterator insert(const iterator& __p,
williamr@2
  1980
                  const iterator& __i, const iterator& __j)
williamr@2
  1981
  { insert(__p.index(), __i, __j); return __p; }
williamr@2
  1982
williamr@2
  1983
  // Replace, range variants.
williamr@2
  1984
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  1985
               const _Self& __r)
williamr@2
  1986
  { replace(__p.index(), __q.index() - __p.index(), __r); }
williamr@2
  1987
  void replace(const iterator& __p, const iterator& __q, _CharT __c)
williamr@2
  1988
  { replace(__p.index(), __q.index() - __p.index(), __c); }
williamr@2
  1989
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  1990
               const _CharT* __c_string)
williamr@2
  1991
  { replace(__p.index(), __q.index() - __p.index(), __c_string); }
williamr@2
  1992
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  1993
               const _CharT* __i, size_t __n)
williamr@2
  1994
  { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
williamr@2
  1995
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  1996
               const _CharT* __i, const _CharT* __j)
williamr@2
  1997
  { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2
  1998
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  1999
               const const_iterator& __i, const const_iterator& __j)
williamr@2
  2000
  { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2
  2001
  void replace(const iterator& __p, const iterator& __q,
williamr@2
  2002
               const iterator& __i, const iterator& __j)
williamr@2
  2003
  { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
williamr@2
  2004
williamr@2
  2005
  // Replace, iterator variants.
williamr@2
  2006
  void replace(const iterator& __p, const _Self& __r)
williamr@2
  2007
  { replace(__p.index(), __r); }
williamr@2
  2008
  void replace(const iterator& __p, _CharT __c)
williamr@2
  2009
  { replace(__p.index(), __c); }
williamr@2
  2010
  void replace(const iterator& __p, const _CharT* __c_string)
williamr@2
  2011
  { replace(__p.index(), __c_string); }
williamr@2
  2012
  void replace(const iterator& __p, const _CharT* __i, size_t __n)
williamr@2
  2013
  { replace(__p.index(), __i, __n); }
williamr@2
  2014
  void replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
williamr@2
  2015
  { replace(__p.index(), __i, __j); }
williamr@2
  2016
  void replace(const iterator& __p, const_iterator __i, 
williamr@2
  2017
               const_iterator __j)
williamr@2
  2018
  { replace(__p.index(), __i, __j); }
williamr@2
  2019
  void replace(const iterator& __p, iterator __i, iterator __j)
williamr@2
  2020
  { replace(__p.index(), __i, __j); }
williamr@2
  2021
williamr@2
  2022
  // Iterator and range variants of erase
williamr@2
  2023
  iterator erase(const iterator& __p, const iterator& __q) {
williamr@2
  2024
    size_t __p_index = __p.index();
williamr@2
  2025
    erase(__p_index, __q.index() - __p_index);
williamr@2
  2026
    return iterator(this, __p_index);
williamr@2
  2027
  }
williamr@2
  2028
  iterator erase(const iterator& __p) {
williamr@2
  2029
    size_t __p_index = __p.index();
williamr@2
  2030
    erase(__p_index, 1);
williamr@2
  2031
    return iterator(this, __p_index);
williamr@2
  2032
  }
williamr@2
  2033
williamr@2
  2034
  _Self substr(size_t __start, size_t __len = 1) const {
williamr@2
  2035
    return rope<_CharT,_Alloc>(
williamr@2
  2036
                               _S_substring(_M_tree_ptr._M_data, __start, __start + __len));
williamr@2
  2037
  }
williamr@2
  2038
williamr@2
  2039
  _Self substr(iterator __start, iterator __end) const {
williamr@2
  2040
    return rope<_CharT,_Alloc>(
williamr@2
  2041
                               _S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
williamr@2
  2042
  }
williamr@2
  2043
        
williamr@2
  2044
  _Self substr(iterator __start) const {
williamr@2
  2045
    size_t __pos = __start.index();
williamr@2
  2046
    return rope<_CharT,_Alloc>(
williamr@2
  2047
                               _S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
williamr@2
  2048
  }
williamr@2
  2049
        
williamr@2
  2050
  _Self substr(const_iterator __start, const_iterator __end) const {
williamr@2
  2051
    // This might eventually take advantage of the cache in the
williamr@2
  2052
    // iterator.
williamr@2
  2053
    return rope<_CharT,_Alloc>(
williamr@2
  2054
                               _S_substring(_M_tree_ptr._M_data, __start.index(), __end.index()));
williamr@2
  2055
  }
williamr@2
  2056
williamr@2
  2057
  rope<_CharT,_Alloc> substr(const_iterator __start) {
williamr@2
  2058
    size_t __pos = __start.index();
williamr@2
  2059
    return rope<_CharT,_Alloc>(
williamr@2
  2060
                               _S_substring(_M_tree_ptr._M_data, __pos, __pos + 1));
williamr@2
  2061
  }
williamr@2
  2062
williamr@2
  2063
  enum { npos = -1 };
williamr@2
  2064
williamr@2
  2065
  //         static const size_type npos;
williamr@2
  2066
williamr@2
  2067
  size_type find(_CharT __c, size_type __pos = 0) const;
williamr@2
  2068
  size_type find(const _CharT* __s, size_type __pos = 0) const {
williamr@2
  2069
    size_type __result_pos;
williamr@2
  2070
    const_iterator __result = search(const_begin() + (ptrdiff_t)__pos, const_end(),
williamr@2
  2071
                                     __s, __s + _S_char_ptr_len(__s));
williamr@2
  2072
    __result_pos = __result.index();
williamr@2
  2073
#           ifndef _STLP_OLD_ROPE_SEMANTICS
williamr@2
  2074
    if (__result_pos == size()) __result_pos = npos;
williamr@2
  2075
#           endif
williamr@2
  2076
    return __result_pos;
williamr@2
  2077
  }
williamr@2
  2078
williamr@2
  2079
  iterator mutable_begin() {
williamr@2
  2080
    return(iterator(this, 0));
williamr@2
  2081
  }
williamr@2
  2082
williamr@2
  2083
  iterator mutable_end() {
williamr@2
  2084
    return(iterator(this, size()));
williamr@2
  2085
  }
williamr@2
  2086
williamr@2
  2087
  reverse_iterator mutable_rbegin() {
williamr@2
  2088
    return reverse_iterator(mutable_end());
williamr@2
  2089
  }
williamr@2
  2090
williamr@2
  2091
  reverse_iterator mutable_rend() {
williamr@2
  2092
    return reverse_iterator(mutable_begin());
williamr@2
  2093
  }
williamr@2
  2094
williamr@2
  2095
  reference mutable_reference_at(size_type __pos) {
williamr@2
  2096
    return reference(this, __pos);
williamr@2
  2097
  }
williamr@2
  2098
williamr@2
  2099
#       ifdef __STD_STUFF
williamr@2
  2100
  reference operator[] (size_type __pos) {
williamr@2
  2101
    return reference(this, __pos);
williamr@2
  2102
  }
williamr@2
  2103
williamr@2
  2104
  reference at(size_type __pos) {
williamr@2
  2105
    // if (__pos >= size()) throw out_of_range;  // XXX
williamr@2
  2106
    return (*this)[__pos];
williamr@2
  2107
  }
williamr@2
  2108
williamr@2
  2109
  void resize(size_type, _CharT) {}
williamr@2
  2110
  void resize(size_type) {}
williamr@2
  2111
  void reserve(size_type = 0) {}
williamr@2
  2112
  size_type capacity() const {
williamr@2
  2113
    return max_size();
williamr@2
  2114
  }
williamr@2
  2115
williamr@2
  2116
  // Stuff below this line is dangerous because it's error prone.
williamr@2
  2117
  // I would really like to get rid of it.
williamr@2
  2118
  // copy function with funny arg ordering.
williamr@2
  2119
  size_type copy(_CharT* __buffer, size_type __n, 
williamr@2
  2120
                 size_type __pos = 0) const {
williamr@2
  2121
    return copy(__pos, __n, __buffer);
williamr@2
  2122
  }
williamr@2
  2123
williamr@2
  2124
  iterator end() { return mutable_end(); }
williamr@2
  2125
williamr@2
  2126
  iterator begin() { return mutable_begin(); }
williamr@2
  2127
williamr@2
  2128
  reverse_iterator rend() { return mutable_rend(); }
williamr@2
  2129
williamr@2
  2130
  reverse_iterator rbegin() { return mutable_rbegin(); }
williamr@2
  2131
williamr@2
  2132
#       else
williamr@2
  2133
williamr@2
  2134
  const_iterator end() { return const_end(); }
williamr@2
  2135
williamr@2
  2136
  const_iterator begin() { return const_begin(); }
williamr@2
  2137
williamr@2
  2138
  const_reverse_iterator rend() { return const_rend(); }
williamr@2
  2139
  
williamr@2
  2140
  const_reverse_iterator rbegin() { return const_rbegin(); }
williamr@2
  2141
williamr@2
  2142
#	endif
williamr@2
  2143
williamr@2
  2144
  __ROPE_DEFINE_ALLOCS(_Alloc, _M_tree_ptr)
williamr@2
  2145
    };
williamr@2
  2146
williamr@2
  2147
# undef __ROPE_DEFINE_ALLOC
williamr@2
  2148
# undef __ROPE_DEFINE_ALLOCS
williamr@2
  2149
williamr@2
  2150
template <class _CharT, class _Alloc>
williamr@2
  2151
inline _CharT 
williamr@2
  2152
_Rope_const_iterator< _CharT, _Alloc>::operator[](size_t __n)
williamr@2
  2153
{
williamr@2
  2154
  return rope<_CharT,_Alloc>::_S_fetch(this->_M_root, this->_M_current_pos + __n);
williamr@2
  2155
}
williamr@2
  2156
williamr@2
  2157
template <class _CharT, class _Alloc>
williamr@2
  2158
inline bool operator== (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2159
                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2160
  return (__x._M_current_pos == __y._M_current_pos && 
williamr@2
  2161
          __x._M_root == __y._M_root);
williamr@2
  2162
}
williamr@2
  2163
williamr@2
  2164
template <class _CharT, class _Alloc>
williamr@2
  2165
inline bool operator< (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2166
                       const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2167
  return (__x._M_current_pos < __y._M_current_pos);
williamr@2
  2168
}
williamr@2
  2169
williamr@2
  2170
#ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
williamr@2
  2171
williamr@2
  2172
template <class _CharT, class _Alloc>
williamr@2
  2173
inline bool operator!= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2174
                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2175
  return !(__x == __y);
williamr@2
  2176
}
williamr@2
  2177
williamr@2
  2178
template <class _CharT, class _Alloc>
williamr@2
  2179
inline bool operator> (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2180
                       const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2181
  return __y < __x;
williamr@2
  2182
}
williamr@2
  2183
williamr@2
  2184
template <class _CharT, class _Alloc>
williamr@2
  2185
inline bool operator<= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2186
                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2187
  return !(__y < __x);
williamr@2
  2188
}
williamr@2
  2189
williamr@2
  2190
template <class _CharT, class _Alloc>
williamr@2
  2191
inline bool operator>= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2192
                        const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2193
  return !(__x < __y);
williamr@2
  2194
}
williamr@2
  2195
williamr@2
  2196
#endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2
  2197
williamr@2
  2198
template <class _CharT, class _Alloc>
williamr@2
  2199
inline ptrdiff_t operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x,
williamr@2
  2200
                           const _Rope_const_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2201
  return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
williamr@2
  2202
}
williamr@2
  2203
williamr@2
  2204
#if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000		// dwa 8/21/97  - "ambiguous access to overloaded function" bug.
williamr@2
  2205
template <class _CharT, class _Alloc>
williamr@2
  2206
inline _Rope_const_iterator<_CharT,_Alloc>
williamr@2
  2207
operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
williamr@2
  2208
  return _Rope_const_iterator<_CharT,_Alloc>(
williamr@2
  2209
                                             __x._M_root, __x._M_current_pos - __n);
williamr@2
  2210
}
williamr@2
  2211
# endif
williamr@2
  2212
williamr@2
  2213
template <class _CharT, class _Alloc>
williamr@2
  2214
inline _Rope_const_iterator<_CharT,_Alloc>
williamr@2
  2215
operator+(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
williamr@2
  2216
  return _Rope_const_iterator<_CharT,_Alloc>(
williamr@2
  2217
                                             __x._M_root, __x._M_current_pos + __n);
williamr@2
  2218
}
williamr@2
  2219
williamr@2
  2220
template <class _CharT, class _Alloc>
williamr@2
  2221
inline _Rope_const_iterator<_CharT,_Alloc>
williamr@2
  2222
operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT,_Alloc>& __x) {
williamr@2
  2223
  return _Rope_const_iterator<_CharT,_Alloc>(
williamr@2
  2224
                                             __x._M_root, __x._M_current_pos + __n);
williamr@2
  2225
}
williamr@2
  2226
williamr@2
  2227
template <class _CharT, class _Alloc>
williamr@2
  2228
inline bool operator== (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2229
                        const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2230
  return (__x._M_current_pos == __y._M_current_pos && 
williamr@2
  2231
          __x._M_root_rope == __y._M_root_rope);
williamr@2
  2232
}
williamr@2
  2233
williamr@2
  2234
template <class _CharT, class _Alloc>
williamr@2
  2235
inline bool operator< (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2236
                       const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2237
  return (__x._M_current_pos < __y._M_current_pos);
williamr@2
  2238
}
williamr@2
  2239
williamr@2
  2240
#ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
williamr@2
  2241
williamr@2
  2242
template <class _CharT, class _Alloc>
williamr@2
  2243
inline bool operator!= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2244
                        const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2245
  return !(__x == __y);
williamr@2
  2246
}
williamr@2
  2247
williamr@2
  2248
template <class _CharT, class _Alloc>
williamr@2
  2249
inline bool operator> (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2250
                       const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2251
  return __y < __x;
williamr@2
  2252
}
williamr@2
  2253
williamr@2
  2254
template <class _CharT, class _Alloc>
williamr@2
  2255
inline bool operator<= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2256
                        const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2257
  return !(__y < __x);
williamr@2
  2258
}
williamr@2
  2259
williamr@2
  2260
template <class _CharT, class _Alloc>
williamr@2
  2261
inline bool operator>= (const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2262
                        const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2263
  return !(__x < __y);
williamr@2
  2264
}
williamr@2
  2265
williamr@2
  2266
#endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2
  2267
williamr@2
  2268
template <class _CharT, class _Alloc>
williamr@2
  2269
inline ptrdiff_t operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2270
                           const _Rope_iterator<_CharT,_Alloc>& __y) {
williamr@2
  2271
  return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
williamr@2
  2272
}
williamr@2
  2273
williamr@2
  2274
#if !defined( __MWERKS__ ) || __MWERKS__ >= 0x2000		// dwa 8/21/97  - "ambiguous access to overloaded function" bug.
williamr@2
  2275
template <class _CharT, class _Alloc>
williamr@2
  2276
inline _Rope_iterator<_CharT,_Alloc>
williamr@2
  2277
operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2278
          ptrdiff_t __n) {
williamr@2
  2279
  return _Rope_iterator<_CharT,_Alloc>(
williamr@2
  2280
                                       __x._M_root_rope, __x._M_current_pos - __n);
williamr@2
  2281
}
williamr@2
  2282
# endif
williamr@2
  2283
williamr@2
  2284
template <class _CharT, class _Alloc>
williamr@2
  2285
inline _Rope_iterator<_CharT,_Alloc>
williamr@2
  2286
operator+(const _Rope_iterator<_CharT,_Alloc>& __x,
williamr@2
  2287
          ptrdiff_t __n) {
williamr@2
  2288
  return _Rope_iterator<_CharT,_Alloc>(
williamr@2
  2289
                                       __x._M_root_rope, __x._M_current_pos + __n);
williamr@2
  2290
}
williamr@2
  2291
williamr@2
  2292
template <class _CharT, class _Alloc>
williamr@2
  2293
inline _Rope_iterator<_CharT,_Alloc>
williamr@2
  2294
operator+(ptrdiff_t __n, const _Rope_iterator<_CharT,_Alloc>& __x) {
williamr@2
  2295
  return _Rope_iterator<_CharT,_Alloc>(
williamr@2
  2296
                                       __x._M_root_rope, __x._M_current_pos + __n);
williamr@2
  2297
}
williamr@2
  2298
williamr@2
  2299
template <class _CharT, class _Alloc>
williamr@2
  2300
inline
williamr@2
  2301
rope<_CharT,_Alloc>
williamr@2
  2302
operator+ (const rope<_CharT,_Alloc>& __left,
williamr@2
  2303
           const rope<_CharT,_Alloc>& __right)
williamr@2
  2304
{
williamr@2
  2305
  _STLP_ASSERT(__left.get_allocator() == __right.get_allocator())
williamr@2
  2306
  return rope<_CharT,_Alloc>(rope<_CharT,_Alloc>::_S_concat_rep(__left._M_tree_ptr._M_data, __right._M_tree_ptr._M_data));
williamr@2
  2307
  // Inlining this should make it possible to keep __left and
williamr@2
  2308
  // __right in registers.
williamr@2
  2309
}
williamr@2
  2310
williamr@2
  2311
template <class _CharT, class _Alloc>
williamr@2
  2312
inline
williamr@2
  2313
rope<_CharT,_Alloc>&
williamr@2
  2314
operator+= (rope<_CharT,_Alloc>& __left, 
williamr@2
  2315
            const rope<_CharT,_Alloc>& __right)
williamr@2
  2316
{
williamr@2
  2317
  __left.append(__right);
williamr@2
  2318
  return __left;
williamr@2
  2319
}
williamr@2
  2320
williamr@2
  2321
template <class _CharT, class _Alloc>
williamr@2
  2322
inline
williamr@2
  2323
rope<_CharT,_Alloc>
williamr@2
  2324
operator+ (const rope<_CharT,_Alloc>& __left,
williamr@2
  2325
           const _CharT* __right) {
williamr@2
  2326
  size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
williamr@2
  2327
  return rope<_CharT,_Alloc>(
williamr@2
  2328
                             rope<_CharT,_Alloc>::_S_concat_char_iter(
williamr@2
  2329
                                                                      __left._M_tree_ptr._M_data, __right, __rlen)); 
williamr@2
  2330
}
williamr@2
  2331
williamr@2
  2332
template <class _CharT, class _Alloc>
williamr@2
  2333
inline
williamr@2
  2334
rope<_CharT,_Alloc>&
williamr@2
  2335
operator+= (rope<_CharT,_Alloc>& __left,
williamr@2
  2336
            const _CharT* __right) {
williamr@2
  2337
  __left.append(__right);
williamr@2
  2338
  return __left;
williamr@2
  2339
}
williamr@2
  2340
williamr@2
  2341
template <class _CharT, class _Alloc>
williamr@2
  2342
inline
williamr@2
  2343
rope<_CharT,_Alloc>
williamr@2
  2344
operator+ (const rope<_CharT,_Alloc>& __left, _STLP_SIMPLE_TYPE(_CharT) __right) {
williamr@2
  2345
  return rope<_CharT,_Alloc>(
williamr@2
  2346
                             rope<_CharT,_Alloc>::_S_concat_char_iter(
williamr@2
  2347
                                                                      __left._M_tree_ptr._M_data, &__right, 1));
williamr@2
  2348
}
williamr@2
  2349
williamr@2
  2350
template <class _CharT, class _Alloc>
williamr@2
  2351
inline
williamr@2
  2352
rope<_CharT,_Alloc>&
williamr@2
  2353
operator+= (rope<_CharT,_Alloc>& __left, _STLP_SIMPLE_TYPE(_CharT) __right) {
williamr@2
  2354
  __left.append(__right);
williamr@2
  2355
  return __left;
williamr@2
  2356
}
williamr@2
  2357
williamr@2
  2358
template <class _CharT, class _Alloc>
williamr@2
  2359
inline bool
williamr@2
  2360
operator< (const rope<_CharT,_Alloc>& __left, 
williamr@2
  2361
           const rope<_CharT,_Alloc>& __right) {
williamr@2
  2362
  return __left.compare(__right) < 0;
williamr@2
  2363
}
williamr@2
  2364
        
williamr@2
  2365
template <class _CharT, class _Alloc>
williamr@2
  2366
inline bool
williamr@2
  2367
operator== (const rope<_CharT,_Alloc>& __left, 
williamr@2
  2368
            const rope<_CharT,_Alloc>& __right) {
williamr@2
  2369
  return __left.compare(__right) == 0;
williamr@2
  2370
}
williamr@2
  2371
williamr@2
  2372
#ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
williamr@2
  2373
williamr@2
  2374
template <class _CharT, class _Alloc>
williamr@2
  2375
inline bool
williamr@2
  2376
operator!= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2
  2377
  return !(__x == __y);
williamr@2
  2378
}
williamr@2
  2379
williamr@2
  2380
template <class _CharT, class _Alloc>
williamr@2
  2381
inline bool
williamr@2
  2382
operator> (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2
  2383
  return __y < __x;
williamr@2
  2384
}
williamr@2
  2385
williamr@2
  2386
template <class _CharT, class _Alloc>
williamr@2
  2387
inline bool
williamr@2
  2388
operator<= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2
  2389
  return !(__y < __x);
williamr@2
  2390
}
williamr@2
  2391
williamr@2
  2392
template <class _CharT, class _Alloc>
williamr@2
  2393
inline bool
williamr@2
  2394
operator>= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
williamr@2
  2395
  return !(__x < __y);
williamr@2
  2396
}
williamr@2
  2397
williamr@2
  2398
template <class _CharT, class _Alloc>
williamr@2
  2399
inline bool operator!= (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
williamr@2
  2400
                        const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
williamr@2
  2401
  return !(__x == __y);
williamr@2
  2402
}
williamr@2
  2403
williamr@2
  2404
#endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
williamr@2
  2405
williamr@2
  2406
template <class _CharT, class _Alloc>
williamr@2
  2407
inline bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
williamr@2
  2408
                        const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
williamr@2
  2409
  return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root);
williamr@2
  2410
}
williamr@2
  2411
williamr@2
  2412
#ifdef _STLP_USE_NEW_IOSTREAMS
williamr@2
  2413
template<class _CharT, class _Traits, class _Alloc>
williamr@2
  2414
basic_ostream<_CharT, _Traits>& operator<< (
williamr@2
  2415
                                            basic_ostream<_CharT, _Traits>& __o,
williamr@2
  2416
                                            const rope<_CharT, _Alloc>& __r);
williamr@2
  2417
#elif ! defined (_STLP_USE_NO_IOSTREAMS)
williamr@2
  2418
template<class _CharT, class _Alloc>
williamr@2
  2419
ostream& operator<< (ostream& __o, const rope<_CharT,_Alloc>& __r);        
williamr@2
  2420
#endif
williamr@2
  2421
        
williamr@2
  2422
typedef rope<char, _STLP_DEFAULT_ALLOCATOR(char) > crope;
williamr@2
  2423
# ifdef _STLP_HAS_WCHAR_T
williamr@2
  2424
typedef rope<wchar_t, _STLP_DEFAULT_ALLOCATOR(wchar_t) > wrope;
williamr@2
  2425
# endif
williamr@2
  2426
williamr@2
  2427
inline crope::reference __mutable_reference_at(crope& __c, size_t __i)
williamr@2
  2428
{
williamr@2
  2429
  return __c.mutable_reference_at(__i);
williamr@2
  2430
}
williamr@2
  2431
williamr@2
  2432
# ifdef _STLP_HAS_WCHAR_T
williamr@2
  2433
inline wrope::reference __mutable_reference_at(wrope& __c, size_t __i)
williamr@2
  2434
{
williamr@2
  2435
  return __c.mutable_reference_at(__i);
williamr@2
  2436
}
williamr@2
  2437
# endif
williamr@2
  2438
williamr@2
  2439
#ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
williamr@2
  2440
williamr@2
  2441
template <class _CharT, class _Alloc>
williamr@2
  2442
inline void swap(rope<_CharT,_Alloc>& __x, rope<_CharT,_Alloc>& __y) {
williamr@2
  2443
  __x.swap(__y);
williamr@2
  2444
}
williamr@2
  2445
#else
williamr@2
  2446
williamr@2
  2447
inline void swap(crope& __x, crope& __y) { __x.swap(__y); }
williamr@2
  2448
# ifdef _STLP_HAS_WCHAR_T	// dwa 8/21/97
williamr@2
  2449
inline void swap(wrope& __x, wrope& __y) { __x.swap(__y); }
williamr@2
  2450
# endif
williamr@2
  2451
williamr@2
  2452
#endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
williamr@2
  2453
williamr@2
  2454
williamr@2
  2455
// Hash functions should probably be revisited later:
williamr@2
  2456
_STLP_TEMPLATE_NULL struct hash<crope>
williamr@2
  2457
{
williamr@2
  2458
  size_t operator()(const crope& __str) const
williamr@2
  2459
  {
williamr@2
  2460
    size_t _p_size = __str.size();
williamr@2
  2461
williamr@2
  2462
    if (0 == _p_size) return 0;
williamr@2
  2463
    return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
williamr@2
  2464
  }
williamr@2
  2465
};
williamr@2
  2466
williamr@2
  2467
# ifdef _STLP_HAS_WCHAR_T	// dwa 8/21/97
williamr@2
  2468
_STLP_TEMPLATE_NULL struct hash<wrope>
williamr@2
  2469
{
williamr@2
  2470
  size_t operator()(const wrope& __str) const
williamr@2
  2471
  {
williamr@2
  2472
    size_t _p_size = __str.size();
williamr@2
  2473
williamr@2
  2474
    if (0 == _p_size) return 0;
williamr@2
  2475
    return 13*__str[0] + 5*__str[_p_size - 1] + _p_size;
williamr@2
  2476
  }
williamr@2
  2477
};
williamr@2
  2478
#endif
williamr@2
  2479
williamr@2
  2480
#ifndef _STLP_MSVC
williamr@2
  2481
// I couldn't get this to work with VC++
williamr@2
  2482
template<class _CharT,class _Alloc>
williamr@2
  2483
void
williamr@2
  2484
_Rope_rotate(_Rope_iterator<_CharT,_Alloc> __first,
williamr@2
  2485
             _Rope_iterator<_CharT,_Alloc> __middle,
williamr@2
  2486
             _Rope_iterator<_CharT,_Alloc> __last);
williamr@2
  2487
williamr@2
  2488
#if !defined(__GNUC__)
williamr@2
  2489
// Appears to confuse g++
williamr@2
  2490
inline void rotate(_Rope_iterator<char,_STLP_DEFAULT_ALLOCATOR(char) > __first,
williamr@2
  2491
                   _Rope_iterator<char,_STLP_DEFAULT_ALLOCATOR(char) > __middle,
williamr@2
  2492
                   _Rope_iterator<char,_STLP_DEFAULT_ALLOCATOR(char) > __last) {
williamr@2
  2493
  _Rope_rotate(__first, __middle, __last);
williamr@2
  2494
}
williamr@2
  2495
#endif
williamr@2
  2496
williamr@2
  2497
#endif
williamr@2
  2498
williamr@2
  2499
template <class _CharT, class _Alloc>
williamr@2
  2500
inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const
williamr@2
  2501
{
williamr@2
  2502
  if (_M_current_valid) {
williamr@2
  2503
	return _M_current;
williamr@2
  2504
  } else {
williamr@2
  2505
    return _My_rope::_S_fetch(_M_root->_M_tree_ptr._M_data, _M_pos);
williamr@2
  2506
  }
williamr@2
  2507
}
williamr@2
  2508
_STLP_END_NAMESPACE
williamr@2
  2509
williamr@2
  2510
# if !defined (_STLP_LINK_TIME_INSTANTIATION)
williamr@2
  2511
#  include <stl/_rope.c>
williamr@2
  2512
# endif
williamr@2
  2513
williamr@2
  2514
# endif /* _STLP_INTERNAL_ROPE_H */
williamr@2
  2515
williamr@2
  2516
// Local Variables:
williamr@2
  2517
// mode:C++
williamr@2
  2518
// End: