ropeimpl.h: #include <ostream> instead of <iostream>
[gcc.git] / libstdc++-v3 / include / ext / ropeimpl.h
index e36fedfada5f27f58e92c3c29ed1a94858ad15ed..6129084deb6d32a67f63021e83257014ec09077a 100644 (file)
@@ -1,6 +1,6 @@
 // SGI's rope class implementation -*- C++ -*-
 
-// Copyright (C) 2001 Free Software Foundation, Inc.
+// 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
  * 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>     
-#include <bits/std_iostream.h>
-# include <bits/functexcept.h>
+#include <cstdio>     
+#include <ostream>
+#include <bits/functexcept.h>
 
-namespace std
+#include <ext/algorithm> // For copy_n and lexicographical_compare_3way
+#include <ext/memory> // For uninitialized_copy_n
+#include <ext/numeric> // For power
+
+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.
@@ -266,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;
     }
 }
 
@@ -288,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>
@@ -309,7 +321,7 @@ inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
 {
     _CharT* __cstr = _M_c_string;
     if (0 != __cstr) {
-       size_t __size = _M_size + 1;
+       size_t __size = this->_M_size + 1;
        _Destroy(__cstr, __cstr + __size);
        _Data_deallocate(__cstr, __size);
     }
@@ -389,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)
 {
@@ -417,7 +429,7 @@ rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
 #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)
 {
@@ -448,7 +460,7 @@ 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, 
@@ -481,6 +493,7 @@ rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
 }
 
 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)
 {
@@ -535,7 +548,7 @@ rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
 
 #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)
 {
@@ -592,7 +605,7 @@ rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
 #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) {
@@ -647,7 +660,7 @@ rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
 }
 
 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)
 {
@@ -835,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)) {
@@ -862,15 +875,15 @@ bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
                size_t __len = __end - __begin;
                bool __result;
                _CharT* __buffer =
-                 (_CharT*)alloc::allocate(__len * sizeof(_CharT));
+                 (_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));
                 }
                catch(...)
                  {
-                   alloc::deallocate(__buffer, __len * sizeof(_CharT));
+                   __alloc::deallocate(__buffer, __len * sizeof(_CharT));
                    __throw_exception_again;
                  }
                return __result;
@@ -899,7 +912,7 @@ 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();
       _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
@@ -947,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;
@@ -976,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;
@@ -1072,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];
@@ -1108,7 +1121,7 @@ rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
       }
 
     if (__result->_M_depth > _RopeRep::_S_max_rope_depth)
-      __throw_length_error("rope too long");
+      __throw_length_error("rope::_S_balance");
     return(__result);
 }
 
@@ -1423,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>
@@ -1432,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);
 }
 
@@ -1528,7 +1534,7 @@ inline void rotate(
 }
 # endif
 
-} // namespace std
+} // namespace __gnu_cxx
 
 // Local Variables:
 // mode:C++