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