- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Configs/TI_DSP_BIOS/stl/ropeimpl.h

    00001 /*
    00002  * Copyright (c) 1997
    00003  * Silicon Graphics Computer Systems, Inc.
    00004  *
    00005  * Permission to use, copy, modify, distribute and sell this software
    00006  * and its documentation for any purpose is hereby granted without fee,
    00007  * provided that the above copyright notice appear in all copies and
    00008  * that both that copyright notice and this permission notice appear
    00009  * in supporting documentation.  Silicon Graphics makes no
    00010  * representations about the suitability of this software for any
    00011  * purpose.  It is provided "as is" without express or implied warranty.
    00012  */
    00013
    00014 /* NOTE: This is an internal header file, included by other STL headers.
    00015  *   You should not attempt to use it directly.
    00016  */
    00017
    00018 # include <stdio.h>     /* XXX should use <cstdio> */
    00019 # include <iostream.h>  /* XXX should use <iostream> */
    00020
    00021 __STL_BEGIN_NAMESPACE
    00022
    00023 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
    00024 #pragma set woff 1174
    00025 #endif
    00026
    00027 // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
    00028 // if necessary.  Assumes _M_path_end[leaf_index] and leaf_pos are correct.
    00029 // Results in a valid buf_ptr if the iterator can be legitimately
    00030 // dereferenced.
    00031 template <class _CharT, class _Alloc>
    00032 void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf(
    00033   _Rope_iterator_base<_CharT,_Alloc>& __x)
    00034 {
    00035     const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index];
    00036     size_t __leaf_pos = __x._M_leaf_pos;
    00037     size_t __pos = __x._M_current_pos;
    00038
    00039     switch(__leaf->_M_tag) {
    00040         case _RopeRep::_S_leaf:
    00041             __x._M_buf_start =
    00042               ((_Rope_RopeLeaf<_CharT,_Alloc>*)__leaf)->_M_data;
    00043             __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos);
    00044             __x._M_buf_end = __x._M_buf_start + __leaf->_M_size;
    00045             break;
    00046         case _RopeRep::_S_function:
    00047         case _RopeRep::_S_substringfn:
    00048             {
    00049                 size_t __len = _S_iterator_buf_len;
    00050                 size_t __buf_start_pos = __leaf_pos;
    00051                 size_t __leaf_end = __leaf_pos + __leaf->_M_size;
    00052                 char_producer<_CharT>* __fn =
    00053                         ((_Rope_RopeFunction<_CharT,_Alloc>*)__leaf)->_M_fn;
    00054
    00055                 if (__buf_start_pos + __len <= __pos) {
    00056                     __buf_start_pos = __pos - __len/4;
    00057                     if (__buf_start_pos + __len > __leaf_end) {
    00058                         __buf_start_pos = __leaf_end - __len;
    00059                     }
    00060                 }
    00061                 if (__buf_start_pos + __len > __leaf_end) {
    00062                     __len = __leaf_end - __buf_start_pos;
    00063                 }
    00064                 (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
    00065                 __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
    00066                 __x._M_buf_start = __x._M_tmp_buf;
    00067                 __x._M_buf_end = __x._M_tmp_buf + __len;
    00068             }
    00069             break;
    00070         default:
    00071             __stl_assert(0);
    00072     }
    00073 }
    00074
    00075 // Set path and buffer inside a rope iterator.  We assume that
    00076 // pos and root are already set.
    00077 template <class _CharT, class _Alloc>
    00078 void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
    00079 (_Rope_iterator_base<_CharT,_Alloc>& __x)
    00080 {
    00081     const _RopeRep* __path[_RopeRep::_S_max_rope_depth+1];
    00082     const _RopeRep* __curr_rope;
    00083     int __curr_depth = -1;  /* index into path    */
    00084     size_t __curr_start_pos = 0;
    00085     size_t __pos = __x._M_current_pos;
    00086     unsigned char __dirns = 0; // Bit vector marking right turns in the path
    00087
    00088     __stl_assert(__pos <= __x._M_root->_M_size);
    00089     if (__pos >= __x._M_root->_M_size) {
    00090         __x._M_buf_ptr = 0;
    00091         return;
    00092     }
    00093     __curr_rope = __x._M_root;
    00094     if (0 != __curr_rope->_M_c_string) {
    00095         /* Treat the root as a leaf. */
    00096         __x._M_buf_start = __curr_rope->_M_c_string;
    00097         __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
    00098         __x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
    00099         __x._M_path_end[0] = __curr_rope;
    00100         __x._M_leaf_index = 0;
    00101         __x._M_leaf_pos = 0;
    00102         return;
    00103     }
    00104     for(;;) {
    00105         ++__curr_depth;
    00106         __stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth);
    00107         __path[__curr_depth] = __curr_rope;
    00108         switch(__curr_rope->_M_tag) {
    00109           case _RopeRep::_S_leaf:
    00110           case _RopeRep::_S_function:
    00111           case _RopeRep::_S_substringfn:
    00112             __x._M_leaf_pos = __curr_start_pos;
    00113             goto done;
    00114           case _RopeRep::_S_concat:
    00115             {
    00116                 _Rope_RopeConcatenation<_CharT,_Alloc>* __c =
    00117                         (_Rope_RopeConcatenation<_CharT,_Alloc>*)__curr_rope;
    00118                 _RopeRep* __left = __c->_M_left;
    00119                 size_t __left_len = __left->_M_size;
    00120
    00121                 __dirns <<= 1;
    00122                 if (__pos >= __curr_start_pos + __left_len) {
    00123                     __dirns |= 1;
    00124                     __curr_rope = __c->_M_right;
    00125                     __curr_start_pos += __left_len;
    00126                 } else {
    00127                     __curr_rope = __left;
    00128                 }
    00129             }
    00130             break;
    00131         }
    00132     }
    00133   done:
    00134     // Copy last section of path into _M_path_end.
    00135       {
    00136         int __i = -1;
    00137         int __j = __curr_depth + 1 - _S_path_cache_len;
    00138
    00139         if (__j < 0) __j = 0;
    00140         while (__j <= __curr_depth) {
    00141             __x._M_path_end[++__i] = __path[__j++];
    00142         }
    00143         __x._M_leaf_index = __i;
    00144       }
    00145       __x._M_path_directions = __dirns;
    00146       _S_setbuf(__x);
    00147 }
    00148
    00149 // Specialized version of the above.  Assumes that
    00150 // the path cache is valid for the previous position.
    00151 template <class _CharT, class _Alloc>
    00152 void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr
    00153 (_Rope_iterator_base<_CharT,_Alloc>& __x)
    00154 {
    00155     int __current_index = __x._M_leaf_index;
    00156     const _RopeRep* __current_node = __x._M_path_end[__current_index];
    00157     size_t __len = __current_node->_M_size;
    00158     size_t __node_start_pos = __x._M_leaf_pos;
    00159     unsigned char __dirns = __x._M_path_directions;
    00160     _Rope_RopeConcatenation<_CharT,_Alloc>* __c;
    00161
    00162     __stl_assert(__x._M_current_pos <= __x._M_root->_M_size);
    00163     if (__x._M_current_pos - __node_start_pos < __len) {
    00164         /* More stuff in this leaf, we just didn't cache it. */
    00165         _S_setbuf(__x);
    00166         return;
    00167     }
    00168     __stl_assert(__node_start_pos + __len == __x._M_current_pos);
    00169     //  node_start_pos is starting position of last_node.
    00170     while (--__current_index >= 0) {
    00171         if (!(__dirns & 1) /* Path turned left */)
    00172           break;
    00173         __current_node = __x._M_path_end[__current_index];
    00174         __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
    00175         // Otherwise we were in the right child.  Thus we should pop
    00176         // the concatenation node.
    00177         __node_start_pos -= __c->_M_left->_M_size;
    00178         __dirns >>= 1;
    00179     }
    00180     if (__current_index < 0) {
    00181         // We underflowed the cache. Punt.
    00182         _S_setcache(__x);
    00183         return;
    00184     }
    00185     __current_node = __x._M_path_end[__current_index];
    00186     __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
    00187     // current_node is a concatenation node.  We are positioned on the first
    00188     // character in its right child.
    00189     // node_start_pos is starting position of current_node.
    00190     __node_start_pos += __c->_M_left->_M_size;
    00191     __current_node = __c->_M_right;
    00192     __x._M_path_end[++__current_index] = __current_node;
    00193     __dirns |= 1;
    00194     while (_RopeRep::_S_concat == __current_node->_M_tag) {
    00195         ++__current_index;
    00196         if (_S_path_cache_len == __current_index) {
    00197             int __i;
    00198             for (__i = 0; __i < _S_path_cache_len-1; __i++) {
    00199                 __x._M_path_end[__i] = __x._M_path_end[__i+1];
    00200             }
    00201             --__current_index;
    00202         }
    00203         __current_node =
    00204             ((_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node)->_M_left;
    00205         __x._M_path_end[__current_index] = __current_node;
    00206         __dirns <<= 1;
    00207         // node_start_pos is unchanged.
    00208     }
    00209     __x._M_leaf_index = __current_index;
    00210     __x._M_leaf_pos = __node_start_pos;
    00211     __x._M_path_directions = __dirns;
    00212     _S_setbuf(__x);
    00213 }
    00214
    00215 template <class _CharT, class _Alloc>
    00216 void _Rope_iterator_base<_CharT,_Alloc>::_M_incr(size_t __n) {
    00217     _M_current_pos += __n;
    00218     if (0 != _M_buf_ptr) {
    00219         size_t __chars_left = _M_buf_end - _M_buf_ptr;
    00220         if (__chars_left > __n) {
    00221             _M_buf_ptr += __n;
    00222         } else if (__chars_left == __n) {
    00223             _M_buf_ptr += __n;
    00224             _S_setcache_for_incr(*this);
    00225         } else {
    00226             _M_buf_ptr = 0;
    00227         }
    00228     }
    00229 }
    00230
    00231 template <class _CharT, class _Alloc>
    00232 void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) {
    00233     if (0 != _M_buf_ptr) {
    00234         size_t __chars_left = _M_buf_ptr - _M_buf_start;
    00235         if (__chars_left >= __n) {
    00236             _M_buf_ptr -= __n;
    00237         } else {
    00238             _M_buf_ptr = 0;
    00239         }
    00240     }
    00241     _M_current_pos -= __n;
    00242 }
    00243
    00244 template <class _CharT, class _Alloc>
    00245 void _Rope_iterator<_CharT,_Alloc>::_M_check() {
    00246     if (_M_root_rope->_M_tree_ptr != _M_root) {
    00247         // _Rope was modified.  Get things fixed up.
    00248         _RopeRep::_S_unref(_M_root);
    00249         _M_root = _M_root_rope->_M_tree_ptr;
    00250         _RopeRep::_S_ref(_M_root);
    00251         _M_buf_ptr = 0;
    00252     }
    00253 }
    00254
    00255 template <class _CharT, class _Alloc>
    00256 inline
    00257 _Rope_const_iterator<_CharT, _Alloc>::_Rope_const_iterator(
    00258   const _Rope_iterator<_CharT,_Alloc>& __x)
    00259 : _Rope_iterator_base<_CharT,_Alloc>(__x)
    00260 { }
    00261
    00262 template <class _CharT, class _Alloc>
    00263 inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator(
    00264   rope<_CharT,_Alloc>& __r, size_t __pos)
    00265 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos),
    00266   _M_root_rope(&__r)
    00267 {
    00268     _RopeRep::_S_ref(_M_root);
    00269 }
    00270
    00271 template <class _CharT, class _Alloc>
    00272 inline size_t
    00273 rope<_CharT,_Alloc>::_S_char_ptr_len(const _CharT* __s)
    00274 {
    00275     const _CharT* __p = __s;
    00276
    00277     while (!_S_is0(*__p)) { ++__p; }
    00278     return (__p - __s);
    00279 }
    00280
    00281
    00282 #ifndef __GC
    00283
    00284 template <class _CharT, class _Alloc>
    00285 inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
    00286 {
    00287     _CharT* __cstr = _M_c_string;
    00288     if (0 != __cstr) {
    00289         size_t __size = _M_size + 1;
    00290         destroy(__cstr, __cstr + __size);
    00291         _Data_deallocate(__cstr, __size);
    00292     }
    00293 }
    00294
    00295
    00296 template <class _CharT, class _Alloc>
    00297 #ifdef __STL_USE_STD_ALLOCATORS
    00298   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
    00299                                                            size_t __n,
    00300                                                            allocator_type __a)
    00301 #else
    00302   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
    00303                                                            size_t __n)
    00304 #endif
    00305 {
    00306     if (!_S_is_basic_char_type((_CharT*)0)) {
    00307         destroy(__s, __s + __n);
    00308     }
    00309 //  This has to be a static member, so this gets a bit messy
    00310 #   ifdef __STL_USE_STD_ALLOCATORS
    00311         __a.deallocate(
    00312             __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
    00313 #   else
    00314         _Data_deallocate(
    00315             __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
    00316 #   endif
    00317 }
    00318
    00319
    00320 //  There are several reasons for not doing this with virtual destructors
    00321 //  and a class specific delete operator:
    00322 //  - A class specific delete operator can't easily get access to
    00323 //    allocator instances if we need them.
    00324 //  - Any virtual function would need a 4 or byte vtable pointer;
    00325 //    this only requires a one byte tag per object.
    00326 template <class _CharT, class _Alloc>
    00327 void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree()
    00328 {
    00329     switch(_M_tag) {
    00330         case _S_leaf:
    00331             {
    00332                 _Rope_RopeLeaf<_CharT,_Alloc>* __l
    00333                         = (_Rope_RopeLeaf<_CharT,_Alloc>*)this;
    00334                 __l->_Rope_RopeLeaf<_CharT,_Alloc>::~_Rope_RopeLeaf();
    00335                 _L_deallocate(__l, 1);
    00336                 break;
    00337             }
    00338         case _S_concat:
    00339             {
    00340                 _Rope_RopeConcatenation<_CharT,_Alloc>* __c
    00341                     = (_Rope_RopeConcatenation<_CharT,_Alloc>*)this;
    00342                 __c->_Rope_RopeConcatenation<_CharT,_Alloc>::
                           ~_Rope_RopeConcatenation();
    00343                 _C_deallocate(__c, 1);
    00344                 break;
    00345             }
    00346         case _S_function:
    00347             {
    00348                 _Rope_RopeFunction<_CharT,_Alloc>* __f
    00349                     = (_Rope_RopeFunction<_CharT,_Alloc>*)this;
    00350                 __f->_Rope_RopeFunction<_CharT,_Alloc>::~_Rope_RopeFunction();
    00351                 _F_deallocate(__f, 1);
    00352                 break;
    00353             }
    00354         case _S_substringfn:
    00355             {
    00356                 _Rope_RopeSubstring<_CharT,_Alloc>* __ss =
    00357                         (_Rope_RopeSubstring<_CharT,_Alloc>*)this;
    00358                 __ss->_Rope_RopeSubstring<_CharT,_Alloc>::
                            ~_Rope_RopeSubstring();
    00359                 _S_deallocate(__ss, 1);
    00360                 break;
    00361             }
    00362     }
    00363 }
    00364 #else
    00365
    00366 template <class _CharT, class _Alloc>
    00367 #ifdef __STL_USE_STD_ALLOCATORS
    00368   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
    00369                 (const _CharT*, size_t, allocator_type)
    00370 #else
    00371   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
    00372                 (const _CharT*, size_t)
    00373 #endif
    00374 {}
    00375
    00376 #endif
    00377
    00378
    00379 // Concatenate a C string onto a leaf rope by copying the rope data.
    00380 // Used for short ropes.
    00381 template <class _CharT, class _Alloc>
    00382 rope<_CharT,_Alloc>::_RopeLeaf*
    00383 rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
    00384                 (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
    00385 {
    00386     size_t __old_len = __r->_M_size;
    00387     _CharT* __new_data = (_CharT*)
    00388         _Data_allocate(_S_rounded_up_size(__old_len + __len));
    00389     _RopeLeaf* __result;
    00390
    00391     uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
    00392     uninitialized_copy_n(__iter, __len, __new_data + __old_len);
    00393     _S_cond_store_eos(__new_data[__old_len + __len]);
    00394     __STL_TRY {
    00395         __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
    00396                                    __r->get_allocator());
    00397     }
    00398     __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
    00399                                              __r->get_allocator()));
    00400     return __result;
    00401 }
    00402
    00403 #ifndef __GC
    00404 // As above, but it's OK to clobber original if refcount is 1
    00405 template <class _CharT, class _Alloc>
    00406 rope<_CharT,_Alloc>::_RopeLeaf*
    00407 rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
    00408                 (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
    00409 {
    00410     __stl_assert(__r->_M_refcount >= 1);
    00411     if (__r->_M_refcount > 1)
    00412       return _S_leaf_concat_char_iter(__r, __iter, __len);
    00413     size_t __old_len = __r->_M_size;
    00414     if (_S_allocated_capacity(__old_len) >= __old_len + __len) {
    00415         // The space has been partially initialized for the standard
    00416         // character types.  But that doesn't matter for those types.
    00417         uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
    00418         if (_S_is_basic_char_type((_CharT*)0)) {
    00419             _S_cond_store_eos(__r->_M_data[__old_len + __len]);
    00420             __stl_assert(__r->_M_c_string == __r->_M_data);
    00421         } else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) {
    00422             __r->_M_free_c_string();
    00423             __r->_M_c_string = 0;
    00424         }
    00425         __r->_M_size = __old_len + __len;
    00426         __stl_assert(__r->_M_refcount == 1);
    00427         __r->_M_refcount = 2;
    00428         return __r;
    00429     } else {
    00430         _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
    00431         __stl_assert(__result->_M_refcount == 1);
    00432         return __result;
    00433     }
    00434 }
    00435 #endif
    00436
    00437 // Assumes left and right are not 0.
    00438 // Does not increment (nor decrement on exception) child reference counts.
    00439 // Result has ref count 1.
    00440 template <class _CharT, class _Alloc>
    00441 rope<_CharT,_Alloc>::_RopeRep*
    00442 rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
    00443 {
    00444     _RopeConcatenation* __result =
    00445       _S_new_RopeConcatenation(__left, __right, __left->get_allocator());
    00446     size_t __depth = __result->_M_depth;
    00447
    00448 #   ifdef __STL_USE_STD_ALLOCATORS
    00449       __stl_assert(__left->get_allocator() == __right->get_allocator());
    00450 #   endif
    00451     if (__depth > 20 && (__result->_M_size < 1000 ||
    00452                          __depth > _RopeRep::_S_max_rope_depth)) {
    00453         _RopeRep* __balanced;
    00454
    00455         __STL_TRY {
    00456            __balanced = _S_balance(__result);
    00457 #          ifndef __GC
    00458              if (__result != __balanced) {
    00459                 __stl_assert(1 == __result->_M_refcount
    00460                              && 1 == __balanced->_M_refcount);
    00461              }
    00462 #          endif
    00463            __result->_M_unref_nonnil();
    00464         }
    00465         __STL_UNWIND((_C_deallocate(__result,1)));
    00466                 // In case of exception, we need to deallocate
    00467                 // otherwise dangling result node.  But caller
    00468                 // still owns its children.  Thus unref is
    00469                 // inappropriate.
    00470         return __balanced;
    00471     } else {
    00472         return __result;
    00473     }
    00474 }
    00475
    00476 template <class _CharT, class _Alloc>
    00477 rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
    00478                 (_RopeRep* __r, const _CharT*__s, size_t __slen)
    00479 {
    00480     _RopeRep* __result;
    00481     if (0 == __slen) {
    00482         _S_ref(__r);
    00483         return __r;
    00484     }
    00485     if (0 == __r)
    00486       return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
    00487                                               __r->get_allocator());
    00488     if (_RopeRep::_S_leaf == __r->_M_tag &&
    00489           __r->_M_size + __slen <= _S_copy_max) {
    00490         __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
    00491 #       ifndef __GC
    00492           __stl_assert(1 == __result->_M_refcount);
    00493 #       endif
    00494         return __result;
    00495     }
    00496     if (_RopeRep::_S_concat == __r->_M_tag
    00497         && _RopeRep::_S_leaf == ((_RopeConcatenation*)__r)->_M_right->_M_tag) {
    00498         _RopeLeaf* __right =
    00499           (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
    00500         if (__right->_M_size + __slen <= _S_copy_max) {
    00501           _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
    00502           _RopeRep* __nright =
    00503             _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
    00504           __left->_M_ref_nonnil();
    00505           __STL_TRY {
    00506             __result = _S_tree_concat(__left, __nright);
    00507           }
    00508           __STL_UNWIND(_S_unref(__left); _S_unref(__nright));
    00509 #         ifndef __GC
    00510             __stl_assert(1 == __result->_M_refcount);
    00511 #         endif
    00512           return __result;
    00513         }
    00514     }
    00515     _RopeRep* __nright =
    00516       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
    00517     __STL_TRY {
    00518       __r->_M_ref_nonnil();
    00519       __result = _S_tree_concat(__r, __nright);
    00520     }
    00521     __STL_UNWIND(_S_unref(__r); _S_unref(__nright));
    00522 #   ifndef __GC
    00523       __stl_assert(1 == __result->_M_refcount);
    00524 #   endif
    00525     return __result;
    00526 }
    00527
    00528 #ifndef __GC
    00529 template <class _CharT, class _Alloc>
    00530 rope<_CharT,_Alloc>::_RopeRep*
    00531 rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
    00532   _RopeRep* __r, const _CharT* __s, size_t __slen)
    00533 {
    00534     _RopeRep* __result;
    00535     if (0 == __r)
    00536       return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
    00537                                               __r->get_allocator());
    00538     size_t __count = __r->_M_refcount;
    00539     size_t __orig_size = __r->_M_size;
    00540     __stl_assert(__count >= 1);
    00541     if (__count > 1) return _S_concat_char_iter(__r, __s, __slen);
    00542     if (0 == __slen) {
    00543         __r->_M_refcount = 2;      // One more than before
    00544         return __r;
    00545     }
    00546     if (__orig_size + __slen <= _S_copy_max &&
    00547           _RopeRep::_S_leaf == __r->_M_tag) {
    00548         __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
    00549         return __result;
    00550     }
    00551     if (_RopeRep::_S_concat == __r->_M_tag) {
    00552         _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)__r)->_M_right);
    00553         if (_RopeRep::_S_leaf == __right->_M_tag
    00554             && __right->_M_size + __slen <= _S_copy_max) {
    00555           _RopeRep* __new_right =
    00556             _S_destr_leaf_concat_char_iter(__right, __s, __slen);
    00557           if (__right == __new_right) {
    00558               __stl_assert(__new_right->_M_refcount == 2);
    00559               __new_right->_M_refcount = 1;
    00560           } else {
    00561               __stl_assert(__new_right->_M_refcount >= 1);
    00562               __right->_M_unref_nonnil();
    00563           }
    00564           __stl_assert(__r->_M_refcount == 1);
    00565           __r->_M_refcount = 2;    // One more than before.
    00566           ((_RopeConcatenation*)__r)->_M_right = __new_right;
    00567           __r->_M_size = __orig_size + __slen;
    00568           if (0 != __r->_M_c_string) {
    00569               __r->_M_free_c_string();
    00570               __r->_M_c_string = 0;
    00571           }
    00572           return __r;
    00573         }
    00574     }
    00575     _RopeRep* __right =
    00576       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
    00577     __r->_M_ref_nonnil();
    00578     __STL_TRY {
    00579       __result = _S_tree_concat(__r, __right);
    00580     }
    00581     __STL_UNWIND(_S_unref(__r); _S_unref(__right))
    00582     __stl_assert(1 == __result->_M_refcount);
    00583     return __result;
    00584 }
    00585 #endif /* !__GC */
    00586
    00587 template <class _CharT, class _Alloc>
    00588 rope<_CharT,_Alloc>::_RopeRep*
    00589 rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
    00590 {
    00591     if (0 == __left) {
    00592         _S_ref(__right);
    00593         return __right;
    00594     }
    00595     if (0 == __right) {
    00596         __left->_M_ref_nonnil();
    00597         return __left;
    00598     }
    00599     if (_RopeRep::_S_leaf == __right->_M_tag) {
    00600         if (_RopeRep::_S_leaf == __left->_M_tag) {
    00601           if (__right->_M_size + __left->_M_size <= _S_copy_max) {
    00602             return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
    00603                                          ((_RopeLeaf*)__right)->_M_data,
    00604                                          __right->_M_size);
    00605           }
    00606         } else if (_RopeRep::_S_concat == __left->_M_tag
    00607                    && _RopeRep::_S_leaf ==
    00608                       ((_RopeConcatenation*)__left)->_M_right->_M_tag) {
    00609           _RopeLeaf* __leftright =
    00610                     (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right);
    00611           if (__leftright->_M_size + __right->_M_size <= _S_copy_max) {
    00612             _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
    00613             _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
    00614                                            ((_RopeLeaf*)__right)->_M_data,
    00615                                            __right->_M_size);
    00616             __leftleft->_M_ref_nonnil();
    00617             __STL_TRY {
    00618               return(_S_tree_concat(__leftleft, __rest));
    00619             }
    00620             __STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest))
    00621           }
    00622         }
    00623     }
    00624     __left->_M_ref_nonnil();
    00625     __right->_M_ref_nonnil();
    00626     __STL_TRY {
    00627       return(_S_tree_concat(__left, __right));
    00628     }
    00629     __STL_UNWIND(_S_unref(__left); _S_unref(__right));
    00630 }
    00631
    00632 template <class _CharT, class _Alloc>
    00633 rope<_CharT,_Alloc>::_RopeRep*
    00634 rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base,
    00635                                size_t __start, size_t __endp1)
    00636 {
    00637     if (0 == __base) return 0;
    00638     size_t __len = __base->_M_size;
    00639     size_t __adj_endp1;
    00640     const size_t __lazy_threshold = 128;
    00641
    00642     if (__endp1 >= __len) {
    00643         if (0 == __start) {
    00644             __base->_M_ref_nonnil();
    00645             return __base;
    00646         } else {
    00647             __adj_endp1 = __len;
    00648         }
    00649     } else {
    00650         __adj_endp1 = __endp1;
    00651     }
    00652     switch(__base->_M_tag) {
    00653         case _RopeRep::_S_concat:
    00654             {
    00655                 _RopeConcatenation* __c = (_RopeConcatenation*)__base;
    00656                 _RopeRep* __left = __c->_M_left;
    00657                 _RopeRep* __right = __c->_M_right;
    00658                 size_t __left_len = __left->_M_size;
    00659                 _RopeRep* __result;
    00660
    00661                 if (__adj_endp1 <= __left_len) {
    00662                     return _S_substring(__left, __start, __endp1);
    00663                 } else if (__start >= __left_len) {
    00664                     return _S_substring(__right, __start - __left_len,
    00665                                   __adj_endp1 - __left_len);
    00666                 }
    00667                 _Self_destruct_ptr __left_result(
    00668                   _S_substring(__left, __start, __left_len));
    00669                 _Self_destruct_ptr __right_result(
    00670                   _S_substring(__right, 0, __endp1 - __left_len));
    00671                 __result = _S_concat(__left_result, __right_result);
    00672 #               ifndef __GC
    00673                   __stl_assert(1 == __result->_M_refcount);
    00674 #               endif
    00675                 return __result;
    00676             }
    00677         case _RopeRep::_S_leaf:
    00678             {
    00679                 _RopeLeaf* __l = (_RopeLeaf*)__base;
    00680                 _RopeLeaf* __result;
    00681                 size_t __result_len;
    00682                 if (__start >= __adj_endp1) return 0;
    00683                 __result_len = __adj_endp1 - __start;
    00684                 if (__result_len > __lazy_threshold) goto lazy;
    00685 #               ifdef __GC
    00686                     const _CharT* __section = __l->_M_data + __start;
    00687                     __result = _S_new_RopeLeaf(__section, __result_len,
    00688                                           __base->get_allocator());
    00689                     __result->_M_c_string = 0;  // Not eos terminated.
    00690 #               else
    00691                     // We should sometimes create substring node instead.
    00692                     __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(
    00693                                         __l->_M_data + __start, __result_len,
    00694                                         __base->get_allocator());
    00695 #               endif
    00696                 return __result;
    00697             }
    00698         case _RopeRep::_S_substringfn:
    00699             // Avoid introducing multiple layers of substring nodes.
    00700             {
    00701                 _RopeSubstring* __old = (_RopeSubstring*)__base;
    00702                 size_t __result_len;
    00703                 if (__start >= __adj_endp1) return 0;
    00704                 __result_len = __adj_endp1 - __start;
    00705                 if (__result_len > __lazy_threshold) {
    00706                     _RopeSubstring* __result =
    00707                         _S_new_RopeSubstring(__old->_M_base,
    00708                                           __start + __old->_M_start,
    00709                                           __adj_endp1 - __start,
    00710                                           __base->get_allocator());
    00711                     return __result;
    00712
    00713                 } // *** else fall through: ***
    00714             }
    00715         case _RopeRep::_S_function:
    00716             {
    00717                 _RopeFunction* __f = (_RopeFunction*)__base;
    00718                 _CharT* __section;
    00719                 size_t __result_len;
    00720                 if (__start >= __adj_endp1) return 0;
    00721                 __result_len = __adj_endp1 - __start;
    00722
    00723                 if (__result_len > __lazy_threshold) goto lazy;
    00724                 __section = (_CharT*)
    00725                         _Data_allocate(_S_rounded_up_size(__result_len));
    00726                 __STL_TRY {
    00727                   (*(__f->_M_fn))(__start, __result_len, __section);
    00728                 }
    00729                 __STL_UNWIND(_RopeRep::__STL_FREE_STRING(
    00730                        __section, __result_len, __base->get_allocator()));
    00731                 _S_cond_store_eos(__section[__result_len]);
    00732                 return _S_new_RopeLeaf(__section, __result_len,
    00733                                        __base->get_allocator());
    00734             }
    00735     }
    00736     /*NOTREACHED*/
    00737     __stl_assert(false);
    00738   lazy:
    00739     {
    00740         // Create substring node.
    00741         return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
    00742                                __base->get_allocator());
    00743     }
    00744 }
    00745
    00746 template<class _CharT>
    00747 class _Rope_flatten_char_consumer : public _Rope_char_consumer<_CharT> {
    00748     private:
    00749         _CharT* _M_buf_ptr;
    00750     public:
    00751         //  _CharT* _M_buffer;  // XXX not used
    00752
    00753         _Rope_flatten_char_consumer(_CharT* __buffer) {
    00754             _M_buf_ptr = __buffer;
    00755         };
    00756         ~_Rope_flatten_char_consumer() {}
    00757         bool operator() (const _CharT* __leaf, size_t __n) {
    00758             uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
    00759             _M_buf_ptr += __n;
    00760             return true;
    00761         }
    00762 };
    00763
    00764 template<class _CharT>
    00765 class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> {
    00766     private:
    00767         _CharT _M_pattern;
    00768     public:
    00769         size_t _M_count;  // Number of nonmatching characters
    00770         _Rope_find_char_char_consumer(_CharT __p)
    00771           : _M_pattern(__p), _M_count(0) {}
    00772         ~_Rope_find_char_char_consumer() {}
    00773         bool operator() (const _CharT* __leaf, size_t __n) {
    00774             size_t __i;
    00775             for (__i = 0; __i < __n; __i++) {
    00776                 if (__leaf[__i] == _M_pattern) {
    00777                     _M_count += __i; return false;
    00778                 }
    00779             }
    00780             _M_count += __n; return true;
    00781         }
    00782 };
    00783
    00784 template<class _CharT>
    00785 class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
    00786     private:
    00787         typedef ostream _Insert_ostream;
    00788         _Insert_ostream& _M_o;
    00789     public:
    00790         // _CharT* buffer;    // XXX not used
    00791         _Rope_insert_char_consumer(_Insert_ostream& __writer)
    00792           : _M_o(__writer) {};
    00793         ~_Rope_insert_char_consumer() { };
    00794                 // Caller is presumed to own the ostream
    00795         bool operator() (const _CharT* __leaf, size_t __n);
    00796                 // Returns true to continue traversal.
    00797 };
    00798
    00799 template<class _CharT>
    00800 bool _Rope_insert_char_consumer<_CharT>::operator()
    00801                                         (const _CharT* __leaf, size_t __n)
    00802 {
    00803     size_t __i;
    00804     //  We assume that formatting is set up correctly for each element.
    00805     for (__i = 0; __i < __n; __i++) _M_o << __leaf[__i];
    00806     return true;
    00807 }
    00808
    00809 inline bool _Rope_insert_char_consumer<char>::operator()
    00810                                         (const char* __leaf, size_t __n)
    00811 {
    00812     size_t __i;
    00813     for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
    00814     return true;
    00815 }
    00816
    00817 #if 0
    00818 // I couldn't get this to work work with the VC++ version of basic_ostream.
    00819 // It also doesn't really do the right thing unless o is a wide stream.
    00820 // Given that wchar_t is often 4 bytes, its not clear to me how useful
    00821 // this stuff is anyway.
    00822 inline bool _Rope_insert_char_consumer<wchar_t>::operator()
    00823                                         (const wchar_t* __leaf, size_t __n)
    00824 {
    00825     size_t __i;
    00826     for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
    00827     return true;
    00828 }
    00829 #endif /* !_MSC_VER  && !BORLAND */
    00830
    00831 template <class _CharT, class _Alloc>
    00832 bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
    00833                                 _Rope_char_consumer<_CharT>& __c,
    00834                                 const _RopeRep* __r,
    00835                                 size_t __begin, size_t __end)
    00836 {
    00837     if (0 == __r) return true;
    00838     switch(__r->_M_tag) {
    00839         case _RopeRep::_S_concat:
    00840             {
    00841                 _RopeConcatenation* __conc = (_RopeConcatenation*)__r;
    00842                 _RopeRep* __left =  __conc->_M_left;
    00843                 size_t __left_len = __left->_M_size;
    00844                 if (__begin < __left_len) {
    00845                     size_t __left_end = min(__left_len, __end);
    00846                     if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
    00847                         return false;
    00848                 }
    00849                 if (__end > __left_len) {
    00850                     _RopeRep* __right =  __conc->_M_right;
    00851                     size_t __right_start = max(__left_len, __begin);
    00852                     if (!_S_apply_to_pieces(__c, __right,
    00853                                          __right_start - __left_len,
    00854                                          __end - __left_len)) {
    00855                         return false;
    00856                     }
    00857                 }
    00858             }
    00859             return true;
    00860         case _RopeRep::_S_leaf:
    00861             {
    00862                 _RopeLeaf* __l = (_RopeLeaf*)__r;
    00863                 return __c(__l->_M_data + __begin, __end - __begin);
    00864             }
    00865         case _RopeRep::_S_function:
    00866         case _RopeRep::_S_substringfn:
    00867             {
    00868                 _RopeFunction* __f = (_RopeFunction*)__r;
    00869                 size_t __len = __end - __begin;
    00870                 bool __result;
    00871                 _CharT* __buffer =
    00872                   (_CharT*)alloc::allocate(__len * sizeof(_CharT));
    00873                 __STL_TRY {
    00874                   (*(__f->_M_fn))(__begin, __end, __buffer);
    00875                   __result = __c(__buffer, __len);
    00876                   alloc::deallocate(__buffer, __len * sizeof(_CharT));
    00877                 }
    00878                 __STL_UNWIND((alloc::deallocate(__buffer,
    00879                                                 __len * sizeof(_CharT))))
    00880                 return __result;
    00881             }
    00882         default:
    00883             __stl_assert(false);
    00884             /*NOTREACHED*/
    00885             return false;
    00886     }
    00887 }
    00888
    00889 inline void _Rope_fill(ostream& __o, size_t __n)
    00890 {
    00891     char __f = __o.fill();
    00892     size_t __i;
    00893
    00894     for (__i = 0; __i < __n; __i++) __o.put(__f);
    00895 }
    00896
    00897
    00898 template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; }
    00899 inline bool _Rope_is_simple(char*) { return true; }
    00900 inline bool _Rope_is_simple(wchar_t*) { return true; }
    00901
    00902
    00903 template<class _CharT, class _Alloc>
    00904 ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r)
    00905 {
    00906     size_t __w = __o.width();
    00907     bool __left = bool(__o.flags() & ios::left);
    00908     size_t __pad_len;
    00909     size_t __rope_len = __r.size();
    00910     _Rope_insert_char_consumer<_CharT> __c(__o);
    00911     bool __is_simple = _Rope_is_simple((_CharT*)0);
    00912
    00913     if (__rope_len < __w) {
    00914         __pad_len = __w - __rope_len;
    00915     } else {
    00916         __pad_len = 0;
    00917     }
    00918     if (!__is_simple) __o.width(__w/__rope_len);
    00919     __STL_TRY {
    00920       if (__is_simple && !__left && __pad_len > 0) {
    00921         _Rope_fill(__o, __pad_len);
    00922       }
    00923       __r.apply_to_pieces(0, __r.size(), __c);
    00924       if (__is_simple && __left && __pad_len > 0) {
    00925         _Rope_fill(__o, __pad_len);
    00926       }
    00927       if (!__is_simple)
    00928         __o.width(__w);
    00929     }
    00930     __STL_UNWIND(if (!__is_simple) __o.width(__w))
    00931     return __o;
    00932 }
    00933
    00934 template <class _CharT, class _Alloc>
    00935 _CharT*
    00936 rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r,
    00937                                  size_t __start, size_t __len,
    00938                                  _CharT* __buffer)
    00939 {
    00940     _Rope_flatten_char_consumer<_CharT> __c(__buffer);
    00941     _S_apply_to_pieces(__c, __r, __start, __start + __len);
    00942     return(__buffer + __len);
    00943 }
    00944
    00945 template <class _CharT, class _Alloc>
    00946 size_t
    00947 rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const
    00948 {
    00949     _Rope_find_char_char_consumer<_CharT> __c(__pattern);
    00950     _S_apply_to_pieces(__c, _M_tree_ptr, __start, size());
    00951     size_type __result_pos = __start + __c._M_count;
    00952 #   ifndef __STL_OLD_ROPE_SEMANTICS
    00953         if (__result_pos == size()) __result_pos = npos;
    00954 #   endif
    00955     return __result_pos;
    00956 }
    00957
    00958 template <class _CharT, class _Alloc>
    00959 _CharT*
    00960 rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
    00961 {
    00962     if (0 == __r) return __buffer;
    00963     switch(__r->_M_tag) {
    00964         case _RopeRep::_S_concat:
    00965             {
    00966                 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
    00967                 _RopeRep* __left = __c->_M_left;
    00968                 _RopeRep* __right = __c->_M_right;
    00969                 _CharT* __rest = _S_flatten(__left, __buffer);
    00970                 return _S_flatten(__right, __rest);
    00971             }
    00972         case _RopeRep::_S_leaf:
    00973             {
    00974                 _RopeLeaf* __l = (_RopeLeaf*)__r;
    00975                 return copy_n(__l->_M_data, __l->_M_size, __buffer).second;
    00976             }
    00977         case _RopeRep::_S_function:
    00978         case _RopeRep::_S_substringfn:
    00979             // We dont yet do anything with substring nodes.
    00980             // This needs to be fixed before ropefiles will work well.
    00981             {
    00982                 _RopeFunction* __f = (_RopeFunction*)__r;
    00983                 (*(__f->_M_fn))(0, __f->_M_size, __buffer);
    00984                 return __buffer + __f->_M_size;
    00985             }
    00986         default:
    00987             __stl_assert(false);
    00988             /*NOTREACHED*/
    00989             return 0;
    00990     }
    00991 }
    00992
    00993
    00994 // This needs work for _CharT != char
    00995 template <class _CharT, class _Alloc>
    00996 void
    00997 rope<_CharT,_Alloc>::_S_dump(_RopeRep* __r, int __indent)
    00998 {
    00999     for (int __i = 0; __i < __indent; __i++) putchar(' ');
    01000     if (0 == __r) {
    01001         printf("NULL\n"); return;
    01002     }
    01003     if (_RopeRep::_S_concat == __r->_M_tag) {
    01004         _RopeConcatenation* __c = (_RopeConcatenation*)__r;
    01005         _RopeRep* __left = __c->_M_left;
    01006         _RopeRep* __right = __c->_M_right;
    01007
    01008 #       ifdef __GC
    01009           printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n",
    01010             __r, __r->_M_depth, __r->_M_size, __r->_M_is_balanced? "" : "not");
    01011 #       else
    01012           printf("Concatenation %p (rc = %ld, depth = %d, "
    01013                    "len = %ld, %s balanced)\n",
    01014                  __r, __r->_M_refcount, __r->_M_depth, __r->_M_size,
    01015                  __r->_M_is_balanced? "" : "not");
    01016 #       endif
    01017         _S_dump(__left, __indent + 2);
    01018         _S_dump(__right, __indent + 2);
    01019         return;
    01020     } else {
    01021         char* __kind;
    01022
    01023         switch (__r->_M_tag) {
    01024             case _RopeRep::_S_leaf:
    01025                 __kind = "Leaf";
    01026                 break;
    01027             case _RopeRep::_S_function:
    01028                 __kind = "Function";
    01029                 break;
    01030             case _RopeRep::_S_substringfn:
    01031                 __kind = "Function representing substring";
    01032                 break;
    01033             default:
    01034                 __kind = "(corrupted kind field!)";
    01035         }
    01036 #       ifdef __GC
    01037           printf("%s %p (depth = %d, len = %ld) ",
    01038                  __kind, __r, __r->_M_depth, __r->_M_size);
    01039 #       else
    01040           printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
    01041                  __kind, __r, __r->_M_refcount, __r->_M_depth, __r->_M_size);
    01042 #       endif
    01043         if (_S_is_one_byte_char_type((_CharT*)0)) {
    01044             const int __max_len = 40;
    01045             _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len));
    01046             _CharT __buffer[__max_len + 1];
    01047             bool __too_big = __r->_M_size > __prefix->_M_size;
    01048
    01049             _S_flatten(__prefix, __buffer);
    01050             __buffer[__prefix->_M_size] = _S_eos((_CharT*)0);
    01051             printf("%s%s\n",
    01052                    (char*)__buffer, __too_big? "...\n" : "\n");
    01053         } else {
    01054             printf("\n");
    01055         }
    01056     }
    01057 }
    01058
    01059 template <class _CharT, class _Alloc>
    01060 const unsigned long
    01061 rope<_CharT,_Alloc>::_S_min_len[
    01062   _Rope_RopeRep<_CharT,_Alloc>::_S_max_rope_depth + 1] = {
    01063 /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21,
    01064 /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377,
    01065 /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181,
    01066 /* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368,
    01067 /* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811,
    01068 /* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309,
    01069 /* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352,
    01070 /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155,
    01071 /* 39 */165580141, /* 40 */267914296, /* 41 */433494437,
    01072 /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903,
    01073 /* 45 */2971215073u };
    01074 // These are Fibonacci numbers < 2**32.
    01075
    01076 template <class _CharT, class _Alloc>
    01077 rope<_CharT,_Alloc>::_RopeRep*
    01078 rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
    01079 {
    01080     _RopeRep* __forest[_RopeRep::_S_max_rope_depth + 1];
    01081     _RopeRep* __result = 0;
    01082     int __i;
    01083     // Invariant:
    01084     // The concatenation of forest in descending order is equal to __r.
    01085     // __forest[__i]._M_size >= _S_min_len[__i]
    01086     // __forest[__i]._M_depth = __i
    01087     // References from forest are included in refcount.
    01088
    01089     for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i)
    01090       __forest[__i] = 0;
    01091     __STL_TRY {
    01092       _S_add_to_forest(__r, __forest);
    01093       for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i)
    01094         if (0 != __forest[__i]) {
    01095 #       ifndef __GC
    01096           _Self_destruct_ptr __old(__result);
    01097 #       endif
    01098           __result = _S_concat(__forest[__i], __result);
    01099         __forest[__i]->_M_unref_nonnil();
    01100 #       if !defined(__GC) && defined(__STL_USE_EXCEPTIONS)
    01101           __forest[__i] = 0;
    01102 #       endif
    01103       }
    01104     }
    01105     __STL_UNWIND(for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++)
    01106                  _S_unref(__forest[__i]))
    01107     if (__result->_M_depth > _RopeRep::_S_max_rope_depth) abort();
    01108     return(__result);
    01109 }
    01110
    01111
    01112 template <class _CharT, class _Alloc>
    01113 void
    01114 rope<_CharT,_Alloc>::_S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
    01115 {
    01116     if (__r->_M_is_balanced) {
    01117         _S_add_leaf_to_forest(__r, __forest);
    01118         return;
    01119     }
    01120     __stl_assert(__r->_M_tag == _RopeRep::_S_concat);
    01121     {
    01122         _RopeConcatenation* __c = (_RopeConcatenation*)__r;
    01123
    01124         _S_add_to_forest(__c->_M_left, __forest);
    01125         _S_add_to_forest(__c->_M_right, __forest);
    01126     }
    01127 }
    01128
    01129
    01130 template <class _CharT, class _Alloc>
    01131 void
    01132 rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
    01133 {
    01134     _RopeRep* __insertee;               // included in refcount
    01135     _RopeRep* __too_tiny = 0;           // included in refcount
    01136     int __i;                            // forest[0..__i-1] is empty
    01137     size_t __s = __r->_M_size;
    01138
    01139     for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i) {
    01140         if (0 != __forest[__i]) {
    01141 #           ifndef __GC
    01142               _Self_destruct_ptr __old(__too_tiny);
    01143 #           endif
    01144             __too_tiny = _S_concat_and_set_balanced(__forest[__i], __too_tiny);
    01145             __forest[__i]->_M_unref_nonnil();
    01146             __forest[__i] = 0;
    01147         }
    01148     }
    01149     {
    01150 #       ifndef __GC
    01151           _Self_destruct_ptr __old(__too_tiny);
    01152 #       endif
    01153         __insertee = _S_concat_and_set_balanced(__too_tiny, __r);
    01154     }
    01155     // Too_tiny dead, and no longer included in refcount.
    01156     // Insertee is live and included.
    01157     __stl_assert(_S_is_almost_balanced(__insertee));
    01158     __stl_assert(__insertee->_M_depth <= __r->_M_depth + 1);
    01159     for (;; ++__i) {
    01160         if (0 != __forest[__i]) {
    01161 #           ifndef __GC
    01162               _Self_destruct_ptr __old(__insertee);
    01163 #           endif
    01164             __insertee = _S_concat_and_set_balanced(__forest[__i], __insertee);
    01165             __forest[__i]->_M_unref_nonnil();
    01166             __forest[__i] = 0;
    01167             __stl_assert(_S_is_almost_balanced(__insertee));
    01168         }
    01169         __stl_assert(_S_min_len[__i] <= __insertee->_M_size);
    01170         __stl_assert(__forest[__i] == 0);
    01171         if (__i == _RopeRep::_S_max_rope_depth ||
    01172               __insertee->_M_size < _S_min_len[__i+1]) {
    01173             __forest[__i] = __insertee;
    01174             // refcount is OK since __insertee is now dead.
    01175             return;
    01176         }
    01177     }
    01178 }
    01179
    01180 template <class _CharT, class _Alloc>
    01181 _CharT
    01182 rope<_CharT,_Alloc>::_S_fetch(_RopeRep* __r, size_type __i)
    01183 {
    01184     __GC_CONST _CharT* __cstr = __r->_M_c_string;
    01185
    01186     __stl_assert(__i < __r->_M_size);
    01187     if (0 != __cstr) return __cstr[__i];
    01188     for(;;) {
    01189       switch(__r->_M_tag) {
    01190         case _RopeRep::_S_concat:
    01191             {
    01192                 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
    01193                 _RopeRep* __left = __c->_M_left;
    01194                 size_t __left_len = __left->_M_size;
    01195
    01196                 if (__i >= __left_len) {
    01197                     __i -= __left_len;
    01198                     __r = __c->_M_right;
    01199                 } else {
    01200                     __r = __left;
    01201                 }
    01202             }
    01203             break;
    01204         case _RopeRep::_S_leaf:
    01205             {
    01206                 _RopeLeaf* __l = (_RopeLeaf*)__r;
    01207                 return __l->_M_data[__i];
    01208             }
    01209         case _RopeRep::_S_function:
    01210         case _RopeRep::_S_substringfn:
    01211             {
    01212                 _RopeFunction* __f = (_RopeFunction*)__r;
    01213                 _CharT __result;
    01214
    01215                 (*(__f->_M_fn))(__i, 1, &__result);
    01216                 return __result;
    01217             }
    01218       }
    01219     }
    01220 }
    01221
    01222 # ifndef __GC
    01223 // Return a uniquely referenced character slot for the given
    01224 // position, or 0 if that's not possible.
    01225 template <class _CharT, class _Alloc>
    01226 _CharT*
    01227 rope<_CharT,_Alloc>::_S_fetch_ptr(_RopeRep* __r, size_type __i)
    01228 {
    01229     _RopeRep* __clrstack[_RopeRep::_S_max_rope_depth];
    01230     size_t __csptr = 0;
    01231
    01232     for(;;) {
    01233       if (__r->_M_refcount > 1) return 0;
    01234       switch(__r->_M_tag) {
    01235         case _RopeRep::_S_concat:
    01236             {
    01237                 _RopeConcatenation* __c = (_RopeConcatenation*)__r;
    01238                 _RopeRep* __left = __c->_M_left;
    01239                 size_t __left_len = __left->_M_size;
    01240
    01241                 if (__c->_M_c_string != 0) __clrstack[__csptr++] = __c;
    01242                 if (__i >= __left_len) {
    01243                     __i -= __left_len;
    01244                     __r = __c->_M_right;
    01245                 } else {
    01246                     __r = __left;
    01247                 }
    01248             }
    01249             break;
    01250         case _RopeRep::_S_leaf:
    01251             {
    01252                 _RopeLeaf* __l = (_RopeLeaf*)__r;
    01253                 if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0)
    01254                     __clrstack[__csptr++] = __l;
    01255                 while (__csptr > 0) {
    01256                     -- __csptr;
    01257                     _RopeRep* __d = __clrstack[__csptr];
    01258                     __d->_M_free_c_string();
    01259                     __d->_M_c_string = 0;
    01260                 }
    01261                 return __l->_M_data + __i;
    01262             }
    01263         case _RopeRep::_S_function:
    01264         case _RopeRep::_S_substringfn:
    01265             return 0;
    01266       }
    01267     }
    01268 }
    01269 # endif /* __GC */
    01270
    01271 // The following could be implemented trivially using
    01272 // lexicographical_compare_3way.
    01273 // We do a little more work to avoid dealing with rope iterators for
    01274 // flat strings.
    01275 template <class _CharT, class _Alloc>
    01276 int
    01277 rope<_CharT,_Alloc>::_S_compare (const _RopeRep* __left,
    01278                                  const _RopeRep* __right)
    01279 {
    01280     size_t __left_len;
    01281     size_t __right_len;
    01282
    01283     if (0 == __right) return 0 != __left;
    01284     if (0 == __left) return -1;
    01285     __left_len = __left->_M_size;
    01286     __right_len = __right->_M_size;
    01287     if (_RopeRep::_S_leaf == __left->_M_tag) {
    01288         _RopeLeaf* __l = (_RopeLeaf*) __left;
    01289         if (_RopeRep::_S_leaf == __right->_M_tag) {
    01290             _RopeLeaf* __r = (_RopeLeaf*) __right;
    01291             return lexicographical_compare_3way(
    01292                         __l->_M_data, __l->_M_data + __left_len,
    01293                         __r->_M_data, __r->_M_data + __right_len);
    01294         } else {
    01295             const_iterator __rstart(__right, 0);
    01296             const_iterator __rend(__right, __right_len);
    01297             return lexicographical_compare_3way(
    01298                         __l->_M_data, __l->_M_data + __left_len,
    01299                         __rstart, __rend);
    01300         }
    01301     } else {
    01302         const_iterator __lstart(__left, 0);
    01303         const_iterator __lend(__left, __left_len);
    01304         if (_RopeRep::_S_leaf == __right->_M_tag) {
    01305             _RopeLeaf* __r = (_RopeLeaf*) __right;
    01306             return lexicographical_compare_3way(
    01307                                    __lstart, __lend,
    01308                                    __r->_M_data, __r->_M_data + __right_len);
    01309         } else {
    01310             const_iterator __rstart(__right, 0);
    01311             const_iterator __rend(__right, __right_len);
    01312             return lexicographical_compare_3way(
    01313                                    __lstart, __lend,
    01314                                    __rstart, __rend);
    01315         }
    01316     }
    01317 }
    01318
    01319 // Assignment to reference proxies.
    01320 template <class _CharT, class _Alloc>
    01321 _Rope_char_ref_proxy<_CharT, _Alloc>&
    01322 _Rope_char_ref_proxy<_CharT, _Alloc>::operator= (_CharT __c) {
    01323     _RopeRep* __old = _M_root->_M_tree_ptr;
    01324 #   ifndef __GC
    01325         // First check for the case in which everything is uniquely
    01326         // referenced.  In that case we can do this destructively.
    01327         _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos);
    01328         if (0 != __ptr) {
    01329             *__ptr = __c;
    01330             return *this;
    01331         }
    01332 #   endif
    01333     _Self_destruct_ptr __left(
    01334       _My_rope::_S_substring(__old, 0, _M_pos));
    01335     _Self_destruct_ptr __right(
    01336       _My_rope::_S_substring(__old, _M_pos+1, __old->_M_size));
    01337     _Self_destruct_ptr __result_left(
    01338       _My_rope::_S_destr_concat_char_iter(__left, &__c, 1));
    01339
    01340 #   ifndef __GC
    01341       __stl_assert(__left == __result_left || 1 == __result_left->_M_refcount);
    01342 #   endif
    01343     _RopeRep* __result =
    01344                 _My_rope::_S_concat(__result_left, __right);
    01345 #   ifndef __GC
    01346       __stl_assert(1 <= __result->_M_refcount);
    01347       _RopeRep::_S_unref(__old);
    01348 #   endif
    01349     _M_root->_M_tree_ptr = __result;
    01350     return *this;
    01351 }
    01352
    01353 template <class _CharT, class _Alloc>
    01354 inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const
    01355 {
    01356     if (_M_current_valid) {
    01357         return _M_current;
    01358     } else {
    01359         return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos);
    01360     }
    01361 }
    01362 template <class _CharT, class _Alloc>
    01363 _Rope_char_ptr_proxy<_CharT, _Alloc>
    01364 _Rope_char_ref_proxy<_CharT, _Alloc>::operator& () const {
    01365     return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this);
    01366 }
    01367
    01368 template <class _CharT, class _Alloc>
    01369 rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
    01370                            const allocator_type& __a)
    01371 : _Base(__a)
    01372 {
    01373     rope<_CharT,_Alloc> __result;
    01374     const size_t __exponentiate_threshold = 32;
    01375     size_t __exponent;
    01376     size_t __rest;
    01377     _CharT* __rest_buffer;
    01378     _RopeRep* __remainder;
    01379     rope<_CharT,_Alloc> __remainder_rope;
    01380
    01381     if (0 == __n)
    01382       return;
    01383
    01384     __exponent = __n / __exponentiate_threshold;
    01385     __rest = __n % __exponentiate_threshold;
    01386     if (0 == __rest) {
    01387         __remainder = 0;
    01388     } else {
    01389         __rest_buffer = _Data_allocate(_S_rounded_up_size(__rest));
    01390         uninitialized_fill_n(__rest_buffer, __rest, __c);
    01391         _S_cond_store_eos(__rest_buffer[__rest]);
    01392         __STL_TRY {
    01393             __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
    01394         }
    01395         __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a))
    01396     }
    01397     __remainder_rope._M_tree_ptr = __remainder;
    01398     if (__exponent != 0) {
    01399         _CharT* __base_buffer =
    01400           _Data_allocate(_S_rounded_up_size(__exponentiate_threshold));
    01401         _RopeLeaf* __base_leaf;
    01402         rope __base_rope;
    01403         uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
    01404         _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
    01405         __STL_TRY {
    01406           __base_leaf = _S_new_RopeLeaf(__base_buffer,
    01407                                         __exponentiate_threshold, __a);
    01408         }
    01409         __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__base_buffer,
    01410                                                  __exponentiate_threshold, __a))
    01411         __base_rope._M_tree_ptr = __base_leaf;
    01412         if (1 == __exponent) {
    01413           __result = __base_rope;
    01414 #         ifndef __GC
    01415             __stl_assert(2 == __result._M_tree_ptr->_M_refcount);
    01416                 // One each for base_rope and __result
    01417 #         endif
    01418         } else {
    01419           // XXX what is power()?
    01420           __result = power(__base_rope, __exponent, _Concat_fn());
    01421         }
    01422         if (0 != __remainder) {
    01423           __result += __remainder_rope;
    01424         }
    01425     } else {
    01426         __result = __remainder_rope;
    01427     }
    01428     _M_tree_ptr = __result._M_tree_ptr;
    01429     _M_tree_ptr->_M_ref_nonnil();
    01430 }
    01431
    01432 template<class _CharT, class _Alloc>
    01433   _CharT rope<_CharT,_Alloc>::_S_empty_c_str[1];
    01434
    01435 # ifdef __STL_PTHREADS
    01436     template<class _CharT, class _Alloc>
    01437     pthread_mutex_t
    01438     rope<_CharT,_Alloc>::_S_swap_lock = PTHREAD_MUTEX_INITIALIZER;
    01439 # endif
    01440
    01441 template<class _CharT, class _Alloc>
    01442 const _CharT* rope<_CharT,_Alloc>::c_str() const {
    01443     if (0 == _M_tree_ptr) {
    01444         _S_empty_c_str[0] = _S_eos((_CharT*)0);  // Possibly redundant,
    01445                                              // but probably fast.
    01446         return _S_empty_c_str;
    01447     }
    01448     __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
    01449     if (0 != __old_c_string) return(__old_c_string);
    01450     size_t __s = size();
    01451     _CharT* __result = _Data_allocate(__s + 1);
    01452     _S_flatten(_M_tree_ptr, __result);
    01453     __result[__s] = _S_eos((_CharT*)0);
    01454 #   ifdef __GC
    01455         _M_tree_ptr->_M_c_string = __result;
    01456 #   else
    01457       if ((__old_c_string =
    01458              _S_atomic_swap(&(_M_tree_ptr->_M_c_string), __result)) != 0) {
    01459         // It must have been added in the interim.  Hence it had to have been
    01460         // separately allocated.  Deallocate the old copy, since we just
    01461         // replaced it.
    01462         destroy(__old_c_string, __old_c_string + __s + 1);
    01463         _Data_deallocate(__old_c_string, __s + 1);
    01464       }
    01465 #   endif
    01466     return(__result);
    01467 }
    01468
    01469 template<class _CharT, class _Alloc>
    01470 const _CharT* rope<_CharT,_Alloc>::replace_with_c_str() {
    01471     if (0 == _M_tree_ptr) {
    01472         _S_empty_c_str[0] = _S_eos((_CharT*)0);
    01473         return _S_empty_c_str;
    01474     }
    01475     __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
    01476     if (_RopeRep::_S_leaf == _M_tree_ptr->_M_tag && 0 != __old_c_string) {
    01477         return(__old_c_string);
    01478     }
    01479     size_t __s = size();
    01480     _CharT* __result = _Data_allocate(_S_rounded_up_size(__s));
    01481     _S_flatten(_M_tree_ptr, __result);
    01482     __result[__s] = _S_eos((_CharT*)0);
    01483     _M_tree_ptr->_M_unref_nonnil();
    01484     _M_tree_ptr = _S_new_RopeLeaf(__result, __s, get_allocator());
    01485     return(__result);
    01486 }
    01487
    01488 // Algorithm specializations.  More should be added.
    01489
    01490 #ifndef _MSC_VER
    01491 // I couldn't get this to work with VC++
    01492 template<class _CharT,class _Alloc>
    01493 void
    01494 _Rope_rotate(_Rope_iterator<_CharT,_Alloc> __first,
    01495               _Rope_iterator<_CharT,_Alloc> __middle,
    01496               _Rope_iterator<_CharT,_Alloc> __last)
    01497 {
    01498     __stl_assert(__first.container() == __middle.container()
    01499                  && __middle.container() == __last.container());
    01500     rope<_CharT,_Alloc>& __r(__first.container());
    01501     rope<_CharT,_Alloc> __prefix = __r.substr(0, __first.index());
    01502     rope<_CharT,_Alloc> __suffix =
    01503       __r.substr(__last.index(), __r.size() - __last.index());
    01504     rope<_CharT,_Alloc> __part1 =
    01505       __r.substr(__middle.index(), __last.index() - __middle.index());
    01506     rope<_CharT,_Alloc> __part2 =
    01507       __r.substr(__first.index(), __middle.index() - __first.index());
    01508     __r = __prefix;
    01509     __r += __part1;
    01510     __r += __part2;
    01511     __r += __suffix;
    01512 }
    01513
    01514 #if !defined(__GNUC__)
    01515 // Appears to confuse g++
    01516 inline void rotate(_Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __first,
    01517                    _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __middle,
    01518                    _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __last) {
    01519     _Rope_rotate(__first, __middle, __last);
    01520 }
    01521 #endif
    01522
    01523 # if 0
    01524 // Probably not useful for several reasons:
    01525 // - for SGIs 7.1 compiler and probably some others,
    01526 //   this forces lots of rope<wchar_t, ...> instantiations, creating a
    01527 //   code bloat and compile time problem.  (Fixed in 7.2.)
    01528 // - wchar_t is 4 bytes wide on most UNIX platforms, making it unattractive
    01529 //   for unicode strings.  Unsigned short may be a better character
    01530 //   type.
    01531 inline void rotate(
    01532                 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __first,
    01533                 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __middle,
    01534                 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __last) {
    01535     _Rope_rotate(__first, __middle, __last);
    01536 }
    01537 # endif
    01538 #endif /* _MSC_VER */
    01539
    01540 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
    01541 #pragma reset woff 1174
    01542 #endif
    01543
    01544 __STL_END_NAMESPACE
    01545
    01546 // Local Variables:
    01547 // mode:C++
    01548 // End:
    01549