ropeimpl.h: #include <ostream> instead of <iostream>
[gcc.git] / libstdc++-v3 / include / ext / ropeimpl.h
index cc4d7bc679e445c99c01f42dbb05190c4f4ef0f8..6129084deb6d32a67f63021e83257014ec09077a 100644 (file)
@@ -1,3 +1,32 @@
+// SGI's rope class implementation -*- C++ -*-
+
+// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
 /*
  * Copyright (c) 1997
  * Silicon Graphics Computer Systems, Inc.
  * purpose.  It is provided "as is" without express or implied warranty.
  */
 
-/* NOTE: This is an internal header file, included by other STL headers.
- *   You should not attempt to use it directly.
+/** @file ropeimpl.h
+ *  This is an internal header file, included by other library headers.
+ *  You should not attempt to use it directly.
  */
 
-# include <bits/std_cstdio.h>     
-
-#ifdef __STL_USE_NEW_IOSTREAMS 
-# include <iostream>
-#else /* __STL_USE_NEW_IOSTREAMS */
-# include <bits/std_iostream.h>
-#endif /* __STL_USE_NEW_IOSTREAMS */
+#include <cstdio>     
+#include <ostream>
+#include <bits/functexcept.h>
 
-#ifdef __STL_USE_EXCEPTIONS
-# include <bits/std_stdexcept.h>
-#endif
+#include <ext/algorithm> // For copy_n and lexicographical_compare_3way
+#include <ext/memory> // For uninitialized_copy_n
+#include <ext/numeric> // For power
 
-__STL_BEGIN_NAMESPACE
-
-#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
-#pragma set woff 1174
-#endif
+namespace __gnu_cxx
+{
+  using std::size_t;
+  using std::printf;
+  using std::basic_ostream;  
+  using std::__throw_length_error;
+  using std::__alloc;
+  using std::_Destroy;
+  using std::uninitialized_fill_n;
 
 // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
 // if necessary.  Assumes _M_path_end[leaf_index] and leaf_pos are correct.
@@ -77,7 +107,7 @@ void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf(
            }
            break;
        default:
-           __stl_assert(0);
+         break;
     }
 }
 
@@ -94,7 +124,6 @@ void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
     size_t __pos = __x._M_current_pos;
     unsigned char __dirns = 0; // Bit vector marking right turns in the path
 
-    __stl_assert(__pos <= __x._M_root->_M_size);
     if (__pos >= __x._M_root->_M_size) {
        __x._M_buf_ptr = 0;
        return;
@@ -112,7 +141,6 @@ void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
     }
     for(;;) {
        ++__curr_depth;
-       __stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth);
        __path[__curr_depth] = __curr_rope;
        switch(__curr_rope->_M_tag) {
          case _RopeRep::_S_leaf:
@@ -168,13 +196,11 @@ void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr
     unsigned char __dirns = __x._M_path_directions;
     _Rope_RopeConcatenation<_CharT,_Alloc>* __c;
 
-    __stl_assert(__x._M_current_pos <= __x._M_root->_M_size);
     if (__x._M_current_pos - __node_start_pos < __len) {
        /* More stuff in this leaf, we just didn't cache it. */
        _S_setbuf(__x);
        return;
     }
-    __stl_assert(__node_start_pos + __len == __x._M_current_pos);
     //  node_start_pos is starting position of last_node.
     while (--__current_index >= 0) {
        if (!(__dirns & 1) /* Path turned left */) 
@@ -252,12 +278,12 @@ void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) {
 
 template <class _CharT, class _Alloc>
 void _Rope_iterator<_CharT,_Alloc>::_M_check() {
-    if (_M_root_rope->_M_tree_ptr != _M_root) {
+    if (_M_root_rope->_M_tree_ptr != this->_M_root) {
         // _Rope was modified.  Get things fixed up.
-        _RopeRep::_S_unref(_M_root);
-        _M_root = _M_root_rope->_M_tree_ptr;
-        _RopeRep::_S_ref(_M_root);
-        _M_buf_ptr = 0;
+        _RopeRep::_S_unref(this->_M_root);
+        this->_M_root = _M_root_rope->_M_tree_ptr;
+        _RopeRep::_S_ref(this->_M_root);
+        this->_M_buf_ptr = 0;
     }
 }
 
@@ -274,7 +300,7 @@ inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator(
 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos), 
   _M_root_rope(&__r)
 {
-    _RopeRep::_S_ref(_M_root);
+    _RopeRep::_S_ref(this->_M_root);
 }
 
 template <class _CharT, class _Alloc>
@@ -295,34 +321,24 @@ inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
 {
     _CharT* __cstr = _M_c_string;
     if (0 != __cstr) {
-       size_t __size = _M_size + 1;
-       destroy(__cstr, __cstr + __size);
+       size_t __size = this->_M_size + 1;
+       _Destroy(__cstr, __cstr + __size);
        _Data_deallocate(__cstr, __size);
     }
 }
 
 
 template <class _CharT, class _Alloc>
-#ifdef __STL_USE_STD_ALLOCATORS
   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
                                                           size_t __n,
                                                           allocator_type __a)
-#else
-  inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
-                                                          size_t __n)
-#endif
 {
     if (!_S_is_basic_char_type((_CharT*)0)) {
-       destroy(__s, __s + __n);
+       _Destroy(__s, __s + __n);
     }
 //  This has to be a static member, so this gets a bit messy
-#   ifdef __STL_USE_STD_ALLOCATORS
         __a.deallocate(
            __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
-#   else
-       _Data_deallocate(
-           __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
-#   endif
 }
 
 
@@ -375,13 +391,8 @@ void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree()
 #else
 
 template <class _CharT, class _Alloc>
-#ifdef __STL_USE_STD_ALLOCATORS
   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
                (const _CharT*, size_t, allocator_type)
-#else
-  inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
-               (const _CharT*, size_t)
-#endif
 {}
 
 #endif
@@ -390,7 +401,7 @@ template <class _CharT, class _Alloc>
 // Concatenate a C string onto a leaf rope by copying the rope data.
 // Used for short ropes.
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeLeaf*
+typename rope<_CharT,_Alloc>::_RopeLeaf*
 rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
                (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
 {
@@ -402,23 +413,26 @@ rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
     uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
     uninitialized_copy_n(__iter, __len, __new_data + __old_len);
     _S_cond_store_eos(__new_data[__old_len + __len]);
-    __STL_TRY {
+    try {
        __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
                                   __r->get_allocator());
     }
-    __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
-                                            __r->get_allocator()));
+    catch(...)
+      {
+       _RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
+                                   __r->get_allocator());
+       __throw_exception_again;
+      }
     return __result;
 }
 
 #ifndef __GC
 // As above, but it's OK to clobber original if refcount is 1
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeLeaf*
+typename rope<_CharT,_Alloc>::_RopeLeaf*
 rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
                (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
 {
-    __stl_assert(__r->_M_ref_count >= 1);
     if (__r->_M_ref_count > 1)
       return _S_leaf_concat_char_iter(__r, __iter, __len);
     size_t __old_len = __r->_M_size;
@@ -428,18 +442,15 @@ rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
        uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
        if (_S_is_basic_char_type((_CharT*)0)) {
            _S_cond_store_eos(__r->_M_data[__old_len + __len]);
-           __stl_assert(__r->_M_c_string == __r->_M_data);
        } else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) {
            __r->_M_free_c_string();
            __r->_M_c_string = 0;
        }
        __r->_M_size = __old_len + __len;
-       __stl_assert(__r->_M_ref_count == 1);
        __r->_M_ref_count = 2;
        return __r;
     } else {
        _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
-       __stl_assert(__result->_M_ref_count == 1);
        return __result;
     }
 }
@@ -449,42 +460,40 @@ rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
 // Does not increment (nor decrement on exception) child reference counts.
 // Result has ref count 1.
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeRep*
+typename rope<_CharT,_Alloc>::_RopeRep*
 rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
 {
-    _RopeConcatenation* __result =
-      _S_new_RopeConcatenation(__left, __right, __left->get_allocator());
-    size_t __depth = __result->_M_depth;
+  _RopeConcatenation* __result = _S_new_RopeConcatenation(__left, __right, 
+                                                     __left->get_allocator());
+  size_t __depth = __result->_M_depth;
     
-#   ifdef __STL_USE_STD_ALLOCATORS
-      __stl_assert(__left->get_allocator() == __right->get_allocator());
-#   endif
-    if (__depth > 20 && (__result->_M_size < 1000 ||
-                        __depth > _RopeRep::_S_max_rope_depth)) {
-        _RopeRep* __balanced;
+  if (__depth > 20 && (__result->_M_size < 1000 ||
+                      __depth > _RopeRep::_S_max_rope_depth)) 
+    {
+      _RopeRep* __balanced;
       
-       __STL_TRY {
-          __balanced = _S_balance(__result);
-#          ifndef __GC
-            if (__result != __balanced) {
-               __stl_assert(1 == __result->_M_ref_count
-                            && 1 == __balanced->_M_ref_count);
-            }
-#          endif
-          __result->_M_unref_nonnil();
+      try 
+       {
+         __balanced = _S_balance(__result);
+         __result->_M_unref_nonnil();
         }
-       __STL_UNWIND((_C_deallocate(__result,1)));
-               // In case of exception, we need to deallocate
-               // otherwise dangling result node.  But caller
-               // still owns its children.  Thus unref is
-               // inappropriate.
-       return __balanced;
-    } else {
-       return __result;
-    }
+      catch(...)
+       { 
+         _C_deallocate(__result,1);
+         __throw_exception_again;
+       }
+      // In case of exception, we need to deallocate
+      // otherwise dangling result node.  But caller
+      // still owns its children.  Thus unref is
+      // inappropriate.
+      return __balanced;
+    } 
+  else 
+    return __result;
 }
 
 template <class _CharT, class _Alloc>
+typename
 rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
                (_RopeRep* __r, const _CharT*__s, size_t __slen)
 {
@@ -499,9 +508,6 @@ rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
     if (_RopeRep::_S_leaf == __r->_M_tag && 
           __r->_M_size + __slen <= _S_copy_max) {
        __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
-#       ifndef __GC
-         __stl_assert(1 == __result->_M_ref_count);
-#       endif
        return __result;
     }
     if (_RopeRep::_S_concat == __r->_M_tag
@@ -513,32 +519,36 @@ rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
          _RopeRep* __nright = 
            _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
          __left->_M_ref_nonnil();
-         __STL_TRY {
+         try {
            __result = _S_tree_concat(__left, __nright);
           }
-         __STL_UNWIND(_S_unref(__left); _S_unref(__nright));
-#         ifndef __GC
-           __stl_assert(1 == __result->_M_ref_count);
-#         endif
+         catch(...)
+           {
+             _S_unref(__left); 
+             _S_unref(__nright);
+             __throw_exception_again;
+           }
          return __result;
        }
     }
     _RopeRep* __nright =
       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
-    __STL_TRY {
+    try {
       __r->_M_ref_nonnil();
       __result = _S_tree_concat(__r, __nright);
     }
-    __STL_UNWIND(_S_unref(__r); _S_unref(__nright));
-#   ifndef __GC
-      __stl_assert(1 == __result->_M_ref_count);
-#   endif
+    catch(...)
+      {
+       _S_unref(__r); 
+       _S_unref(__nright);
+       __throw_exception_again;
+      }
     return __result;
 }
 
 #ifndef __GC
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeRep* 
+typename rope<_CharT,_Alloc>::_RopeRep* 
 rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
   _RopeRep* __r, const _CharT* __s, size_t __slen)
 {
@@ -548,7 +558,6 @@ rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
                                              __r->get_allocator());
     size_t __count = __r->_M_ref_count;
     size_t __orig_size = __r->_M_size;
-    __stl_assert(__count >= 1);
     if (__count > 1) return _S_concat_char_iter(__r, __s, __slen);
     if (0 == __slen) {
        __r->_M_ref_count = 2;      // One more than before
@@ -565,14 +574,10 @@ rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
            && __right->_M_size + __slen <= _S_copy_max) {
          _RopeRep* __new_right = 
            _S_destr_leaf_concat_char_iter(__right, __s, __slen);
-         if (__right == __new_right) {
-             __stl_assert(__new_right->_M_ref_count == 2);
-             __new_right->_M_ref_count = 1;
-         } else {
-             __stl_assert(__new_right->_M_ref_count >= 1);
-             __right->_M_unref_nonnil();
-         }
-         __stl_assert(__r->_M_ref_count == 1);
+         if (__right == __new_right) 
+           __new_right->_M_ref_count = 1;
+         else 
+           __right->_M_unref_nonnil();
          __r->_M_ref_count = 2;    // One more than before.
          ((_RopeConcatenation*)__r)->_M_right = __new_right;
          __r->_M_size = __orig_size + __slen;
@@ -586,17 +591,21 @@ rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
     _RopeRep* __right =
       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
     __r->_M_ref_nonnil();
-    __STL_TRY {
+    try {
       __result = _S_tree_concat(__r, __right);
     }
-    __STL_UNWIND(_S_unref(__r); _S_unref(__right))
-    __stl_assert(1 == __result->_M_ref_count);
+    catch(...)
+      {
+       _S_unref(__r); 
+       _S_unref(__right);
+       __throw_exception_again;
+      }
     return __result;
 }
 #endif /* !__GC */
 
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeRep*
+typename rope<_CharT,_Alloc>::_RopeRep*
 rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
 {
     if (0 == __left) {
@@ -625,23 +634,33 @@ rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
                                           ((_RopeLeaf*)__right)->_M_data,
                                           __right->_M_size);
            __leftleft->_M_ref_nonnil();
-           __STL_TRY {
+           try {
              return(_S_tree_concat(__leftleft, __rest));
             }
-           __STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest))
+           catch(...)
+             {
+               _S_unref(__leftleft); 
+               _S_unref(__rest);
+               __throw_exception_again;
+             }
          }
        }
     }
     __left->_M_ref_nonnil();
     __right->_M_ref_nonnil();
-    __STL_TRY {
+    try {
       return(_S_tree_concat(__left, __right));
     }
-    __STL_UNWIND(_S_unref(__left); _S_unref(__right));
+    catch(...)
+      {
+       _S_unref(__left); 
+       _S_unref(__right);
+       __throw_exception_again;
+      }        
 }
 
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeRep*
+typename rope<_CharT,_Alloc>::_RopeRep*
 rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base, 
                                size_t __start, size_t __endp1)
 {
@@ -680,9 +699,6 @@ rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base,
                _Self_destruct_ptr __right_result(
                  _S_substring(__right, 0, __endp1 - __left_len));
                __result = _S_concat(__left_result, __right_result);
-#               ifndef __GC
-                 __stl_assert(1 == __result->_M_ref_count);
-#               endif
                return __result;
            }
        case _RopeRep::_S_leaf:
@@ -734,18 +750,20 @@ rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base,
                if (__result_len > __lazy_threshold) goto lazy;
                __section = (_CharT*)
                        _Data_allocate(_S_rounded_up_size(__result_len));
-               __STL_TRY {
+               try {
                  (*(__f->_M_fn))(__start, __result_len, __section);
                 }
-               __STL_UNWIND(_RopeRep::__STL_FREE_STRING(
-                      __section, __result_len, __base->get_allocator()));
+               catch(...)
+                 {
+                   _RopeRep::__STL_FREE_STRING(
+                      __section, __result_len, __base->get_allocator());
+                   __throw_exception_again;
+                 }
                _S_cond_store_eos(__section[__result_len]);
                return _S_new_RopeLeaf(__section, __result_len,
                                       __base->get_allocator());
            }
     }
-    /*NOTREACHED*/
-    __stl_assert(false);
   lazy:
     {
        // Create substring node.
@@ -791,22 +809,11 @@ class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> {
        }
 };
            
-#ifdef __STL_USE_NEW_IOSTREAMS
   template<class _CharT, class _Traits>
   // Here _CharT is both the stream and rope character type.
-#else
-  template<class _CharT>
-  // Here _CharT is the rope character type.  Unlike in the
-  // above case, we somewhat handle the case in which it doesn't
-  // match the stream character type, i.e. char.
-#endif
 class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
     private:
-#       ifdef __STL_USE_NEW_IOSTREAMS
          typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
-#      else
-         typedef ostream _Insert_ostream;
-#      endif
        _Insert_ostream& _M_o;
     public:
        _Rope_insert_char_consumer(_Insert_ostream& __writer) 
@@ -817,38 +824,15 @@ class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
                // Returns true to continue traversal.
 };
            
-#ifdef __STL_USE_NEW_IOSTREAMS
-  template<class _CharT, class _Traits>
-  bool _Rope_insert_char_consumer<_CharT, _Traits>::operator()
-                                        (const _CharT* __leaf, size_t __n)
-  {
-    size_t __i;
-    //  We assume that formatting is set up correctly for each element.
-    for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
-    return true;
-  }
-
-#else
-  template<class _CharT>
-  bool _Rope_insert_char_consumer<_CharT>::operator()
-                                       (const _CharT* __leaf, size_t __n)
-  {
-    size_t __i;
-    //  We assume that formatting is set up correctly for each element.
-    for (__i = 0; __i < __n; __i++) _M_o << __leaf[__i];
-    return true;
-  }
-
-
-  __STL_TEMPLATE_NULL
-  inline bool _Rope_insert_char_consumer<char>::operator()
-                                       (const char* __leaf, size_t __n)
-  {
-    size_t __i;
-    for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
-    return true;
-  }
-#endif
+template<class _CharT, class _Traits>
+bool _Rope_insert_char_consumer<_CharT, _Traits>::operator()
+                                      (const _CharT* __leaf, size_t __n)
+{
+  size_t __i;
+  //  We assume that formatting is set up correctly for each element.
+  for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
+  return true;
+}
 
 template <class _CharT, class _Alloc>
 bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
@@ -864,13 +848,13 @@ bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
                _RopeRep* __left =  __conc->_M_left;
                size_t __left_len = __left->_M_size;
                if (__begin < __left_len) {
-                   size_t __left_end = min(__left_len, __end);
+                   size_t __left_end = std::min(__left_len, __end);
                    if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
                        return false;
                }
                if (__end > __left_len) {
                    _RopeRep* __right =  __conc->_M_right;
-                   size_t __right_start = max(__left_len, __begin);
+                   size_t __right_start = std::max(__left_len, __begin);
                    if (!_S_apply_to_pieces(__c, __right,
                                         __right_start - __left_len,
                                         __end - __left_len)) {
@@ -891,29 +875,26 @@ bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
                size_t __len = __end - __begin;
                bool __result;
                _CharT* __buffer =
-                 (_CharT*)alloc::allocate(__len * sizeof(_CharT));
-               __STL_TRY {
+                 (_CharT*)__alloc::allocate(__len * sizeof(_CharT));
+               try {
                  (*(__f->_M_fn))(__begin, __len, __buffer);
                  __result = __c(__buffer, __len);
-                  alloc::deallocate(__buffer, __len * sizeof(_CharT));
+                  __alloc::deallocate(__buffer, __len * sizeof(_CharT));
                 }
-               __STL_UNWIND((alloc::deallocate(__buffer,
-                                               __len * sizeof(_CharT))))
+               catch(...)
+                 {
+                   __alloc::deallocate(__buffer, __len * sizeof(_CharT));
+                   __throw_exception_again;
+                 }
                return __result;
            }
        default:
-           __stl_assert(false);
-           /*NOTREACHED*/
-           return false;
+         return false;
     }
 }
 
-#ifdef __STL_USE_NEW_IOSTREAMS
   template<class _CharT, class _Traits>
   inline void _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
-#else
-  inline void _Rope_fill(ostream& __o, size_t __n)
-#endif
 {
     char __f = __o.fill();
     size_t __i;
@@ -926,25 +907,15 @@ template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; }
 inline bool _Rope_is_simple(char*) { return true; }
 inline bool _Rope_is_simple(wchar_t*) { return true; }
 
-#ifdef __STL_USE_NEW_IOSTREAMS
-  template<class _CharT, class _Traits, class _Alloc>
-  basic_ostream<_CharT, _Traits>& operator<<
-                                       (basic_ostream<_CharT, _Traits>& __o,
-                                        const rope<_CharT, _Alloc>& __r)
-#else
-  template<class _CharT, class _Alloc>
-  ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r)
-#endif
+template<class _CharT, class _Traits, class _Alloc>
+basic_ostream<_CharT, _Traits>& operator<< (basic_ostream<_CharT, _Traits>& __o,
+                                            const rope<_CharT, _Alloc>& __r)
 {
     size_t __w = __o.width();
-    bool __left = bool(__o.flags() & ios::left);
+    bool __left = bool(__o.flags() & std::ios::left);
     size_t __pad_len;
     size_t __rope_len = __r.size();
-#   ifdef __STL_USE_NEW_IOSTREAMS
       _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
-#   else
-      _Rope_insert_char_consumer<_CharT> __c(__o);
-#   endif
     bool __is_simple = _Rope_is_simple((_CharT*)0);
     
     if (__rope_len < __w) {
@@ -953,7 +924,7 @@ inline bool _Rope_is_simple(wchar_t*) { return true; }
        __pad_len = 0;
     }
     if (!__is_simple) __o.width(__w/__rope_len);
-    __STL_TRY {
+    try {
       if (__is_simple && !__left && __pad_len > 0) {
        _Rope_fill(__o, __pad_len);
       }
@@ -964,7 +935,12 @@ inline bool _Rope_is_simple(wchar_t*) { return true; }
       if (!__is_simple)
         __o.width(__w);
     }
-    __STL_UNWIND(if (!__is_simple) __o.width(__w))
+    catch(...)
+      {
+       if (!__is_simple) 
+         __o.width(__w);
+       __throw_exception_again;
+      }
     return __o;
 }
 
@@ -984,7 +960,7 @@ size_t
 rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const
 {
     _Rope_find_char_char_consumer<_CharT> __c(__pattern);
-    _S_apply_to_pieces(__c, _M_tree_ptr, __start, size());
+    _S_apply_to_pieces(__c, this->_M_tree_ptr, __start, size());
     size_type __result_pos = __start + __c._M_count;
 #   ifndef __STL_OLD_ROPE_SEMANTICS
        if (__result_pos == size()) __result_pos = npos;
@@ -1013,7 +989,7 @@ rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
            }
        case _RopeRep::_S_function:
        case _RopeRep::_S_substringfn:
-           // We dont yet do anything with substring nodes.
+           // We don't yet do anything with substring nodes.
            // This needs to be fixed before ropefiles will work well.
            {
                _RopeFunction* __f = (_RopeFunction*)__r;
@@ -1021,8 +997,6 @@ rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
                return __buffer + __f->_M_size;
            }
        default:
-           __stl_assert(false);
-           /*NOTREACHED*/
            return 0;
     }
 }
@@ -1111,7 +1085,7 @@ rope<_CharT,_Alloc>::_S_min_len[
 // These are Fibonacci numbers < 2**32.
 
 template <class _CharT, class _Alloc>
-rope<_CharT,_Alloc>::_RopeRep*
+typename rope<_CharT,_Alloc>::_RopeRep*
 rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
 {
     _RopeRep* __forest[_RopeRep::_S_max_rope_depth + 1];
@@ -1125,7 +1099,7 @@ rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
 
     for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) 
       __forest[__i] = 0;
-    __STL_TRY {
+    try {
       _S_add_to_forest(__r, __forest);
       for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) 
         if (0 != __forest[__i]) {
@@ -1134,20 +1108,20 @@ rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
 #      endif
          __result = _S_concat(__forest[__i], __result);
        __forest[__i]->_M_unref_nonnil();
-#      if !defined(__GC) && defined(__STL_USE_EXCEPTIONS)
+#      if !defined(__GC) && defined(__EXCEPTIONS)
          __forest[__i] = 0;
 #      endif
       }
     }
-    __STL_UNWIND(for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++)
-                _S_unref(__forest[__i]))
-    if (__result->_M_depth > _RopeRep::_S_max_rope_depth) {
-#     ifdef __STL_USE_EXCEPTIONS
-       __STL_THROW(length_error("rope too long"));
-#     else
-       abort();
-#     endif
-    }
+    catch(...)
+      {
+       for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++)
+         _S_unref(__forest[__i]);
+       __throw_exception_again;
+      }
+
+    if (__result->_M_depth > _RopeRep::_S_max_rope_depth)
+      __throw_length_error("rope::_S_balance");
     return(__result);
 }
 
@@ -1160,7 +1134,7 @@ rope<_CharT,_Alloc>::_S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
        _S_add_leaf_to_forest(__r, __forest);
        return;
     }
-    __stl_assert(__r->_M_tag == _RopeRep::_S_concat);
+
     {
        _RopeConcatenation* __c = (_RopeConcatenation*)__r;
 
@@ -1197,8 +1171,6 @@ rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
     }
     // Too_tiny dead, and no longer included in refcount.
     // Insertee is live and included.
-    __stl_assert(_S_is_almost_balanced(__insertee));
-    __stl_assert(__insertee->_M_depth <= __r->_M_depth + 1);
     for (;; ++__i) {
        if (0 != __forest[__i]) {
 #          ifndef __GC
@@ -1207,10 +1179,7 @@ rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
            __insertee = _S_concat_and_set_balanced(__forest[__i], __insertee);
            __forest[__i]->_M_unref_nonnil();
            __forest[__i] = 0;
-           __stl_assert(_S_is_almost_balanced(__insertee));
        }
-       __stl_assert(_S_min_len[__i] <= __insertee->_M_size);
-       __stl_assert(__forest[__i] == 0);
        if (__i == _RopeRep::_S_max_rope_depth || 
              __insertee->_M_size < _S_min_len[__i+1]) {
            __forest[__i] = __insertee;
@@ -1226,7 +1195,6 @@ rope<_CharT,_Alloc>::_S_fetch(_RopeRep* __r, size_type __i)
 {
     __GC_CONST _CharT* __cstr = __r->_M_c_string;
 
-    __stl_assert(__i < __r->_M_size);
     if (0 != __cstr) return __cstr[__i]; 
     for(;;) {
       switch(__r->_M_tag) {
@@ -1380,13 +1348,9 @@ _Rope_char_ref_proxy<_CharT, _Alloc>::operator= (_CharT __c) {
     _Self_destruct_ptr __result_left(
       _My_rope::_S_destr_concat_char_iter(__left, &__c, 1));
 
-#   ifndef __GC
-      __stl_assert(__left == __result_left || 1 == __result_left->_M_ref_count);
-#   endif
     _RopeRep* __result =
                _My_rope::_S_concat(__result_left, __right);
 #   ifndef __GC
-      __stl_assert(1 <= __result->_M_ref_count);
       _RopeRep::_S_unref(__old);
 #   endif
     _M_root->_M_tree_ptr = __result;
@@ -1432,10 +1396,14 @@ rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
        __rest_buffer = _Data_allocate(_S_rounded_up_size(__rest));
        uninitialized_fill_n(__rest_buffer, __rest, __c);
        _S_cond_store_eos(__rest_buffer[__rest]);
-       __STL_TRY {
+       try {
            __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
         }
-       __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a))
+       catch(...)
+         {
+           _RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a);
+           __throw_exception_again;
+         }
     }
     __remainder_rope._M_tree_ptr = __remainder;
     if (__exponent != 0) {
@@ -1445,19 +1413,19 @@ rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
        rope __base_rope;
        uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
        _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
-       __STL_TRY {
+       try {
           __base_leaf = _S_new_RopeLeaf(__base_buffer,
                                         __exponentiate_threshold, __a);
         }
-       __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__base_buffer, 
-                                                __exponentiate_threshold, __a))
+       catch(...)
+         {
+           _RopeRep::__STL_FREE_STRING(__base_buffer, 
+                                       __exponentiate_threshold, __a);
+           __throw_exception_again;
+         }
        __base_rope._M_tree_ptr = __base_leaf;
        if (1 == __exponent) {
          __result = __base_rope;
-#         ifndef __GC
-           __stl_assert(2 == __result._M_tree_ptr->_M_ref_count);
-               // One each for base_rope and __result
-#         endif
        } else {
          __result = power(__base_rope, __exponent,
                           _Rope_Concat_fn<_CharT,_Alloc>());
@@ -1468,8 +1436,8 @@ rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
     } else {
        __result = __remainder_rope;
     }
-    _M_tree_ptr = __result._M_tree_ptr;
-    _M_tree_ptr->_M_ref_nonnil();
+    this->_M_tree_ptr = __result._M_tree_ptr;
+    this->_M_tree_ptr->_M_ref_nonnil();
 }
 
 template<class _CharT, class _Alloc>
@@ -1477,49 +1445,42 @@ template<class _CharT, class _Alloc>
 
 template<class _CharT, class _Alloc>
 const _CharT* rope<_CharT,_Alloc>::c_str() const {
-    if (0 == _M_tree_ptr) {
+    if (0 == this->_M_tree_ptr) {
         _S_empty_c_str[0] = _S_eos((_CharT*)0);  // Possibly redundant,
                                             // but probably fast.
         return _S_empty_c_str;
     }
-    __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
-    if (0 != __old_c_string) return(__old_c_string);
-    size_t __s = size();
-    _CharT* __result = _Data_allocate(__s + 1);
-    _S_flatten(_M_tree_ptr, __result);
-    __result[__s] = _S_eos((_CharT*)0);
-#   ifdef __GC
-       _M_tree_ptr->_M_c_string = __result;
-#   else
-      if ((__old_c_string = (__GC_CONST _CharT*)
-             _Atomic_swap((unsigned long *)(&(_M_tree_ptr->_M_c_string)),
-                         (unsigned long)__result)) != 0) {
-       // It must have been added in the interim.  Hence it had to have been
-       // separately allocated.  Deallocate the old copy, since we just
-       // replaced it.
-       destroy(__old_c_string, __old_c_string + __s + 1);
-       _Data_deallocate(__old_c_string, __s + 1);
+    __gthread_mutex_lock (&this->_M_tree_ptr->_M_c_string_lock);
+    __GC_CONST _CharT* __result = this->_M_tree_ptr->_M_c_string;
+    if (0 == __result)
+      {
+       size_t __s = size();
+       __result = this->_Data_allocate(__s + 1);
+       _S_flatten(this->_M_tree_ptr, __result);
+       __result[__s] = _S_eos((_CharT*)0);
+       this->_M_tree_ptr->_M_c_string = __result;
       }
-#   endif
+    __gthread_mutex_unlock (&this->_M_tree_ptr->_M_c_string_lock);
     return(__result);
 }
 
 template<class _CharT, class _Alloc>
 const _CharT* rope<_CharT,_Alloc>::replace_with_c_str() {
-    if (0 == _M_tree_ptr) {
+    if (0 == this->_M_tree_ptr) {
         _S_empty_c_str[0] = _S_eos((_CharT*)0);
         return _S_empty_c_str;
     }
-    __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
-    if (_RopeRep::_S_leaf == _M_tree_ptr->_M_tag && 0 != __old_c_string) {
+    __GC_CONST _CharT* __old_c_string = this->_M_tree_ptr->_M_c_string;
+    if (_RopeRep::_S_leaf == this->_M_tree_ptr->_M_tag
+       && 0 != __old_c_string) {
        return(__old_c_string);
     }
     size_t __s = size();
     _CharT* __result = _Data_allocate(_S_rounded_up_size(__s));
-    _S_flatten(_M_tree_ptr, __result);
+    _S_flatten(this->_M_tree_ptr, __result);
     __result[__s] = _S_eos((_CharT*)0);
-    _M_tree_ptr->_M_unref_nonnil();
-    _M_tree_ptr = _S_new_RopeLeaf(__result, __s, get_allocator());
+    this->_M_tree_ptr->_M_unref_nonnil();
+    this->_M_tree_ptr = _S_new_RopeLeaf(__result, __s, this->get_allocator());
     return(__result);
 }
 
@@ -1534,8 +1495,6 @@ _Rope_rotate(_Rope_iterator __first,
   typedef typename _Rope_iterator::value_type _CharT;
   typedef typename _Rope_iterator::_allocator_type _Alloc;
   
-  __stl_assert(__first.container() == __middle.container()
-                           && __middle.container() == __last.container());
   rope<_CharT,_Alloc>& __r(__first.container());
   rope<_CharT,_Alloc> __prefix = __r.substr(0, __first.index());
   rope<_CharT,_Alloc> __suffix = 
@@ -1575,12 +1534,7 @@ inline void rotate(
 }
 # endif
 
-
-#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
-#pragma reset woff 1174
-#endif
-
-__STL_END_NAMESPACE
+} // namespace __gnu_cxx
 
 // Local Variables:
 // mode:C++