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