diff options
author | jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4> | 1998-09-02 17:25:15 +0000 |
---|---|---|
committer | jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4> | 1998-09-02 17:25:15 +0000 |
commit | 79f8bd54b87824aefef05ecc6431fb51215b1af7 (patch) | |
tree | f1b8986118d98ea46eb8767eba9ca67b12541d4f /libstdc++/stl/ropeimpl.h | |
parent | 0df40fee7af4306861fad8917697f25d7e2b4ea8 (diff) | |
download | gcc-79f8bd54b87824aefef05ecc6431fb51215b1af7.tar.gz |
* algorithm alloc.h defalloc.h hash_map.h hash_set.h iterator
memory pthread_alloc pthread_alloc.h rope ropeimpl.h stl_algo.h
stl_algobase.h stl_alloc.h stl_bvector.h stl_config.h
stl_construct.h stl_deque.h stl_function.h stl_hash_fun.h
stl_hash_map.h stl_hash_set.h stl_hashtable.h stl_heap.h
stl_iterator.h stl_list.h stl_map.h stl_multimap.h stl_multiset.h
stl_numeric.h stl_pair.h stl_queue.h stl_raw_storage_iter.h
stl_relops.h stl_rope.h stl_set.h stl_slist.h stl_stack.h
stl_tempbuf.h stl_tree.h stl_uninitialized.h stl_vector.h
tempbuf.h type_traits.h: Update to SGI STL 3.11.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@22190 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++/stl/ropeimpl.h')
-rw-r--r-- | libstdc++/stl/ropeimpl.h | 1939 |
1 files changed, 976 insertions, 963 deletions
diff --git a/libstdc++/stl/ropeimpl.h b/libstdc++/stl/ropeimpl.h index b4af525c38e..18bb2c9ec9d 100644 --- a/libstdc++/stl/ropeimpl.h +++ b/libstdc++/stl/ropeimpl.h @@ -15,8 +15,8 @@ * You should not attempt to use it directly. */ -# include <stdio.h> -# include <iostream.h> +# include <stdio.h> /* XXX should use <cstdio> */ +# include <iostream.h> /* XXX should use <iostream> */ __STL_BEGIN_NAMESPACE @@ -25,45 +25,46 @@ __STL_BEGIN_NAMESPACE #endif // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf -// if necessary. Assumes path_end[leaf_index] and leaf_pos are correct. +// if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct. // Results in a valid buf_ptr if the iterator can be legitimately // dereferenced. -template <class charT, class Alloc> -void __rope_iterator_base<charT,Alloc>::setbuf -(__rope_iterator_base<charT,Alloc> &x) +template <class _CharT, class _Alloc> +void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf( + _Rope_iterator_base<_CharT,_Alloc>& __x) { - const RopeBase * leaf = x.path_end[x.leaf_index]; - size_t leaf_pos = x.leaf_pos; - size_t pos = x.current_pos; - - switch(leaf -> tag) { - case RopeBase::leaf: - x.buf_start = ((__rope_RopeLeaf<charT,Alloc> *)leaf) -> data; - x.buf_ptr = x.buf_start + (pos - leaf_pos); - x.buf_end = x.buf_start + leaf -> size; + const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index]; + size_t __leaf_pos = __x._M_leaf_pos; + size_t __pos = __x._M_current_pos; + + switch(__leaf->_M_tag) { + case _RopeRep::_S_leaf: + __x._M_buf_start = + ((_Rope_RopeLeaf<_CharT,_Alloc>*)__leaf)->_M_data; + __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos); + __x._M_buf_end = __x._M_buf_start + __leaf->_M_size; break; - case RopeBase::function: - case RopeBase::substringfn: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: { - size_t len = iterator_buf_len; - size_t buf_start_pos = leaf_pos; - size_t leaf_end = leaf_pos + leaf -> size; - char_producer<charT> *fn = - ((__rope_RopeFunction<charT,Alloc> *)leaf) -> fn; - - if (buf_start_pos + len <= pos) { - buf_start_pos = pos - len/4; - if (buf_start_pos + len > leaf_end) { - buf_start_pos = leaf_end - len; + size_t __len = _S_iterator_buf_len; + size_t __buf_start_pos = __leaf_pos; + size_t __leaf_end = __leaf_pos + __leaf->_M_size; + char_producer<_CharT>* __fn = + ((_Rope_RopeFunction<_CharT,_Alloc>*)__leaf)->_M_fn; + + if (__buf_start_pos + __len <= __pos) { + __buf_start_pos = __pos - __len/4; + if (__buf_start_pos + __len > __leaf_end) { + __buf_start_pos = __leaf_end - __len; } } - if (buf_start_pos + len > leaf_end) { - len = leaf_end - buf_start_pos; + if (__buf_start_pos + __len > __leaf_end) { + __len = __leaf_end - __buf_start_pos; } - (*fn)(buf_start_pos - leaf_pos, len, x.tmp_buf); - x.buf_ptr = x.tmp_buf + (pos - buf_start_pos); - x.buf_start = x.tmp_buf; - x.buf_end = x.tmp_buf + len; + (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf); + __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos); + __x._M_buf_start = __x._M_tmp_buf; + __x._M_buf_end = __x._M_tmp_buf + __len; } break; default: @@ -73,339 +74,305 @@ void __rope_iterator_base<charT,Alloc>::setbuf // Set path and buffer inside a rope iterator. We assume that // pos and root are already set. -template <class charT, class Alloc> -void __rope_iterator_base<charT,Alloc>::setcache -(__rope_iterator_base<charT,Alloc> &x) +template <class _CharT, class _Alloc> +void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache +(_Rope_iterator_base<_CharT,_Alloc>& __x) { - const RopeBase * path[RopeBase::max_rope_depth+1]; - const RopeBase * curr_rope; - int curr_depth = -1; /* index into path */ - size_t curr_start_pos = 0; - size_t pos = x.current_pos; - unsigned char dirns = 0; // Bit vector indicating right turns in the path - - __stl_assert(pos <= x.root -> size); - if (pos >= x.root -> size) { - x.buf_ptr = 0; + const _RopeRep* __path[_RopeRep::_S_max_rope_depth+1]; + const _RopeRep* __curr_rope; + int __curr_depth = -1; /* index into path */ + size_t __curr_start_pos = 0; + size_t __pos = __x._M_current_pos; + unsigned char __dirns = 0; // Bit vector marking right turns in the path + + __stl_assert(__pos <= __x._M_root->_M_size); + if (__pos >= __x._M_root->_M_size) { + __x._M_buf_ptr = 0; return; } - curr_rope = x.root; - if (0 != curr_rope -> c_string) { + __curr_rope = __x._M_root; + if (0 != __curr_rope->_M_c_string) { /* Treat the root as a leaf. */ - x.buf_start = curr_rope -> c_string; - x.buf_end = curr_rope -> c_string + curr_rope -> size; - x.buf_ptr = curr_rope -> c_string + pos; - x.path_end[0] = curr_rope; - x.leaf_index = 0; - x.leaf_pos = 0; + __x._M_buf_start = __curr_rope->_M_c_string; + __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size; + __x._M_buf_ptr = __curr_rope->_M_c_string + __pos; + __x._M_path_end[0] = __curr_rope; + __x._M_leaf_index = 0; + __x._M_leaf_pos = 0; return; } for(;;) { - ++curr_depth; - __stl_assert(curr_depth <= RopeBase::max_rope_depth); - path[curr_depth] = curr_rope; - switch(curr_rope -> tag) { - case RopeBase::leaf: - case RopeBase::function: - case RopeBase::substringfn: - x.leaf_pos = curr_start_pos; + ++__curr_depth; + __stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth); + __path[__curr_depth] = __curr_rope; + switch(__curr_rope->_M_tag) { + case _RopeRep::_S_leaf: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: + __x._M_leaf_pos = __curr_start_pos; goto done; - case RopeBase::concat: + case _RopeRep::_S_concat: { - __rope_RopeConcatenation<charT,Alloc> *c = - (__rope_RopeConcatenation<charT,Alloc> *)curr_rope; - RopeBase * left = c -> left; - size_t left_len = left -> size; + _Rope_RopeConcatenation<_CharT,_Alloc>* __c = + (_Rope_RopeConcatenation<_CharT,_Alloc>*)__curr_rope; + _RopeRep* __left = __c->_M_left; + size_t __left_len = __left->_M_size; - dirns <<= 1; - if (pos >= curr_start_pos + left_len) { - dirns |= 1; - curr_rope = c -> right; - curr_start_pos += left_len; + __dirns <<= 1; + if (__pos >= __curr_start_pos + __left_len) { + __dirns |= 1; + __curr_rope = __c->_M_right; + __curr_start_pos += __left_len; } else { - curr_rope = left; + __curr_rope = __left; } } break; } } done: - // Copy last section of path into path_end. + // Copy last section of path into _M_path_end. { - int i = -1; - int j = curr_depth + 1 - path_cache_len; + int __i = -1; + int __j = __curr_depth + 1 - _S_path_cache_len; - if (j < 0) j = 0; - while (j <= curr_depth) { - x.path_end[++i] = path[j++]; + if (__j < 0) __j = 0; + while (__j <= __curr_depth) { + __x._M_path_end[++__i] = __path[__j++]; } - x.leaf_index = i; + __x._M_leaf_index = __i; } - x.path_directions = dirns; - setbuf(x); + __x._M_path_directions = __dirns; + _S_setbuf(__x); } // Specialized version of the above. Assumes that // the path cache is valid for the previous position. -template <class charT, class Alloc> -void __rope_iterator_base<charT,Alloc>::setcache_for_incr -(__rope_iterator_base<charT,Alloc> &x) +template <class _CharT, class _Alloc> +void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr +(_Rope_iterator_base<_CharT,_Alloc>& __x) { - int current_index = x.leaf_index; - const RopeBase * current_node = x.path_end[current_index]; - size_t len = current_node -> size; - size_t node_start_pos = x.leaf_pos; - unsigned char dirns = x.path_directions; - __rope_RopeConcatenation<charT,Alloc> * c; - - __stl_assert(x.current_pos <= x.root -> size); - if (x.current_pos - node_start_pos < len) { + int __current_index = __x._M_leaf_index; + const _RopeRep* __current_node = __x._M_path_end[__current_index]; + size_t __len = __current_node->_M_size; + size_t __node_start_pos = __x._M_leaf_pos; + unsigned char __dirns = __x._M_path_directions; + _Rope_RopeConcatenation<_CharT,_Alloc>* __c; + + __stl_assert(__x._M_current_pos <= __x._M_root->_M_size); + if (__x._M_current_pos - __node_start_pos < __len) { /* More stuff in this leaf, we just didn't cache it. */ - setbuf(x); + _S_setbuf(__x); return; } - __stl_assert(node_start_pos + len == x.current_pos); + __stl_assert(__node_start_pos + __len == __x._M_current_pos); // node_start_pos is starting position of last_node. - while (--current_index >= 0) { - if (!(dirns & 1) /* Path turned left */) break; - current_node = x.path_end[current_index]; - c = (__rope_RopeConcatenation<charT,Alloc> *)current_node; + while (--__current_index >= 0) { + if (!(__dirns & 1) /* Path turned left */) + break; + __current_node = __x._M_path_end[__current_index]; + __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node; // Otherwise we were in the right child. Thus we should pop // the concatenation node. - node_start_pos -= c -> left -> size; - dirns >>= 1; + __node_start_pos -= __c->_M_left->_M_size; + __dirns >>= 1; } - if (current_index < 0) { + if (__current_index < 0) { // We underflowed the cache. Punt. - setcache(x); + _S_setcache(__x); return; } - current_node = x.path_end[current_index]; - c = (__rope_RopeConcatenation<charT,Alloc> *)current_node; + __current_node = __x._M_path_end[__current_index]; + __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node; // current_node is a concatenation node. We are positioned on the first // character in its right child. // node_start_pos is starting position of current_node. - node_start_pos += c -> left -> size; - current_node = c -> right; - x.path_end[++current_index] = current_node; - dirns |= 1; - while (RopeBase::concat == current_node -> tag) { - ++current_index; - if (path_cache_len == current_index) { - int i; - for (i = 0; i < path_cache_len-1; i++) { - x.path_end[i] = x.path_end[i+1]; + __node_start_pos += __c->_M_left->_M_size; + __current_node = __c->_M_right; + __x._M_path_end[++__current_index] = __current_node; + __dirns |= 1; + while (_RopeRep::_S_concat == __current_node->_M_tag) { + ++__current_index; + if (_S_path_cache_len == __current_index) { + int __i; + for (__i = 0; __i < _S_path_cache_len-1; __i++) { + __x._M_path_end[__i] = __x._M_path_end[__i+1]; } - --current_index; + --__current_index; } - current_node = - ((__rope_RopeConcatenation<charT,Alloc> *)current_node) -> left; - x.path_end[current_index] = current_node; - dirns <<= 1; + __current_node = + ((_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node)->_M_left; + __x._M_path_end[__current_index] = __current_node; + __dirns <<= 1; // node_start_pos is unchanged. } - x.leaf_index = current_index; - x.leaf_pos = node_start_pos; - x.path_directions = dirns; - setbuf(x); + __x._M_leaf_index = __current_index; + __x._M_leaf_pos = __node_start_pos; + __x._M_path_directions = __dirns; + _S_setbuf(__x); } -template <class charT, class Alloc> -void __rope_iterator_base<charT,Alloc>::incr(size_t n) { - current_pos += n; - if (0 != buf_ptr) { - size_t chars_left = buf_end - buf_ptr; - if (chars_left > n) { - buf_ptr += n; - } else if (chars_left == n) { - buf_ptr += n; - setcache_for_incr(*this); +template <class _CharT, class _Alloc> +void _Rope_iterator_base<_CharT,_Alloc>::_M_incr(size_t __n) { + _M_current_pos += __n; + if (0 != _M_buf_ptr) { + size_t __chars_left = _M_buf_end - _M_buf_ptr; + if (__chars_left > __n) { + _M_buf_ptr += __n; + } else if (__chars_left == __n) { + _M_buf_ptr += __n; + _S_setcache_for_incr(*this); } else { - buf_ptr = 0; + _M_buf_ptr = 0; } } } -template <class charT, class Alloc> -void __rope_iterator_base<charT,Alloc>::decr(size_t n) { - if (0 != buf_ptr) { - size_t chars_left = buf_ptr - buf_start; - if (chars_left >= n) { - buf_ptr -= n; +template <class _CharT, class _Alloc> +void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) { + if (0 != _M_buf_ptr) { + size_t __chars_left = _M_buf_ptr - _M_buf_start; + if (__chars_left >= __n) { + _M_buf_ptr -= __n; } else { - buf_ptr = 0; + _M_buf_ptr = 0; } } - current_pos -= n; + _M_current_pos -= __n; } -template <class charT, class Alloc> -void __rope_iterator<charT,Alloc>::check() { - if (root_rope -> tree_ptr != root) { - // Rope was modified. Get things fixed up. - RopeBase::unref(root); - root = root_rope -> tree_ptr; - RopeBase::ref(root); - buf_ptr = 0; +template <class _CharT, class _Alloc> +void _Rope_iterator<_CharT,_Alloc>::_M_check() { + if (_M_root_rope->_M_tree_ptr != _M_root) { + // _Rope was modified. Get things fixed up. + _RopeRep::_S_unref(_M_root); + _M_root = _M_root_rope->_M_tree_ptr; + _RopeRep::_S_ref(_M_root); + _M_buf_ptr = 0; } } -template <class charT, class Alloc> -inline __rope_const_iterator<charT, Alloc>::__rope_const_iterator -(const __rope_iterator<charT,Alloc> & x) -: __rope_iterator_base<charT,Alloc>(x) { } - -template <class charT, class Alloc> -inline __rope_iterator<charT,Alloc>::__rope_iterator -(rope<charT,Alloc>& r, size_t pos) - : __rope_iterator_base<charT,Alloc>(r.tree_ptr, pos), root_rope(&r) { - RopeBase::ref(root); -} - -template <class charT, class Alloc> -inline size_t rope<charT,Alloc>::char_ptr_len(const charT *s) +template <class _CharT, class _Alloc> +inline +_Rope_const_iterator<_CharT, _Alloc>::_Rope_const_iterator( + const _Rope_iterator<_CharT,_Alloc>& __x) +: _Rope_iterator_base<_CharT,_Alloc>(__x) +{ } + +template <class _CharT, class _Alloc> +inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator( + rope<_CharT,_Alloc>& __r, size_t __pos) +: _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos), + _M_root_rope(&__r) { - const charT *p = s; - - while (!is0(*p)) { ++p; } - return(p - s); + _RopeRep::_S_ref(_M_root); } -template <class charT, class Alloc> -rope<charT,Alloc>::RopeLeaf * -rope<charT,Alloc>::RopeLeaf_from_char_ptr(__GC_CONST charT *s, size_t size) +template <class _CharT, class _Alloc> +inline size_t +rope<_CharT,_Alloc>::_S_char_ptr_len(const _CharT* __s) { - RopeLeaf *t = LAlloc::allocate(); + const _CharT* __p = __s; - t -> tag = RopeBase::leaf; - if (__is_basic_char_type((charT *)0)) { - // already eos terminated. - t -> c_string = s; - } else { - t -> c_string = 0; - } - t -> is_balanced = true; - t -> depth = 0; - t -> size = size; - t -> data = s; -# ifndef __GC - t -> refcount = 1; - t -> init_refcount_lock(); -# endif - return (t); + while (!_S_is0(*__p)) { ++__p; } + return (__p - __s); } -# ifdef __GC -template <class charT, class Alloc> -void __rope_RopeBase<charT,Alloc>::fn_finalization_proc(void * tree, void *) -{ - delete ((__rope_RopeFunction<charT,Alloc> *)tree) -> fn; -} -# endif - -template <class charT, class Alloc> -rope<charT,Alloc>::RopeFunction * -rope<charT,Alloc>::RopeFunction_from_fn -(char_producer<charT> *fn, size_t size, bool delete_fn) -{ - if (0 == size) return 0; - RopeFunction *t = FAlloc::allocate(); - t -> tag = RopeBase::function; - t -> c_string = 0; - t -> is_balanced = true; - t -> depth = 0; - t -> size = size; - t -> fn = fn; -# ifdef __GC - if (delete_fn) { - GC_REGISTER_FINALIZER(t, RopeBase::fn_finalization_proc, 0, 0, 0); - } -# else - t -> delete_when_done = delete_fn; - t -> refcount = 1; - t -> init_refcount_lock(); -# endif - return (t); -} #ifndef __GC -template <class charT, class Alloc> -inline void __rope_RopeBase<charT,Alloc>::free_c_string() +template <class _CharT, class _Alloc> +inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string() { - charT * cstr = c_string; - if (0 != cstr) { - size_t sz = size + 1; - destroy(cstr, cstr + sz); - DataAlloc::deallocate(cstr, sz); + _CharT* __cstr = _M_c_string; + if (0 != __cstr) { + size_t __size = _M_size + 1; + destroy(__cstr, __cstr + __size); + _Data_deallocate(__cstr, __size); } } -template <class charT, class Alloc> -inline void __rope_RopeBase<charT,Alloc>::free_string(charT* s, size_t n) + +template <class _CharT, class _Alloc> +#ifdef __STL_USE_STD_ALLOCATORS + inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s, + size_t __n, + allocator_type __a) +#else + inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s, + size_t __n) +#endif { - if (!__is_basic_char_type((charT *)0)) { - destroy(s, s + n); + if (!_S_is_basic_char_type((_CharT*)0)) { + destroy(__s, __s + __n); } - DataAlloc::deallocate(s, rounded_up_size(n)); +// This has to be a static member, so this gets a bit messy +# ifdef __STL_USE_STD_ALLOCATORS + __a.deallocate( + __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n)); +# else + _Data_deallocate( + __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n)); +# endif } -template <class charT, class Alloc> -void __rope_RopeBase<charT,Alloc>::free_tree() + +// There are several reasons for not doing this with virtual destructors +// and a class specific delete operator: +// - A class specific delete operator can't easily get access to +// allocator instances if we need them. +// - Any virtual function would need a 4 or byte vtable pointer; +// this only requires a one byte tag per object. +template <class _CharT, class _Alloc> +void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree() { - switch(tag) { - case leaf: + switch(_M_tag) { + case _S_leaf: { - __rope_RopeLeaf<charT,Alloc> * l = - (__rope_RopeLeaf<charT,Alloc> *)this; - charT * d = l -> data; - - if (d != c_string) { - free_c_string(); - } - free_string(d, size); - LAlloc::deallocate(l); + _Rope_RopeLeaf<_CharT,_Alloc>* __l + = (_Rope_RopeLeaf<_CharT,_Alloc>*)this; + __l->_Rope_RopeLeaf<_CharT,_Alloc>::~_Rope_RopeLeaf(); + _L_deallocate(__l, 1); + break; } - break; - case concat: + case _S_concat: { - __rope_RopeConcatenation<charT,Alloc> * c = - (__rope_RopeConcatenation<charT,Alloc> *)this; - __rope_RopeBase * left = c -> left; - __rope_RopeBase * right = c -> right; - free_c_string(); - left -> unref_nonnil(); - right -> unref_nonnil(); - CAlloc::deallocate(c); + _Rope_RopeConcatenation<_CharT,_Alloc>* __c + = (_Rope_RopeConcatenation<_CharT,_Alloc>*)this; + __c->_Rope_RopeConcatenation<_CharT,_Alloc>:: + ~_Rope_RopeConcatenation(); + _C_deallocate(__c, 1); + break; } - break; - case function: + case _S_function: { - __rope_RopeFunction<charT,Alloc> * fn = - (__rope_RopeFunction<charT,Alloc> *)this; - free_c_string(); - if ( fn -> delete_when_done) { - delete fn -> fn; - } - FAlloc::deallocate(fn); + _Rope_RopeFunction<_CharT,_Alloc>* __f + = (_Rope_RopeFunction<_CharT,_Alloc>*)this; + __f->_Rope_RopeFunction<_CharT,_Alloc>::~_Rope_RopeFunction(); + _F_deallocate(__f, 1); break; } - case substringfn: + case _S_substringfn: { - __rope_RopeSubstring<charT,Alloc> * ss = - (__rope_RopeSubstring<charT,Alloc> *)this; - __rope_RopeBase *base = ss -> base; - free_c_string(); - base -> unref_nonnil(); - SAlloc::deallocate(ss); + _Rope_RopeSubstring<_CharT,_Alloc>* __ss = + (_Rope_RopeSubstring<_CharT,_Alloc>*)this; + __ss->_Rope_RopeSubstring<_CharT,_Alloc>:: + ~_Rope_RopeSubstring(); + _S_deallocate(__ss, 1); break; } } } #else -template <class charT, class Alloc> -inline void __rope_RopeBase<charT,Alloc>::free_string(charT* s, size_t n) +template <class _CharT, class _Alloc> +#ifdef __STL_USE_STD_ALLOCATORS + inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string + (const _CharT*, size_t, allocator_type) +#else + inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string + (const _CharT*, size_t) +#endif {} #endif @@ -413,55 +380,58 @@ inline void __rope_RopeBase<charT,Alloc>::free_string(charT* s, size_t n) // Concatenate a C string onto a leaf rope by copying the rope data. // Used for short ropes. -template <class charT, class Alloc> -rope<charT,Alloc>::RopeLeaf * -rope<charT,Alloc>::leaf_concat_char_iter - (RopeLeaf * r, const charT * iter, size_t len) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeLeaf* +rope<_CharT,_Alloc>::_S_leaf_concat_char_iter + (_RopeLeaf* __r, const _CharT* __iter, size_t __len) { - size_t old_len = r -> size; - charT * new_data = (charT *) - DataAlloc::allocate(rounded_up_size(old_len + len)); - RopeLeaf * result; + size_t __old_len = __r->_M_size; + _CharT* __new_data = (_CharT*) + _Data_allocate(_S_rounded_up_size(__old_len + __len)); + _RopeLeaf* __result; - uninitialized_copy_n(r -> data, old_len, new_data); - uninitialized_copy_n(iter, len, new_data + old_len); - __cond_store_eos(new_data[old_len + len]); + uninitialized_copy_n(__r->_M_data, __old_len, __new_data); + uninitialized_copy_n(__iter, __len, __new_data + __old_len); + _S_cond_store_eos(__new_data[__old_len + __len]); __STL_TRY { - result = RopeLeaf_from_char_ptr(new_data, old_len + len); + __result = _S_new_RopeLeaf(__new_data, __old_len + __len, + __r->get_allocator()); } - __STL_UNWIND(RopeBase::free_string(new_data, old_len + len)); - return result; + __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len, + __r->get_allocator())); + return __result; } #ifndef __GC // As above, but it's OK to clobber original if refcount is 1 -template <class charT, class Alloc> -rope<charT,Alloc>::RopeLeaf * -rope<charT,Alloc>::destr_leaf_concat_char_iter - (RopeLeaf * r, const charT * iter, size_t len) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeLeaf* +rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter + (_RopeLeaf* __r, const _CharT* __iter, size_t __len) { - __stl_assert(r -> refcount >= 1); - if (r -> refcount > 1) return leaf_concat_char_iter(r, iter, len); - size_t old_len = r -> size; - if (allocated_capacity(old_len) >= old_len + len) { + __stl_assert(__r->_M_refcount >= 1); + if (__r->_M_refcount > 1) + return _S_leaf_concat_char_iter(__r, __iter, __len); + size_t __old_len = __r->_M_size; + if (_S_allocated_capacity(__old_len) >= __old_len + __len) { // The space has been partially initialized for the standard // character types. But that doesn't matter for those types. - uninitialized_copy_n(iter, len, r -> data + old_len); - if (__is_basic_char_type((charT *)0)) { - __cond_store_eos(r -> data[old_len + len]); - __stl_assert(r -> c_string == r -> data); - } else if (r -> c_string != r -> data && 0 != r -> c_string) { - r -> free_c_string(); - r -> c_string = 0; + uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len); + if (_S_is_basic_char_type((_CharT*)0)) { + _S_cond_store_eos(__r->_M_data[__old_len + __len]); + __stl_assert(__r->_M_c_string == __r->_M_data); + } else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) { + __r->_M_free_c_string(); + __r->_M_c_string = 0; } - r -> size = old_len + len; - __stl_assert(r -> refcount == 1); - r -> refcount = 2; - return r; + __r->_M_size = __old_len + __len; + __stl_assert(__r->_M_refcount == 1); + __r->_M_refcount = 2; + return __r; } else { - RopeLeaf * result = leaf_concat_char_iter(r, iter, len); - __stl_assert(result -> refcount == 1); - return result; + _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len); + __stl_assert(__result->_M_refcount == 1); + return __result; } } #endif @@ -469,292 +439,300 @@ rope<charT,Alloc>::destr_leaf_concat_char_iter // Assumes left and right are not 0. // Does not increment (nor decrement on exception) child reference counts. // Result has ref count 1. -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * -rope<charT,Alloc>::tree_concat (RopeBase * left, RopeBase * right) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* +rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right) { - RopeConcatenation * result = CAlloc::allocate(); - unsigned char child_depth = left -> depth; - size_t rsize; - - result -> tag = RopeBase::concat; - result -> c_string = 0; - result -> is_balanced = false; - result -> size = rsize = left -> size + right -> size; - if (right -> depth > child_depth) child_depth = right -> depth; - unsigned char depth = (unsigned char)(child_depth + 1); - result -> depth = depth; - result -> left = left; - result -> right = right; -# ifndef __GC - result -> refcount = 1; - result -> init_refcount_lock(); + _RopeConcatenation* __result = + _S_new_RopeConcatenation(__left, __right, __left->get_allocator()); + size_t __depth = __result->_M_depth; + +# ifdef __STL_USE_STD_ALLOCATORS + __stl_assert(__left->get_allocator() == __right->get_allocator()); # endif - if (depth > 20 && (rsize < 1000 || depth > RopeBase::max_rope_depth)) { - RopeBase * balanced; - + if (__depth > 20 && (__result->_M_size < 1000 || + __depth > _RopeRep::_S_max_rope_depth)) { + _RopeRep* __balanced; + __STL_TRY { - balanced = balance(result); + __balanced = _S_balance(__result); # ifndef __GC - if (result != balanced) { - __stl_assert(1 == result -> refcount - && 1 == balanced -> refcount); + if (__result != __balanced) { + __stl_assert(1 == __result->_M_refcount + && 1 == __balanced->_M_refcount); } # endif - result -> unref_nonnil(); + __result->_M_unref_nonnil(); } - __STL_UNWIND(CAlloc::deallocate(result)); + __STL_UNWIND((_C_deallocate(__result,1))); // In case of exception, we need to deallocate // otherwise dangling result node. But caller // still owns its children. Thus unref is // inappropriate. - return balanced; + return __balanced; } else { - return result; + return __result; } } -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * rope<charT,Alloc>::concat_char_iter - (RopeBase * r, const charT *s, size_t slen) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter + (_RopeRep* __r, const _CharT*__s, size_t __slen) { - RopeBase *result; - if (0 == slen) { - ref(r); - return r; + _RopeRep* __result; + if (0 == __slen) { + _S_ref(__r); + return __r; } - if (0 == r) return RopeLeaf_from_unowned_char_ptr(s, slen); - if (RopeBase::leaf == r -> tag && r -> size + slen <= copy_max) { - result = leaf_concat_char_iter((RopeLeaf *)r, s, slen); + if (0 == __r) + return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, + __r->get_allocator()); + if (_RopeRep::_S_leaf == __r->_M_tag && + __r->_M_size + __slen <= _S_copy_max) { + __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen); # ifndef __GC - __stl_assert(1 == result -> refcount); + __stl_assert(1 == __result->_M_refcount); # endif - return result; + return __result; } - if (RopeBase::concat == r -> tag - && RopeBase::leaf == ((RopeConcatenation *)r) -> right -> tag) { - RopeLeaf *right = (RopeLeaf *)(((RopeConcatenation *)r) -> right); - if (right -> size + slen <= copy_max) { - RopeBase * left = ((RopeConcatenation *)r) -> left; - RopeBase * nright = leaf_concat_char_iter((RopeLeaf *)right, s, slen); - left -> ref_nonnil(); + if (_RopeRep::_S_concat == __r->_M_tag + && _RopeRep::_S_leaf == ((_RopeConcatenation*)__r)->_M_right->_M_tag) { + _RopeLeaf* __right = + (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right); + if (__right->_M_size + __slen <= _S_copy_max) { + _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left; + _RopeRep* __nright = + _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen); + __left->_M_ref_nonnil(); __STL_TRY { - result = tree_concat(left, nright); + __result = _S_tree_concat(__left, __nright); } - __STL_UNWIND(unref(left); unref(nright)); + __STL_UNWIND(_S_unref(__left); _S_unref(__nright)); # ifndef __GC - __stl_assert(1 == result -> refcount); + __stl_assert(1 == __result->_M_refcount); # endif - return result; + return __result; } } - RopeBase * nright = RopeLeaf_from_unowned_char_ptr(s, slen); + _RopeRep* __nright = + __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator()); __STL_TRY { - r -> ref_nonnil(); - result = tree_concat(r, nright); + __r->_M_ref_nonnil(); + __result = _S_tree_concat(__r, __nright); } - __STL_UNWIND(unref(r); unref(nright)); + __STL_UNWIND(_S_unref(__r); _S_unref(__nright)); # ifndef __GC - __stl_assert(1 == result -> refcount); + __stl_assert(1 == __result->_M_refcount); # endif - return result; + return __result; } #ifndef __GC -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * rope<charT,Alloc> -::destr_concat_char_iter - (RopeBase * r, const charT *s, size_t slen) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* +rope<_CharT,_Alloc>::_S_destr_concat_char_iter( + _RopeRep* __r, const _CharT* __s, size_t __slen) { - RopeBase *result; - if (0 == r) return RopeLeaf_from_unowned_char_ptr(s, slen); - size_t count = r -> refcount; - size_t orig_size = r -> size; - __stl_assert(count >= 1); - if (count > 1) return concat_char_iter(r, s, slen); - if (0 == slen) { - r -> refcount = 2; // One more than before - return r; + _RopeRep* __result; + if (0 == __r) + return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, + __r->get_allocator()); + size_t __count = __r->_M_refcount; + size_t __orig_size = __r->_M_size; + __stl_assert(__count >= 1); + if (__count > 1) return _S_concat_char_iter(__r, __s, __slen); + if (0 == __slen) { + __r->_M_refcount = 2; // One more than before + return __r; } - if (orig_size + slen <= copy_max && RopeBase::leaf == r -> tag) { - result = destr_leaf_concat_char_iter((RopeLeaf *)r, s, slen); - return result; + if (__orig_size + __slen <= _S_copy_max && + _RopeRep::_S_leaf == __r->_M_tag) { + __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen); + return __result; } - if (RopeBase::concat == r -> tag) { - RopeLeaf *right = (RopeLeaf *)(((RopeConcatenation *)r) -> right); - if (RopeBase::leaf == right -> tag - && right -> size + slen <= copy_max) { - RopeBase * new_right = destr_leaf_concat_char_iter(right, s, slen); - if (right == new_right) { - __stl_assert(new_right -> refcount == 2); - new_right -> refcount = 1; + if (_RopeRep::_S_concat == __r->_M_tag) { + _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)__r)->_M_right); + if (_RopeRep::_S_leaf == __right->_M_tag + && __right->_M_size + __slen <= _S_copy_max) { + _RopeRep* __new_right = + _S_destr_leaf_concat_char_iter(__right, __s, __slen); + if (__right == __new_right) { + __stl_assert(__new_right->_M_refcount == 2); + __new_right->_M_refcount = 1; } else { - __stl_assert(new_right -> refcount >= 1); - right -> unref_nonnil(); + __stl_assert(__new_right->_M_refcount >= 1); + __right->_M_unref_nonnil(); } - __stl_assert(r -> refcount == 1); - r -> refcount = 2; // One more than before. - ((RopeConcatenation *)r) -> right = new_right; - r -> size = orig_size + slen; - if (0 != r -> c_string) { - r -> free_c_string(); - r -> c_string = 0; + __stl_assert(__r->_M_refcount == 1); + __r->_M_refcount = 2; // One more than before. + ((_RopeConcatenation*)__r)->_M_right = __new_right; + __r->_M_size = __orig_size + __slen; + if (0 != __r->_M_c_string) { + __r->_M_free_c_string(); + __r->_M_c_string = 0; } - return r; + return __r; } } - RopeBase *right = RopeLeaf_from_unowned_char_ptr(s, slen); - r -> ref_nonnil(); + _RopeRep* __right = + __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator()); + __r->_M_ref_nonnil(); __STL_TRY { - result = tree_concat(r, right); + __result = _S_tree_concat(__r, __right); } - __STL_UNWIND(unref(r); unref(right)) - __stl_assert(1 == result -> refcount); - return result; + __STL_UNWIND(_S_unref(__r); _S_unref(__right)) + __stl_assert(1 == __result->_M_refcount); + return __result; } #endif /* !__GC */ -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * -rope<charT,Alloc>::concat(RopeBase * left, RopeBase * right) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* +rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right) { - if (0 == left) { - ref(right); - return right; + if (0 == __left) { + _S_ref(__right); + return __right; } - if (0 == right) { - left -> ref_nonnil(); - return left; + if (0 == __right) { + __left->_M_ref_nonnil(); + return __left; } - if (RopeBase::leaf == right -> tag) { - if (RopeBase::leaf == left -> tag) { - if (right -> size + left -> size <= copy_max) { - return leaf_concat_char_iter((RopeLeaf *)left, - ((RopeLeaf *)right) -> data, - right -> size); + if (_RopeRep::_S_leaf == __right->_M_tag) { + if (_RopeRep::_S_leaf == __left->_M_tag) { + if (__right->_M_size + __left->_M_size <= _S_copy_max) { + return _S_leaf_concat_char_iter((_RopeLeaf*)__left, + ((_RopeLeaf*)__right)->_M_data, + __right->_M_size); } - } else if (RopeBase::concat == left -> tag - && RopeBase::leaf == - ((RopeConcatenation *)left) -> right -> tag) { - RopeLeaf * leftright = - (RopeLeaf *)(((RopeConcatenation *)left) -> right); - if (leftright -> size + right -> size <= copy_max) { - RopeBase * leftleft = ((RopeConcatenation *)left) -> left; - RopeBase * rest = leaf_concat_char_iter(leftright, - ((RopeLeaf *)right) -> data, - right -> size); - leftleft -> ref_nonnil(); + } else if (_RopeRep::_S_concat == __left->_M_tag + && _RopeRep::_S_leaf == + ((_RopeConcatenation*)__left)->_M_right->_M_tag) { + _RopeLeaf* __leftright = + (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right); + if (__leftright->_M_size + __right->_M_size <= _S_copy_max) { + _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left; + _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright, + ((_RopeLeaf*)__right)->_M_data, + __right->_M_size); + __leftleft->_M_ref_nonnil(); __STL_TRY { - return(tree_concat(leftleft, rest)); + return(_S_tree_concat(__leftleft, __rest)); } - __STL_UNWIND(unref(leftleft); unref(rest)) + __STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest)) } } } - left -> ref_nonnil(); - right -> ref_nonnil(); + __left->_M_ref_nonnil(); + __right->_M_ref_nonnil(); __STL_TRY { - return(tree_concat(left, right)); + return(_S_tree_concat(__left, __right)); } - __STL_UNWIND(unref(left); unref(right)); + __STL_UNWIND(_S_unref(__left); _S_unref(__right)); } -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * -rope<charT,Alloc>::substring(RopeBase * base, size_t start, size_t endp1) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* +rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base, + size_t __start, size_t __endp1) { - if (0 == base) return 0; - size_t len = base -> size; - size_t adj_endp1; - const size_t lazy_threshold = 128; + if (0 == __base) return 0; + size_t __len = __base->_M_size; + size_t __adj_endp1; + const size_t __lazy_threshold = 128; - if (endp1 >= len) { - if (0 == start) { - base -> ref_nonnil(); - return base; + if (__endp1 >= __len) { + if (0 == __start) { + __base->_M_ref_nonnil(); + return __base; } else { - adj_endp1 = len; + __adj_endp1 = __len; } } else { - adj_endp1 = endp1; + __adj_endp1 = __endp1; } - switch(base -> tag) { - case RopeBase::concat: + switch(__base->_M_tag) { + case _RopeRep::_S_concat: { - RopeConcatenation *c = (RopeConcatenation *)base; - RopeBase *left = c -> left; - RopeBase *right = c -> right; - size_t left_len = left -> size; - RopeBase * result; - - if (adj_endp1 <= left_len) { - return substring(left, start, endp1); - } else if (start >= left_len) { - return substring(right, start - left_len, - adj_endp1 - left_len); + _RopeConcatenation* __c = (_RopeConcatenation*)__base; + _RopeRep* __left = __c->_M_left; + _RopeRep* __right = __c->_M_right; + size_t __left_len = __left->_M_size; + _RopeRep* __result; + + if (__adj_endp1 <= __left_len) { + return _S_substring(__left, __start, __endp1); + } else if (__start >= __left_len) { + return _S_substring(__right, __start - __left_len, + __adj_endp1 - __left_len); } - self_destruct_ptr left_result(substring(left, start, - left_len)); - self_destruct_ptr right_result( - substring(right, 0, endp1 - left_len)); - result = concat(left_result, right_result); + _Self_destruct_ptr __left_result( + _S_substring(__left, __start, __left_len)); + _Self_destruct_ptr __right_result( + _S_substring(__right, 0, __endp1 - __left_len)); + __result = _S_concat(__left_result, __right_result); # ifndef __GC - __stl_assert(1 == result -> refcount); + __stl_assert(1 == __result->_M_refcount); # endif - return result; + return __result; } - case RopeBase::leaf: + case _RopeRep::_S_leaf: { - RopeLeaf * l = (RopeLeaf *)base; - RopeLeaf * result; - size_t result_len; - if (start >= adj_endp1) return 0; - result_len = adj_endp1 - start; - if (result_len > lazy_threshold) goto lazy; + _RopeLeaf* __l = (_RopeLeaf*)__base; + _RopeLeaf* __result; + size_t __result_len; + if (__start >= __adj_endp1) return 0; + __result_len = __adj_endp1 - __start; + if (__result_len > __lazy_threshold) goto lazy; # ifdef __GC - const charT *section = l -> data + start; - result = RopeLeaf_from_char_ptr(section, result_len); - result -> c_string = 0; // Not eos terminated. + const _CharT* __section = __l->_M_data + __start; + __result = _S_new_RopeLeaf(__section, __result_len, + __base->get_allocator()); + __result->_M_c_string = 0; // Not eos terminated. # else // We should sometimes create substring node instead. - result = RopeLeaf_from_unowned_char_ptr( - l -> data + start, result_len); + __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR( + __l->_M_data + __start, __result_len, + __base->get_allocator()); # endif - return result; + return __result; } - case RopeBase::substringfn: - // Avoid introducing mutiple layers of substring nodes. + case _RopeRep::_S_substringfn: + // Avoid introducing multiple layers of substring nodes. { - RopeSubstring *old = (RopeSubstring *)base; - size_t result_len; - if (start >= adj_endp1) return 0; - result_len = adj_endp1 - start; - if (result_len > lazy_threshold) { - RopeSubstring * space = SAlloc::allocate(); - RopeSubstring * result = - new(space) RopeSubstring(old -> base, - start + old -> start, - adj_endp1 - start); - return result; - } // else fall through: + _RopeSubstring* __old = (_RopeSubstring*)__base; + size_t __result_len; + if (__start >= __adj_endp1) return 0; + __result_len = __adj_endp1 - __start; + if (__result_len > __lazy_threshold) { + _RopeSubstring* __result = + _S_new_RopeSubstring(__old->_M_base, + __start + __old->_M_start, + __adj_endp1 - __start, + __base->get_allocator()); + return __result; + + } // *** else fall through: *** } - case RopeBase::function: + case _RopeRep::_S_function: { - RopeFunction * f = (RopeFunction *)base; - charT *section; - size_t result_len; - if (start >= adj_endp1) return 0; - result_len = adj_endp1 - start; - - if (result_len > lazy_threshold) goto lazy; - section = (charT *) - DataAlloc::allocate(rounded_up_size(result_len)); + _RopeFunction* __f = (_RopeFunction*)__base; + _CharT* __section; + size_t __result_len; + if (__start >= __adj_endp1) return 0; + __result_len = __adj_endp1 - __start; + + if (__result_len > __lazy_threshold) goto lazy; + __section = (_CharT*) + _Data_allocate(_S_rounded_up_size(__result_len)); __STL_TRY { - (*(f -> fn))(start, result_len, section); + (*(__f->_M_fn))(__start, __result_len, __section); } - __STL_UNWIND(RopeBase::free_string(section, result_len)); - __cond_store_eos(section[result_len]); - return RopeLeaf_from_char_ptr(section, result_len); + __STL_UNWIND(_RopeRep::__STL_FREE_STRING( + __section, __result_len, __base->get_allocator())); + _S_cond_store_eos(__section[__result_len]); + return _S_new_RopeLeaf(__section, __result_len, + __base->get_allocator()); } } /*NOTREACHED*/ @@ -762,141 +740,146 @@ rope<charT,Alloc>::substring(RopeBase * base, size_t start, size_t endp1) lazy: { // Create substring node. - RopeSubstring * space = SAlloc::allocate(); - RopeSubstring * result = new(space) RopeSubstring(base, start, - adj_endp1 - start); - return result; + return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start, + __base->get_allocator()); } } -template<class charT> -class __rope_flatten_char_consumer : public __rope_char_consumer<charT> { +template<class _CharT> +class _Rope_flatten_char_consumer : public _Rope_char_consumer<_CharT> { private: - charT * buf_ptr; + _CharT* _M_buf_ptr; public: - charT * buffer; - __rope_flatten_char_consumer(charT * buffer) { - buf_ptr = buffer; + // _CharT* _M_buffer; // XXX not used + + _Rope_flatten_char_consumer(_CharT* __buffer) { + _M_buf_ptr = __buffer; }; - ~__rope_flatten_char_consumer() {} - bool operator() (const charT* leaf, size_t n) { - uninitialized_copy_n(leaf, n, buf_ptr); - buf_ptr += n; + ~_Rope_flatten_char_consumer() {} + bool operator() (const _CharT* __leaf, size_t __n) { + uninitialized_copy_n(__leaf, __n, _M_buf_ptr); + _M_buf_ptr += __n; return true; } }; -template<class charT> -class __rope_find_char_char_consumer : public __rope_char_consumer<charT> { +template<class _CharT> +class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> { private: - charT pattern; + _CharT _M_pattern; public: - size_t count; // Number of nonmatching characters - __rope_find_char_char_consumer(charT p) : pattern(p), count(0) {} - ~__rope_find_char_char_consumer() {} - bool operator() (const charT* leaf, size_t n) { - size_t i; - for (i = 0; i < n; i++) { - if (leaf[i] == pattern) { - count += i; return false; + size_t _M_count; // Number of nonmatching characters + _Rope_find_char_char_consumer(_CharT __p) + : _M_pattern(__p), _M_count(0) {} + ~_Rope_find_char_char_consumer() {} + bool operator() (const _CharT* __leaf, size_t __n) { + size_t __i; + for (__i = 0; __i < __n; __i++) { + if (__leaf[__i] == _M_pattern) { + _M_count += __i; return false; } } - count += n; return true; + _M_count += __n; return true; } }; -template<class charT> -class __rope_insert_char_consumer : public __rope_char_consumer<charT> { +template<class _CharT> +class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> { private: - typedef ostream insert_ostream; - insert_ostream & o; + typedef ostream _Insert_ostream; + _Insert_ostream& _M_o; public: - charT * buffer; - __rope_insert_char_consumer(insert_ostream & writer) : o(writer) {}; - ~__rope_insert_char_consumer() { }; + // _CharT* buffer; // XXX not used + _Rope_insert_char_consumer(_Insert_ostream& __writer) + : _M_o(__writer) {}; + ~_Rope_insert_char_consumer() { }; // Caller is presumed to own the ostream - bool operator() (const charT* leaf, size_t n); + bool operator() (const _CharT* __leaf, size_t __n); // Returns true to continue traversal. }; -template<class charT> -bool __rope_insert_char_consumer<charT>::operator() - (const charT * leaf, size_t n) +template<class _CharT> +bool _Rope_insert_char_consumer<_CharT>::operator() + (const _CharT* __leaf, size_t __n) { - size_t i; + size_t __i; // We assume that formatting is set up correctly for each element. - for (i = 0; i < n; i++) o << leaf[i]; + for (__i = 0; __i < __n; __i++) _M_o << __leaf[__i]; return true; } -inline bool __rope_insert_char_consumer<char>::operator() - (const char * leaf, size_t n) +inline bool _Rope_insert_char_consumer<char>::operator() + (const char* __leaf, size_t __n) { - size_t i; - for (i = 0; i < n; i++) o.put(leaf[i]); + size_t __i; + for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]); return true; } -#if !defined(_MSC_VER) && !defined(__BORLANDC__) -// I couldn't get this to work with the VC++ version of basic_ostream. -inline bool __rope_insert_char_consumer<wchar_t>::operator() - (const wchar_t * leaf, size_t n) +#if 0 +// I couldn't get this to work work with the VC++ version of basic_ostream. +// It also doesn't really do the right thing unless o is a wide stream. +// Given that wchar_t is often 4 bytes, its not clear to me how useful +// this stuff is anyway. +inline bool _Rope_insert_char_consumer<wchar_t>::operator() + (const wchar_t* __leaf, size_t __n) { - size_t i; - for (i = 0; i < n; i++) o.put(leaf[i]); + size_t __i; + for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]); return true; } #endif /* !_MSC_VER && !BORLAND */ -template <class charT, class Alloc> -bool rope<charT, Alloc>::apply_to_pieces( - __rope_char_consumer<charT>& c, - const RopeBase * r, - size_t begin, size_t end) +template <class _CharT, class _Alloc> +bool rope<_CharT, _Alloc>::_S_apply_to_pieces( + _Rope_char_consumer<_CharT>& __c, + const _RopeRep* __r, + size_t __begin, size_t __end) { - if (0 == r) return true; - switch(r -> tag) { - case RopeBase::concat: + if (0 == __r) return true; + switch(__r->_M_tag) { + case _RopeRep::_S_concat: { - RopeConcatenation *conc = (RopeConcatenation *)r; - RopeBase *left = conc -> left; - size_t left_len = left -> size; - if (begin < left_len) { - size_t left_end = min(left_len, end); - if (!apply_to_pieces(c, left, begin, left_end)) { + _RopeConcatenation* __conc = (_RopeConcatenation*)__r; + _RopeRep* __left = __conc->_M_left; + size_t __left_len = __left->_M_size; + if (__begin < __left_len) { + size_t __left_end = min(__left_len, __end); + if (!_S_apply_to_pieces(__c, __left, __begin, __left_end)) return false; - } } - if (end > left_len) { - RopeBase *right = conc -> right; - size_t right_start = max(left_len, begin); - if (!apply_to_pieces(c, right, - right_start - left_len, - end - left_len)) { + if (__end > __left_len) { + _RopeRep* __right = __conc->_M_right; + size_t __right_start = max(__left_len, __begin); + if (!_S_apply_to_pieces(__c, __right, + __right_start - __left_len, + __end - __left_len)) { return false; } } } return true; - case RopeBase::leaf: + case _RopeRep::_S_leaf: { - RopeLeaf * l = (RopeLeaf *)r; - return c(l -> data + begin, end - begin); + _RopeLeaf* __l = (_RopeLeaf*)__r; + return __c(__l->_M_data + __begin, __end - __begin); } - case RopeBase::function: - case RopeBase::substringfn: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: { - RopeFunction * f = (RopeFunction *)r; - size_t len = end - begin; - bool result; - charT * buffer = DataAlloc::allocate(len); + _RopeFunction* __f = (_RopeFunction*)__r; + size_t __len = __end - __begin; + bool __result; + _CharT* __buffer = + (_CharT*)alloc::allocate(__len * sizeof(_CharT)); __STL_TRY { - (*(f -> fn))(begin, end, buffer); - result = c(buffer, len); - DataAlloc::deallocate(buffer, len); + (*(__f->_M_fn))(__begin, __end, __buffer); + __result = __c(__buffer, __len); + alloc::deallocate(__buffer, __len * sizeof(_CharT)); } - __STL_UNWIND(DataAlloc::deallocate(buffer, len)) - return result; + __STL_UNWIND((alloc::deallocate(__buffer, + __len * sizeof(_CharT)))) + return __result; } default: __stl_assert(false); @@ -905,98 +888,102 @@ bool rope<charT, Alloc>::apply_to_pieces( } } -inline void __rope_fill(ostream& o, size_t n) +inline void _Rope_fill(ostream& __o, size_t __n) { - char f = o.fill(); - size_t i; + char __f = __o.fill(); + size_t __i; - for (i = 0; i < n; i++) o.put(f); + for (__i = 0; __i < __n; __i++) __o.put(__f); } -template <class charT> inline bool __rope_is_simple(charT *) { return false; } -inline bool __rope_is_simple(char *) { return true; } -inline bool __rope_is_simple(wchar_t *) { return true; } +template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; } +inline bool _Rope_is_simple(char*) { return true; } +inline bool _Rope_is_simple(wchar_t*) { return true; } -template<class charT, class Alloc> -ostream& operator<< (ostream& o, const rope<charT, Alloc>& r) +template<class _CharT, class _Alloc> +ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r) { - size_t w = o.width(); - bool left = bool(o.flags() & ios::left); - size_t pad_len; - size_t rope_len = r.size(); - __rope_insert_char_consumer<charT> c(o); - bool is_simple = __rope_is_simple((charT *)0); + size_t __w = __o.width(); + bool __left = bool(__o.flags() & ios::left); + size_t __pad_len; + size_t __rope_len = __r.size(); + _Rope_insert_char_consumer<_CharT> __c(__o); + bool __is_simple = _Rope_is_simple((_CharT*)0); - if (rope_len < w) { - pad_len = w - rope_len; + if (__rope_len < __w) { + __pad_len = __w - __rope_len; } else { - pad_len = 0; + __pad_len = 0; } - if (!is_simple) o.width(w/rope_len); + if (!__is_simple) __o.width(__w/__rope_len); __STL_TRY { - if (is_simple && !left && pad_len > 0) { - __rope_fill(o, pad_len); + if (__is_simple && !__left && __pad_len > 0) { + _Rope_fill(__o, __pad_len); } - r.apply_to_pieces(0, r.size(), c); - if (is_simple && left && pad_len > 0) { - __rope_fill(o, pad_len); + __r.apply_to_pieces(0, __r.size(), __c); + if (__is_simple && __left && __pad_len > 0) { + _Rope_fill(__o, __pad_len); } - if (!is_simple) - o.width(w); + if (!__is_simple) + __o.width(__w); } - __STL_UNWIND(if (!is_simple) o.width(w)) - return o; + __STL_UNWIND(if (!__is_simple) __o.width(__w)) + return __o; } -template <class charT, class Alloc> -charT * -rope<charT,Alloc>::flatten(RopeBase * r, - size_t start, size_t len, - charT * buffer) +template <class _CharT, class _Alloc> +_CharT* +rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, + size_t __start, size_t __len, + _CharT* __buffer) { - __rope_flatten_char_consumer<charT> c(buffer); - apply_to_pieces(c, r, start, start + len); - return(buffer + len); + _Rope_flatten_char_consumer<_CharT> __c(__buffer); + _S_apply_to_pieces(__c, __r, __start, __start + __len); + return(__buffer + __len); } -template <class charT, class Alloc> +template <class _CharT, class _Alloc> size_t -rope<charT,Alloc>::find(charT pattern, size_t start) const +rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const { - __rope_find_char_char_consumer<charT> c(pattern); - apply_to_pieces(c, tree_ptr, start, size()); - return start + c.count; + _Rope_find_char_char_consumer<_CharT> __c(__pattern); + _S_apply_to_pieces(__c, _M_tree_ptr, __start, size()); + size_type __result_pos = __start + __c._M_count; +# ifndef __STL_OLD_ROPE_SEMANTICS + if (__result_pos == size()) __result_pos = npos; +# endif + return __result_pos; } -template <class charT, class Alloc> -charT * -rope<charT,Alloc>::flatten(RopeBase * r, charT * buffer) +template <class _CharT, class _Alloc> +_CharT* +rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer) { - if (0 == r) return buffer; - switch(r -> tag) { - case RopeBase::concat: + if (0 == __r) return __buffer; + switch(__r->_M_tag) { + case _RopeRep::_S_concat: { - RopeConcatenation *c = (RopeConcatenation *)r; - RopeBase *left = c -> left; - RopeBase *right = c -> right; - charT * rest = flatten(left, buffer); - return flatten(right, rest); + _RopeConcatenation* __c = (_RopeConcatenation*)__r; + _RopeRep* __left = __c->_M_left; + _RopeRep* __right = __c->_M_right; + _CharT* __rest = _S_flatten(__left, __buffer); + return _S_flatten(__right, __rest); } - case RopeBase::leaf: + case _RopeRep::_S_leaf: { - RopeLeaf * l = (RopeLeaf *)r; - return copy_n(l -> data, l -> size, buffer).second; + _RopeLeaf* __l = (_RopeLeaf*)__r; + return copy_n(__l->_M_data, __l->_M_size, __buffer).second; } - case RopeBase::function: - case RopeBase::substringfn: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: // We dont yet do anything with substring nodes. // This needs to be fixed before ropefiles will work well. { - RopeFunction * f = (RopeFunction *)r; - (*(f -> fn))(0, f -> size, buffer); - return buffer + f -> size; + _RopeFunction* __f = (_RopeFunction*)__r; + (*(__f->_M_fn))(0, __f->_M_size, __buffer); + return __buffer + __f->_M_size; } default: __stl_assert(false); @@ -1006,72 +993,75 @@ rope<charT,Alloc>::flatten(RopeBase * r, charT * buffer) } -// This needs work for charT != char -template <class charT, class Alloc> +// This needs work for _CharT != char +template <class _CharT, class _Alloc> void -rope<charT,Alloc>::dump(RopeBase * r, int indent) +rope<_CharT,_Alloc>::_S_dump(_RopeRep* __r, int __indent) { - for (int i = 0; i < indent; i++) putchar(' '); - if (0 == r) { + for (int __i = 0; __i < __indent; __i++) putchar(' '); + if (0 == __r) { printf("NULL\n"); return; } - if (RopeBase::concat == r -> tag) { - RopeConcatenation *c = (RopeConcatenation *)r; - RopeBase *left = c -> left; - RopeBase *right = c -> right; + if (_RopeRep::_S_concat == __r->_M_tag) { + _RopeConcatenation* __c = (_RopeConcatenation*)__r; + _RopeRep* __left = __c->_M_left; + _RopeRep* __right = __c->_M_right; # ifdef __GC printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n", - r, r -> depth, r -> size, r -> is_balanced? "" : "not"); + __r, __r->_M_depth, __r->_M_size, __r->_M_is_balanced? "" : "not"); # else - printf("Concatenation %p (rc = %ld, depth = %d, len = %ld, %s balanced)\n", - r, r -> refcount, r -> depth, r -> size, - r -> is_balanced? "" : "not"); + printf("Concatenation %p (rc = %ld, depth = %d, " + "len = %ld, %s balanced)\n", + __r, __r->_M_refcount, __r->_M_depth, __r->_M_size, + __r->_M_is_balanced? "" : "not"); # endif - dump(left, indent + 2); - dump(right, indent + 2); + _S_dump(__left, __indent + 2); + _S_dump(__right, __indent + 2); return; } else { - char * kind; + char* __kind; - switch (r -> tag) { - case RopeBase::leaf: - kind = "Leaf"; + switch (__r->_M_tag) { + case _RopeRep::_S_leaf: + __kind = "Leaf"; break; - case RopeBase::function: - kind = "Function"; + case _RopeRep::_S_function: + __kind = "Function"; break; - case RopeBase::substringfn: - kind = "Function representing substring"; + case _RopeRep::_S_substringfn: + __kind = "Function representing substring"; break; default: - kind = "(corrupted kind field!)"; + __kind = "(corrupted kind field!)"; } # ifdef __GC printf("%s %p (depth = %d, len = %ld) ", - kind, r, r -> depth, r -> size); + __kind, __r, __r->_M_depth, __r->_M_size); # else printf("%s %p (rc = %ld, depth = %d, len = %ld) ", - kind, r, r -> refcount, r -> depth, r -> size); + __kind, __r, __r->_M_refcount, __r->_M_depth, __r->_M_size); # endif - if (__is_one_byte_char_type((charT *)0)) { - const int max_len = 40; - self_destruct_ptr prefix(substring(r, 0, max_len)); - charT buffer[max_len + 1]; - bool too_big = r -> size > prefix-> size; - - flatten(prefix, buffer); - buffer[prefix -> size] = __eos((charT *)0); - printf("%s%s\n", (char *)buffer, too_big? "...\n" : "\n"); + if (_S_is_one_byte_char_type((_CharT*)0)) { + const int __max_len = 40; + _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len)); + _CharT __buffer[__max_len + 1]; + bool __too_big = __r->_M_size > __prefix->_M_size; + + _S_flatten(__prefix, __buffer); + __buffer[__prefix->_M_size] = _S_eos((_CharT*)0); + printf("%s%s\n", + (char*)__buffer, __too_big? "...\n" : "\n"); } else { printf("\n"); } } } -template <class charT, class Alloc> +template <class _CharT, class _Alloc> const unsigned long -rope<charT,Alloc>::min_len[__rope_RopeBase<charT,Alloc>::max_rope_depth + 1] = { +rope<_CharT,_Alloc>::_S_min_len[ + _Rope_RopeRep<_CharT,_Alloc>::_S_max_rope_depth + 1] = { /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21, /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377, /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181, @@ -1082,148 +1072,150 @@ rope<charT,Alloc>::min_len[__rope_RopeBase<charT,Alloc>::max_rope_depth + 1] = { /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155, /* 39 */165580141, /* 40 */267914296, /* 41 */433494437, /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903, -/* 45 */2971215073 }; +/* 45 */2971215073u }; // These are Fibonacci numbers < 2**32. -template <class charT, class Alloc> -rope<charT,Alloc>::RopeBase * -rope<charT,Alloc>::balance(RopeBase *r) +template <class _CharT, class _Alloc> +rope<_CharT,_Alloc>::_RopeRep* +rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r) { - RopeBase * forest[RopeBase::max_rope_depth + 1]; - RopeBase * result = 0; - int i; - // Inariant: - // The concatenation of forest in descending order is equal to r. - // forest[i].size >= min_len[i] - // forest[i].depth = i + _RopeRep* __forest[_RopeRep::_S_max_rope_depth + 1]; + _RopeRep* __result = 0; + int __i; + // Invariant: + // The concatenation of forest in descending order is equal to __r. + // __forest[__i]._M_size >= _S_min_len[__i] + // __forest[__i]._M_depth = __i // References from forest are included in refcount. - for (i = 0; i <= RopeBase::max_rope_depth; ++i) forest[i] = 0; + for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) + __forest[__i] = 0; __STL_TRY { - add_to_forest(r, forest); - for (i = 0; i <= RopeBase::max_rope_depth; ++i) if (0 != forest[i]) { + _S_add_to_forest(__r, __forest); + for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) + if (0 != __forest[__i]) { # ifndef __GC - self_destruct_ptr old(result); + _Self_destruct_ptr __old(__result); # endif - result = concat(forest[i], result); - forest[i] -> unref_nonnil(); + __result = _S_concat(__forest[__i], __result); + __forest[__i]->_M_unref_nonnil(); # if !defined(__GC) && defined(__STL_USE_EXCEPTIONS) - forest[i] = 0; + __forest[__i] = 0; # endif } } - __STL_UNWIND(for(i = 0; i <= RopeBase::max_rope_depth; i++) - unref(forest[i])) - if (result -> depth > RopeBase::max_rope_depth) abort(); - return(result); + __STL_UNWIND(for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++) + _S_unref(__forest[__i])) + if (__result->_M_depth > _RopeRep::_S_max_rope_depth) abort(); + return(__result); } -template <class charT, class Alloc> +template <class _CharT, class _Alloc> void -rope<charT,Alloc>::add_to_forest(RopeBase *r, RopeBase **forest) +rope<_CharT,_Alloc>::_S_add_to_forest(_RopeRep* __r, _RopeRep** __forest) { - if (r -> is_balanced) { - add_leaf_to_forest(r, forest); + if (__r->_M_is_balanced) { + _S_add_leaf_to_forest(__r, __forest); return; } - __stl_assert(r -> tag == RopeBase::concat); + __stl_assert(__r->_M_tag == _RopeRep::_S_concat); { - RopeConcatenation *c = (RopeConcatenation *)r; + _RopeConcatenation* __c = (_RopeConcatenation*)__r; - add_to_forest(c -> left, forest); - add_to_forest(c -> right, forest); + _S_add_to_forest(__c->_M_left, __forest); + _S_add_to_forest(__c->_M_right, __forest); } } -template <class charT, class Alloc> +template <class _CharT, class _Alloc> void -rope<charT,Alloc>::add_leaf_to_forest(RopeBase *r, RopeBase **forest) +rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest) { - RopeBase * insertee; // included in refcount - RopeBase * too_tiny = 0; // included in refcount - int i; // forest[0..i-1] is empty - size_t s = r -> size; + _RopeRep* __insertee; // included in refcount + _RopeRep* __too_tiny = 0; // included in refcount + int __i; // forest[0..__i-1] is empty + size_t __s = __r->_M_size; - for (i = 0; s >= min_len[i+1]/* not this bucket */; ++i) { - if (0 != forest[i]) { + for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i) { + if (0 != __forest[__i]) { # ifndef __GC - self_destruct_ptr old(too_tiny); + _Self_destruct_ptr __old(__too_tiny); # endif - too_tiny = concat_and_set_balanced(forest[i], too_tiny); - forest[i] -> unref_nonnil(); - forest[i] = 0; + __too_tiny = _S_concat_and_set_balanced(__forest[__i], __too_tiny); + __forest[__i]->_M_unref_nonnil(); + __forest[__i] = 0; } } { # ifndef __GC - self_destruct_ptr old(too_tiny); + _Self_destruct_ptr __old(__too_tiny); # endif - insertee = concat_and_set_balanced(too_tiny, r); + __insertee = _S_concat_and_set_balanced(__too_tiny, __r); } // Too_tiny dead, and no longer included in refcount. // Insertee is live and included. - __stl_assert(is_almost_balanced(insertee)); - __stl_assert(insertee -> depth <= r -> depth + 1); - for (;; ++i) { - if (0 != forest[i]) { + __stl_assert(_S_is_almost_balanced(__insertee)); + __stl_assert(__insertee->_M_depth <= __r->_M_depth + 1); + for (;; ++__i) { + if (0 != __forest[__i]) { # ifndef __GC - self_destruct_ptr old(insertee); + _Self_destruct_ptr __old(__insertee); # endif - insertee = concat_and_set_balanced(forest[i], insertee); - forest[i] -> unref_nonnil(); - forest[i] = 0; - __stl_assert(is_almost_balanced(insertee)); + __insertee = _S_concat_and_set_balanced(__forest[__i], __insertee); + __forest[__i]->_M_unref_nonnil(); + __forest[__i] = 0; + __stl_assert(_S_is_almost_balanced(__insertee)); } - __stl_assert(min_len[i] <= insertee -> size); - __stl_assert(forest[i] == 0); - if (i == RopeBase::max_rope_depth - || insertee -> size < min_len[i+1]) { - forest[i] = insertee; - // refcount is OK since insertee is now dead. + __stl_assert(_S_min_len[__i] <= __insertee->_M_size); + __stl_assert(__forest[__i] == 0); + if (__i == _RopeRep::_S_max_rope_depth || + __insertee->_M_size < _S_min_len[__i+1]) { + __forest[__i] = __insertee; + // refcount is OK since __insertee is now dead. return; } } } -template <class charT, class Alloc> -charT -rope<charT,Alloc>::fetch(RopeBase *r, size_type i) +template <class _CharT, class _Alloc> +_CharT +rope<_CharT,_Alloc>::_S_fetch(_RopeRep* __r, size_type __i) { - __GC_CONST charT * cstr = r -> c_string; + __GC_CONST _CharT* __cstr = __r->_M_c_string; - __stl_assert(i < r -> size); - if (0 != cstr) return cstr[i]; + __stl_assert(__i < __r->_M_size); + if (0 != __cstr) return __cstr[__i]; for(;;) { - switch(r -> tag) { - case RopeBase::concat: + switch(__r->_M_tag) { + case _RopeRep::_S_concat: { - RopeConcatenation *c = (RopeConcatenation *)r; - RopeBase *left = c -> left; - size_t left_len = left -> size; + _RopeConcatenation* __c = (_RopeConcatenation*)__r; + _RopeRep* __left = __c->_M_left; + size_t __left_len = __left->_M_size; - if (i >= left_len) { - i -= left_len; - r = c -> right; + if (__i >= __left_len) { + __i -= __left_len; + __r = __c->_M_right; } else { - r = left; + __r = __left; } } break; - case RopeBase::leaf: + case _RopeRep::_S_leaf: { - RopeLeaf * l = (RopeLeaf *)r; - return l -> data[i]; + _RopeLeaf* __l = (_RopeLeaf*)__r; + return __l->_M_data[__i]; } - case RopeBase::function: - case RopeBase::substringfn: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: { - RopeFunction * f = (RopeFunction *)r; - charT result; + _RopeFunction* __f = (_RopeFunction*)__r; + _CharT __result; - (*(f -> fn))(i, 1, &result); - return result; + (*(__f->_M_fn))(__i, 1, &__result); + return __result; } } } @@ -1232,46 +1224,46 @@ rope<charT,Alloc>::fetch(RopeBase *r, size_type i) # ifndef __GC // Return a uniquely referenced character slot for the given // position, or 0 if that's not possible. -template <class charT, class Alloc> -charT* -rope<charT,Alloc>::fetch_ptr(RopeBase *r, size_type i) +template <class _CharT, class _Alloc> +_CharT* +rope<_CharT,_Alloc>::_S_fetch_ptr(_RopeRep* __r, size_type __i) { - RopeBase * clrstack[RopeBase::max_rope_depth]; - size_t csptr = 0; + _RopeRep* __clrstack[_RopeRep::_S_max_rope_depth]; + size_t __csptr = 0; for(;;) { - if (r -> refcount > 1) return 0; - switch(r -> tag) { - case RopeBase::concat: + if (__r->_M_refcount > 1) return 0; + switch(__r->_M_tag) { + case _RopeRep::_S_concat: { - RopeConcatenation *c = (RopeConcatenation *)r; - RopeBase *left = c -> left; - size_t left_len = left -> size; - - if (c -> c_string != 0) clrstack[csptr++] = c; - if (i >= left_len) { - i -= left_len; - r = c -> right; + _RopeConcatenation* __c = (_RopeConcatenation*)__r; + _RopeRep* __left = __c->_M_left; + size_t __left_len = __left->_M_size; + + if (__c->_M_c_string != 0) __clrstack[__csptr++] = __c; + if (__i >= __left_len) { + __i -= __left_len; + __r = __c->_M_right; } else { - r = left; + __r = __left; } } break; - case RopeBase::leaf: + case _RopeRep::_S_leaf: { - RopeLeaf * l = (RopeLeaf *)r; - if (l -> c_string != l -> data && l -> c_string != 0) - clrstack[csptr++] = l; - while (csptr > 0) { - -- csptr; - RopeBase * d = clrstack[csptr]; - d -> free_c_string(); - d -> c_string = 0; + _RopeLeaf* __l = (_RopeLeaf*)__r; + if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0) + __clrstack[__csptr++] = __l; + while (__csptr > 0) { + -- __csptr; + _RopeRep* __d = __clrstack[__csptr]; + __d->_M_free_c_string(); + __d->_M_c_string = 0; } - return l -> data + i; + return __l->_M_data + __i; } - case RopeBase::function: - case RopeBase::substringfn: + case _RopeRep::_S_function: + case _RopeRep::_S_substringfn: return 0; } } @@ -1282,233 +1274,253 @@ rope<charT,Alloc>::fetch_ptr(RopeBase *r, size_type i) // lexicographical_compare_3way. // We do a little more work to avoid dealing with rope iterators for // flat strings. -template <class charT, class Alloc> +template <class _CharT, class _Alloc> int -rope<charT,Alloc>::compare (const RopeBase *left, const RopeBase *right) +rope<_CharT,_Alloc>::_S_compare (const _RopeRep* __left, + const _RopeRep* __right) { - size_t left_len; - size_t right_len; - - if (0 == right) return 0 != left; - if (0 == left) return -1; - left_len = left -> size; - right_len = right -> size; - if (RopeBase::leaf == left -> tag) { - RopeLeaf *l = (RopeLeaf *) left; - if (RopeBase::leaf == right -> tag) { - RopeLeaf *r = (RopeLeaf *) right; + size_t __left_len; + size_t __right_len; + + if (0 == __right) return 0 != __left; + if (0 == __left) return -1; + __left_len = __left->_M_size; + __right_len = __right->_M_size; + if (_RopeRep::_S_leaf == __left->_M_tag) { + _RopeLeaf* __l = (_RopeLeaf*) __left; + if (_RopeRep::_S_leaf == __right->_M_tag) { + _RopeLeaf* __r = (_RopeLeaf*) __right; return lexicographical_compare_3way( - l -> data, l -> data + left_len, - r -> data, r -> data + right_len); + __l->_M_data, __l->_M_data + __left_len, + __r->_M_data, __r->_M_data + __right_len); } else { - const_iterator rstart(right, 0); - const_iterator rend(right, right_len); + const_iterator __rstart(__right, 0); + const_iterator __rend(__right, __right_len); return lexicographical_compare_3way( - l -> data, l -> data + left_len, - rstart, rend); + __l->_M_data, __l->_M_data + __left_len, + __rstart, __rend); } } else { - const_iterator lstart(left, 0); - const_iterator lend(left, left_len); - if (RopeBase::leaf == right -> tag) { - RopeLeaf *r = (RopeLeaf *) right; + const_iterator __lstart(__left, 0); + const_iterator __lend(__left, __left_len); + if (_RopeRep::_S_leaf == __right->_M_tag) { + _RopeLeaf* __r = (_RopeLeaf*) __right; return lexicographical_compare_3way( - lstart, lend, - r -> data, r -> data + right_len); + __lstart, __lend, + __r->_M_data, __r->_M_data + __right_len); } else { - const_iterator rstart(right, 0); - const_iterator rend(right, right_len); + const_iterator __rstart(__right, 0); + const_iterator __rend(__right, __right_len); return lexicographical_compare_3way( - lstart, lend, - rstart, rend); + __lstart, __lend, + __rstart, __rend); } } } // Assignment to reference proxies. -template <class charT, class Alloc> -__rope_charT_ref_proxy<charT, Alloc>& -__rope_charT_ref_proxy<charT, Alloc>::operator= (charT c) { - RopeBase * old = root -> tree_ptr; +template <class _CharT, class _Alloc> +_Rope_char_ref_proxy<_CharT, _Alloc>& +_Rope_char_ref_proxy<_CharT, _Alloc>::operator= (_CharT __c) { + _RopeRep* __old = _M_root->_M_tree_ptr; # ifndef __GC // First check for the case in which everything is uniquely // referenced. In that case we can do this destructively. - charT * charT_ptr = my_rope::fetch_ptr(old, pos); - if (0 != charT_ptr) { - *charT_ptr = c; + _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos); + if (0 != __ptr) { + *__ptr = __c; return *this; } # endif - self_destruct_ptr left(my_rope::substring(old, 0, pos)); - self_destruct_ptr right(my_rope::substring(old, pos+1, old -> size)); - self_destruct_ptr result_left(my_rope::destr_concat_char_iter(left, &c, 1)); + _Self_destruct_ptr __left( + _My_rope::_S_substring(__old, 0, _M_pos)); + _Self_destruct_ptr __right( + _My_rope::_S_substring(__old, _M_pos+1, __old->_M_size)); + _Self_destruct_ptr __result_left( + _My_rope::_S_destr_concat_char_iter(__left, &__c, 1)); + # ifndef __GC - __stl_assert(left == result_left || 1 == result_left -> refcount); + __stl_assert(__left == __result_left || 1 == __result_left->_M_refcount); # endif - RopeBase * result = - my_rope::concat(result_left, right); + _RopeRep* __result = + _My_rope::_S_concat(__result_left, __right); # ifndef __GC - __stl_assert(1 <= result -> refcount); - RopeBase::unref(old); + __stl_assert(1 <= __result->_M_refcount); + _RopeRep::_S_unref(__old); # endif - root -> tree_ptr = result; + _M_root->_M_tree_ptr = __result; return *this; } -template <class charT, class Alloc> -inline __rope_charT_ref_proxy<charT, Alloc>::operator charT () const +template <class _CharT, class _Alloc> +inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const { - if (current_valid) { - return current; + if (_M_current_valid) { + return _M_current; } else { - return my_rope::fetch(root->tree_ptr, pos); + return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos); } } -template <class charT, class Alloc> -__rope_charT_ptr_proxy<charT, Alloc> -__rope_charT_ref_proxy<charT, Alloc>::operator& () const { - return __rope_charT_ptr_proxy<charT, Alloc>(*this); +template <class _CharT, class _Alloc> +_Rope_char_ptr_proxy<_CharT, _Alloc> +_Rope_char_ref_proxy<_CharT, _Alloc>::operator& () const { + return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this); } -template <class charT, class Alloc> -rope<charT, Alloc>::rope(size_t n, charT c) +template <class _CharT, class _Alloc> +rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c, + const allocator_type& __a) +: _Base(__a) { - rope result; - const size_t exponentiate_threshold = 32; - size_t exponent; - size_t rest; - charT *rest_buffer; - RopeBase * remainder; - rope remainder_rope; - - if (0 == n) { tree_ptr = 0; return; } - exponent = n / exponentiate_threshold; - rest = n % exponentiate_threshold; - if (0 == rest) { - remainder = 0; + rope<_CharT,_Alloc> __result; + const size_t __exponentiate_threshold = 32; + size_t __exponent; + size_t __rest; + _CharT* __rest_buffer; + _RopeRep* __remainder; + rope<_CharT,_Alloc> __remainder_rope; + + if (0 == __n) + return; + + __exponent = __n / __exponentiate_threshold; + __rest = __n % __exponentiate_threshold; + if (0 == __rest) { + __remainder = 0; } else { - rest_buffer = DataAlloc::allocate(rounded_up_size(rest)); - uninitialized_fill_n(rest_buffer, rest, c); - __cond_store_eos(rest_buffer[rest]); + __rest_buffer = _Data_allocate(_S_rounded_up_size(__rest)); + uninitialized_fill_n(__rest_buffer, __rest, __c); + _S_cond_store_eos(__rest_buffer[__rest]); __STL_TRY { - remainder = RopeLeaf_from_char_ptr(rest_buffer, rest); + __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a); } - __STL_UNWIND(RopeBase::free_string(rest_buffer, rest)) + __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a)) } - remainder_rope.tree_ptr = remainder; - if (exponent != 0) { - charT * base_buffer = - DataAlloc::allocate(rounded_up_size(exponentiate_threshold)); - RopeLeaf * base_leaf; - rope base_rope; - uninitialized_fill_n(base_buffer, exponentiate_threshold, c); - __cond_store_eos(base_buffer[exponentiate_threshold]); + __remainder_rope._M_tree_ptr = __remainder; + if (__exponent != 0) { + _CharT* __base_buffer = + _Data_allocate(_S_rounded_up_size(__exponentiate_threshold)); + _RopeLeaf* __base_leaf; + rope __base_rope; + uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c); + _S_cond_store_eos(__base_buffer[__exponentiate_threshold]); __STL_TRY { - base_leaf = RopeLeaf_from_char_ptr(base_buffer, - exponentiate_threshold); + __base_leaf = _S_new_RopeLeaf(__base_buffer, + __exponentiate_threshold, __a); } - __STL_UNWIND(RopeBase::free_string(base_buffer, exponentiate_threshold)) - base_rope.tree_ptr = base_leaf; - if (1 == exponent) { - result = base_rope; + __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__base_buffer, + __exponentiate_threshold, __a)) + __base_rope._M_tree_ptr = __base_leaf; + if (1 == __exponent) { + __result = __base_rope; # ifndef __GC - __stl_assert(1 == result -> tree_ptr -> refcount); + __stl_assert(2 == __result._M_tree_ptr->_M_refcount); + // One each for base_rope and __result # endif } else { - result = power(base_rope, exponent, concat_fn()); + // XXX what is power()? + __result = power(__base_rope, __exponent, _Concat_fn()); } - if (0 != remainder) { - result += remainder_rope; + if (0 != __remainder) { + __result += __remainder_rope; } } else { - result = remainder_rope; + __result = __remainder_rope; } - tree_ptr = result.tree_ptr; - tree_ptr -> ref_nonnil(); + _M_tree_ptr = __result._M_tree_ptr; + _M_tree_ptr->_M_ref_nonnil(); } -template<class charT, class Alloc> charT rope<charT,Alloc>::empty_c_str[1]; +template<class _CharT, class _Alloc> + _CharT rope<_CharT,_Alloc>::_S_empty_c_str[1]; # ifdef __STL_PTHREADS - template<class charT, class Alloc> - pthread_mutex_t rope<charT,Alloc>::swap_lock = PTHREAD_MUTEX_INITIALIZER; + template<class _CharT, class _Alloc> + pthread_mutex_t + rope<_CharT,_Alloc>::_S_swap_lock = PTHREAD_MUTEX_INITIALIZER; # endif -template<class charT, class Alloc> -const charT * rope<charT,Alloc>::c_str() const { - if (0 == tree_ptr) { - empty_c_str[0] = __eos((charT *)0); // Possibly redundant, +template<class _CharT, class _Alloc> +const _CharT* rope<_CharT,_Alloc>::c_str() const { + if (0 == _M_tree_ptr) { + _S_empty_c_str[0] = _S_eos((_CharT*)0); // Possibly redundant, // but probably fast. - return empty_c_str; + return _S_empty_c_str; } - __GC_CONST charT * old_c_string = tree_ptr -> c_string; - if (0 != old_c_string) return(old_c_string); - size_t s = size(); - charT * result = DataAlloc::allocate(s + 1); - flatten(tree_ptr, result); - result[s] = __eos((charT *)0); + __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string; + if (0 != __old_c_string) return(__old_c_string); + size_t __s = size(); + _CharT* __result = _Data_allocate(__s + 1); + _S_flatten(_M_tree_ptr, __result); + __result[__s] = _S_eos((_CharT*)0); # ifdef __GC - tree_ptr -> c_string = result; + _M_tree_ptr->_M_c_string = __result; # else - if ((old_c_string = atomic_swap(&(tree_ptr -> c_string), result)) != 0) { + if ((__old_c_string = + _S_atomic_swap(&(_M_tree_ptr->_M_c_string), __result)) != 0) { // It must have been added in the interim. Hence it had to have been // separately allocated. Deallocate the old copy, since we just // replaced it. - destroy(old_c_string, old_c_string + s + 1); - DataAlloc::deallocate(old_c_string, s + 1); + destroy(__old_c_string, __old_c_string + __s + 1); + _Data_deallocate(__old_c_string, __s + 1); } # endif - return(result); + return(__result); } -template<class charT, class Alloc> -const charT * rope<charT,Alloc>::replace_with_c_str() { - if (0 == tree_ptr) { - empty_c_str[0] = __eos((charT *)0); - return empty_c_str; +template<class _CharT, class _Alloc> +const _CharT* rope<_CharT,_Alloc>::replace_with_c_str() { + if (0 == _M_tree_ptr) { + _S_empty_c_str[0] = _S_eos((_CharT*)0); + return _S_empty_c_str; } - __GC_CONST charT * old_c_string = tree_ptr -> c_string; - if (RopeBase::leaf == tree_ptr -> tag && 0 != old_c_string) { - return(old_c_string); + __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string; + if (_RopeRep::_S_leaf == _M_tree_ptr->_M_tag && 0 != __old_c_string) { + return(__old_c_string); } - size_t s = size(); - charT * result = DataAlloc::allocate(rounded_up_size(s)); - flatten(tree_ptr, result); - result[s] = __eos((charT *)0); - tree_ptr -> unref_nonnil(); - tree_ptr = RopeLeaf_from_char_ptr(result, s); - return(result); + size_t __s = size(); + _CharT* __result = _Data_allocate(_S_rounded_up_size(__s)); + _S_flatten(_M_tree_ptr, __result); + __result[__s] = _S_eos((_CharT*)0); + _M_tree_ptr->_M_unref_nonnil(); + _M_tree_ptr = _S_new_RopeLeaf(__result, __s, get_allocator()); + return(__result); } // Algorithm specializations. More should be added. #ifndef _MSC_VER // I couldn't get this to work with VC++ -template<class charT,class Alloc> +template<class _CharT,class _Alloc> void -__rope_rotate(__rope_iterator<charT,Alloc> first, - __rope_iterator<charT,Alloc> middle, - __rope_iterator<charT,Alloc> last) { - __stl_assert(first.container() == middle.container() - && middle.container() == last.container()); - rope<charT,Alloc>& r(first.container()); - rope<charT,Alloc> prefix = r.substr(0, first.index()); - rope<charT,Alloc> suffix = r.substr(last.index(), r.size() - last.index()); - rope<charT,Alloc> part1 = r.substr(middle.index(), - last.index() - middle.index()); - rope<charT,Alloc> part2 = r.substr(first.index(), - middle.index() - first.index()); - r = prefix; - r += part1; - r += part2; - r += suffix; +_Rope_rotate(_Rope_iterator<_CharT,_Alloc> __first, + _Rope_iterator<_CharT,_Alloc> __middle, + _Rope_iterator<_CharT,_Alloc> __last) +{ + __stl_assert(__first.container() == __middle.container() + && __middle.container() == __last.container()); + rope<_CharT,_Alloc>& __r(__first.container()); + rope<_CharT,_Alloc> __prefix = __r.substr(0, __first.index()); + rope<_CharT,_Alloc> __suffix = + __r.substr(__last.index(), __r.size() - __last.index()); + rope<_CharT,_Alloc> __part1 = + __r.substr(__middle.index(), __last.index() - __middle.index()); + rope<_CharT,_Alloc> __part2 = + __r.substr(__first.index(), __middle.index() - __first.index()); + __r = __prefix; + __r += __part1; + __r += __part2; + __r += __suffix; } -inline void rotate(__rope_iterator<char,__ALLOC> first, - __rope_iterator<char,__ALLOC> middle, - __rope_iterator<char,__ALLOC> last) { - __rope_rotate(first, middle, last); +#if !defined(__GNUC__) +// Appears to confuse g++ +inline void rotate(_Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __first, + _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __middle, + _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __last) { + _Rope_rotate(__first, __middle, __last); } +#endif # if 0 // Probably not useful for several reasons: @@ -1518,10 +1530,11 @@ inline void rotate(__rope_iterator<char,__ALLOC> first, // - wchar_t is 4 bytes wide on most UNIX platforms, making it unattractive // for unicode strings. Unsigned short may be a better character // type. -inline void rotate(__rope_iterator<wchar_t,__ALLOC> first, - __rope_iterator<wchar_t,__ALLOC> middle, - __rope_iterator<wchar_t,__ALLOC> last) { - __rope_rotate(first, middle, last); +inline void rotate( + _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __first, + _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __middle, + _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __last) { + _Rope_rotate(__first, __middle, __last); } # endif #endif /* _MSC_VER */ |