1 // SGI's rope class -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 * This file is a GNU extension to the Standard C++ Library (possibly
45 * containing extensions from the HP/SGI STL subset). You should only
46 * include this header if you are using GCC 3 or later.
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
76 using std::reverse_iterator;
79 // The _S_eos function is used for those functions that
80 // convert to/from C-like strings to detect the end of the string.
82 // The end-of-C-string character.
83 // This is what the draft standard says it should be.
84 template <class _CharT>
85 inline _CharT _S_eos(_CharT*) { return _CharT(); }
87 // Test for basic character types.
88 // For basic character types leaves having a trailing eos.
89 template <class _CharT>
90 inline bool _S_is_basic_char_type(_CharT*) { return false; }
91 template <class _CharT>
92 inline bool _S_is_one_byte_char_type(_CharT*) { return false; }
94 inline bool _S_is_basic_char_type(char*) { return true; }
95 inline bool _S_is_one_byte_char_type(char*) { return true; }
96 inline bool _S_is_basic_char_type(wchar_t*) { return true; }
98 // Store an eos iff _CharT is a basic character type.
99 // Do not reference _S_eos if it isn't.
100 template <class _CharT>
101 inline void _S_cond_store_eos(_CharT&) {}
103 inline void _S_cond_store_eos(char& __c) { __c = 0; }
104 inline void _S_cond_store_eos(wchar_t& __c) { __c = 0; }
106 // char_producers are logically functions that generate a section of
107 // a string. These can be convereted to ropes. The resulting rope
108 // invokes the char_producer on demand. This allows, for example,
109 // files to be viewed as ropes without reading the entire file.
110 template <class _CharT>
111 class char_producer {
113 virtual ~char_producer() {};
114 virtual void operator()(size_t __start_pos, size_t __len,
115 _CharT* __buffer) = 0;
116 // Buffer should really be an arbitrary output iterator.
117 // That way we could flatten directly into an ostream, etc.
118 // This is thoroughly impossible, since iterator types don't
119 // have runtime descriptions.
124 // Sequence must provide an append operation that appends an
125 // array to the sequence. Sequence buffers are useful only if
126 // appending an entire array is cheaper than appending element by element.
127 // This is true for many string representations.
128 // This should perhaps inherit from ostream<sequence::value_type>
129 // and be implemented correspondingly, so that they can be used
130 // for formatted. For the sake of portability, we don't do this yet.
132 // For now, sequence buffers behave as output iterators. But they also
133 // behave a little like basic_ostringstream<sequence::value_type> and a
134 // little like containers.
136 template<class _Sequence, size_t _Buf_sz = 100>
137 class sequence_buffer : public iterator<std::output_iterator_tag,void,void,void,void>
140 typedef typename _Sequence::value_type value_type;
142 _Sequence* _M_prefix;
143 value_type _M_buffer[_Buf_sz];
147 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
150 ~sequence_buffer() { flush(); }
151 sequence_buffer() : _M_prefix(0), _M_buf_count(0) {}
152 sequence_buffer(const sequence_buffer& __x) {
153 _M_prefix = __x._M_prefix;
154 _M_buf_count = __x._M_buf_count;
155 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
157 sequence_buffer(sequence_buffer& __x) {
159 _M_prefix = __x._M_prefix;
162 sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) {}
163 sequence_buffer& operator= (sequence_buffer& __x) {
165 _M_prefix = __x._M_prefix;
169 sequence_buffer& operator= (const sequence_buffer& __x) {
170 _M_prefix = __x._M_prefix;
171 _M_buf_count = __x._M_buf_count;
172 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
175 void push_back(value_type __x)
177 if (_M_buf_count < _Buf_sz) {
178 _M_buffer[_M_buf_count] = __x;
186 void append(value_type* __s, size_t __len)
188 if (__len + _M_buf_count <= _Buf_sz) {
189 size_t __i = _M_buf_count;
190 for (size_t __j = 0; __j < __len; __i++, __j++) {
191 _M_buffer[__i] = __s[__j];
193 _M_buf_count += __len;
194 } else if (0 == _M_buf_count) {
195 _M_prefix->append(__s, __s + __len);
201 sequence_buffer& write(value_type* __s, size_t __len)
206 sequence_buffer& put(value_type __x)
211 sequence_buffer& operator=(const value_type& __rhs)
216 sequence_buffer& operator*() { return *this; }
217 sequence_buffer& operator++() { return *this; }
218 sequence_buffer operator++(int) { return *this; }
221 // The following should be treated as private, at least for now.
222 template<class _CharT>
223 class _Rope_char_consumer {
225 // If we had member templates, these should not be virtual.
226 // For now we need to use run-time parametrization where
227 // compile-time would do. Hence this should all be private
229 // The symmetry with char_producer is accidental and temporary.
230 virtual ~_Rope_char_consumer() {};
231 virtual bool operator()(const _CharT* __buffer, size_t __len) = 0;
234 // First a lot of forward declarations. The standard seems to require
235 // much stricter "declaration before use" than many of the implementations
237 template<class _CharT, class _Alloc = allocator<_CharT> > class rope;
238 template<class _CharT, class _Alloc> struct _Rope_RopeConcatenation;
239 template<class _CharT, class _Alloc> struct _Rope_RopeLeaf;
240 template<class _CharT, class _Alloc> struct _Rope_RopeFunction;
241 template<class _CharT, class _Alloc> struct _Rope_RopeSubstring;
242 template<class _CharT, class _Alloc> class _Rope_iterator;
243 template<class _CharT, class _Alloc> class _Rope_const_iterator;
244 template<class _CharT, class _Alloc> class _Rope_char_ref_proxy;
245 template<class _CharT, class _Alloc> class _Rope_char_ptr_proxy;
247 template<class _CharT, class _Alloc>
248 bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
249 const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y);
251 template<class _CharT, class _Alloc>
252 _Rope_const_iterator<_CharT,_Alloc> operator-
253 (const _Rope_const_iterator<_CharT,_Alloc>& __x,
256 template<class _CharT, class _Alloc>
257 _Rope_const_iterator<_CharT,_Alloc> operator+
258 (const _Rope_const_iterator<_CharT,_Alloc>& __x,
261 template<class _CharT, class _Alloc>
262 _Rope_const_iterator<_CharT,_Alloc> operator+
264 const _Rope_const_iterator<_CharT,_Alloc>& __x);
266 template<class _CharT, class _Alloc>
268 (const _Rope_const_iterator<_CharT,_Alloc>& __x,
269 const _Rope_const_iterator<_CharT,_Alloc>& __y);
271 template<class _CharT, class _Alloc>
273 (const _Rope_const_iterator<_CharT,_Alloc>& __x,
274 const _Rope_const_iterator<_CharT,_Alloc>& __y);
276 template<class _CharT, class _Alloc>
278 (const _Rope_const_iterator<_CharT,_Alloc>& __x,
279 const _Rope_const_iterator<_CharT,_Alloc>& __y);
281 template<class _CharT, class _Alloc>
282 _Rope_iterator<_CharT,_Alloc> operator-
283 (const _Rope_iterator<_CharT,_Alloc>& __x,
286 template<class _CharT, class _Alloc>
287 _Rope_iterator<_CharT,_Alloc> operator+
288 (const _Rope_iterator<_CharT,_Alloc>& __x,
291 template<class _CharT, class _Alloc>
292 _Rope_iterator<_CharT,_Alloc> operator+
294 const _Rope_iterator<_CharT,_Alloc>& __x);
296 template<class _CharT, class _Alloc>
298 (const _Rope_iterator<_CharT,_Alloc>& __x,
299 const _Rope_iterator<_CharT,_Alloc>& __y);
301 template<class _CharT, class _Alloc>
303 (const _Rope_iterator<_CharT,_Alloc>& __x,
304 const _Rope_iterator<_CharT,_Alloc>& __y);
306 template<class _CharT, class _Alloc>
308 (const _Rope_iterator<_CharT,_Alloc>& __x,
309 const _Rope_iterator<_CharT,_Alloc>& __y);
311 template<class _CharT, class _Alloc>
312 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
313 const rope<_CharT,_Alloc>& __right);
315 template<class _CharT, class _Alloc>
316 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
317 const _CharT* __right);
319 template<class _CharT, class _Alloc>
320 rope<_CharT,_Alloc> operator+ (const rope<_CharT,_Alloc>& __left,
323 // Some helpers, so we can use power on ropes.
324 // See below for why this isn't local to the implementation.
326 // This uses a nonstandard refcount convention.
327 // The result has refcount 0.
328 template<class _CharT, class _Alloc>
329 struct _Rope_Concat_fn
330 : public std::binary_function<rope<_CharT,_Alloc>, rope<_CharT,_Alloc>,
331 rope<_CharT,_Alloc> > {
332 rope<_CharT,_Alloc> operator() (const rope<_CharT,_Alloc>& __x,
333 const rope<_CharT,_Alloc>& __y) {
338 template <class _CharT, class _Alloc>
341 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
343 return rope<_CharT,_Alloc>();
347 // Class _Refcount_Base provides a type, _RC_t, a data member,
348 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
349 // atomic preincrement/predecrement. The constructor initializes
351 struct _Refcount_Base
354 typedef size_t _RC_t;
356 // The data member _M_ref_count
357 volatile _RC_t _M_ref_count;
360 __gthread_mutex_t _M_ref_count_lock;
362 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
364 #ifdef __GTHREAD_MUTEX_INIT
365 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
366 _M_ref_count_lock = __tmp;
367 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
368 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
370 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
377 __gthread_mutex_lock(&_M_ref_count_lock);
379 __gthread_mutex_unlock(&_M_ref_count_lock);
385 __gthread_mutex_lock(&_M_ref_count_lock);
386 volatile _RC_t __tmp = --_M_ref_count;
387 __gthread_mutex_unlock(&_M_ref_count_lock);
393 // What follows should really be local to rope. Unfortunately,
394 // that doesn't work, since it makes it impossible to define generic
395 // equality on rope iterators. According to the draft standard, the
396 // template parameters for such an equality operator cannot be inferred
397 // from the occurrence of a member class as a parameter.
398 // (SGI compilers in fact allow this, but the __result wouldn't be
400 // Similarly, some of the static member functions are member functions
401 // only to avoid polluting the global namespace, and to circumvent
402 // restrictions on type inference for template functions.
406 // The internal data structure for representing a rope. This is
407 // private to the implementation. A rope is really just a pointer
410 // A few basic functions for manipulating this data structure
411 // are members of _RopeRep. Most of the more complex algorithms
412 // are implemented as rope members.
414 // Some of the static member functions of _RopeRep have identically
415 // named functions in rope that simply invoke the _RopeRep versions.
417 #define __ROPE_DEFINE_ALLOCS(__a) \
418 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
419 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
420 __ROPE_DEFINE_ALLOC(__C,_C) \
421 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
422 __ROPE_DEFINE_ALLOC(__L,_L) \
423 typedef _Rope_RopeFunction<_CharT,__a> __F; \
424 __ROPE_DEFINE_ALLOC(__F,_F) \
425 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
426 __ROPE_DEFINE_ALLOC(__S,_S)
428 // Internal rope nodes potentially store a copy of the allocator
429 // instance used to allocate them. This is mostly redundant.
430 // But the alternative would be to pass allocator instances around
431 // in some form to nearly all internal functions, since any pointer
432 // assignment may result in a zero reference count and thus require
435 #define __STATIC_IF_SGI_ALLOC /* not static */
437 template <class _CharT, class _Alloc>
438 struct _Rope_rep_base
441 typedef _Alloc allocator_type;
444 get_allocator() const { return *static_cast<const _Alloc*>(this); }
446 _Rope_rep_base(size_t __size, const allocator_type&)
451 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
453 _Alloc::template rebind<_Tp>::other __name##Alloc; \
454 static _Tp* __name##_allocate(size_t __n) \
455 { return __name##Alloc().allocate(__n); } \
456 static void __name##_deallocate(_Tp *__p, size_t __n) \
457 { __name##Alloc().deallocate(__p, __n); }
458 __ROPE_DEFINE_ALLOCS(_Alloc)
459 # undef __ROPE_DEFINE_ALLOC
462 namespace _Rope_constants
464 enum { _S_max_rope_depth = 45 };
465 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
468 template<class _CharT, class _Alloc>
469 struct _Rope_RopeRep : public _Rope_rep_base<_CharT,_Alloc>
475 _Rope_constants::_Tag _M_tag:8;
476 bool _M_is_balanced:8;
477 unsigned char _M_depth;
478 __GC_CONST _CharT* _M_c_string;
479 __gthread_mutex_t _M_c_string_lock;
480 /* Flattened version of string, if needed. */
482 /* If it's not 0, then the memory is owned */
484 /* In the case of a leaf, this may point to */
485 /* the same memory as the data field. */
486 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
488 using _Rope_rep_base<_CharT,_Alloc>::get_allocator;
489 _Rope_RopeRep(_Rope_constants::_Tag __t, int __d, bool __b, size_t __size,
491 : _Rope_rep_base<_CharT,_Alloc>(__size, __a),
495 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
496 #ifdef __GTHREAD_MUTEX_INIT
498 // Do not copy a POSIX/gthr mutex once in use. However, bits are bits.
499 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
500 _M_c_string_lock = __tmp;
503 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
508 static void _S_free_string(__GC_CONST _CharT*, size_t __len,
510 # define __STL_FREE_STRING(__s, __l, __a) _S_free_string(__s, __l, __a);
511 // Deallocate data section of a leaf.
512 // This shouldn't be a member function.
513 // But its hard to do anything else at the
514 // moment, because it's templatized w.r.t.
516 // Does nothing if __GC is defined.
518 void _M_free_c_string();
520 // Deallocate t. Assumes t is not 0.
521 void _M_unref_nonnil()
523 if (0 == _M_decr()) _M_free_tree();
529 static void _S_unref(_Rope_RopeRep* __t)
532 __t->_M_unref_nonnil();
535 static void _S_ref(_Rope_RopeRep* __t)
537 if (0 != __t) __t->_M_incr();
539 static void _S_free_if_unref(_Rope_RopeRep* __t)
541 if (0 != __t && 0 == __t->_M_ref_count) __t->_M_free_tree();
544 void _M_unref_nonnil() {}
545 void _M_ref_nonnil() {}
546 static void _S_unref(_Rope_RopeRep*) {}
547 static void _S_ref(_Rope_RopeRep*) {}
548 static void _S_free_if_unref(_Rope_RopeRep*) {}
552 operator=(const _Rope_RopeRep&);
554 _Rope_RopeRep(const _Rope_RopeRep&);
557 template<class _CharT, class _Alloc>
558 struct _Rope_RopeLeaf : public _Rope_RopeRep<_CharT,_Alloc> {
560 // Apparently needed by VC++
561 // The data fields of leaves are allocated with some
562 // extra space, to accommodate future growth and for basic
563 // character types, to hold a trailing eos character.
564 enum { _S_alloc_granularity = 8 };
565 static size_t _S_rounded_up_size(size_t __n) {
566 size_t __size_with_eos;
568 if (_S_is_basic_char_type((_CharT*)0)) {
569 __size_with_eos = __n + 1;
571 __size_with_eos = __n;
574 return __size_with_eos;
576 // Allow slop for in-place expansion.
577 return (__size_with_eos + _S_alloc_granularity-1)
578 &~ (_S_alloc_granularity-1);
581 __GC_CONST _CharT* _M_data; /* Not necessarily 0 terminated. */
582 /* The allocated size is */
583 /* _S_rounded_up_size(size), except */
584 /* in the GC case, in which it */
585 /* doesn't matter. */
586 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
588 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size, allocator_type __a)
589 : _Rope_RopeRep<_CharT,_Alloc>(_Rope_constants::_S_leaf, 0, true, __size, __a), _M_data(__d)
591 if (_S_is_basic_char_type((_CharT *)0)) {
592 // already eos terminated.
593 this->_M_c_string = __d;
596 // The constructor assumes that d has been allocated with
597 // the proper allocator and the properly padded size.
598 // In contrast, the destructor deallocates the data:
600 ~_Rope_RopeLeaf() throw() {
601 if (_M_data != this->_M_c_string) {
602 this->_M_free_c_string();
604 __STL_FREE_STRING(_M_data, this->_M_size, this->get_allocator());
609 operator=(const _Rope_RopeLeaf&);
611 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
614 template<class _CharT, class _Alloc>
615 struct _Rope_RopeConcatenation : public _Rope_RopeRep<_CharT,_Alloc> {
617 _Rope_RopeRep<_CharT,_Alloc>* _M_left;
618 _Rope_RopeRep<_CharT,_Alloc>* _M_right;
619 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
621 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT,_Alloc>* __l,
622 _Rope_RopeRep<_CharT,_Alloc>* __r,
625 : _Rope_RopeRep<_CharT,_Alloc>(_Rope_constants::_S_concat,
626 std::max(__l->_M_depth, __r->_M_depth) + 1,
628 __l->_M_size + __r->_M_size, __a),
629 _M_left(__l), _M_right(__r)
632 ~_Rope_RopeConcatenation() throw() {
633 this->_M_free_c_string();
634 _M_left->_M_unref_nonnil();
635 _M_right->_M_unref_nonnil();
639 _Rope_RopeConcatenation&
640 operator=(const _Rope_RopeConcatenation&);
642 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
645 template<class _CharT, class _Alloc>
646 struct _Rope_RopeFunction : public _Rope_RopeRep<_CharT,_Alloc> {
648 char_producer<_CharT>* _M_fn;
650 bool _M_delete_when_done; // Char_producer is owned by the
651 // rope and should be explicitly
652 // deleted when the rope becomes
655 // In the GC case, we either register the rope for
656 // finalization, or not. Thus the field is unnecessary;
657 // the information is stored in the collector data structures.
658 // We do need a finalization procedure to be invoked by the
660 static void _S_fn_finalization_proc(void * __tree, void *) {
661 delete ((_Rope_RopeFunction *)__tree) -> _M_fn;
664 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
666 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
667 bool __d, allocator_type __a)
668 : _Rope_RopeRep<_CharT,_Alloc>(_Rope_constants::_S_function,
669 0, true, __size, __a)
672 , _M_delete_when_done(__d)
677 GC_REGISTER_FINALIZER(
678 this, _Rope_RopeFunction::_S_fn_finalization_proc, 0, 0, 0);
683 ~_Rope_RopeFunction() throw() {
684 this->_M_free_c_string();
685 if (_M_delete_when_done) {
692 operator=(const _Rope_RopeFunction&);
694 _Rope_RopeFunction(const _Rope_RopeFunction&);
696 // Substring results are usually represented using just
697 // concatenation nodes. But in the case of very long flat ropes
698 // or ropes with a functional representation that isn't practical.
699 // In that case, we represent the __result as a special case of
700 // RopeFunction, whose char_producer points back to the rope itself.
701 // In all cases except repeated substring operations and
702 // deallocation, we treat the __result as a RopeFunction.
703 template<class _CharT, class _Alloc>
704 struct _Rope_RopeSubstring : public _Rope_RopeFunction<_CharT,_Alloc>,
705 public char_producer<_CharT> {
707 // XXX this whole class should be rewritten.
708 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
710 virtual void operator()(size_t __start_pos, size_t __req_len,
712 switch(_M_base->_M_tag) {
713 case _Rope_constants::_S_function:
714 case _Rope_constants::_S_substringfn:
716 char_producer<_CharT>* __fn =
717 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
718 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
721 case _Rope_constants::_S_leaf:
723 __GC_CONST _CharT* __s =
724 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
725 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
733 typedef typename _Rope_rep_base<_CharT,_Alloc>::allocator_type
735 _Rope_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
736 size_t __l, allocator_type __a)
737 : _Rope_RopeFunction<_CharT,_Alloc>(this, __l, false, __a),
738 char_producer<_CharT>(),
743 _M_base->_M_ref_nonnil();
745 this->_M_tag = _Rope_constants::_S_substringfn;
747 virtual ~_Rope_RopeSubstring() throw()
750 _M_base->_M_unref_nonnil();
751 // _M_free_c_string(); -- done by parent class
757 // Self-destructing pointers to Rope_rep.
758 // These are not conventional smart pointers. Their
759 // only purpose in life is to ensure that unref is called
760 // on the pointer either at normal exit or if an exception
761 // is raised. It is the caller's responsibility to
762 // adjust reference counts when these pointers are initialized
763 // or assigned to. (This convention significantly reduces
764 // the number of potentially expensive reference count
767 template<class _CharT, class _Alloc>
768 struct _Rope_self_destruct_ptr {
769 _Rope_RopeRep<_CharT,_Alloc>* _M_ptr;
770 ~_Rope_self_destruct_ptr()
771 { _Rope_RopeRep<_CharT,_Alloc>::_S_unref(_M_ptr); }
773 _Rope_self_destruct_ptr() : _M_ptr(0) {};
775 _Rope_self_destruct_ptr() {};
777 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT,_Alloc>* __p) : _M_ptr(__p) {}
778 _Rope_RopeRep<_CharT,_Alloc>& operator*() { return *_M_ptr; }
779 _Rope_RopeRep<_CharT,_Alloc>* operator->() { return _M_ptr; }
780 operator _Rope_RopeRep<_CharT,_Alloc>*() { return _M_ptr; }
781 _Rope_self_destruct_ptr& operator= (_Rope_RopeRep<_CharT,_Alloc>* __x)
782 { _M_ptr = __x; return *this; }
786 // Dereferencing a nonconst iterator has to return something
787 // that behaves almost like a reference. It's not possible to
788 // return an actual reference since assignment requires extra
789 // work. And we would get into the same problems as with the
790 // CD2 version of basic_string.
791 template<class _CharT, class _Alloc>
792 class _Rope_char_ref_proxy {
793 friend class rope<_CharT,_Alloc>;
794 friend class _Rope_iterator<_CharT,_Alloc>;
795 friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
797 typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
799 typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
801 typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
802 typedef rope<_CharT,_Alloc> _My_rope;
805 bool _M_current_valid;
806 _My_rope* _M_root; // The whole rope.
808 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
809 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) {}
811 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
812 : _M_pos(__x._M_pos), _M_current(__x._M_current), _M_current_valid(false),
813 _M_root(__x._M_root) {}
815 // Don't preserve cache if the reference can outlive the
816 // expression. We claim that's not possible without calling
817 // a copy constructor or generating reference to a proxy
818 // reference. We declare the latter to have undefined semantics.
819 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
820 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
821 inline operator _CharT () const;
822 _Rope_char_ref_proxy& operator= (_CharT __c);
823 _Rope_char_ptr_proxy<_CharT,_Alloc> operator& () const;
824 _Rope_char_ref_proxy& operator= (const _Rope_char_ref_proxy& __c) {
825 return operator=((_CharT)__c);
829 template<class _CharT, class __Alloc>
830 inline void swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
831 _Rope_char_ref_proxy <_CharT, __Alloc > __b) {
837 template<class _CharT, class _Alloc>
838 class _Rope_char_ptr_proxy {
839 // XXX this class should be rewritten.
840 friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
842 rope<_CharT,_Alloc>* _M_root; // The whole rope.
844 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
845 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
846 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
847 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
848 _Rope_char_ptr_proxy() {}
849 _Rope_char_ptr_proxy(_CharT* __x) : _M_root(0), _M_pos(0) {
851 _Rope_char_ptr_proxy&
852 operator= (const _Rope_char_ptr_proxy& __x) {
854 _M_root = __x._M_root;
857 template<class _CharT2, class _Alloc2>
858 friend bool operator== (const _Rope_char_ptr_proxy<_CharT2,_Alloc2>& __x,
859 const _Rope_char_ptr_proxy<_CharT2,_Alloc2>& __y);
860 _Rope_char_ref_proxy<_CharT,_Alloc> operator*() const {
861 return _Rope_char_ref_proxy<_CharT,_Alloc>(_M_root, _M_pos);
867 // Unlike in the C version, we cache only part of the stack
868 // for rope iterators, since they must be efficiently copyable.
869 // When we run out of cache, we have to reconstruct the iterator
871 // Pointers from iterators are not included in reference counts.
872 // Iterators are assumed to be thread private. Ropes can
875 template<class _CharT, class _Alloc>
876 class _Rope_iterator_base
877 : public iterator<std::random_access_iterator_tag, _CharT>
879 friend class rope<_CharT,_Alloc>;
881 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
882 typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
883 // Borland doesn't want this to be protected.
885 enum { _S_path_cache_len = 4 }; // Must be <= 9.
886 enum { _S_iterator_buf_len = 15 };
887 size_t _M_current_pos;
888 _RopeRep* _M_root; // The whole rope.
889 size_t _M_leaf_pos; // Starting position for current leaf
890 __GC_CONST _CharT* _M_buf_start;
892 // containing current char.
893 __GC_CONST _CharT* _M_buf_ptr;
894 // Pointer to current char in buffer.
895 // != 0 ==> buffer valid.
896 __GC_CONST _CharT* _M_buf_end;
897 // One past __last valid char in buffer.
898 // What follows is the path cache. We go out of our
899 // way to make this compact.
900 // Path_end contains the bottom section of the path from
901 // the root to the current leaf.
902 const _RopeRep* _M_path_end[_S_path_cache_len];
903 int _M_leaf_index; // Last valid __pos in path_end;
904 // _M_path_end[0] ... _M_path_end[leaf_index-1]
905 // point to concatenation nodes.
906 unsigned char _M_path_directions;
907 // (path_directions >> __i) & 1 is 1
908 // iff we got from _M_path_end[leaf_index - __i - 1]
909 // to _M_path_end[leaf_index - __i] by going to the
910 // __right. Assumes path_cache_len <= 9.
911 _CharT _M_tmp_buf[_S_iterator_buf_len];
912 // Short buffer for surrounding chars.
913 // This is useful primarily for
914 // RopeFunctions. We put the buffer
915 // here to avoid locking in the
916 // multithreaded case.
917 // The cached path is generally assumed to be valid
918 // only if the buffer is valid.
919 static void _S_setbuf(_Rope_iterator_base& __x);
920 // Set buffer contents given
922 static void _S_setcache(_Rope_iterator_base& __x);
923 // Set buffer contents and
925 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
926 // As above, but assumes path
927 // cache is valid for previous posn.
928 _Rope_iterator_base() {}
929 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
930 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) {}
931 void _M_incr(size_t __n);
932 void _M_decr(size_t __n);
934 size_t index() const { return _M_current_pos; }
935 _Rope_iterator_base(const _Rope_iterator_base& __x) {
936 if (0 != __x._M_buf_ptr) {
939 _M_current_pos = __x._M_current_pos;
940 _M_root = __x._M_root;
946 template<class _CharT, class _Alloc> class _Rope_iterator;
948 template<class _CharT, class _Alloc>
949 class _Rope_const_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
950 friend class rope<_CharT,_Alloc>;
952 typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
953 // The one from the base class may not be directly visible.
954 _Rope_const_iterator(const _RopeRep* __root, size_t __pos):
955 _Rope_iterator_base<_CharT,_Alloc>(
956 const_cast<_RopeRep*>(__root), __pos)
957 // Only nonconst iterators modify root ref count
960 typedef _CharT reference; // Really a value. Returning a reference
961 // Would be a mess, since it would have
962 // to be included in refcount.
963 typedef const _CharT* pointer;
966 _Rope_const_iterator() {};
967 _Rope_const_iterator(const _Rope_const_iterator& __x) :
968 _Rope_iterator_base<_CharT,_Alloc>(__x) { }
969 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
970 _Rope_const_iterator(const rope<_CharT,_Alloc>& __r, size_t __pos) :
971 _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) {}
972 _Rope_const_iterator& operator= (const _Rope_const_iterator& __x) {
973 if (0 != __x._M_buf_ptr) {
974 *(static_cast<_Rope_iterator_base<_CharT,_Alloc>*>(this)) = __x;
976 this->_M_current_pos = __x._M_current_pos;
977 this->_M_root = __x._M_root;
978 this->_M_buf_ptr = 0;
982 reference operator*() {
983 if (0 == this->_M_buf_ptr) _S_setcache(*this);
984 return *this->_M_buf_ptr;
986 _Rope_const_iterator& operator++() {
987 __GC_CONST _CharT* __next;
988 if (0 != this->_M_buf_ptr
989 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end) {
990 this->_M_buf_ptr = __next;
991 ++this->_M_current_pos;
997 _Rope_const_iterator& operator+=(ptrdiff_t __n) {
1001 this->_M_decr(-__n);
1005 _Rope_const_iterator& operator--() {
1009 _Rope_const_iterator& operator-=(ptrdiff_t __n) {
1013 this->_M_incr(-__n);
1017 _Rope_const_iterator operator++(int) {
1018 size_t __old_pos = this->_M_current_pos;
1020 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1021 // This makes a subsequent dereference expensive.
1022 // Perhaps we should instead copy the iterator
1023 // if it has a valid cache?
1025 _Rope_const_iterator operator--(int) {
1026 size_t __old_pos = this->_M_current_pos;
1028 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1030 template<class _CharT2, class _Alloc2>
1031 friend _Rope_const_iterator<_CharT2,_Alloc2> operator-
1032 (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
1034 template<class _CharT2, class _Alloc2>
1035 friend _Rope_const_iterator<_CharT2,_Alloc2> operator+
1036 (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
1038 template<class _CharT2, class _Alloc2>
1039 friend _Rope_const_iterator<_CharT2,_Alloc2> operator+
1041 const _Rope_const_iterator<_CharT2,_Alloc2>& __x);
1042 reference operator[](size_t __n) {
1043 return rope<_CharT,_Alloc>::_S_fetch(this->_M_root,
1044 this->_M_current_pos + __n);
1047 template<class _CharT2, class _Alloc2>
1048 friend bool operator==
1049 (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
1050 const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
1051 template<class _CharT2, class _Alloc2>
1052 friend bool operator<
1053 (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
1054 const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
1055 template<class _CharT2, class _Alloc2>
1056 friend ptrdiff_t operator-
1057 (const _Rope_const_iterator<_CharT2,_Alloc2>& __x,
1058 const _Rope_const_iterator<_CharT2,_Alloc2>& __y);
1061 template<class _CharT, class _Alloc>
1062 class _Rope_iterator : public _Rope_iterator_base<_CharT,_Alloc> {
1063 friend class rope<_CharT,_Alloc>;
1065 typedef typename _Rope_iterator_base<_CharT,_Alloc>::_RopeRep _RopeRep;
1066 rope<_CharT,_Alloc>* _M_root_rope;
1067 // root is treated as a cached version of this,
1068 // and is used to detect changes to the underlying
1070 // Root is included in the reference count.
1071 // This is necessary so that we can detect changes reliably.
1072 // Unfortunately, it requires careful bookkeeping for the
1074 _Rope_iterator(rope<_CharT,_Alloc>* __r, size_t __pos)
1075 : _Rope_iterator_base<_CharT,_Alloc>(__r->_M_tree_ptr, __pos),
1077 { _RopeRep::_S_ref(this->_M_root);
1078 if (!(__r -> empty()))_S_setcache(*this); }
1082 typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
1083 typedef _Rope_char_ref_proxy<_CharT,_Alloc>* pointer;
1086 rope<_CharT,_Alloc>& container() { return *_M_root_rope; }
1088 this->_M_root = 0; // Needed for reference counting.
1090 _Rope_iterator(const _Rope_iterator& __x) :
1091 _Rope_iterator_base<_CharT,_Alloc>(__x) {
1092 _M_root_rope = __x._M_root_rope;
1093 _RopeRep::_S_ref(this->_M_root);
1095 _Rope_iterator(rope<_CharT,_Alloc>& __r, size_t __pos);
1097 _RopeRep::_S_unref(this->_M_root);
1099 _Rope_iterator& operator= (const _Rope_iterator& __x) {
1100 _RopeRep* __old = this->_M_root;
1102 _RopeRep::_S_ref(__x._M_root);
1103 if (0 != __x._M_buf_ptr) {
1104 _M_root_rope = __x._M_root_rope;
1105 *(static_cast<_Rope_iterator_base<_CharT,_Alloc>*>(this)) = __x;
1107 this->_M_current_pos = __x._M_current_pos;
1108 this->_M_root = __x._M_root;
1109 _M_root_rope = __x._M_root_rope;
1110 this->_M_buf_ptr = 0;
1112 _RopeRep::_S_unref(__old);
1115 reference operator*() {
1117 if (0 == this->_M_buf_ptr) {
1118 return _Rope_char_ref_proxy<_CharT,_Alloc>(
1119 _M_root_rope, this->_M_current_pos);
1121 return _Rope_char_ref_proxy<_CharT,_Alloc>(
1122 _M_root_rope, this->_M_current_pos, *this->_M_buf_ptr);
1125 _Rope_iterator& operator++() {
1129 _Rope_iterator& operator+=(ptrdiff_t __n) {
1133 this->_M_decr(-__n);
1137 _Rope_iterator& operator--() {
1141 _Rope_iterator& operator-=(ptrdiff_t __n) {
1145 this->_M_incr(-__n);
1149 _Rope_iterator operator++(int) {
1150 size_t __old_pos = this->_M_current_pos;
1152 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1154 _Rope_iterator operator--(int) {
1155 size_t __old_pos = this->_M_current_pos;
1157 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1159 reference operator[](ptrdiff_t __n) {
1160 return _Rope_char_ref_proxy<_CharT,_Alloc>(
1161 _M_root_rope, this->_M_current_pos + __n);
1164 template<class _CharT2, class _Alloc2>
1165 friend bool operator==
1166 (const _Rope_iterator<_CharT2,_Alloc2>& __x,
1167 const _Rope_iterator<_CharT2,_Alloc2>& __y);
1168 template<class _CharT2, class _Alloc2>
1169 friend bool operator<
1170 (const _Rope_iterator<_CharT2,_Alloc2>& __x,
1171 const _Rope_iterator<_CharT2,_Alloc2>& __y);
1172 template<class _CharT2, class _Alloc2>
1173 friend ptrdiff_t operator-
1174 (const _Rope_iterator<_CharT2,_Alloc2>& __x,
1175 const _Rope_iterator<_CharT2,_Alloc2>& __y);
1176 template<class _CharT2, class _Alloc2>
1177 friend _Rope_iterator<_CharT2,_Alloc2> operator-
1178 (const _Rope_iterator<_CharT2,_Alloc2>& __x,
1180 template<class _CharT2, class _Alloc2>
1181 friend _Rope_iterator<_CharT2,_Alloc2> operator+
1182 (const _Rope_iterator<_CharT2,_Alloc2>& __x,
1184 template<class _CharT2, class _Alloc2>
1185 friend _Rope_iterator<_CharT2,_Alloc2> operator+
1187 const _Rope_iterator<_CharT2,_Alloc2>& __x);
1191 template <class _CharT, class _Alloc>
1195 typedef _Alloc allocator_type;
1198 get_allocator() const { return *static_cast<const _Alloc*>(this); }
1200 typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
1201 // The one in _Base may not be visible due to template rules.
1203 _Rope_base(_RopeRep* __t, const allocator_type&)
1204 : _M_tree_ptr(__t) {}
1205 _Rope_base(const allocator_type&) {}
1207 // The only data member of a rope:
1208 _RopeRep *_M_tree_ptr;
1210 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1212 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1213 static _Tp* __name##_allocate(size_t __n) \
1214 { return __name##Alloc().allocate(__n); } \
1215 static void __name##_deallocate(_Tp *__p, size_t __n) \
1216 { __name##Alloc().deallocate(__p, __n); }
1217 __ROPE_DEFINE_ALLOCS(_Alloc)
1218 # undef __ROPE_DEFINE_ALLOC
1222 operator=(const _Rope_base&);
1224 _Rope_base(const _Rope_base&);
1229 * This is an SGI extension.
1230 * @ingroup SGIextensions
1233 template <class _CharT, class _Alloc>
1234 class rope : public _Rope_base<_CharT,_Alloc> {
1236 typedef _CharT value_type;
1237 typedef ptrdiff_t difference_type;
1238 typedef size_t size_type;
1239 typedef _CharT const_reference;
1240 typedef const _CharT* const_pointer;
1241 typedef _Rope_iterator<_CharT,_Alloc> iterator;
1242 typedef _Rope_const_iterator<_CharT,_Alloc> const_iterator;
1243 typedef _Rope_char_ref_proxy<_CharT,_Alloc> reference;
1244 typedef _Rope_char_ptr_proxy<_CharT,_Alloc> pointer;
1246 friend class _Rope_iterator<_CharT,_Alloc>;
1247 friend class _Rope_const_iterator<_CharT,_Alloc>;
1248 friend struct _Rope_RopeRep<_CharT,_Alloc>;
1249 friend class _Rope_iterator_base<_CharT,_Alloc>;
1250 friend class _Rope_char_ptr_proxy<_CharT,_Alloc>;
1251 friend class _Rope_char_ref_proxy<_CharT,_Alloc>;
1252 friend struct _Rope_RopeSubstring<_CharT,_Alloc>;
1255 typedef _Rope_base<_CharT,_Alloc> _Base;
1256 typedef typename _Base::allocator_type allocator_type;
1257 using _Base::_M_tree_ptr;
1258 using _Base::get_allocator;
1259 typedef __GC_CONST _CharT* _Cstrptr;
1261 static _CharT _S_empty_c_str[1];
1263 static bool _S_is0(_CharT __c) { return __c == _S_eos((_CharT*)0); }
1264 enum { _S_copy_max = 23 };
1265 // For strings shorter than _S_copy_max, we copy to
1268 typedef _Rope_RopeRep<_CharT,_Alloc> _RopeRep;
1269 typedef _Rope_RopeConcatenation<_CharT,_Alloc> _RopeConcatenation;
1270 typedef _Rope_RopeLeaf<_CharT,_Alloc> _RopeLeaf;
1271 typedef _Rope_RopeFunction<_CharT,_Alloc> _RopeFunction;
1272 typedef _Rope_RopeSubstring<_CharT,_Alloc> _RopeSubstring;
1274 // Retrieve a character at the indicated position.
1275 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1278 // Obtain a pointer to the character at the indicated position.
1279 // The pointer can be used to change the character.
1280 // If such a pointer cannot be produced, as is frequently the
1281 // case, 0 is returned instead.
1282 // (Returns nonzero only if all nodes in the path have a refcount
1284 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1287 static bool _S_apply_to_pieces(
1288 // should be template parameter
1289 _Rope_char_consumer<_CharT>& __c,
1290 const _RopeRep* __r,
1291 size_t __begin, size_t __end);
1292 // begin and end are assumed to be in range.
1295 static void _S_unref(_RopeRep* __t)
1297 _RopeRep::_S_unref(__t);
1299 static void _S_ref(_RopeRep* __t)
1301 _RopeRep::_S_ref(__t);
1304 static void _S_unref(_RopeRep*) {}
1305 static void _S_ref(_RopeRep*) {}
1310 typedef _Rope_RopeRep<_CharT,_Alloc>* _Self_destruct_ptr;
1312 typedef _Rope_self_destruct_ptr<_CharT,_Alloc> _Self_destruct_ptr;
1315 // _Result is counted in refcount.
1316 static _RopeRep* _S_substring(_RopeRep* __base,
1317 size_t __start, size_t __endp1);
1319 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1320 const _CharT* __iter, size_t __slen);
1321 // Concatenate rope and char ptr, copying __s.
1322 // Should really take an arbitrary iterator.
1323 // Result is counted in refcount.
1324 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1325 const _CharT* __iter, size_t __slen)
1326 // As above, but one reference to __r is about to be
1327 // destroyed. Thus the pieces may be recycled if all
1328 // relevant reference counts are 1.
1330 // We can't really do anything since refcounts are unavailable.
1331 { return _S_concat_char_iter(__r, __iter, __slen); }
1336 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1337 // General concatenation on _RopeRep. _Result
1338 // has refcount of 1. Adjusts argument refcounts.
1341 void apply_to_pieces( size_t __begin, size_t __end,
1342 _Rope_char_consumer<_CharT>& __c) const {
1343 _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end);
1349 static size_t _S_rounded_up_size(size_t __n) {
1350 return _RopeLeaf::_S_rounded_up_size(__n);
1353 static size_t _S_allocated_capacity(size_t __n) {
1354 if (_S_is_basic_char_type((_CharT*)0)) {
1355 return _S_rounded_up_size(__n) - 1;
1357 return _S_rounded_up_size(__n);
1361 // Allocate and construct a RopeLeaf using the supplied allocator
1362 // Takes ownership of s instead of copying.
1363 static _RopeLeaf* _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1364 size_t __size, allocator_type __a)
1366 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1367 return new(__space) _RopeLeaf(__s, __size, __a);
1370 static _RopeConcatenation* _S_new_RopeConcatenation(
1371 _RopeRep* __left, _RopeRep* __right,
1374 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1375 return new(__space) _RopeConcatenation(__left, __right, __a);
1378 static _RopeFunction* _S_new_RopeFunction(char_producer<_CharT>* __f,
1379 size_t __size, bool __d, allocator_type __a)
1381 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1382 return new(__space) _RopeFunction(__f, __size, __d, __a);
1385 static _RopeSubstring* _S_new_RopeSubstring(
1386 _Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1387 size_t __l, allocator_type __a)
1389 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1390 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1394 _RopeLeaf* _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1395 size_t __size, allocator_type __a)
1396 # define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1397 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1399 if (0 == __size) return 0;
1400 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1402 uninitialized_copy_n(__s, __size, __buf);
1403 _S_cond_store_eos(__buf[__size]);
1405 return _S_new_RopeLeaf(__buf, __size, __a);
1409 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1410 __throw_exception_again;
1415 // Concatenation of nonempty strings.
1416 // Always builds a concatenation node.
1417 // Rebalances if the result is too deep.
1418 // Result has refcount 1.
1419 // Does not increment left and right ref counts even though
1420 // they are referenced.
1422 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1424 // Concatenation helper functions
1426 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1427 const _CharT* __iter, size_t __slen);
1428 // Concatenate by copying leaf.
1429 // should take an arbitrary iterator
1430 // result has refcount 1.
1432 static _RopeLeaf* _S_destr_leaf_concat_char_iter
1433 (_RopeLeaf* __r, const _CharT* __iter, size_t __slen);
1434 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1439 static size_t _S_char_ptr_len(const _CharT* __s);
1440 // slightly generalized strlen
1442 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1443 : _Base(__t,__a) { }
1446 // Copy __r to the _CharT buffer.
1447 // Returns __buffer + __r->_M_size.
1448 // Assumes that buffer is uninitialized.
1449 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1451 // Again, with explicit starting position and length.
1452 // Assumes that buffer is uninitialized.
1453 static _CharT* _S_flatten(_RopeRep* __r,
1454 size_t __start, size_t __len,
1457 static const unsigned long
1458 _S_min_len[_Rope_constants::_S_max_rope_depth + 1];
1460 static bool _S_is_balanced(_RopeRep* __r)
1461 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1463 static bool _S_is_almost_balanced(_RopeRep* __r)
1464 { return (__r->_M_depth == 0 ||
1465 __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1467 static bool _S_is_roughly_balanced(_RopeRep* __r)
1468 { return (__r->_M_depth <= 1 ||
1469 __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1471 // Assumes the result is not empty.
1472 static _RopeRep* _S_concat_and_set_balanced(_RopeRep* __left,
1475 _RopeRep* __result = _S_concat(__left, __right);
1476 if (_S_is_balanced(__result)) __result->_M_is_balanced = true;
1480 // The basic rebalancing operation. Logically copies the
1481 // rope. The result has refcount of 1. The client will
1482 // usually decrement the reference count of __r.
1483 // The result is within height 2 of balanced by the above
1485 static _RopeRep* _S_balance(_RopeRep* __r);
1487 // Add all unbalanced subtrees to the forest of balanceed trees.
1488 // Used only by balance.
1489 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1491 // Add __r to forest, assuming __r is already balanced.
1492 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1494 // Print to stdout, exposing structure
1495 static void _S_dump(_RopeRep* __r, int __indent = 0);
1497 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1498 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1501 bool empty() const { return 0 == this->_M_tree_ptr; }
1503 // Comparison member function. This is public only for those
1504 // clients that need a ternary comparison. Others
1505 // should use the comparison operators below.
1506 int compare(const rope& __y) const {
1507 return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr);
1510 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1511 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1515 rope(const _CharT* __s, size_t __len,
1516 const allocator_type& __a = allocator_type())
1517 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
1520 // Should perhaps be templatized with respect to the iterator type
1521 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1523 rope(const _CharT *__s, const _CharT *__e,
1524 const allocator_type& __a = allocator_type())
1525 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
1528 rope(const const_iterator& __s, const const_iterator& __e,
1529 const allocator_type& __a = allocator_type())
1530 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1531 __e._M_current_pos), __a)
1534 rope(const iterator& __s, const iterator& __e,
1535 const allocator_type& __a = allocator_type())
1536 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1537 __e._M_current_pos), __a)
1540 rope(_CharT __c, const allocator_type& __a = allocator_type())
1543 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1545 std::_Construct(__buf, __c);
1547 this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a);
1551 _RopeRep::__STL_FREE_STRING(__buf, 1, __a);
1552 __throw_exception_again;
1556 rope(size_t __n, _CharT __c,
1557 const allocator_type& __a = allocator_type());
1559 rope(const allocator_type& __a = allocator_type())
1562 // Construct a rope from a function that can compute its members
1563 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1564 const allocator_type& __a = allocator_type())
1567 this->_M_tree_ptr = (0 == __len) ?
1568 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1571 rope(const rope& __x, const allocator_type& __a = allocator_type())
1572 : _Base(__x._M_tree_ptr, __a)
1574 _S_ref(this->_M_tree_ptr);
1578 { _S_unref(this->_M_tree_ptr); }
1580 rope& operator=(const rope& __x)
1582 _RopeRep* __old = this->_M_tree_ptr;
1583 this->_M_tree_ptr = __x._M_tree_ptr;
1584 _S_ref(this->_M_tree_ptr);
1591 _S_unref(this->_M_tree_ptr);
1592 this->_M_tree_ptr = 0;
1595 void push_back(_CharT __x)
1597 _RopeRep* __old = this->_M_tree_ptr;
1599 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1605 _RopeRep* __old = this->_M_tree_ptr;
1607 _S_substring(this->_M_tree_ptr,
1609 this->_M_tree_ptr->_M_size - 1);
1615 return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1);
1618 void push_front(_CharT __x)
1620 _RopeRep* __old = this->_M_tree_ptr;
1622 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, this->get_allocator());
1624 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1631 __throw_exception_again;
1637 _RopeRep* __old = this->_M_tree_ptr;
1639 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1643 _CharT front() const
1645 return _S_fetch(this->_M_tree_ptr, 0);
1650 _RopeRep* __old = this->_M_tree_ptr;
1651 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1655 void copy(_CharT* __buffer) const {
1656 _Destroy(__buffer, __buffer + size());
1657 _S_flatten(this->_M_tree_ptr, __buffer);
1660 // This is the copy function from the standard, but
1661 // with the arguments reordered to make it consistent with the
1662 // rest of the interface.
1663 // Note that this guaranteed not to compile if the draft standard
1664 // order is assumed.
1665 size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const
1667 size_t __size = size();
1668 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1670 _Destroy(__buffer, __buffer + __len);
1671 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1675 // Print to stdout, exposing structure. May be useful for
1676 // performance debugging.
1678 _S_dump(this->_M_tree_ptr);
1681 // Convert to 0 terminated string in new allocated memory.
1682 // Embedded 0s in the input do not terminate the copy.
1683 const _CharT* c_str() const;
1685 // As above, but lso use the flattened representation as the
1686 // the new rope representation.
1687 const _CharT* replace_with_c_str();
1689 // Reclaim memory for the c_str generated flattened string.
1690 // Intentionally undocumented, since it's hard to say when this
1691 // is safe for multiple threads.
1692 void delete_c_str () {
1693 if (0 == this->_M_tree_ptr) return;
1694 if (_Rope_constants::_S_leaf == this->_M_tree_ptr->_M_tag &&
1695 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1696 this->_M_tree_ptr->_M_c_string) {
1697 // Representation shared
1701 this->_M_tree_ptr->_M_free_c_string();
1703 this->_M_tree_ptr->_M_c_string = 0;
1706 _CharT operator[] (size_type __pos) const {
1707 return _S_fetch(this->_M_tree_ptr, __pos);
1710 _CharT at(size_type __pos) const {
1711 // if (__pos >= size()) throw out_of_range; // XXX
1712 return (*this)[__pos];
1715 const_iterator begin() const {
1716 return(const_iterator(this->_M_tree_ptr, 0));
1719 // An easy way to get a const iterator from a non-const container.
1720 const_iterator const_begin() const {
1721 return(const_iterator(this->_M_tree_ptr, 0));
1724 const_iterator end() const {
1725 return(const_iterator(this->_M_tree_ptr, size()));
1728 const_iterator const_end() const {
1729 return(const_iterator(this->_M_tree_ptr, size()));
1732 size_type size() const {
1733 return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size);
1736 size_type length() const {
1740 size_type max_size() const {
1741 return _S_min_len[_Rope_constants::_S_max_rope_depth - 1] - 1;
1742 // Guarantees that the result can be sufficirntly
1743 // balanced. Longer ropes will probably still work,
1744 // but it's harder to make guarantees.
1747 typedef reverse_iterator<const_iterator> const_reverse_iterator;
1749 const_reverse_iterator rbegin() const {
1750 return const_reverse_iterator(end());
1753 const_reverse_iterator const_rbegin() const {
1754 return const_reverse_iterator(end());
1757 const_reverse_iterator rend() const {
1758 return const_reverse_iterator(begin());
1761 const_reverse_iterator const_rend() const {
1762 return const_reverse_iterator(begin());
1765 template<class _CharT2, class _Alloc2>
1766 friend rope<_CharT2,_Alloc2>
1767 operator+ (const rope<_CharT2,_Alloc2>& __left,
1768 const rope<_CharT2,_Alloc2>& __right);
1770 template<class _CharT2, class _Alloc2>
1771 friend rope<_CharT2,_Alloc2>
1772 operator+ (const rope<_CharT2,_Alloc2>& __left,
1773 const _CharT2* __right);
1775 template<class _CharT2, class _Alloc2>
1776 friend rope<_CharT2,_Alloc2>
1777 operator+ (const rope<_CharT2,_Alloc2>& __left, _CharT2 __right);
1778 // The symmetric cases are intentionally omitted, since they're presumed
1779 // to be less common, and we don't handle them as well.
1781 // The following should really be templatized.
1782 // The first argument should be an input iterator or
1783 // forward iterator with value_type _CharT.
1784 rope& append(const _CharT* __iter, size_t __n) {
1785 _RopeRep* __result =
1786 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
1787 _S_unref(this->_M_tree_ptr);
1788 this->_M_tree_ptr = __result;
1792 rope& append(const _CharT* __c_string) {
1793 size_t __len = _S_char_ptr_len(__c_string);
1794 append(__c_string, __len);
1798 rope& append(const _CharT* __s, const _CharT* __e) {
1799 _RopeRep* __result =
1800 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
1801 _S_unref(this->_M_tree_ptr);
1802 this->_M_tree_ptr = __result;
1806 rope& append(const_iterator __s, const_iterator __e) {
1807 _Self_destruct_ptr __appendee(_S_substring(
1808 __s._M_root, __s._M_current_pos, __e._M_current_pos));
1809 _RopeRep* __result =
1810 _S_concat(this->_M_tree_ptr, (_RopeRep*)__appendee);
1811 _S_unref(this->_M_tree_ptr);
1812 this->_M_tree_ptr = __result;
1816 rope& append(_CharT __c) {
1817 _RopeRep* __result =
1818 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
1819 _S_unref(this->_M_tree_ptr);
1820 this->_M_tree_ptr = __result;
1824 rope& append() { return append(_CharT()); } // XXX why?
1826 rope& append(const rope& __y) {
1827 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
1828 _S_unref(this->_M_tree_ptr);
1829 this->_M_tree_ptr = __result;
1833 rope& append(size_t __n, _CharT __c) {
1834 rope<_CharT,_Alloc> __last(__n, __c);
1835 return append(__last);
1838 void swap(rope& __b) {
1839 _RopeRep* __tmp = this->_M_tree_ptr;
1840 this->_M_tree_ptr = __b._M_tree_ptr;
1841 __b._M_tree_ptr = __tmp;
1846 // Result is included in refcount.
1847 static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
1848 size_t __pos2, _RopeRep* __r) {
1849 if (0 == __old) { _S_ref(__r); return __r; }
1850 _Self_destruct_ptr __left(
1851 _S_substring(__old, 0, __pos1));
1852 _Self_destruct_ptr __right(
1853 _S_substring(__old, __pos2, __old->_M_size));
1857 __result = _S_concat(__left, __right);
1859 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
1860 __result = _S_concat(__left_result, __right);
1866 void insert(size_t __p, const rope& __r) {
1867 _RopeRep* __result =
1868 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
1869 _S_unref(this->_M_tree_ptr);
1870 this->_M_tree_ptr = __result;
1873 void insert(size_t __p, size_t __n, _CharT __c) {
1874 rope<_CharT,_Alloc> __r(__n,__c);
1878 void insert(size_t __p, const _CharT* __i, size_t __n) {
1879 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
1880 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
1882 _Self_destruct_ptr __left_result(
1883 _S_concat_char_iter(__left, __i, __n));
1884 // _S_ destr_concat_char_iter should be safe here.
1885 // But as it stands it's probably not a win, since __left
1886 // is likely to have additional references.
1887 _RopeRep* __result = _S_concat(__left_result, __right);
1888 _S_unref(this->_M_tree_ptr);
1889 this->_M_tree_ptr = __result;
1892 void insert(size_t __p, const _CharT* __c_string) {
1893 insert(__p, __c_string, _S_char_ptr_len(__c_string));
1896 void insert(size_t __p, _CharT __c) {
1897 insert(__p, &__c, 1);
1900 void insert(size_t __p) {
1901 _CharT __c = _CharT();
1902 insert(__p, &__c, 1);
1905 void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
1910 void insert(size_t __p, const const_iterator& __i,
1911 const const_iterator& __j) {
1916 void insert(size_t __p, const iterator& __i,
1917 const iterator& __j) {
1922 // (position, length) versions of replace operations:
1924 void replace(size_t __p, size_t __n, const rope& __r) {
1925 _RopeRep* __result =
1926 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
1927 _S_unref(this->_M_tree_ptr);
1928 this->_M_tree_ptr = __result;
1931 void replace(size_t __p, size_t __n,
1932 const _CharT* __i, size_t __i_len) {
1933 rope __r(__i, __i_len);
1934 replace(__p, __n, __r);
1937 void replace(size_t __p, size_t __n, _CharT __c) {
1939 replace(__p, __n, __r);
1942 void replace(size_t __p, size_t __n, const _CharT* __c_string) {
1943 rope __r(__c_string);
1944 replace(__p, __n, __r);
1947 void replace(size_t __p, size_t __n,
1948 const _CharT* __i, const _CharT* __j) {
1950 replace(__p, __n, __r);
1953 void replace(size_t __p, size_t __n,
1954 const const_iterator& __i, const const_iterator& __j) {
1956 replace(__p, __n, __r);
1959 void replace(size_t __p, size_t __n,
1960 const iterator& __i, const iterator& __j) {
1962 replace(__p, __n, __r);
1965 // Single character variants:
1966 void replace(size_t __p, _CharT __c) {
1967 iterator __i(this, __p);
1971 void replace(size_t __p, const rope& __r) {
1972 replace(__p, 1, __r);
1975 void replace(size_t __p, const _CharT* __i, size_t __i_len) {
1976 replace(__p, 1, __i, __i_len);
1979 void replace(size_t __p, const _CharT* __c_string) {
1980 replace(__p, 1, __c_string);
1983 void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
1984 replace(__p, 1, __i, __j);
1987 void replace(size_t __p, const const_iterator& __i,
1988 const const_iterator& __j) {
1989 replace(__p, 1, __i, __j);
1992 void replace(size_t __p, const iterator& __i,
1993 const iterator& __j) {
1994 replace(__p, 1, __i, __j);
1997 // Erase, (position, size) variant.
1998 void erase(size_t __p, size_t __n) {
1999 _RopeRep* __result = replace(this->_M_tree_ptr, __p, __p + __n, 0);
2000 _S_unref(this->_M_tree_ptr);
2001 this->_M_tree_ptr = __result;
2004 // Erase, single character
2005 void erase(size_t __p) {
2006 erase(__p, __p + 1);
2009 // Insert, iterator variants.
2010 iterator insert(const iterator& __p, const rope& __r)
2011 { insert(__p.index(), __r); return __p; }
2012 iterator insert(const iterator& __p, size_t __n, _CharT __c)
2013 { insert(__p.index(), __n, __c); return __p; }
2014 iterator insert(const iterator& __p, _CharT __c)
2015 { insert(__p.index(), __c); return __p; }
2016 iterator insert(const iterator& __p )
2017 { insert(__p.index()); return __p; }
2018 iterator insert(const iterator& __p, const _CharT* c_string)
2019 { insert(__p.index(), c_string); return __p; }
2020 iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
2021 { insert(__p.index(), __i, __n); return __p; }
2022 iterator insert(const iterator& __p, const _CharT* __i,
2024 { insert(__p.index(), __i, __j); return __p; }
2025 iterator insert(const iterator& __p,
2026 const const_iterator& __i, const const_iterator& __j)
2027 { insert(__p.index(), __i, __j); return __p; }
2028 iterator insert(const iterator& __p,
2029 const iterator& __i, const iterator& __j)
2030 { insert(__p.index(), __i, __j); return __p; }
2032 // Replace, range variants.
2033 void replace(const iterator& __p, const iterator& __q,
2035 { replace(__p.index(), __q.index() - __p.index(), __r); }
2036 void replace(const iterator& __p, const iterator& __q, _CharT __c)
2037 { replace(__p.index(), __q.index() - __p.index(), __c); }
2038 void replace(const iterator& __p, const iterator& __q,
2039 const _CharT* __c_string)
2040 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2041 void replace(const iterator& __p, const iterator& __q,
2042 const _CharT* __i, size_t __n)
2043 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2044 void replace(const iterator& __p, const iterator& __q,
2045 const _CharT* __i, const _CharT* __j)
2046 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2047 void replace(const iterator& __p, const iterator& __q,
2048 const const_iterator& __i, const const_iterator& __j)
2049 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2050 void replace(const iterator& __p, const iterator& __q,
2051 const iterator& __i, const iterator& __j)
2052 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2054 // Replace, iterator variants.
2055 void replace(const iterator& __p, const rope& __r)
2056 { replace(__p.index(), __r); }
2057 void replace(const iterator& __p, _CharT __c)
2058 { replace(__p.index(), __c); }
2059 void replace(const iterator& __p, const _CharT* __c_string)
2060 { replace(__p.index(), __c_string); }
2061 void replace(const iterator& __p, const _CharT* __i, size_t __n)
2062 { replace(__p.index(), __i, __n); }
2063 void replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2064 { replace(__p.index(), __i, __j); }
2065 void replace(const iterator& __p, const_iterator __i,
2067 { replace(__p.index(), __i, __j); }
2068 void replace(const iterator& __p, iterator __i, iterator __j)
2069 { replace(__p.index(), __i, __j); }
2071 // Iterator and range variants of erase
2072 iterator erase(const iterator& __p, const iterator& __q) {
2073 size_t __p_index = __p.index();
2074 erase(__p_index, __q.index() - __p_index);
2075 return iterator(this, __p_index);
2077 iterator erase(const iterator& __p) {
2078 size_t __p_index = __p.index();
2079 erase(__p_index, 1);
2080 return iterator(this, __p_index);
2083 rope substr(size_t __start, size_t __len = 1) const {
2084 return rope<_CharT,_Alloc>(
2085 _S_substring(this->_M_tree_ptr,
2090 rope substr(iterator __start, iterator __end) const {
2091 return rope<_CharT,_Alloc>(
2092 _S_substring(this->_M_tree_ptr,
2097 rope substr(iterator __start) const {
2098 size_t __pos = __start.index();
2099 return rope<_CharT,_Alloc>(
2100 _S_substring(this->_M_tree_ptr, __pos, __pos + 1));
2103 rope substr(const_iterator __start, const_iterator __end) const {
2104 // This might eventually take advantage of the cache in the
2106 return rope<_CharT,_Alloc>(
2107 _S_substring(this->_M_tree_ptr, __start.index(), __end.index()));
2110 rope<_CharT,_Alloc> substr(const_iterator __start) {
2111 size_t __pos = __start.index();
2112 return rope<_CharT,_Alloc>(
2113 _S_substring(this->_M_tree_ptr, __pos, __pos + 1));
2116 static const size_type npos;
2118 size_type find(_CharT __c, size_type __pos = 0) const;
2119 size_type find(const _CharT* __s, size_type __pos = 0) const {
2120 size_type __result_pos;
2121 const_iterator __result =
2122 std::search(const_begin() + __pos, const_end(),
2123 __s, __s + _S_char_ptr_len(__s));
2124 __result_pos = __result.index();
2125 # ifndef __STL_OLD_ROPE_SEMANTICS
2126 if (__result_pos == size()) __result_pos = npos;
2128 return __result_pos;
2131 iterator mutable_begin() {
2132 return(iterator(this, 0));
2135 iterator mutable_end() {
2136 return(iterator(this, size()));
2139 typedef reverse_iterator<iterator> reverse_iterator;
2141 reverse_iterator mutable_rbegin() {
2142 return reverse_iterator(mutable_end());
2145 reverse_iterator mutable_rend() {
2146 return reverse_iterator(mutable_begin());
2149 reference mutable_reference_at(size_type __pos) {
2150 return reference(this, __pos);
2154 reference operator[] (size_type __pos) {
2155 return _char_ref_proxy(this, __pos);
2158 reference at(size_type __pos) {
2159 // if (__pos >= size()) throw out_of_range; // XXX
2160 return (*this)[__pos];
2163 void resize(size_type __n, _CharT __c) {}
2164 void resize(size_type __n) {}
2165 void reserve(size_type __res_arg = 0) {}
2166 size_type capacity() const {
2170 // Stuff below this line is dangerous because it's error prone.
2171 // I would really like to get rid of it.
2172 // copy function with funny arg ordering.
2173 size_type copy(_CharT* __buffer, size_type __n,
2174 size_type __pos = 0) const {
2175 return copy(__pos, __n, __buffer);
2178 iterator end() { return mutable_end(); }
2180 iterator begin() { return mutable_begin(); }
2182 reverse_iterator rend() { return mutable_rend(); }
2184 reverse_iterator rbegin() { return mutable_rbegin(); }
2188 const_iterator end() { return const_end(); }
2190 const_iterator begin() { return const_begin(); }
2192 const_reverse_iterator rend() { return const_rend(); }
2194 const_reverse_iterator rbegin() { return const_rbegin(); }
2200 template <class _CharT, class _Alloc>
2201 const typename rope<_CharT, _Alloc>::size_type rope<_CharT, _Alloc>::npos =
2204 template <class _CharT, class _Alloc>
2205 inline bool operator== (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2206 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2207 return (__x._M_current_pos == __y._M_current_pos &&
2208 __x._M_root == __y._M_root);
2211 template <class _CharT, class _Alloc>
2212 inline bool operator< (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2213 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2214 return (__x._M_current_pos < __y._M_current_pos);
2217 template <class _CharT, class _Alloc>
2218 inline bool operator!= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2219 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2220 return !(__x == __y);
2223 template <class _CharT, class _Alloc>
2224 inline bool operator> (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2225 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2229 template <class _CharT, class _Alloc>
2230 inline bool operator<= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2231 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2232 return !(__y < __x);
2235 template <class _CharT, class _Alloc>
2236 inline bool operator>= (const _Rope_const_iterator<_CharT,_Alloc>& __x,
2237 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2238 return !(__x < __y);
2241 template <class _CharT, class _Alloc>
2242 inline ptrdiff_t operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x,
2243 const _Rope_const_iterator<_CharT,_Alloc>& __y) {
2244 return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
2247 template <class _CharT, class _Alloc>
2248 inline _Rope_const_iterator<_CharT,_Alloc>
2249 operator-(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
2250 return _Rope_const_iterator<_CharT,_Alloc>(
2251 __x._M_root, __x._M_current_pos - __n);
2254 template <class _CharT, class _Alloc>
2255 inline _Rope_const_iterator<_CharT,_Alloc>
2256 operator+(const _Rope_const_iterator<_CharT,_Alloc>& __x, ptrdiff_t __n) {
2257 return _Rope_const_iterator<_CharT,_Alloc>(
2258 __x._M_root, __x._M_current_pos + __n);
2261 template <class _CharT, class _Alloc>
2262 inline _Rope_const_iterator<_CharT,_Alloc>
2263 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT,_Alloc>& __x) {
2264 return _Rope_const_iterator<_CharT,_Alloc>(
2265 __x._M_root, __x._M_current_pos + __n);
2268 template <class _CharT, class _Alloc>
2269 inline bool operator== (const _Rope_iterator<_CharT,_Alloc>& __x,
2270 const _Rope_iterator<_CharT,_Alloc>& __y) {
2271 return (__x._M_current_pos == __y._M_current_pos &&
2272 __x._M_root_rope == __y._M_root_rope);
2275 template <class _CharT, class _Alloc>
2276 inline bool operator< (const _Rope_iterator<_CharT,_Alloc>& __x,
2277 const _Rope_iterator<_CharT,_Alloc>& __y) {
2278 return (__x._M_current_pos < __y._M_current_pos);
2281 template <class _CharT, class _Alloc>
2282 inline bool operator!= (const _Rope_iterator<_CharT,_Alloc>& __x,
2283 const _Rope_iterator<_CharT,_Alloc>& __y) {
2284 return !(__x == __y);
2287 template <class _CharT, class _Alloc>
2288 inline bool operator> (const _Rope_iterator<_CharT,_Alloc>& __x,
2289 const _Rope_iterator<_CharT,_Alloc>& __y) {
2293 template <class _CharT, class _Alloc>
2294 inline bool operator<= (const _Rope_iterator<_CharT,_Alloc>& __x,
2295 const _Rope_iterator<_CharT,_Alloc>& __y) {
2296 return !(__y < __x);
2299 template <class _CharT, class _Alloc>
2300 inline bool operator>= (const _Rope_iterator<_CharT,_Alloc>& __x,
2301 const _Rope_iterator<_CharT,_Alloc>& __y) {
2302 return !(__x < __y);
2305 template <class _CharT, class _Alloc>
2306 inline ptrdiff_t operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
2307 const _Rope_iterator<_CharT,_Alloc>& __y) {
2308 return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos;
2311 template <class _CharT, class _Alloc>
2312 inline _Rope_iterator<_CharT,_Alloc>
2313 operator-(const _Rope_iterator<_CharT,_Alloc>& __x,
2315 return _Rope_iterator<_CharT,_Alloc>(
2316 __x._M_root_rope, __x._M_current_pos - __n);
2319 template <class _CharT, class _Alloc>
2320 inline _Rope_iterator<_CharT,_Alloc>
2321 operator+(const _Rope_iterator<_CharT,_Alloc>& __x,
2323 return _Rope_iterator<_CharT,_Alloc>(
2324 __x._M_root_rope, __x._M_current_pos + __n);
2327 template <class _CharT, class _Alloc>
2328 inline _Rope_iterator<_CharT,_Alloc>
2329 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT,_Alloc>& __x) {
2330 return _Rope_iterator<_CharT,_Alloc>(
2331 __x._M_root_rope, __x._M_current_pos + __n);
2334 template <class _CharT, class _Alloc>
2337 operator+ (const rope<_CharT,_Alloc>& __left,
2338 const rope<_CharT,_Alloc>& __right)
2340 return rope<_CharT,_Alloc>(
2341 rope<_CharT,_Alloc>::_S_concat(__left._M_tree_ptr, __right._M_tree_ptr));
2342 // Inlining this should make it possible to keep __left and
2343 // __right in registers.
2346 template <class _CharT, class _Alloc>
2348 rope<_CharT,_Alloc>&
2349 operator+= (rope<_CharT,_Alloc>& __left,
2350 const rope<_CharT,_Alloc>& __right)
2352 __left.append(__right);
2356 template <class _CharT, class _Alloc>
2359 operator+ (const rope<_CharT,_Alloc>& __left,
2360 const _CharT* __right) {
2361 size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
2362 return rope<_CharT,_Alloc>(
2363 rope<_CharT,_Alloc>::_S_concat_char_iter(
2364 __left._M_tree_ptr, __right, __rlen));
2367 template <class _CharT, class _Alloc>
2369 rope<_CharT,_Alloc>&
2370 operator+= (rope<_CharT,_Alloc>& __left,
2371 const _CharT* __right) {
2372 __left.append(__right);
2376 template <class _CharT, class _Alloc>
2379 operator+ (const rope<_CharT,_Alloc>& __left, _CharT __right) {
2380 return rope<_CharT,_Alloc>(
2381 rope<_CharT,_Alloc>::_S_concat_char_iter(
2382 __left._M_tree_ptr, &__right, 1));
2385 template <class _CharT, class _Alloc>
2387 rope<_CharT,_Alloc>&
2388 operator+= (rope<_CharT,_Alloc>& __left, _CharT __right) {
2389 __left.append(__right);
2393 template <class _CharT, class _Alloc>
2395 operator< (const rope<_CharT,_Alloc>& __left,
2396 const rope<_CharT,_Alloc>& __right) {
2397 return __left.compare(__right) < 0;
2400 template <class _CharT, class _Alloc>
2402 operator== (const rope<_CharT,_Alloc>& __left,
2403 const rope<_CharT,_Alloc>& __right) {
2404 return __left.compare(__right) == 0;
2407 template <class _CharT, class _Alloc>
2408 inline bool operator== (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
2409 const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
2410 return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root);
2413 template <class _CharT, class _Alloc>
2415 operator!= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
2416 return !(__x == __y);
2419 template <class _CharT, class _Alloc>
2421 operator> (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
2425 template <class _CharT, class _Alloc>
2427 operator<= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
2428 return !(__y < __x);
2431 template <class _CharT, class _Alloc>
2433 operator>= (const rope<_CharT,_Alloc>& __x, const rope<_CharT,_Alloc>& __y) {
2434 return !(__x < __y);
2437 template <class _CharT, class _Alloc>
2438 inline bool operator!= (const _Rope_char_ptr_proxy<_CharT,_Alloc>& __x,
2439 const _Rope_char_ptr_proxy<_CharT,_Alloc>& __y) {
2440 return !(__x == __y);
2443 template<class _CharT, class _Traits, class _Alloc>
2444 std::basic_ostream<_CharT, _Traits>& operator<<
2445 (std::basic_ostream<_CharT, _Traits>& __o,
2446 const rope<_CharT, _Alloc>& __r);
2448 typedef rope<char> crope;
2449 typedef rope<wchar_t> wrope;
2451 inline crope::reference __mutable_reference_at(crope& __c, size_t __i)
2453 return __c.mutable_reference_at(__i);
2456 inline wrope::reference __mutable_reference_at(wrope& __c, size_t __i)
2458 return __c.mutable_reference_at(__i);
2461 template <class _CharT, class _Alloc>
2462 inline void swap(rope<_CharT,_Alloc>& __x, rope<_CharT,_Alloc>& __y) {
2466 // Hash functions should probably be revisited later:
2467 template<> struct hash<crope>
2469 size_t operator()(const crope& __str) const
2471 size_t __size = __str.size();
2473 if (0 == __size) return 0;
2474 return 13*__str[0] + 5*__str[__size - 1] + __size;
2479 template<> struct hash<wrope>
2481 size_t operator()(const wrope& __str) const
2483 size_t __size = __str.size();
2485 if (0 == __size) return 0;
2486 return 13*__str[0] + 5*__str[__size - 1] + __size;
2490 } // namespace __gnu_cxx
2492 # include <ext/ropeimpl.h>