1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * This file is a GNU extension to the Standard C++ Library (possibly
46 * containing extensions from the HP/SGI STL subset).
52 #include <bits/stl_algobase.h>
53 #include <bits/stl_construct.h>
54 #include <bits/stl_uninitialized.h>
55 #include <bits/stl_algo.h>
56 #include <bits/stl_function.h>
57 #include <bits/stl_numeric.h>
58 #include <bits/allocator.h>
59 #include <ext/hash_fun.h>
62 # define __GC_CONST const
64 # include <bits/gthr.h>
65 # define __GC_CONST // constant except for deallocation
68 #include <ext/memory> // For uninitialized_copy_n
70 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
74 enum { _S_max_rope_depth = 45 };
75 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
76 } // namespace __detail
82 using std::reverse_iterator;
85 // The _S_eos function is used for those functions that
86 // convert to/from C-like strings to detect the end of the string.
88 // The end-of-C-string character.
89 // This is what the draft standard says it should be.
90 template <class _CharT>
95 // Test for basic character types.
96 // For basic character types leaves having a trailing eos.
97 template <class _CharT>
99 _S_is_basic_char_type(_CharT*)
102 template <class _CharT>
104 _S_is_one_byte_char_type(_CharT*)
108 _S_is_basic_char_type(char*)
112 _S_is_one_byte_char_type(char*)
116 _S_is_basic_char_type(wchar_t*)
119 // Store an eos iff _CharT is a basic character type.
120 // Do not reference _S_eos if it isn't.
121 template <class _CharT>
123 _S_cond_store_eos(_CharT&) { }
126 _S_cond_store_eos(char& __c)
130 _S_cond_store_eos(wchar_t& __c)
133 // char_producers are logically functions that generate a section of
134 // a string. These can be convereted to ropes. The resulting rope
135 // invokes the char_producer on demand. This allows, for example,
136 // files to be viewed as ropes without reading the entire file.
137 template <class _CharT>
141 virtual ~char_producer() { };
144 operator()(size_t __start_pos, size_t __len,
145 _CharT* __buffer) = 0;
146 // Buffer should really be an arbitrary output iterator.
147 // That way we could flatten directly into an ostream, etc.
148 // This is thoroughly impossible, since iterator types don't
149 // have runtime descriptions.
154 // Sequence must provide an append operation that appends an
155 // array to the sequence. Sequence buffers are useful only if
156 // appending an entire array is cheaper than appending element by element.
157 // This is true for many string representations.
158 // This should perhaps inherit from ostream<sequence::value_type>
159 // and be implemented correspondingly, so that they can be used
160 // for formatted. For the sake of portability, we don't do this yet.
162 // For now, sequence buffers behave as output iterators. But they also
163 // behave a little like basic_ostringstream<sequence::value_type> and a
164 // little like containers.
166 template<class _Sequence, size_t _Buf_sz = 100>
167 class sequence_buffer
168 : public iterator<std::output_iterator_tag, void, void, void, void>
171 typedef typename _Sequence::value_type value_type;
173 _Sequence* _M_prefix;
174 value_type _M_buffer[_Buf_sz];
181 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
189 : _M_prefix(0), _M_buf_count(0) { }
191 sequence_buffer(const sequence_buffer& __x)
193 _M_prefix = __x._M_prefix;
194 _M_buf_count = __x._M_buf_count;
195 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
198 sequence_buffer(sequence_buffer& __x)
201 _M_prefix = __x._M_prefix;
205 sequence_buffer(_Sequence& __s)
206 : _M_prefix(&__s), _M_buf_count(0) { }
209 operator=(sequence_buffer& __x)
212 _M_prefix = __x._M_prefix;
218 operator=(const sequence_buffer& __x)
220 _M_prefix = __x._M_prefix;
221 _M_buf_count = __x._M_buf_count;
222 std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
227 push_back(value_type __x)
229 if (_M_buf_count < _Buf_sz)
231 _M_buffer[_M_buf_count] = __x;
243 append(value_type* __s, size_t __len)
245 if (__len + _M_buf_count <= _Buf_sz)
247 size_t __i = _M_buf_count;
248 for (size_t __j = 0; __j < __len; __i++, __j++)
249 _M_buffer[__i] = __s[__j];
250 _M_buf_count += __len;
252 else if (0 == _M_buf_count)
253 _M_prefix->append(__s, __s + __len);
262 write(value_type* __s, size_t __len)
276 operator=(const value_type& __rhs)
295 // The following should be treated as private, at least for now.
296 template<class _CharT>
297 class _Rope_char_consumer
300 // If we had member templates, these should not be virtual.
301 // For now we need to use run-time parametrization where
302 // compile-time would do. Hence this should all be private
304 // The symmetry with char_producer is accidental and temporary.
305 virtual ~_Rope_char_consumer() { };
308 operator()(const _CharT* __buffer, size_t __len) = 0;
311 // First a lot of forward declarations. The standard seems to require
312 // much stricter "declaration before use" than many of the implementations
314 template<class _CharT, class _Alloc = allocator<_CharT> >
317 template<class _CharT, class _Alloc>
318 struct _Rope_RopeConcatenation;
320 template<class _CharT, class _Alloc>
321 struct _Rope_RopeLeaf;
323 template<class _CharT, class _Alloc>
324 struct _Rope_RopeFunction;
326 template<class _CharT, class _Alloc>
327 struct _Rope_RopeSubstring;
329 template<class _CharT, class _Alloc>
330 class _Rope_iterator;
332 template<class _CharT, class _Alloc>
333 class _Rope_const_iterator;
335 template<class _CharT, class _Alloc>
336 class _Rope_char_ref_proxy;
338 template<class _CharT, class _Alloc>
339 class _Rope_char_ptr_proxy;
341 template<class _CharT, class _Alloc>
343 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
344 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
346 template<class _CharT, class _Alloc>
347 _Rope_const_iterator<_CharT, _Alloc>
348 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
351 template<class _CharT, class _Alloc>
352 _Rope_const_iterator<_CharT, _Alloc>
353 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
356 template<class _CharT, class _Alloc>
357 _Rope_const_iterator<_CharT, _Alloc>
358 operator+(ptrdiff_t __n,
359 const _Rope_const_iterator<_CharT, _Alloc>& __x);
361 template<class _CharT, class _Alloc>
363 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
364 const _Rope_const_iterator<_CharT, _Alloc>& __y);
366 template<class _CharT, class _Alloc>
368 operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
369 const _Rope_const_iterator<_CharT, _Alloc>& __y);
371 template<class _CharT, class _Alloc>
373 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
374 const _Rope_const_iterator<_CharT, _Alloc>& __y);
376 template<class _CharT, class _Alloc>
377 _Rope_iterator<_CharT, _Alloc>
378 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
380 template<class _CharT, class _Alloc>
381 _Rope_iterator<_CharT, _Alloc>
382 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
384 template<class _CharT, class _Alloc>
385 _Rope_iterator<_CharT, _Alloc>
386 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
388 template<class _CharT, class _Alloc>
390 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
391 const _Rope_iterator<_CharT, _Alloc>& __y);
393 template<class _CharT, class _Alloc>
395 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
396 const _Rope_iterator<_CharT, _Alloc>& __y);
398 template<class _CharT, class _Alloc>
400 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
401 const _Rope_iterator<_CharT, _Alloc>& __y);
403 template<class _CharT, class _Alloc>
405 operator+(const rope<_CharT, _Alloc>& __left,
406 const rope<_CharT, _Alloc>& __right);
408 template<class _CharT, class _Alloc>
410 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
412 template<class _CharT, class _Alloc>
414 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
416 // Some helpers, so we can use power on ropes.
417 // See below for why this isn't local to the implementation.
419 // This uses a nonstandard refcount convention.
420 // The result has refcount 0.
421 template<class _CharT, class _Alloc>
422 struct _Rope_Concat_fn
423 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
424 rope<_CharT, _Alloc> >
427 operator()(const rope<_CharT, _Alloc>& __x,
428 const rope<_CharT, _Alloc>& __y)
429 { return __x + __y; }
432 template <class _CharT, class _Alloc>
433 inline rope<_CharT, _Alloc>
434 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
435 { return rope<_CharT, _Alloc>(); }
437 // Class _Refcount_Base provides a type, _RC_t, a data member,
438 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
439 // atomic preincrement/predecrement. The constructor initializes
441 struct _Refcount_Base
444 typedef size_t _RC_t;
446 // The data member _M_ref_count
447 volatile _RC_t _M_ref_count;
450 __gthread_mutex_t _M_ref_count_lock;
452 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
454 #ifdef __GTHREAD_MUTEX_INIT
455 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
456 _M_ref_count_lock = __tmp;
457 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
458 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
460 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
467 __gthread_mutex_lock(&_M_ref_count_lock);
469 __gthread_mutex_unlock(&_M_ref_count_lock);
475 __gthread_mutex_lock(&_M_ref_count_lock);
476 volatile _RC_t __tmp = --_M_ref_count;
477 __gthread_mutex_unlock(&_M_ref_count_lock);
483 // What follows should really be local to rope. Unfortunately,
484 // that doesn't work, since it makes it impossible to define generic
485 // equality on rope iterators. According to the draft standard, the
486 // template parameters for such an equality operator cannot be inferred
487 // from the occurrence of a member class as a parameter.
488 // (SGI compilers in fact allow this, but the __result wouldn't be
490 // Similarly, some of the static member functions are member functions
491 // only to avoid polluting the global namespace, and to circumvent
492 // restrictions on type inference for template functions.
496 // The internal data structure for representing a rope. This is
497 // private to the implementation. A rope is really just a pointer
500 // A few basic functions for manipulating this data structure
501 // are members of _RopeRep. Most of the more complex algorithms
502 // are implemented as rope members.
504 // Some of the static member functions of _RopeRep have identically
505 // named functions in rope that simply invoke the _RopeRep versions.
507 #define __ROPE_DEFINE_ALLOCS(__a) \
508 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
509 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
510 __ROPE_DEFINE_ALLOC(__C,_C) \
511 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
512 __ROPE_DEFINE_ALLOC(__L,_L) \
513 typedef _Rope_RopeFunction<_CharT,__a> __F; \
514 __ROPE_DEFINE_ALLOC(__F,_F) \
515 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
516 __ROPE_DEFINE_ALLOC(__S,_S)
518 // Internal rope nodes potentially store a copy of the allocator
519 // instance used to allocate them. This is mostly redundant.
520 // But the alternative would be to pass allocator instances around
521 // in some form to nearly all internal functions, since any pointer
522 // assignment may result in a zero reference count and thus require
525 #define __STATIC_IF_SGI_ALLOC /* not static */
527 template <class _CharT, class _Alloc>
528 struct _Rope_rep_base
531 typedef _Alloc allocator_type;
534 get_allocator() const
535 { return *static_cast<const _Alloc*>(this); }
537 _Rope_rep_base(size_t __size, const allocator_type&)
538 : _M_size(__size) { }
542 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
544 _Alloc::template rebind<_Tp>::other __name##Alloc; \
545 static _Tp* __name##_allocate(size_t __n) \
546 { return __name##Alloc().allocate(__n); } \
547 static void __name##_deallocate(_Tp *__p, size_t __n) \
548 { __name##Alloc().deallocate(__p, __n); }
549 __ROPE_DEFINE_ALLOCS(_Alloc)
550 # undef __ROPE_DEFINE_ALLOC
553 template<class _CharT, class _Alloc>
555 : public _Rope_rep_base<_CharT, _Alloc>
561 __detail::_Tag _M_tag:8;
562 bool _M_is_balanced:8;
563 unsigned char _M_depth;
564 __GC_CONST _CharT* _M_c_string;
565 __gthread_mutex_t _M_c_string_lock;
566 /* Flattened version of string, if needed. */
568 /* If it's not 0, then the memory is owned */
570 /* In the case of a leaf, this may point to */
571 /* the same memory as the data field. */
572 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
575 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
577 _Rope_RopeRep(__detail::_Tag __t, int __d, bool __b, size_t __size,
579 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
583 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
584 #ifdef __GTHREAD_MUTEX_INIT
586 // Do not copy a POSIX/gthr mutex once in use. However, bits are bits.
587 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
588 _M_c_string_lock = __tmp;
591 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
598 _S_free_string(__GC_CONST _CharT*, size_t __len,
600 #define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
601 // Deallocate data section of a leaf.
602 // This shouldn't be a member function.
603 // But its hard to do anything else at the
604 // moment, because it's templatized w.r.t.
606 // Does nothing if __GC is defined.
608 void _M_free_c_string();
610 // Deallocate t. Assumes t is not 0.
623 _S_unref(_Rope_RopeRep* __t)
626 __t->_M_unref_nonnil();
630 _S_ref(_Rope_RopeRep* __t)
637 _S_free_if_unref(_Rope_RopeRep* __t)
639 if (0 != __t && 0 == __t->_M_ref_count)
643 void _M_unref_nonnil() { }
644 void _M_ref_nonnil() { }
645 static void _S_unref(_Rope_RopeRep*) { }
646 static void _S_ref(_Rope_RopeRep*) { }
647 static void _S_free_if_unref(_Rope_RopeRep*) { }
651 operator=(const _Rope_RopeRep&);
653 _Rope_RopeRep(const _Rope_RopeRep&);
656 template<class _CharT, class _Alloc>
657 struct _Rope_RopeLeaf
658 : public _Rope_RopeRep<_CharT, _Alloc>
661 // Apparently needed by VC++
662 // The data fields of leaves are allocated with some
663 // extra space, to accommodate future growth and for basic
664 // character types, to hold a trailing eos character.
665 enum { _S_alloc_granularity = 8 };
668 _S_rounded_up_size(size_t __n)
670 size_t __size_with_eos;
672 if (_S_is_basic_char_type((_CharT*)0))
673 __size_with_eos = __n + 1;
675 __size_with_eos = __n;
677 return __size_with_eos;
679 // Allow slop for in-place expansion.
680 return ((__size_with_eos + size_t(_S_alloc_granularity) - 1)
681 &~ (size_t(_S_alloc_granularity) - 1));
684 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
685 /* The allocated size is */
686 /* _S_rounded_up_size(size), except */
687 /* in the GC case, in which it */
688 /* doesn't matter. */
689 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
692 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size,
694 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_leaf, 0, true,
695 __size, __a), _M_data(__d)
697 if (_S_is_basic_char_type((_CharT *)0))
699 // already eos terminated.
700 this->_M_c_string = __d;
703 // The constructor assumes that d has been allocated with
704 // the proper allocator and the properly padded size.
705 // In contrast, the destructor deallocates the data:
707 ~_Rope_RopeLeaf() throw()
709 if (_M_data != this->_M_c_string)
710 this->_M_free_c_string();
712 __STL_FREE_STRING(_M_data, this->_M_size, this->get_allocator());
717 operator=(const _Rope_RopeLeaf&);
719 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
722 template<class _CharT, class _Alloc>
723 struct _Rope_RopeConcatenation
724 : public _Rope_RopeRep<_CharT, _Alloc>
727 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
728 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
730 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
733 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
734 _Rope_RopeRep<_CharT, _Alloc>* __r,
736 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_concat,
737 std::max(__l->_M_depth,
740 __l->_M_size + __r->_M_size, __a),
741 _M_left(__l), _M_right(__r)
744 ~_Rope_RopeConcatenation() throw()
746 this->_M_free_c_string();
747 _M_left->_M_unref_nonnil();
748 _M_right->_M_unref_nonnil();
752 _Rope_RopeConcatenation&
753 operator=(const _Rope_RopeConcatenation&);
755 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
758 template<class _CharT, class _Alloc>
759 struct _Rope_RopeFunction
760 : public _Rope_RopeRep<_CharT, _Alloc>
763 char_producer<_CharT>* _M_fn;
765 bool _M_delete_when_done; // Char_producer is owned by the
766 // rope and should be explicitly
767 // deleted when the rope becomes
770 // In the GC case, we either register the rope for
771 // finalization, or not. Thus the field is unnecessary;
772 // the information is stored in the collector data structures.
773 // We do need a finalization procedure to be invoked by the
776 _S_fn_finalization_proc(void * __tree, void *)
777 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
779 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
782 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
783 bool __d, allocator_type __a)
784 : _Rope_RopeRep<_CharT, _Alloc>(__detail::_S_function, 0, true, __size, __a)
787 , _M_delete_when_done(__d)
793 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
794 _S_fn_finalization_proc, 0, 0, 0);
799 ~_Rope_RopeFunction() throw()
801 this->_M_free_c_string();
802 if (_M_delete_when_done)
808 operator=(const _Rope_RopeFunction&);
810 _Rope_RopeFunction(const _Rope_RopeFunction&);
812 // Substring results are usually represented using just
813 // concatenation nodes. But in the case of very long flat ropes
814 // or ropes with a functional representation that isn't practical.
815 // In that case, we represent the __result as a special case of
816 // RopeFunction, whose char_producer points back to the rope itself.
817 // In all cases except repeated substring operations and
818 // deallocation, we treat the __result as a RopeFunction.
819 template<class _CharT, class _Alloc>
820 struct _Rope_RopeSubstring
821 : public _Rope_RopeFunction<_CharT, _Alloc>,
822 public char_producer<_CharT>
825 // XXX this whole class should be rewritten.
826 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
830 operator()(size_t __start_pos, size_t __req_len,
833 switch(_M_base->_M_tag)
835 case __detail::_S_function:
836 case __detail::_S_substringfn:
838 char_producer<_CharT>* __fn =
839 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
840 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
843 case __detail::_S_leaf:
845 __GC_CONST _CharT* __s =
846 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
847 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
856 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
859 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_t __s,
860 size_t __l, allocator_type __a)
861 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
862 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
865 _M_base->_M_ref_nonnil();
867 this->_M_tag = __detail::_S_substringfn;
869 virtual ~_Rope_RopeSubstring() throw()
872 _M_base->_M_unref_nonnil();
873 // _M_free_c_string(); -- done by parent class
878 // Self-destructing pointers to Rope_rep.
879 // These are not conventional smart pointers. Their
880 // only purpose in life is to ensure that unref is called
881 // on the pointer either at normal exit or if an exception
882 // is raised. It is the caller's responsibility to
883 // adjust reference counts when these pointers are initialized
884 // or assigned to. (This convention significantly reduces
885 // the number of potentially expensive reference count
888 template<class _CharT, class _Alloc>
889 struct _Rope_self_destruct_ptr
891 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
893 ~_Rope_self_destruct_ptr()
894 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
896 _Rope_self_destruct_ptr() : _M_ptr(0) { };
898 _Rope_self_destruct_ptr() { };
900 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
903 _Rope_RopeRep<_CharT, _Alloc>&
907 _Rope_RopeRep<_CharT, _Alloc>*
911 operator _Rope_RopeRep<_CharT, _Alloc>*()
914 _Rope_self_destruct_ptr&
915 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
916 { _M_ptr = __x; return *this; }
920 // Dereferencing a nonconst iterator has to return something
921 // that behaves almost like a reference. It's not possible to
922 // return an actual reference since assignment requires extra
923 // work. And we would get into the same problems as with the
924 // CD2 version of basic_string.
925 template<class _CharT, class _Alloc>
926 class _Rope_char_ref_proxy
928 friend class rope<_CharT, _Alloc>;
929 friend class _Rope_iterator<_CharT, _Alloc>;
930 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
932 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
934 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
936 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
937 typedef rope<_CharT, _Alloc> _My_rope;
940 bool _M_current_valid;
941 _My_rope* _M_root; // The whole rope.
943 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
944 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) { }
946 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
947 : _M_pos(__x._M_pos), _M_current(__x._M_current),
948 _M_current_valid(false), _M_root(__x._M_root) { }
950 // Don't preserve cache if the reference can outlive the
951 // expression. We claim that's not possible without calling
952 // a copy constructor or generating reference to a proxy
953 // reference. We declare the latter to have undefined semantics.
954 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
955 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) { }
957 inline operator _CharT () const;
959 _Rope_char_ref_proxy&
960 operator=(_CharT __c);
962 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
964 _Rope_char_ref_proxy&
965 operator=(const _Rope_char_ref_proxy& __c)
966 { return operator=((_CharT)__c); }
969 template<class _CharT, class __Alloc>
971 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
972 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
979 template<class _CharT, class _Alloc>
980 class _Rope_char_ptr_proxy
982 // XXX this class should be rewritten.
983 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
985 rope<_CharT,_Alloc>* _M_root; // The whole rope.
987 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
988 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
990 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
991 : _M_pos(__x._M_pos), _M_root(__x._M_root) { }
993 _Rope_char_ptr_proxy() { }
995 _Rope_char_ptr_proxy(_CharT* __x)
996 : _M_root(0), _M_pos(0) { }
998 _Rope_char_ptr_proxy&
999 operator=(const _Rope_char_ptr_proxy& __x)
1001 _M_pos = __x._M_pos;
1002 _M_root = __x._M_root;
1006 template<class _CharT2, class _Alloc2>
1008 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1009 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1011 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1012 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1016 // Unlike in the C version, we cache only part of the stack
1017 // for rope iterators, since they must be efficiently copyable.
1018 // When we run out of cache, we have to reconstruct the iterator
1020 // Pointers from iterators are not included in reference counts.
1021 // Iterators are assumed to be thread private. Ropes can
1024 template<class _CharT, class _Alloc>
1025 class _Rope_iterator_base
1026 : public iterator<std::random_access_iterator_tag, _CharT>
1028 friend class rope<_CharT, _Alloc>;
1030 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1031 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1032 // Borland doesn't want this to be protected.
1034 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1035 enum { _S_iterator_buf_len = 15 };
1036 size_t _M_current_pos;
1037 _RopeRep* _M_root; // The whole rope.
1038 size_t _M_leaf_pos; // Starting position for current leaf
1039 __GC_CONST _CharT* _M_buf_start;
1041 // containing current char.
1042 __GC_CONST _CharT* _M_buf_ptr;
1043 // Pointer to current char in buffer.
1044 // != 0 ==> buffer valid.
1045 __GC_CONST _CharT* _M_buf_end;
1046 // One past __last valid char in buffer.
1047 // What follows is the path cache. We go out of our
1048 // way to make this compact.
1049 // Path_end contains the bottom section of the path from
1050 // the root to the current leaf.
1051 const _RopeRep* _M_path_end[_S_path_cache_len];
1052 int _M_leaf_index; // Last valid __pos in path_end;
1053 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1054 // point to concatenation nodes.
1055 unsigned char _M_path_directions;
1056 // (path_directions >> __i) & 1 is 1
1057 // iff we got from _M_path_end[leaf_index - __i - 1]
1058 // to _M_path_end[leaf_index - __i] by going to the
1059 // __right. Assumes path_cache_len <= 9.
1060 _CharT _M_tmp_buf[_S_iterator_buf_len];
1061 // Short buffer for surrounding chars.
1062 // This is useful primarily for
1063 // RopeFunctions. We put the buffer
1064 // here to avoid locking in the
1065 // multithreaded case.
1066 // The cached path is generally assumed to be valid
1067 // only if the buffer is valid.
1068 static void _S_setbuf(_Rope_iterator_base& __x);
1069 // Set buffer contents given
1071 static void _S_setcache(_Rope_iterator_base& __x);
1072 // Set buffer contents and
1074 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1075 // As above, but assumes path
1076 // cache is valid for previous posn.
1077 _Rope_iterator_base() { }
1079 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
1080 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) { }
1082 void _M_incr(size_t __n);
1083 void _M_decr(size_t __n);
1087 { return _M_current_pos; }
1089 _Rope_iterator_base(const _Rope_iterator_base& __x)
1091 if (0 != __x._M_buf_ptr)
1095 _M_current_pos = __x._M_current_pos;
1096 _M_root = __x._M_root;
1102 template<class _CharT, class _Alloc>
1103 class _Rope_iterator;
1105 template<class _CharT, class _Alloc>
1106 class _Rope_const_iterator
1107 : public _Rope_iterator_base<_CharT, _Alloc>
1109 friend class rope<_CharT, _Alloc>;
1111 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1112 // The one from the base class may not be directly visible.
1113 _Rope_const_iterator(const _RopeRep* __root, size_t __pos)
1114 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1116 // Only nonconst iterators modify root ref count
1119 typedef _CharT reference; // Really a value. Returning a reference
1120 // Would be a mess, since it would have
1121 // to be included in refcount.
1122 typedef const _CharT* pointer;
1125 _Rope_const_iterator() { };
1127 _Rope_const_iterator(const _Rope_const_iterator& __x)
1128 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1130 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1132 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, size_t __pos)
1133 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1135 _Rope_const_iterator&
1136 operator=(const _Rope_const_iterator& __x)
1138 if (0 != __x._M_buf_ptr)
1139 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1142 this->_M_current_pos = __x._M_current_pos;
1143 this->_M_root = __x._M_root;
1144 this->_M_buf_ptr = 0;
1152 if (0 == this->_M_buf_ptr)
1154 return *this->_M_buf_ptr;
1157 // Without this const version, Rope iterators do not meet the
1158 // requirements of an Input Iterator.
1162 return *const_cast<_Rope_const_iterator&>(*this);
1165 _Rope_const_iterator&
1168 __GC_CONST _CharT* __next;
1169 if (0 != this->_M_buf_ptr
1170 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1172 this->_M_buf_ptr = __next;
1173 ++this->_M_current_pos;
1180 _Rope_const_iterator&
1181 operator+=(ptrdiff_t __n)
1186 this->_M_decr(-__n);
1190 _Rope_const_iterator&
1197 _Rope_const_iterator&
1198 operator-=(ptrdiff_t __n)
1203 this->_M_incr(-__n);
1207 _Rope_const_iterator
1210 size_t __old_pos = this->_M_current_pos;
1212 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1213 // This makes a subsequent dereference expensive.
1214 // Perhaps we should instead copy the iterator
1215 // if it has a valid cache?
1218 _Rope_const_iterator
1221 size_t __old_pos = this->_M_current_pos;
1223 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1226 template<class _CharT2, class _Alloc2>
1227 friend _Rope_const_iterator<_CharT2, _Alloc2>
1228 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1231 template<class _CharT2, class _Alloc2>
1232 friend _Rope_const_iterator<_CharT2, _Alloc2>
1233 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1236 template<class _CharT2, class _Alloc2>
1237 friend _Rope_const_iterator<_CharT2, _Alloc2>
1238 operator+(ptrdiff_t __n,
1239 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1242 operator[](size_t __n)
1243 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1244 this->_M_current_pos + __n); }
1246 template<class _CharT2, class _Alloc2>
1248 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1249 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1251 template<class _CharT2, class _Alloc2>
1253 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1254 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1256 template<class _CharT2, class _Alloc2>
1258 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1259 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1262 template<class _CharT, class _Alloc>
1263 class _Rope_iterator
1264 : public _Rope_iterator_base<_CharT, _Alloc>
1266 friend class rope<_CharT, _Alloc>;
1268 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1269 rope<_CharT, _Alloc>* _M_root_rope;
1271 // root is treated as a cached version of this, and is used to
1272 // detect changes to the underlying rope.
1274 // Root is included in the reference count. This is necessary
1275 // so that we can detect changes reliably. Unfortunately, it
1276 // requires careful bookkeeping for the nonGC case.
1277 _Rope_iterator(rope<_CharT, _Alloc>* __r, size_t __pos)
1278 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1280 { _RopeRep::_S_ref(this->_M_root);
1281 if (!(__r -> empty()))
1287 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1288 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1290 rope<_CharT, _Alloc>&
1292 { return *_M_root_rope; }
1296 this->_M_root = 0; // Needed for reference counting.
1299 _Rope_iterator(const _Rope_iterator& __x)
1300 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1302 _M_root_rope = __x._M_root_rope;
1303 _RopeRep::_S_ref(this->_M_root);
1306 _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos);
1309 { _RopeRep::_S_unref(this->_M_root); }
1312 operator=(const _Rope_iterator& __x)
1314 _RopeRep* __old = this->_M_root;
1316 _RopeRep::_S_ref(__x._M_root);
1317 if (0 != __x._M_buf_ptr)
1319 _M_root_rope = __x._M_root_rope;
1320 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1324 this->_M_current_pos = __x._M_current_pos;
1325 this->_M_root = __x._M_root;
1326 _M_root_rope = __x._M_root_rope;
1327 this->_M_buf_ptr = 0;
1329 _RopeRep::_S_unref(__old);
1337 if (0 == this->_M_buf_ptr)
1338 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1339 this->_M_current_pos);
1341 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1342 this->_M_current_pos,
1346 // See above comment.
1350 return *const_cast<_Rope_iterator&>(*this);
1361 operator+=(ptrdiff_t __n)
1366 this->_M_decr(-__n);
1378 operator-=(ptrdiff_t __n)
1383 this->_M_incr(-__n);
1390 size_t __old_pos = this->_M_current_pos;
1392 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1398 size_t __old_pos = this->_M_current_pos;
1400 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1404 operator[](ptrdiff_t __n)
1405 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1406 this->_M_current_pos
1409 template<class _CharT2, class _Alloc2>
1411 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1412 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1414 template<class _CharT2, class _Alloc2>
1416 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1417 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1419 template<class _CharT2, class _Alloc2>
1421 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1422 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1424 template<class _CharT2, class _Alloc2>
1425 friend _Rope_iterator<_CharT2, _Alloc2>
1426 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1428 template<class _CharT2, class _Alloc2>
1429 friend _Rope_iterator<_CharT2, _Alloc2>
1430 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1432 template<class _CharT2, class _Alloc2>
1433 friend _Rope_iterator<_CharT2, _Alloc2>
1434 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT2, _Alloc2>& __x);
1438 template <class _CharT, class _Alloc>
1442 typedef _Alloc allocator_type;
1445 get_allocator() const
1446 { return *static_cast<const _Alloc*>(this); }
1448 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1449 // The one in _Base may not be visible due to template rules.
1451 _Rope_base(_RopeRep* __t, const allocator_type&)
1452 : _M_tree_ptr(__t) { }
1454 _Rope_base(const allocator_type&) { }
1456 // The only data member of a rope:
1457 _RopeRep *_M_tree_ptr;
1459 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1461 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1462 static _Tp* __name##_allocate(size_t __n) \
1463 { return __name##Alloc().allocate(__n); } \
1464 static void __name##_deallocate(_Tp *__p, size_t __n) \
1465 { __name##Alloc().deallocate(__p, __n); }
1466 __ROPE_DEFINE_ALLOCS(_Alloc)
1467 #undef __ROPE_DEFINE_ALLOC
1471 operator=(const _Rope_base&);
1473 _Rope_base(const _Rope_base&);
1477 * This is an SGI extension.
1478 * @ingroup SGIextensions
1481 template <class _CharT, class _Alloc>
1482 class rope : public _Rope_base<_CharT, _Alloc>
1485 typedef _CharT value_type;
1486 typedef ptrdiff_t difference_type;
1487 typedef size_t size_type;
1488 typedef _CharT const_reference;
1489 typedef const _CharT* const_pointer;
1490 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1491 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1492 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1493 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1495 friend class _Rope_iterator<_CharT, _Alloc>;
1496 friend class _Rope_const_iterator<_CharT, _Alloc>;
1497 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1498 friend class _Rope_iterator_base<_CharT, _Alloc>;
1499 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1500 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1501 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1504 typedef _Rope_base<_CharT, _Alloc> _Base;
1505 typedef typename _Base::allocator_type allocator_type;
1506 using _Base::_M_tree_ptr;
1507 using _Base::get_allocator;
1508 typedef __GC_CONST _CharT* _Cstrptr;
1510 static _CharT _S_empty_c_str[1];
1514 { return __c == _S_eos((_CharT*)0); }
1516 enum { _S_copy_max = 23 };
1517 // For strings shorter than _S_copy_max, we copy to
1520 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1521 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1522 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1523 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1524 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1526 // Retrieve a character at the indicated position.
1527 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1530 // Obtain a pointer to the character at the indicated position.
1531 // The pointer can be used to change the character.
1532 // If such a pointer cannot be produced, as is frequently the
1533 // case, 0 is returned instead.
1534 // (Returns nonzero only if all nodes in the path have a refcount
1536 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1540 _S_apply_to_pieces(// should be template parameter
1541 _Rope_char_consumer<_CharT>& __c,
1542 const _RopeRep* __r,
1543 size_t __begin, size_t __end);
1544 // begin and end are assumed to be in range.
1548 _S_unref(_RopeRep* __t)
1549 { _RopeRep::_S_unref(__t); }
1552 _S_ref(_RopeRep* __t)
1553 { _RopeRep::_S_ref(__t); }
1556 static void _S_unref(_RopeRep*) { }
1557 static void _S_ref(_RopeRep*) { }
1561 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1563 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1566 // _Result is counted in refcount.
1567 static _RopeRep* _S_substring(_RopeRep* __base,
1568 size_t __start, size_t __endp1);
1570 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1571 const _CharT* __iter, size_t __slen);
1572 // Concatenate rope and char ptr, copying __s.
1573 // Should really take an arbitrary iterator.
1574 // Result is counted in refcount.
1575 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1576 const _CharT* __iter,
1578 // As above, but one reference to __r is about to be
1579 // destroyed. Thus the pieces may be recycled if all
1580 // relevant reference counts are 1.
1582 // We can't really do anything since refcounts are unavailable.
1583 { return _S_concat_char_iter(__r, __iter, __slen); }
1588 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1589 // General concatenation on _RopeRep. _Result
1590 // has refcount of 1. Adjusts argument refcounts.
1594 apply_to_pieces(size_t __begin, size_t __end,
1595 _Rope_char_consumer<_CharT>& __c) const
1596 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1601 _S_rounded_up_size(size_t __n)
1602 { return _RopeLeaf::_S_rounded_up_size(__n); }
1605 _S_allocated_capacity(size_t __n)
1607 if (_S_is_basic_char_type((_CharT*)0))
1608 return _S_rounded_up_size(__n) - 1;
1610 return _S_rounded_up_size(__n);
1614 // Allocate and construct a RopeLeaf using the supplied allocator
1615 // Takes ownership of s instead of copying.
1617 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1618 size_t __size, allocator_type __a)
1620 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1621 return new(__space) _RopeLeaf(__s, __size, __a);
1624 static _RopeConcatenation*
1625 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1628 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1629 return new(__space) _RopeConcatenation(__left, __right, __a);
1632 static _RopeFunction*
1633 _S_new_RopeFunction(char_producer<_CharT>* __f,
1634 size_t __size, bool __d, allocator_type __a)
1636 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1637 return new(__space) _RopeFunction(__f, __size, __d, __a);
1640 static _RopeSubstring*
1641 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1642 size_t __l, allocator_type __a)
1644 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1645 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1649 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1650 size_t __size, allocator_type __a)
1651 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1652 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1656 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1658 __uninitialized_copy_n_a(__s, __size, __buf, __a);
1659 _S_cond_store_eos(__buf[__size]);
1661 { return _S_new_RopeLeaf(__buf, __size, __a); }
1664 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1665 __throw_exception_again;
1669 // Concatenation of nonempty strings.
1670 // Always builds a concatenation node.
1671 // Rebalances if the result is too deep.
1672 // Result has refcount 1.
1673 // Does not increment left and right ref counts even though
1674 // they are referenced.
1676 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1678 // Concatenation helper functions
1680 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1681 const _CharT* __iter, size_t __slen);
1682 // Concatenate by copying leaf.
1683 // should take an arbitrary iterator
1684 // result has refcount 1.
1687 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1688 const _CharT* __iter, size_t __slen);
1689 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1694 static size_t _S_char_ptr_len(const _CharT* __s);
1695 // slightly generalized strlen
1697 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1698 : _Base(__t, __a) { }
1701 // Copy __r to the _CharT buffer.
1702 // Returns __buffer + __r->_M_size.
1703 // Assumes that buffer is uninitialized.
1704 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1706 // Again, with explicit starting position and length.
1707 // Assumes that buffer is uninitialized.
1708 static _CharT* _S_flatten(_RopeRep* __r,
1709 size_t __start, size_t __len,
1712 static const unsigned long
1713 _S_min_len[__detail::_S_max_rope_depth + 1];
1716 _S_is_balanced(_RopeRep* __r)
1717 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1720 _S_is_almost_balanced(_RopeRep* __r)
1721 { return (__r->_M_depth == 0
1722 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1725 _S_is_roughly_balanced(_RopeRep* __r)
1726 { return (__r->_M_depth <= 1
1727 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1729 // Assumes the result is not empty.
1731 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1733 _RopeRep* __result = _S_concat(__left, __right);
1734 if (_S_is_balanced(__result))
1735 __result->_M_is_balanced = true;
1739 // The basic rebalancing operation. Logically copies the
1740 // rope. The result has refcount of 1. The client will
1741 // usually decrement the reference count of __r.
1742 // The result is within height 2 of balanced by the above
1744 static _RopeRep* _S_balance(_RopeRep* __r);
1746 // Add all unbalanced subtrees to the forest of balanceed trees.
1747 // Used only by balance.
1748 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1750 // Add __r to forest, assuming __r is already balanced.
1751 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1753 // Print to stdout, exposing structure
1754 static void _S_dump(_RopeRep* __r, int __indent = 0);
1756 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1757 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1762 { return 0 == this->_M_tree_ptr; }
1764 // Comparison member function. This is public only for those
1765 // clients that need a ternary comparison. Others
1766 // should use the comparison operators below.
1768 compare(const rope& __y) const
1769 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1771 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1772 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1776 rope(const _CharT* __s, size_t __len,
1777 const allocator_type& __a = allocator_type())
1778 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
1781 // Should perhaps be templatized with respect to the iterator type
1782 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1784 rope(const _CharT *__s, const _CharT *__e,
1785 const allocator_type& __a = allocator_type())
1786 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
1789 rope(const const_iterator& __s, const const_iterator& __e,
1790 const allocator_type& __a = allocator_type())
1791 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1792 __e._M_current_pos), __a)
1795 rope(const iterator& __s, const iterator& __e,
1796 const allocator_type& __a = allocator_type())
1797 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1798 __e._M_current_pos), __a)
1801 rope(_CharT __c, const allocator_type& __a = allocator_type())
1804 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1806 get_allocator().construct(__buf, __c);
1808 { this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a); }
1811 _RopeRep::__STL_FREE_STRING(__buf, 1, __a);
1812 __throw_exception_again;
1816 rope(size_t __n, _CharT __c,
1817 const allocator_type& __a = allocator_type());
1819 rope(const allocator_type& __a = allocator_type())
1822 // Construct a rope from a function that can compute its members
1823 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1824 const allocator_type& __a = allocator_type())
1827 this->_M_tree_ptr = (0 == __len) ?
1828 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1831 rope(const rope& __x, const allocator_type& __a = allocator_type())
1832 : _Base(__x._M_tree_ptr, __a)
1833 { _S_ref(this->_M_tree_ptr); }
1836 { _S_unref(this->_M_tree_ptr); }
1839 operator=(const rope& __x)
1841 _RopeRep* __old = this->_M_tree_ptr;
1842 this->_M_tree_ptr = __x._M_tree_ptr;
1843 _S_ref(this->_M_tree_ptr);
1851 _S_unref(this->_M_tree_ptr);
1852 this->_M_tree_ptr = 0;
1856 push_back(_CharT __x)
1858 _RopeRep* __old = this->_M_tree_ptr;
1860 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1867 _RopeRep* __old = this->_M_tree_ptr;
1868 this->_M_tree_ptr = _S_substring(this->_M_tree_ptr,
1869 0, this->_M_tree_ptr->_M_size - 1);
1875 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1878 push_front(_CharT __x)
1880 _RopeRep* __old = this->_M_tree_ptr;
1882 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, this->get_allocator());
1885 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1892 __throw_exception_again;
1899 _RopeRep* __old = this->_M_tree_ptr;
1901 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1907 { return _S_fetch(this->_M_tree_ptr, 0); }
1912 _RopeRep* __old = this->_M_tree_ptr;
1913 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1918 copy(_CharT* __buffer) const
1920 _Destroy(__buffer, __buffer + size(), get_allocator());
1921 _S_flatten(this->_M_tree_ptr, __buffer);
1924 // This is the copy function from the standard, but
1925 // with the arguments reordered to make it consistent with the
1926 // rest of the interface.
1927 // Note that this guaranteed not to compile if the draft standard
1928 // order is assumed.
1930 copy(size_type __pos, size_type __n, _CharT* __buffer) const
1932 size_t __size = size();
1933 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1935 _Destroy(__buffer, __buffer + __len, get_allocator());
1936 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1940 // Print to stdout, exposing structure. May be useful for
1941 // performance debugging.
1944 { _S_dump(this->_M_tree_ptr); }
1946 // Convert to 0 terminated string in new allocated memory.
1947 // Embedded 0s in the input do not terminate the copy.
1948 const _CharT* c_str() const;
1950 // As above, but lso use the flattened representation as the
1951 // the new rope representation.
1952 const _CharT* replace_with_c_str();
1954 // Reclaim memory for the c_str generated flattened string.
1955 // Intentionally undocumented, since it's hard to say when this
1956 // is safe for multiple threads.
1960 if (0 == this->_M_tree_ptr)
1962 if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag &&
1963 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1964 this->_M_tree_ptr->_M_c_string)
1966 // Representation shared
1970 this->_M_tree_ptr->_M_free_c_string();
1972 this->_M_tree_ptr->_M_c_string = 0;
1976 operator[] (size_type __pos) const
1977 { return _S_fetch(this->_M_tree_ptr, __pos); }
1980 at(size_type __pos) const
1982 // if (__pos >= size()) throw out_of_range; // XXX
1983 return (*this)[__pos];
1988 { return(const_iterator(this->_M_tree_ptr, 0)); }
1990 // An easy way to get a const iterator from a non-const container.
1993 { return(const_iterator(this->_M_tree_ptr, 0)); }
1997 { return(const_iterator(this->_M_tree_ptr, size())); }
2001 { return(const_iterator(this->_M_tree_ptr, size())); }
2005 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
2014 return _S_min_len[int(__detail::_S_max_rope_depth) - 1] - 1;
2015 // Guarantees that the result can be sufficirntly
2016 // balanced. Longer ropes will probably still work,
2017 // but it's harder to make guarantees.
2020 typedef reverse_iterator<const_iterator> const_reverse_iterator;
2022 const_reverse_iterator
2024 { return const_reverse_iterator(end()); }
2026 const_reverse_iterator
2027 const_rbegin() const
2028 { return const_reverse_iterator(end()); }
2030 const_reverse_iterator
2032 { return const_reverse_iterator(begin()); }
2034 const_reverse_iterator
2036 { return const_reverse_iterator(begin()); }
2038 template<class _CharT2, class _Alloc2>
2039 friend rope<_CharT2, _Alloc2>
2040 operator+(const rope<_CharT2, _Alloc2>& __left,
2041 const rope<_CharT2, _Alloc2>& __right);
2043 template<class _CharT2, class _Alloc2>
2044 friend rope<_CharT2, _Alloc2>
2045 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2047 template<class _CharT2, class _Alloc2>
2048 friend rope<_CharT2, _Alloc2>
2049 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2051 // The symmetric cases are intentionally omitted, since they're
2052 // presumed to be less common, and we don't handle them as well.
2054 // The following should really be templatized. The first
2055 // argument should be an input iterator or forward iterator with
2056 // value_type _CharT.
2058 append(const _CharT* __iter, size_t __n)
2060 _RopeRep* __result =
2061 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
2062 _S_unref(this->_M_tree_ptr);
2063 this->_M_tree_ptr = __result;
2068 append(const _CharT* __c_string)
2070 size_t __len = _S_char_ptr_len(__c_string);
2071 append(__c_string, __len);
2076 append(const _CharT* __s, const _CharT* __e)
2078 _RopeRep* __result =
2079 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
2080 _S_unref(this->_M_tree_ptr);
2081 this->_M_tree_ptr = __result;
2086 append(const_iterator __s, const_iterator __e)
2088 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2090 __e._M_current_pos));
2091 _RopeRep* __result = _S_concat(this->_M_tree_ptr,
2092 (_RopeRep*)__appendee);
2093 _S_unref(this->_M_tree_ptr);
2094 this->_M_tree_ptr = __result;
2101 _RopeRep* __result =
2102 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
2103 _S_unref(this->_M_tree_ptr);
2104 this->_M_tree_ptr = __result;
2110 { return append(_CharT()); } // XXX why?
2113 append(const rope& __y)
2115 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2116 _S_unref(this->_M_tree_ptr);
2117 this->_M_tree_ptr = __result;
2122 append(size_t __n, _CharT __c)
2124 rope<_CharT,_Alloc> __last(__n, __c);
2125 return append(__last);
2131 _RopeRep* __tmp = this->_M_tree_ptr;
2132 this->_M_tree_ptr = __b._M_tree_ptr;
2133 __b._M_tree_ptr = __tmp;
2137 // Result is included in refcount.
2139 replace(_RopeRep* __old, size_t __pos1,
2140 size_t __pos2, _RopeRep* __r)
2147 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2148 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2152 __result = _S_concat(__left, __right);
2155 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2156 __result = _S_concat(__left_result, __right);
2163 insert(size_t __p, const rope& __r)
2165 _RopeRep* __result =
2166 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2167 _S_unref(this->_M_tree_ptr);
2168 this->_M_tree_ptr = __result;
2172 insert(size_t __p, size_t __n, _CharT __c)
2174 rope<_CharT,_Alloc> __r(__n,__c);
2179 insert(size_t __p, const _CharT* __i, size_t __n)
2181 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2182 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2184 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n));
2185 // _S_ destr_concat_char_iter should be safe here.
2186 // But as it stands it's probably not a win, since __left
2187 // is likely to have additional references.
2188 _RopeRep* __result = _S_concat(__left_result, __right);
2189 _S_unref(this->_M_tree_ptr);
2190 this->_M_tree_ptr = __result;
2194 insert(size_t __p, const _CharT* __c_string)
2195 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2198 insert(size_t __p, _CharT __c)
2199 { insert(__p, &__c, 1); }
2204 _CharT __c = _CharT();
2205 insert(__p, &__c, 1);
2209 insert(size_t __p, const _CharT* __i, const _CharT* __j)
2216 insert(size_t __p, const const_iterator& __i,
2217 const const_iterator& __j)
2224 insert(size_t __p, const iterator& __i,
2225 const iterator& __j)
2231 // (position, length) versions of replace operations:
2234 replace(size_t __p, size_t __n, const rope& __r)
2236 _RopeRep* __result =
2237 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2238 _S_unref(this->_M_tree_ptr);
2239 this->_M_tree_ptr = __result;
2243 replace(size_t __p, size_t __n,
2244 const _CharT* __i, size_t __i_len)
2246 rope __r(__i, __i_len);
2247 replace(__p, __n, __r);
2251 replace(size_t __p, size_t __n, _CharT __c)
2254 replace(__p, __n, __r);
2258 replace(size_t __p, size_t __n, const _CharT* __c_string)
2260 rope __r(__c_string);
2261 replace(__p, __n, __r);
2265 replace(size_t __p, size_t __n,
2266 const _CharT* __i, const _CharT* __j)
2269 replace(__p, __n, __r);
2273 replace(size_t __p, size_t __n,
2274 const const_iterator& __i, const const_iterator& __j)
2277 replace(__p, __n, __r);
2281 replace(size_t __p, size_t __n,
2282 const iterator& __i, const iterator& __j)
2285 replace(__p, __n, __r);
2288 // Single character variants:
2290 replace(size_t __p, _CharT __c)
2292 iterator __i(this, __p);
2297 replace(size_t __p, const rope& __r)
2298 { replace(__p, 1, __r); }
2301 replace(size_t __p, const _CharT* __i, size_t __i_len)
2302 { replace(__p, 1, __i, __i_len); }
2305 replace(size_t __p, const _CharT* __c_string)
2306 { replace(__p, 1, __c_string); }
2309 replace(size_t __p, const _CharT* __i, const _CharT* __j)
2310 { replace(__p, 1, __i, __j); }
2313 replace(size_t __p, const const_iterator& __i,
2314 const const_iterator& __j)
2315 { replace(__p, 1, __i, __j); }
2318 replace(size_t __p, const iterator& __i,
2319 const iterator& __j)
2320 { replace(__p, 1, __i, __j); }
2322 // Erase, (position, size) variant.
2324 erase(size_t __p, size_t __n)
2326 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2328 _S_unref(this->_M_tree_ptr);
2329 this->_M_tree_ptr = __result;
2332 // Erase, single character
2335 { erase(__p, __p + 1); }
2337 // Insert, iterator variants.
2339 insert(const iterator& __p, const rope& __r)
2341 insert(__p.index(), __r);
2346 insert(const iterator& __p, size_t __n, _CharT __c)
2348 insert(__p.index(), __n, __c);
2352 iterator insert(const iterator& __p, _CharT __c)
2354 insert(__p.index(), __c);
2359 insert(const iterator& __p )
2361 insert(__p.index());
2366 insert(const iterator& __p, const _CharT* c_string)
2368 insert(__p.index(), c_string);
2373 insert(const iterator& __p, const _CharT* __i, size_t __n)
2375 insert(__p.index(), __i, __n);
2380 insert(const iterator& __p, const _CharT* __i,
2383 insert(__p.index(), __i, __j);
2388 insert(const iterator& __p,
2389 const const_iterator& __i, const const_iterator& __j)
2391 insert(__p.index(), __i, __j);
2396 insert(const iterator& __p,
2397 const iterator& __i, const iterator& __j)
2399 insert(__p.index(), __i, __j);
2403 // Replace, range variants.
2405 replace(const iterator& __p, const iterator& __q, const rope& __r)
2406 { replace(__p.index(), __q.index() - __p.index(), __r); }
2409 replace(const iterator& __p, const iterator& __q, _CharT __c)
2410 { replace(__p.index(), __q.index() - __p.index(), __c); }
2413 replace(const iterator& __p, const iterator& __q,
2414 const _CharT* __c_string)
2415 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2418 replace(const iterator& __p, const iterator& __q,
2419 const _CharT* __i, size_t __n)
2420 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2423 replace(const iterator& __p, const iterator& __q,
2424 const _CharT* __i, const _CharT* __j)
2425 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2428 replace(const iterator& __p, const iterator& __q,
2429 const const_iterator& __i, const const_iterator& __j)
2430 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2433 replace(const iterator& __p, const iterator& __q,
2434 const iterator& __i, const iterator& __j)
2435 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2437 // Replace, iterator variants.
2439 replace(const iterator& __p, const rope& __r)
2440 { replace(__p.index(), __r); }
2443 replace(const iterator& __p, _CharT __c)
2444 { replace(__p.index(), __c); }
2447 replace(const iterator& __p, const _CharT* __c_string)
2448 { replace(__p.index(), __c_string); }
2451 replace(const iterator& __p, const _CharT* __i, size_t __n)
2452 { replace(__p.index(), __i, __n); }
2455 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2456 { replace(__p.index(), __i, __j); }
2459 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2460 { replace(__p.index(), __i, __j); }
2463 replace(const iterator& __p, iterator __i, iterator __j)
2464 { replace(__p.index(), __i, __j); }
2466 // Iterator and range variants of erase
2468 erase(const iterator& __p, const iterator& __q)
2470 size_t __p_index = __p.index();
2471 erase(__p_index, __q.index() - __p_index);
2472 return iterator(this, __p_index);
2476 erase(const iterator& __p)
2478 size_t __p_index = __p.index();
2479 erase(__p_index, 1);
2480 return iterator(this, __p_index);
2484 substr(size_t __start, size_t __len = 1) const
2486 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2492 substr(iterator __start, iterator __end) const
2494 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2500 substr(iterator __start) const
2502 size_t __pos = __start.index();
2503 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2508 substr(const_iterator __start, const_iterator __end) const
2510 // This might eventually take advantage of the cache in the
2512 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2517 rope<_CharT, _Alloc>
2518 substr(const_iterator __start)
2520 size_t __pos = __start.index();
2521 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2525 static const size_type npos;
2527 size_type find(_CharT __c, size_type __pos = 0) const;
2530 find(const _CharT* __s, size_type __pos = 0) const
2532 size_type __result_pos;
2533 const_iterator __result =
2534 std::search(const_begin() + __pos, const_end(),
2535 __s, __s + _S_char_ptr_len(__s));
2536 __result_pos = __result.index();
2537 #ifndef __STL_OLD_ROPE_SEMANTICS
2538 if (__result_pos == size())
2539 __result_pos = npos;
2541 return __result_pos;
2546 { return(iterator(this, 0)); }
2550 { return(iterator(this, size())); }
2552 typedef reverse_iterator<iterator> reverse_iterator;
2556 { return reverse_iterator(mutable_end()); }
2560 { return reverse_iterator(mutable_begin()); }
2563 mutable_reference_at(size_type __pos)
2564 { return reference(this, __pos); }
2568 operator[] (size_type __pos)
2569 { return _char_ref_proxy(this, __pos); }
2574 // if (__pos >= size()) throw out_of_range; // XXX
2575 return (*this)[__pos];
2578 void resize(size_type __n, _CharT __c) { }
2579 void resize(size_type __n) { }
2580 void reserve(size_type __res_arg = 0) { }
2584 { return max_size(); }
2586 // Stuff below this line is dangerous because it's error prone.
2587 // I would really like to get rid of it.
2588 // copy function with funny arg ordering.
2590 copy(_CharT* __buffer, size_type __n,
2591 size_type __pos = 0) const
2592 { return copy(__pos, __n, __buffer); }
2596 { return mutable_end(); }
2600 { return mutable_begin(); }
2604 { return mutable_rend(); }
2608 { return mutable_rbegin(); }
2613 { return const_end(); }
2617 { return const_begin(); }
2619 const_reverse_iterator
2621 { return const_rend(); }
2623 const_reverse_iterator
2625 { return const_rbegin(); }
2630 template <class _CharT, class _Alloc>
2631 const typename rope<_CharT, _Alloc>::size_type
2632 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2634 template <class _CharT, class _Alloc>
2635 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2636 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2637 { return (__x._M_current_pos == __y._M_current_pos
2638 && __x._M_root == __y._M_root); }
2640 template <class _CharT, class _Alloc>
2641 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2642 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2643 { return (__x._M_current_pos < __y._M_current_pos); }
2645 template <class _CharT, class _Alloc>
2646 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2647 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2648 { return !(__x == __y); }
2650 template <class _CharT, class _Alloc>
2651 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2652 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2653 { return __y < __x; }
2655 template <class _CharT, class _Alloc>
2657 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2658 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2659 { return !(__y < __x); }
2661 template <class _CharT, class _Alloc>
2663 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2664 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2665 { return !(__x < __y); }
2667 template <class _CharT, class _Alloc>
2669 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2670 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2671 { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
2673 template <class _CharT, class _Alloc>
2674 inline _Rope_const_iterator<_CharT, _Alloc>
2675 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2676 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2677 __x._M_current_pos - __n); }
2679 template <class _CharT, class _Alloc>
2680 inline _Rope_const_iterator<_CharT, _Alloc>
2681 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2682 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2683 __x._M_current_pos + __n); }
2685 template <class _CharT, class _Alloc>
2686 inline _Rope_const_iterator<_CharT, _Alloc>
2687 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT, _Alloc>& __x)
2688 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2689 __x._M_current_pos + __n); }
2691 template <class _CharT, class _Alloc>
2693 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2694 const _Rope_iterator<_CharT, _Alloc>& __y)
2695 {return (__x._M_current_pos == __y._M_current_pos
2696 && __x._M_root_rope == __y._M_root_rope); }
2698 template <class _CharT, class _Alloc>
2700 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2701 const _Rope_iterator<_CharT, _Alloc>& __y)
2702 { return (__x._M_current_pos < __y._M_current_pos); }
2704 template <class _CharT, class _Alloc>
2706 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2707 const _Rope_iterator<_CharT, _Alloc>& __y)
2708 { return !(__x == __y); }
2710 template <class _CharT, class _Alloc>
2712 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2713 const _Rope_iterator<_CharT, _Alloc>& __y)
2714 { return __y < __x; }
2716 template <class _CharT, class _Alloc>
2718 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2719 const _Rope_iterator<_CharT, _Alloc>& __y)
2720 { return !(__y < __x); }
2722 template <class _CharT, class _Alloc>
2724 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2725 const _Rope_iterator<_CharT, _Alloc>& __y)
2726 { return !(__x < __y); }
2728 template <class _CharT, class _Alloc>
2730 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2731 const _Rope_iterator<_CharT, _Alloc>& __y)
2732 { return ((ptrdiff_t)__x._M_current_pos
2733 - (ptrdiff_t)__y._M_current_pos); }
2735 template <class _CharT, class _Alloc>
2736 inline _Rope_iterator<_CharT, _Alloc>
2737 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2739 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2740 __x._M_current_pos - __n); }
2742 template <class _CharT, class _Alloc>
2743 inline _Rope_iterator<_CharT, _Alloc>
2744 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2745 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2746 __x._M_current_pos + __n); }
2748 template <class _CharT, class _Alloc>
2749 inline _Rope_iterator<_CharT, _Alloc>
2750 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2751 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2752 __x._M_current_pos + __n); }
2754 template <class _CharT, class _Alloc>
2755 inline rope<_CharT, _Alloc>
2756 operator+(const rope<_CharT, _Alloc>& __left,
2757 const rope<_CharT, _Alloc>& __right)
2759 // Inlining this should make it possible to keep __left and
2760 // __right in registers.
2761 typedef rope<_CharT, _Alloc> rope_type;
2762 return rope_type(rope_type::_S_concat(__left._M_tree_ptr,
2763 __right._M_tree_ptr));
2766 template <class _CharT, class _Alloc>
2767 inline rope<_CharT, _Alloc>&
2768 operator+=(rope<_CharT, _Alloc>& __left,
2769 const rope<_CharT, _Alloc>& __right)
2771 __left.append(__right);
2775 template <class _CharT, class _Alloc>
2776 inline rope<_CharT, _Alloc>
2777 operator+(const rope<_CharT, _Alloc>& __left,
2778 const _CharT* __right)
2780 typedef rope<_CharT, _Alloc> rope_type;
2781 size_t __rlen = rope_type::_S_char_ptr_len(__right);
2782 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2786 template <class _CharT, class _Alloc>
2787 inline rope<_CharT, _Alloc>&
2788 operator+=(rope<_CharT, _Alloc>& __left,
2789 const _CharT* __right)
2791 __left.append(__right);
2795 template <class _CharT, class _Alloc>
2796 inline rope<_CharT, _Alloc>
2797 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2799 typedef rope<_CharT, _Alloc> rope_type;
2800 return rope_type(rope_type::_S_concat_char_iter(__left._M_tree_ptr,
2804 template <class _CharT, class _Alloc>
2805 inline rope<_CharT, _Alloc>&
2806 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2808 __left.append(__right);
2812 template <class _CharT, class _Alloc>
2814 operator<(const rope<_CharT, _Alloc>& __left,
2815 const rope<_CharT, _Alloc>& __right)
2816 { return __left.compare(__right) < 0; }
2818 template <class _CharT, class _Alloc>
2820 operator==(const rope<_CharT, _Alloc>& __left,
2821 const rope<_CharT, _Alloc>& __right)
2822 { return __left.compare(__right) == 0; }
2824 template <class _CharT, class _Alloc>
2826 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2827 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2828 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2830 template <class _CharT, class _Alloc>
2832 operator!=(const rope<_CharT, _Alloc>& __x,
2833 const rope<_CharT, _Alloc>& __y)
2834 { return !(__x == __y); }
2836 template <class _CharT, class _Alloc>
2838 operator>(const rope<_CharT, _Alloc>& __x,
2839 const rope<_CharT, _Alloc>& __y)
2840 { return __y < __x; }
2842 template <class _CharT, class _Alloc>
2844 operator<=(const rope<_CharT, _Alloc>& __x,
2845 const rope<_CharT, _Alloc>& __y)
2846 { return !(__y < __x); }
2848 template <class _CharT, class _Alloc>
2850 operator>=(const rope<_CharT, _Alloc>& __x,
2851 const rope<_CharT, _Alloc>& __y)
2852 { return !(__x < __y); }
2854 template <class _CharT, class _Alloc>
2856 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2857 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2858 { return !(__x == __y); }
2860 template<class _CharT, class _Traits, class _Alloc>
2861 std::basic_ostream<_CharT, _Traits>&
2862 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2863 const rope<_CharT, _Alloc>& __r);
2865 typedef rope<char> crope;
2866 typedef rope<wchar_t> wrope;
2868 inline crope::reference
2869 __mutable_reference_at(crope& __c, size_t __i)
2870 { return __c.mutable_reference_at(__i); }
2872 inline wrope::reference
2873 __mutable_reference_at(wrope& __c, size_t __i)
2874 { return __c.mutable_reference_at(__i); }
2876 template <class _CharT, class _Alloc>
2878 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2881 // Hash functions should probably be revisited later:
2886 operator()(const crope& __str) const
2888 size_t __size = __str.size();
2891 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2900 operator()(const wrope& __str) const
2902 size_t __size = __str.size();
2905 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2909 _GLIBCXX_END_NAMESPACE
2911 # include <ext/ropeimpl.h>