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