]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/rope
rope: Trivial formatting fixes.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / rope
1 // SGI's rope class -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31 * Copyright (c) 1997
32 * Silicon Graphics Computer Systems, Inc.
33 *
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.
41 */
42
43 /** @file ext/rope
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.
47 */
48
49 #ifndef _ROPE
50 #define _ROPE 1
51
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>
60
61 # ifdef __GC
62 # define __GC_CONST const
63 # else
64 # include <bits/gthr.h>
65 # define __GC_CONST // constant except for deallocation
66 # endif
67
68 #include <ext/memory> // For uninitialized_copy_n
69
70 namespace __gnu_cxx
71 {
72 using std::size_t;
73 using std::ptrdiff_t;
74 using std::allocator;
75 using std::iterator;
76 using std::reverse_iterator;
77 using std::_Destroy;
78
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.
81
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
86 _S_eos(_CharT*)
87 { return _CharT(); }
88
89 // Test for basic character types.
90 // For basic character types leaves having a trailing eos.
91 template <class _CharT>
92 inline bool
93 _S_is_basic_char_type(_CharT*)
94 { return false; }
95
96 template <class _CharT>
97 inline bool
98 _S_is_one_byte_char_type(_CharT*)
99 { return false; }
100
101 inline bool
102 _S_is_basic_char_type(char*)
103 { return true; }
104
105 inline bool
106 _S_is_one_byte_char_type(char*)
107 { return true; }
108
109 inline bool
110 _S_is_basic_char_type(wchar_t*)
111 { return true; }
112
113 // Store an eos iff _CharT is a basic character type.
114 // Do not reference _S_eos if it isn't.
115 template <class _CharT>
116 inline void
117 _S_cond_store_eos(_CharT&) { }
118
119 inline void
120 _S_cond_store_eos(char& __c)
121 { __c = 0; }
122
123 inline void
124 _S_cond_store_eos(wchar_t& __c)
125 { __c = 0; }
126
127 // char_producers are logically functions that generate a section of
128 // a string. These can be convereted to ropes. The resulting rope
129 // invokes the char_producer on demand. This allows, for example,
130 // files to be viewed as ropes without reading the entire file.
131 template <class _CharT>
132 class char_producer
133 {
134 public:
135 virtual ~char_producer() {};
136
137 virtual void
138 operator()(size_t __start_pos, size_t __len,
139 _CharT* __buffer) = 0;
140 // Buffer should really be an arbitrary output iterator.
141 // That way we could flatten directly into an ostream, etc.
142 // This is thoroughly impossible, since iterator types don't
143 // have runtime descriptions.
144 };
145
146 // Sequence buffers:
147 //
148 // Sequence must provide an append operation that appends an
149 // array to the sequence. Sequence buffers are useful only if
150 // appending an entire array is cheaper than appending element by element.
151 // This is true for many string representations.
152 // This should perhaps inherit from ostream<sequence::value_type>
153 // and be implemented correspondingly, so that they can be used
154 // for formatted. For the sake of portability, we don't do this yet.
155 //
156 // For now, sequence buffers behave as output iterators. But they also
157 // behave a little like basic_ostringstream<sequence::value_type> and a
158 // little like containers.
159
160 template<class _Sequence, size_t _Buf_sz = 100>
161 class sequence_buffer
162 : public iterator<std::output_iterator_tag, void, void, void, void>
163 {
164 public:
165 typedef typename _Sequence::value_type value_type;
166 protected:
167 _Sequence* _M_prefix;
168 value_type _M_buffer[_Buf_sz];
169 size_t _M_buf_count;
170 public:
171
172 void
173 flush()
174 {
175 _M_prefix->append(_M_buffer, _M_buffer + _M_buf_count);
176 _M_buf_count = 0;
177 }
178
179 ~sequence_buffer()
180 { flush(); }
181
182 sequence_buffer()
183 : _M_prefix(0), _M_buf_count(0) { }
184
185 sequence_buffer(const sequence_buffer& __x)
186 {
187 _M_prefix = __x._M_prefix;
188 _M_buf_count = __x._M_buf_count;
189 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
190 }
191
192 sequence_buffer(sequence_buffer& __x)
193 {
194 __x.flush();
195 _M_prefix = __x._M_prefix;
196 _M_buf_count = 0;
197 }
198
199 sequence_buffer(_Sequence& __s)
200 : _M_prefix(&__s), _M_buf_count(0) { }
201
202 sequence_buffer&
203 operator=(sequence_buffer& __x)
204 {
205 __x.flush();
206 _M_prefix = __x._M_prefix;
207 _M_buf_count = 0;
208 return *this;
209 }
210
211 sequence_buffer&
212 operator=(const sequence_buffer& __x)
213 {
214 _M_prefix = __x._M_prefix;
215 _M_buf_count = __x._M_buf_count;
216 copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer);
217 return *this;
218 }
219
220 void
221 push_back(value_type __x)
222 {
223 if (_M_buf_count < _Buf_sz)
224 {
225 _M_buffer[_M_buf_count] = __x;
226 ++_M_buf_count;
227 }
228 else
229 {
230 flush();
231 _M_buffer[0] = __x;
232 _M_buf_count = 1;
233 }
234 }
235
236 void
237 append(value_type* __s, size_t __len)
238 {
239 if (__len + _M_buf_count <= _Buf_sz)
240 {
241 size_t __i = _M_buf_count;
242 for (size_t __j = 0; __j < __len; __i++, __j++)
243 _M_buffer[__i] = __s[__j];
244 _M_buf_count += __len;
245 }
246 else if (0 == _M_buf_count)
247 _M_prefix->append(__s, __s + __len);
248 else
249 {
250 flush();
251 append(__s, __len);
252 }
253 }
254
255 sequence_buffer&
256 write(value_type* __s, size_t __len)
257 {
258 append(__s, __len);
259 return *this;
260 }
261
262 sequence_buffer&
263 put(value_type __x)
264 {
265 push_back(__x);
266 return *this;
267 }
268
269 sequence_buffer&
270 operator=(const value_type& __rhs)
271 {
272 push_back(__rhs);
273 return *this;
274 }
275
276 sequence_buffer&
277 operator*()
278 { return *this; }
279
280 sequence_buffer&
281 operator++()
282 { return *this; }
283
284 sequence_buffer
285 operator++(int)
286 { return *this; }
287 };
288
289 // The following should be treated as private, at least for now.
290 template<class _CharT>
291 class _Rope_char_consumer
292 {
293 public:
294 // If we had member templates, these should not be virtual.
295 // For now we need to use run-time parametrization where
296 // compile-time would do. Hence this should all be private
297 // for now.
298 // The symmetry with char_producer is accidental and temporary.
299 virtual ~_Rope_char_consumer() {};
300
301 virtual bool
302 operator()(const _CharT* __buffer, size_t __len) = 0;
303 };
304
305 // First a lot of forward declarations. The standard seems to require
306 // much stricter "declaration before use" than many of the implementations
307 // that preceded it.
308 template<class _CharT, class _Alloc = allocator<_CharT> >
309 class rope;
310
311 template<class _CharT, class _Alloc>
312 struct _Rope_RopeConcatenation;
313
314 template<class _CharT, class _Alloc>
315 struct _Rope_RopeLeaf;
316
317 template<class _CharT, class _Alloc>
318 struct _Rope_RopeFunction;
319
320 template<class _CharT, class _Alloc>
321 struct _Rope_RopeSubstring;
322
323 template<class _CharT, class _Alloc>
324 class _Rope_iterator;
325
326 template<class _CharT, class _Alloc>
327 class _Rope_const_iterator;
328
329 template<class _CharT, class _Alloc>
330 class _Rope_char_ref_proxy;
331
332 template<class _CharT, class _Alloc>
333 class _Rope_char_ptr_proxy;
334
335 template<class _CharT, class _Alloc>
336 bool
337 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
338 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y);
339
340 template<class _CharT, class _Alloc>
341 _Rope_const_iterator<_CharT, _Alloc>
342 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
343 ptrdiff_t __n);
344
345 template<class _CharT, class _Alloc>
346 _Rope_const_iterator<_CharT, _Alloc>
347 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x,
348 ptrdiff_t __n);
349
350 template<class _CharT, class _Alloc>
351 _Rope_const_iterator<_CharT, _Alloc>
352 operator+(ptrdiff_t __n,
353 const _Rope_const_iterator<_CharT, _Alloc>& __x);
354
355 template<class _CharT, class _Alloc>
356 bool
357 operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
358 const _Rope_const_iterator<_CharT, _Alloc>& __y);
359
360 template<class _CharT, class _Alloc>
361 bool
362 operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
363 const _Rope_const_iterator<_CharT, _Alloc>& __y);
364
365 template<class _CharT, class _Alloc>
366 ptrdiff_t
367 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
368 const _Rope_const_iterator<_CharT, _Alloc>& __y);
369
370 template<class _CharT, class _Alloc>
371 _Rope_iterator<_CharT, _Alloc>
372 operator-(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
373
374 template<class _CharT, class _Alloc>
375 _Rope_iterator<_CharT, _Alloc>
376 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n);
377
378 template<class _CharT, class _Alloc>
379 _Rope_iterator<_CharT, _Alloc>
380 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x);
381
382 template<class _CharT, class _Alloc>
383 bool
384 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
385 const _Rope_iterator<_CharT, _Alloc>& __y);
386
387 template<class _CharT, class _Alloc>
388 bool
389 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
390 const _Rope_iterator<_CharT, _Alloc>& __y);
391
392 template<class _CharT, class _Alloc>
393 ptrdiff_t
394 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
395 const _Rope_iterator<_CharT, _Alloc>& __y);
396
397 template<class _CharT, class _Alloc>
398 rope<_CharT, _Alloc>
399 operator+(const rope<_CharT, _Alloc>& __left,
400 const rope<_CharT, _Alloc>& __right);
401
402 template<class _CharT, class _Alloc>
403 rope<_CharT, _Alloc>
404 operator+(const rope<_CharT, _Alloc>& __left, const _CharT* __right);
405
406 template<class _CharT, class _Alloc>
407 rope<_CharT, _Alloc>
408 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right);
409
410 // Some helpers, so we can use power on ropes.
411 // See below for why this isn't local to the implementation.
412
413 // This uses a nonstandard refcount convention.
414 // The result has refcount 0.
415 template<class _CharT, class _Alloc>
416 struct _Rope_Concat_fn
417 : public std::binary_function<rope<_CharT, _Alloc>, rope<_CharT, _Alloc>,
418 rope<_CharT, _Alloc> >
419 {
420 rope<_CharT, _Alloc>
421 operator()(const rope<_CharT, _Alloc>& __x,
422 const rope<_CharT, _Alloc>& __y)
423 { return __x + __y; }
424 };
425
426 template <class _CharT, class _Alloc>
427 inline rope<_CharT, _Alloc>
428 identity_element(_Rope_Concat_fn<_CharT, _Alloc>)
429 { return rope<_CharT, _Alloc>(); }
430
431 // Class _Refcount_Base provides a type, _RC_t, a data member,
432 // _M_ref_count, and member functions _M_incr and _M_decr, which perform
433 // atomic preincrement/predecrement. The constructor initializes
434 // _M_ref_count.
435 struct _Refcount_Base
436 {
437 // The type _RC_t
438 typedef size_t _RC_t;
439
440 // The data member _M_ref_count
441 volatile _RC_t _M_ref_count;
442
443 // Constructor
444 __gthread_mutex_t _M_ref_count_lock;
445
446 _Refcount_Base(_RC_t __n) : _M_ref_count(__n), _M_ref_count_lock()
447 {
448 #ifdef __GTHREAD_MUTEX_INIT
449 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
450 _M_ref_count_lock = __tmp;
451 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
452 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
453 #else
454 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
455 #endif
456 }
457
458 void
459 _M_incr()
460 {
461 __gthread_mutex_lock(&_M_ref_count_lock);
462 ++_M_ref_count;
463 __gthread_mutex_unlock(&_M_ref_count_lock);
464 }
465
466 _RC_t
467 _M_decr()
468 {
469 __gthread_mutex_lock(&_M_ref_count_lock);
470 volatile _RC_t __tmp = --_M_ref_count;
471 __gthread_mutex_unlock(&_M_ref_count_lock);
472 return __tmp;
473 }
474 };
475
476 //
477 // What follows should really be local to rope. Unfortunately,
478 // that doesn't work, since it makes it impossible to define generic
479 // equality on rope iterators. According to the draft standard, the
480 // template parameters for such an equality operator cannot be inferred
481 // from the occurrence of a member class as a parameter.
482 // (SGI compilers in fact allow this, but the __result wouldn't be
483 // portable.)
484 // Similarly, some of the static member functions are member functions
485 // only to avoid polluting the global namespace, and to circumvent
486 // restrictions on type inference for template functions.
487 //
488
489 //
490 // The internal data structure for representing a rope. This is
491 // private to the implementation. A rope is really just a pointer
492 // to one of these.
493 //
494 // A few basic functions for manipulating this data structure
495 // are members of _RopeRep. Most of the more complex algorithms
496 // are implemented as rope members.
497 //
498 // Some of the static member functions of _RopeRep have identically
499 // named functions in rope that simply invoke the _RopeRep versions.
500
501 #define __ROPE_DEFINE_ALLOCS(__a) \
502 __ROPE_DEFINE_ALLOC(_CharT,_Data) /* character data */ \
503 typedef _Rope_RopeConcatenation<_CharT,__a> __C; \
504 __ROPE_DEFINE_ALLOC(__C,_C) \
505 typedef _Rope_RopeLeaf<_CharT,__a> __L; \
506 __ROPE_DEFINE_ALLOC(__L,_L) \
507 typedef _Rope_RopeFunction<_CharT,__a> __F; \
508 __ROPE_DEFINE_ALLOC(__F,_F) \
509 typedef _Rope_RopeSubstring<_CharT,__a> __S; \
510 __ROPE_DEFINE_ALLOC(__S,_S)
511
512 // Internal rope nodes potentially store a copy of the allocator
513 // instance used to allocate them. This is mostly redundant.
514 // But the alternative would be to pass allocator instances around
515 // in some form to nearly all internal functions, since any pointer
516 // assignment may result in a zero reference count and thus require
517 // deallocation.
518
519 #define __STATIC_IF_SGI_ALLOC /* not static */
520
521 template <class _CharT, class _Alloc>
522 struct _Rope_rep_base
523 : public _Alloc
524 {
525 typedef _Alloc allocator_type;
526
527 allocator_type
528 get_allocator() const
529 { return *static_cast<const _Alloc*>(this); }
530
531 _Rope_rep_base(size_t __size, const allocator_type&)
532 : _M_size(__size) {}
533
534 size_t _M_size;
535
536 # define __ROPE_DEFINE_ALLOC(_Tp, __name) \
537 typedef typename \
538 _Alloc::template rebind<_Tp>::other __name##Alloc; \
539 static _Tp* __name##_allocate(size_t __n) \
540 { return __name##Alloc().allocate(__n); } \
541 static void __name##_deallocate(_Tp *__p, size_t __n) \
542 { __name##Alloc().deallocate(__p, __n); }
543 __ROPE_DEFINE_ALLOCS(_Alloc)
544 # undef __ROPE_DEFINE_ALLOC
545 };
546
547 namespace _Rope_constants
548 {
549 enum { _S_max_rope_depth = 45 };
550 enum _Tag {_S_leaf, _S_concat, _S_substringfn, _S_function};
551 }
552
553 template<class _CharT, class _Alloc>
554 struct _Rope_RopeRep
555 : public _Rope_rep_base<_CharT, _Alloc>
556 # ifndef __GC
557 , _Refcount_Base
558 # endif
559 {
560 public:
561 _Rope_constants::_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. */
567 /* typically 0. */
568 /* If it's not 0, then the memory is owned */
569 /* by this node. */
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
573 allocator_type;
574
575 using _Rope_rep_base<_CharT, _Alloc>::get_allocator;
576
577 _Rope_RopeRep(_Rope_constants::_Tag __t, int __d, bool __b, size_t __size,
578 allocator_type __a)
579 : _Rope_rep_base<_CharT, _Alloc>(__size, __a),
580 #ifndef __GC
581 _Refcount_Base(1),
582 #endif
583 _M_tag(__t), _M_is_balanced(__b), _M_depth(__d), _M_c_string(0)
584 #ifdef __GTHREAD_MUTEX_INIT
585 {
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;
589 }
590 #else
591 { __GTHREAD_MUTEX_INIT_FUNCTION (&_M_c_string_lock); }
592 #endif
593 #ifdef __GC
594 void
595 _M_incr () {}
596 #endif
597 static void
598 _S_free_string(__GC_CONST _CharT*, size_t __len,
599 allocator_type __a);
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.
605 // an allocator.
606 // Does nothing if __GC is defined.
607 #ifndef __GC
608 void _M_free_c_string();
609 void _M_free_tree();
610 // Deallocate t. Assumes t is not 0.
611 void
612 _M_unref_nonnil()
613 {
614 if (0 == _M_decr())
615 _M_free_tree();
616 }
617
618 void
619 _M_ref_nonnil()
620 { _M_incr(); }
621
622 static void
623 _S_unref(_Rope_RopeRep* __t)
624 {
625 if (0 != __t)
626 __t->_M_unref_nonnil();
627 }
628
629 static void
630 _S_ref(_Rope_RopeRep* __t)
631 {
632 if (0 != __t)
633 __t->_M_incr();
634 }
635
636 static void
637 _S_free_if_unref(_Rope_RopeRep* __t)
638 {
639 if (0 != __t && 0 == __t->_M_ref_count)
640 __t->_M_free_tree();
641 }
642 # else /* __GC */
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*) {}
648 # endif
649 protected:
650 _Rope_RopeRep&
651 operator=(const _Rope_RopeRep&);
652
653 _Rope_RopeRep(const _Rope_RopeRep&);
654 };
655
656 template<class _CharT, class _Alloc>
657 struct _Rope_RopeLeaf
658 : public _Rope_RopeRep<_CharT, _Alloc>
659 {
660 public:
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 };
666
667 static size_t
668 _S_rounded_up_size(size_t __n)
669 {
670 size_t __size_with_eos;
671
672 if (_S_is_basic_char_type((_CharT*)0))
673 __size_with_eos = __n + 1;
674 else
675 __size_with_eos = __n;
676 #ifdef __GC
677 return __size_with_eos;
678 #else
679 // Allow slop for in-place expansion.
680 return ((__size_with_eos + _S_alloc_granularity - 1)
681 &~ (_S_alloc_granularity - 1));
682 #endif
683 }
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
690 allocator_type;
691
692 _Rope_RopeLeaf(__GC_CONST _CharT* __d, size_t __size,
693 allocator_type __a)
694 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_leaf, 0, true,
695 __size, __a), _M_data(__d)
696 {
697 if (_S_is_basic_char_type((_CharT *)0))
698 {
699 // already eos terminated.
700 this->_M_c_string = __d;
701 }
702 }
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:
706 #ifndef __GC
707 ~_Rope_RopeLeaf() throw()
708 {
709 if (_M_data != this->_M_c_string)
710 this->_M_free_c_string();
711
712 __STL_FREE_STRING(_M_data, this->_M_size, this->get_allocator());
713 }
714 #endif
715 protected:
716 _Rope_RopeLeaf&
717 operator=(const _Rope_RopeLeaf&);
718
719 _Rope_RopeLeaf(const _Rope_RopeLeaf&);
720 };
721
722 template<class _CharT, class _Alloc>
723 struct _Rope_RopeConcatenation
724 : public _Rope_RopeRep<_CharT, _Alloc>
725 {
726 public:
727 _Rope_RopeRep<_CharT, _Alloc>* _M_left;
728 _Rope_RopeRep<_CharT, _Alloc>* _M_right;
729
730 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
731 allocator_type;
732
733 _Rope_RopeConcatenation(_Rope_RopeRep<_CharT, _Alloc>* __l,
734 _Rope_RopeRep<_CharT, _Alloc>* __r,
735 allocator_type __a)
736 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_concat,
737 std::max(__l->_M_depth,
738 __r->_M_depth) + 1,
739 false,
740 __l->_M_size + __r->_M_size, __a),
741 _M_left(__l), _M_right(__r)
742 { }
743 #ifndef __GC
744 ~_Rope_RopeConcatenation() throw()
745 {
746 this->_M_free_c_string();
747 _M_left->_M_unref_nonnil();
748 _M_right->_M_unref_nonnil();
749 }
750 #endif
751 protected:
752 _Rope_RopeConcatenation&
753 operator=(const _Rope_RopeConcatenation&);
754
755 _Rope_RopeConcatenation(const _Rope_RopeConcatenation&);
756 };
757
758 template<class _CharT, class _Alloc>
759 struct _Rope_RopeFunction
760 : public _Rope_RopeRep<_CharT, _Alloc>
761 {
762 public:
763 char_producer<_CharT>* _M_fn;
764 #ifndef __GC
765 bool _M_delete_when_done; // Char_producer is owned by the
766 // rope and should be explicitly
767 // deleted when the rope becomes
768 // inaccessible.
769 #else
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
774 // collector.
775 static void
776 _S_fn_finalization_proc(void * __tree, void *)
777 { delete ((_Rope_RopeFunction *)__tree) -> _M_fn; }
778 #endif
779 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
780 allocator_type;
781
782 _Rope_RopeFunction(char_producer<_CharT>* __f, size_t __size,
783 bool __d, allocator_type __a)
784 : _Rope_RopeRep<_CharT, _Alloc>(_Rope_constants::_S_function,
785 0, true, __size, __a)
786 , _M_fn(__f)
787 #ifndef __GC
788 , _M_delete_when_done(__d)
789 #endif
790 {
791 #ifdef __GC
792 if (__d)
793 {
794 GC_REGISTER_FINALIZER(this, _Rope_RopeFunction::
795 _S_fn_finalization_proc, 0, 0, 0);
796 }
797 #endif
798 }
799 #ifndef __GC
800 ~_Rope_RopeFunction() throw()
801 {
802 this->_M_free_c_string();
803 if (_M_delete_when_done)
804 delete _M_fn;
805 }
806 # endif
807 protected:
808 _Rope_RopeFunction&
809 operator=(const _Rope_RopeFunction&);
810
811 _Rope_RopeFunction(const _Rope_RopeFunction&);
812 };
813 // Substring results are usually represented using just
814 // concatenation nodes. But in the case of very long flat ropes
815 // or ropes with a functional representation that isn't practical.
816 // In that case, we represent the __result as a special case of
817 // RopeFunction, whose char_producer points back to the rope itself.
818 // In all cases except repeated substring operations and
819 // deallocation, we treat the __result as a RopeFunction.
820 template<class _CharT, class _Alloc>
821 struct _Rope_RopeSubstring
822 : public _Rope_RopeFunction<_CharT, _Alloc>,
823 public char_producer<_CharT>
824 {
825 public:
826 // XXX this whole class should be rewritten.
827 _Rope_RopeRep<_CharT,_Alloc>* _M_base; // not 0
828 size_t _M_start;
829
830 virtual void
831 operator()(size_t __start_pos, size_t __req_len,
832 _CharT* __buffer)
833 {
834 switch(_M_base->_M_tag)
835 {
836 case _Rope_constants::_S_function:
837 case _Rope_constants::_S_substringfn:
838 {
839 char_producer<_CharT>* __fn =
840 ((_Rope_RopeFunction<_CharT,_Alloc>*)_M_base)->_M_fn;
841 (*__fn)(__start_pos + _M_start, __req_len, __buffer);
842 }
843 break;
844 case _Rope_constants::_S_leaf:
845 {
846 __GC_CONST _CharT* __s =
847 ((_Rope_RopeLeaf<_CharT,_Alloc>*)_M_base)->_M_data;
848 uninitialized_copy_n(__s + __start_pos + _M_start, __req_len,
849 __buffer);
850 }
851 break;
852 default:
853 break;
854 }
855 }
856
857 typedef typename _Rope_rep_base<_CharT, _Alloc>::allocator_type
858 allocator_type;
859
860 _Rope_RopeSubstring(_Rope_RopeRep<_CharT, _Alloc>* __b, size_t __s,
861 size_t __l, allocator_type __a)
862 : _Rope_RopeFunction<_CharT, _Alloc>(this, __l, false, __a),
863 char_producer<_CharT>(), _M_base(__b), _M_start(__s)
864 {
865 #ifndef __GC
866 _M_base->_M_ref_nonnil();
867 #endif
868 this->_M_tag = _Rope_constants::_S_substringfn;
869 }
870 virtual ~_Rope_RopeSubstring() throw()
871 {
872 #ifndef __GC
873 _M_base->_M_unref_nonnil();
874 // _M_free_c_string(); -- done by parent class
875 #endif
876 }
877 };
878
879 // Self-destructing pointers to Rope_rep.
880 // These are not conventional smart pointers. Their
881 // only purpose in life is to ensure that unref is called
882 // on the pointer either at normal exit or if an exception
883 // is raised. It is the caller's responsibility to
884 // adjust reference counts when these pointers are initialized
885 // or assigned to. (This convention significantly reduces
886 // the number of potentially expensive reference count
887 // updates.)
888 #ifndef __GC
889 template<class _CharT, class _Alloc>
890 struct _Rope_self_destruct_ptr
891 {
892 _Rope_RopeRep<_CharT, _Alloc>* _M_ptr;
893
894 ~_Rope_self_destruct_ptr()
895 { _Rope_RopeRep<_CharT, _Alloc>::_S_unref(_M_ptr); }
896 #ifdef __EXCEPTIONS
897 _Rope_self_destruct_ptr() : _M_ptr(0) {};
898 #else
899 _Rope_self_destruct_ptr() {};
900 #endif
901 _Rope_self_destruct_ptr(_Rope_RopeRep<_CharT, _Alloc>* __p)
902 : _M_ptr(__p) {}
903
904 _Rope_RopeRep<_CharT, _Alloc>&
905 operator*()
906 { return *_M_ptr; }
907
908 _Rope_RopeRep<_CharT, _Alloc>*
909 operator->()
910 { return _M_ptr; }
911
912 operator _Rope_RopeRep<_CharT, _Alloc>*()
913 { return _M_ptr; }
914
915 _Rope_self_destruct_ptr&
916 operator=(_Rope_RopeRep<_CharT, _Alloc>* __x)
917 { _M_ptr = __x; return *this; }
918 };
919 #endif
920
921 // Dereferencing a nonconst iterator has to return something
922 // that behaves almost like a reference. It's not possible to
923 // return an actual reference since assignment requires extra
924 // work. And we would get into the same problems as with the
925 // CD2 version of basic_string.
926 template<class _CharT, class _Alloc>
927 class _Rope_char_ref_proxy
928 {
929 friend class rope<_CharT, _Alloc>;
930 friend class _Rope_iterator<_CharT, _Alloc>;
931 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
932 #ifdef __GC
933 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
934 #else
935 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
936 #endif
937 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
938 typedef rope<_CharT, _Alloc> _My_rope;
939 size_t _M_pos;
940 _CharT _M_current;
941 bool _M_current_valid;
942 _My_rope* _M_root; // The whole rope.
943 public:
944 _Rope_char_ref_proxy(_My_rope* __r, size_t __p)
945 : _M_pos(__p), _M_current(), _M_current_valid(false), _M_root(__r) {}
946
947 _Rope_char_ref_proxy(const _Rope_char_ref_proxy& __x)
948 : _M_pos(__x._M_pos), _M_current(__x._M_current), _M_current_valid(false),
949 _M_root(__x._M_root) {}
950
951 // Don't preserve cache if the reference can outlive the
952 // expression. We claim that's not possible without calling
953 // a copy constructor or generating reference to a proxy
954 // reference. We declare the latter to have undefined semantics.
955 _Rope_char_ref_proxy(_My_rope* __r, size_t __p, _CharT __c)
956 : _M_pos(__p), _M_current(__c), _M_current_valid(true), _M_root(__r) {}
957
958 inline operator _CharT () const;
959
960 _Rope_char_ref_proxy&
961 operator=(_CharT __c);
962
963 _Rope_char_ptr_proxy<_CharT, _Alloc> operator&() const;
964
965 _Rope_char_ref_proxy&
966 operator=(const _Rope_char_ref_proxy& __c)
967 { return operator=((_CharT)__c); }
968 };
969
970 template<class _CharT, class __Alloc>
971 inline void
972 swap(_Rope_char_ref_proxy <_CharT, __Alloc > __a,
973 _Rope_char_ref_proxy <_CharT, __Alloc > __b)
974 {
975 _CharT __tmp = __a;
976 __a = __b;
977 __b = __tmp;
978 }
979
980 template<class _CharT, class _Alloc>
981 class _Rope_char_ptr_proxy
982 {
983 // XXX this class should be rewritten.
984 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
985 size_t _M_pos;
986 rope<_CharT,_Alloc>* _M_root; // The whole rope.
987 public:
988 _Rope_char_ptr_proxy(const _Rope_char_ref_proxy<_CharT,_Alloc>& __x)
989 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
990
991 _Rope_char_ptr_proxy(const _Rope_char_ptr_proxy& __x)
992 : _M_pos(__x._M_pos), _M_root(__x._M_root) {}
993
994 _Rope_char_ptr_proxy() {}
995
996 _Rope_char_ptr_proxy(_CharT* __x)
997 : _M_root(0), _M_pos(0) { }
998
999 _Rope_char_ptr_proxy&
1000 operator=(const _Rope_char_ptr_proxy& __x)
1001 {
1002 _M_pos = __x._M_pos;
1003 _M_root = __x._M_root;
1004 return *this;
1005 }
1006
1007 template<class _CharT2, class _Alloc2>
1008 friend bool
1009 operator==(const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __x,
1010 const _Rope_char_ptr_proxy<_CharT2, _Alloc2>& __y);
1011
1012 _Rope_char_ref_proxy<_CharT, _Alloc> operator*() const
1013 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root, _M_pos); }
1014 };
1015
1016 // Rope iterators:
1017 // Unlike in the C version, we cache only part of the stack
1018 // for rope iterators, since they must be efficiently copyable.
1019 // When we run out of cache, we have to reconstruct the iterator
1020 // value.
1021 // Pointers from iterators are not included in reference counts.
1022 // Iterators are assumed to be thread private. Ropes can
1023 // be shared.
1024
1025 template<class _CharT, class _Alloc>
1026 class _Rope_iterator_base
1027 : public iterator<std::random_access_iterator_tag, _CharT>
1028 {
1029 friend class rope<_CharT, _Alloc>;
1030 public:
1031 typedef _Alloc _allocator_type; // used in _Rope_rotate, VC++ workaround
1032 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1033 // Borland doesn't want this to be protected.
1034 protected:
1035 enum { _S_path_cache_len = 4 }; // Must be <= 9.
1036 enum { _S_iterator_buf_len = 15 };
1037 size_t _M_current_pos;
1038 _RopeRep* _M_root; // The whole rope.
1039 size_t _M_leaf_pos; // Starting position for current leaf
1040 __GC_CONST _CharT* _M_buf_start;
1041 // Buffer possibly
1042 // containing current char.
1043 __GC_CONST _CharT* _M_buf_ptr;
1044 // Pointer to current char in buffer.
1045 // != 0 ==> buffer valid.
1046 __GC_CONST _CharT* _M_buf_end;
1047 // One past __last valid char in buffer.
1048 // What follows is the path cache. We go out of our
1049 // way to make this compact.
1050 // Path_end contains the bottom section of the path from
1051 // the root to the current leaf.
1052 const _RopeRep* _M_path_end[_S_path_cache_len];
1053 int _M_leaf_index; // Last valid __pos in path_end;
1054 // _M_path_end[0] ... _M_path_end[leaf_index-1]
1055 // point to concatenation nodes.
1056 unsigned char _M_path_directions;
1057 // (path_directions >> __i) & 1 is 1
1058 // iff we got from _M_path_end[leaf_index - __i - 1]
1059 // to _M_path_end[leaf_index - __i] by going to the
1060 // __right. Assumes path_cache_len <= 9.
1061 _CharT _M_tmp_buf[_S_iterator_buf_len];
1062 // Short buffer for surrounding chars.
1063 // This is useful primarily for
1064 // RopeFunctions. We put the buffer
1065 // here to avoid locking in the
1066 // multithreaded case.
1067 // The cached path is generally assumed to be valid
1068 // only if the buffer is valid.
1069 static void _S_setbuf(_Rope_iterator_base& __x);
1070 // Set buffer contents given
1071 // path cache.
1072 static void _S_setcache(_Rope_iterator_base& __x);
1073 // Set buffer contents and
1074 // path cache.
1075 static void _S_setcache_for_incr(_Rope_iterator_base& __x);
1076 // As above, but assumes path
1077 // cache is valid for previous posn.
1078 _Rope_iterator_base() {}
1079
1080 _Rope_iterator_base(_RopeRep* __root, size_t __pos)
1081 : _M_current_pos(__pos), _M_root(__root), _M_buf_ptr(0) {}
1082
1083 void _M_incr(size_t __n);
1084 void _M_decr(size_t __n);
1085 public:
1086 size_t
1087 index() const
1088 { return _M_current_pos; }
1089
1090 _Rope_iterator_base(const _Rope_iterator_base& __x)
1091 {
1092 if (0 != __x._M_buf_ptr)
1093 *this = __x;
1094 else
1095 {
1096 _M_current_pos = __x._M_current_pos;
1097 _M_root = __x._M_root;
1098 _M_buf_ptr = 0;
1099 }
1100 }
1101 };
1102
1103 template<class _CharT, class _Alloc>
1104 class _Rope_iterator;
1105
1106 template<class _CharT, class _Alloc>
1107 class _Rope_const_iterator
1108 : public _Rope_iterator_base<_CharT, _Alloc>
1109 {
1110 friend class rope<_CharT, _Alloc>;
1111 protected:
1112 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1113 // The one from the base class may not be directly visible.
1114 _Rope_const_iterator(const _RopeRep* __root, size_t __pos)
1115 : _Rope_iterator_base<_CharT, _Alloc>(const_cast<_RopeRep*>(__root),
1116 __pos)
1117 // Only nonconst iterators modify root ref count
1118 {}
1119 public:
1120 typedef _CharT reference; // Really a value. Returning a reference
1121 // Would be a mess, since it would have
1122 // to be included in refcount.
1123 typedef const _CharT* pointer;
1124
1125 public:
1126 _Rope_const_iterator() {};
1127
1128 _Rope_const_iterator(const _Rope_const_iterator& __x)
1129 : _Rope_iterator_base<_CharT,_Alloc>(__x) { }
1130
1131 _Rope_const_iterator(const _Rope_iterator<_CharT,_Alloc>& __x);
1132
1133 _Rope_const_iterator(const rope<_CharT, _Alloc>& __r, size_t __pos)
1134 : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos) { }
1135
1136 _Rope_const_iterator&
1137 operator=(const _Rope_const_iterator& __x)
1138 {
1139 if (0 != __x._M_buf_ptr)
1140 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1141 else
1142 {
1143 this->_M_current_pos = __x._M_current_pos;
1144 this->_M_root = __x._M_root;
1145 this->_M_buf_ptr = 0;
1146 }
1147 return(*this);
1148 }
1149
1150 reference
1151 operator*()
1152 {
1153 if (0 == this->_M_buf_ptr)
1154 _S_setcache(*this);
1155 return *this->_M_buf_ptr;
1156 }
1157
1158 _Rope_const_iterator&
1159 operator++()
1160 {
1161 __GC_CONST _CharT* __next;
1162 if (0 != this->_M_buf_ptr
1163 && (__next = this->_M_buf_ptr + 1) < this->_M_buf_end)
1164 {
1165 this->_M_buf_ptr = __next;
1166 ++this->_M_current_pos;
1167 }
1168 else
1169 this->_M_incr(1);
1170 return *this;
1171 }
1172
1173 _Rope_const_iterator&
1174 operator+=(ptrdiff_t __n)
1175 {
1176 if (__n >= 0)
1177 this->_M_incr(__n);
1178 else
1179 this->_M_decr(-__n);
1180 return *this;
1181 }
1182
1183 _Rope_const_iterator&
1184 operator--()
1185 {
1186 this->_M_decr(1);
1187 return *this;
1188 }
1189
1190 _Rope_const_iterator&
1191 operator-=(ptrdiff_t __n)
1192 {
1193 if (__n >= 0)
1194 this->_M_decr(__n);
1195 else
1196 this->_M_incr(-__n);
1197 return *this;
1198 }
1199
1200 _Rope_const_iterator
1201 operator++(int)
1202 {
1203 size_t __old_pos = this->_M_current_pos;
1204 this->_M_incr(1);
1205 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1206 // This makes a subsequent dereference expensive.
1207 // Perhaps we should instead copy the iterator
1208 // if it has a valid cache?
1209 }
1210
1211 _Rope_const_iterator
1212 operator--(int)
1213 {
1214 size_t __old_pos = this->_M_current_pos;
1215 this->_M_decr(1);
1216 return _Rope_const_iterator<_CharT,_Alloc>(this->_M_root, __old_pos);
1217 }
1218
1219 template<class _CharT2, class _Alloc2>
1220 friend _Rope_const_iterator<_CharT2, _Alloc2>
1221 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1222 ptrdiff_t __n);
1223
1224 template<class _CharT2, class _Alloc2>
1225 friend _Rope_const_iterator<_CharT2, _Alloc2>
1226 operator+(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1227 ptrdiff_t __n);
1228
1229 template<class _CharT2, class _Alloc2>
1230 friend _Rope_const_iterator<_CharT2, _Alloc2>
1231 operator+(ptrdiff_t __n,
1232 const _Rope_const_iterator<_CharT2, _Alloc2>& __x);
1233
1234 reference
1235 operator[](size_t __n)
1236 { return rope<_CharT, _Alloc>::_S_fetch(this->_M_root,
1237 this->_M_current_pos + __n); }
1238
1239 template<class _CharT2, class _Alloc2>
1240 friend bool
1241 operator==(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1242 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1243
1244 template<class _CharT2, class _Alloc2>
1245 friend bool
1246 operator<(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1247 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1248
1249 template<class _CharT2, class _Alloc2>
1250 friend ptrdiff_t
1251 operator-(const _Rope_const_iterator<_CharT2, _Alloc2>& __x,
1252 const _Rope_const_iterator<_CharT2, _Alloc2>& __y);
1253 };
1254
1255 template<class _CharT, class _Alloc>
1256 class _Rope_iterator
1257 : public _Rope_iterator_base<_CharT, _Alloc>
1258 {
1259 friend class rope<_CharT, _Alloc>;
1260 protected:
1261 typedef typename _Rope_iterator_base<_CharT, _Alloc>::_RopeRep _RopeRep;
1262 rope<_CharT, _Alloc>* _M_root_rope;
1263 // root is treated as a cached version of this,
1264 // and is used to detect changes to the underlying
1265 // rope.
1266 // Root is included in the reference count.
1267 // This is necessary so that we can detect changes reliably.
1268 // Unfortunately, it requires careful bookkeeping for the
1269 // nonGC case.
1270 _Rope_iterator(rope<_CharT, _Alloc>* __r, size_t __pos)
1271 : _Rope_iterator_base<_CharT, _Alloc>(__r->_M_tree_ptr, __pos),
1272 _M_root_rope(__r)
1273 { _RopeRep::_S_ref(this->_M_root);
1274 if (!(__r -> empty()))
1275 _S_setcache(*this);
1276 }
1277
1278 void _M_check();
1279 public:
1280 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1281 typedef _Rope_char_ref_proxy<_CharT, _Alloc>* pointer;
1282
1283 public:
1284 rope<_CharT, _Alloc>&
1285 container()
1286 { return *_M_root_rope; }
1287
1288 _Rope_iterator()
1289 {
1290 this->_M_root = 0; // Needed for reference counting.
1291 };
1292
1293 _Rope_iterator(const _Rope_iterator& __x)
1294 : _Rope_iterator_base<_CharT, _Alloc>(__x)
1295 {
1296 _M_root_rope = __x._M_root_rope;
1297 _RopeRep::_S_ref(this->_M_root);
1298 }
1299
1300 _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos);
1301
1302 ~_Rope_iterator()
1303 { _RopeRep::_S_unref(this->_M_root); }
1304
1305 _Rope_iterator&
1306 operator=(const _Rope_iterator& __x)
1307 {
1308 _RopeRep* __old = this->_M_root;
1309
1310 _RopeRep::_S_ref(__x._M_root);
1311 if (0 != __x._M_buf_ptr)
1312 {
1313 _M_root_rope = __x._M_root_rope;
1314 *(static_cast<_Rope_iterator_base<_CharT, _Alloc>*>(this)) = __x;
1315 }
1316 else
1317 {
1318 this->_M_current_pos = __x._M_current_pos;
1319 this->_M_root = __x._M_root;
1320 _M_root_rope = __x._M_root_rope;
1321 this->_M_buf_ptr = 0;
1322 }
1323 _RopeRep::_S_unref(__old);
1324 return(*this);
1325 }
1326
1327 reference
1328 operator*()
1329 {
1330 _M_check();
1331 if (0 == this->_M_buf_ptr)
1332 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1333 this->_M_current_pos);
1334 else
1335 return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1336 this->_M_current_pos,
1337 *this->_M_buf_ptr);
1338 }
1339
1340 _Rope_iterator&
1341 operator++()
1342 {
1343 this->_M_incr(1);
1344 return *this;
1345 }
1346
1347 _Rope_iterator&
1348 operator+=(ptrdiff_t __n)
1349 {
1350 if (__n >= 0)
1351 this->_M_incr(__n);
1352 else
1353 this->_M_decr(-__n);
1354 return *this;
1355 }
1356
1357 _Rope_iterator&
1358 operator--()
1359 {
1360 this->_M_decr(1);
1361 return *this;
1362 }
1363
1364 _Rope_iterator&
1365 operator-=(ptrdiff_t __n)
1366 {
1367 if (__n >= 0)
1368 this->_M_decr(__n);
1369 else
1370 this->_M_incr(-__n);
1371 return *this;
1372 }
1373
1374 _Rope_iterator
1375 operator++(int)
1376 {
1377 size_t __old_pos = this->_M_current_pos;
1378 this->_M_incr(1);
1379 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1380 }
1381
1382 _Rope_iterator
1383 operator--(int)
1384 {
1385 size_t __old_pos = this->_M_current_pos;
1386 this->_M_decr(1);
1387 return _Rope_iterator<_CharT,_Alloc>(_M_root_rope, __old_pos);
1388 }
1389
1390 reference
1391 operator[](ptrdiff_t __n)
1392 { return _Rope_char_ref_proxy<_CharT, _Alloc>(_M_root_rope,
1393 this->_M_current_pos
1394 + __n); }
1395
1396 template<class _CharT2, class _Alloc2>
1397 friend bool
1398 operator==(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1399 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1400
1401 template<class _CharT2, class _Alloc2>
1402 friend bool
1403 operator<(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1404 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1405
1406 template<class _CharT2, class _Alloc2>
1407 friend ptrdiff_t
1408 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x,
1409 const _Rope_iterator<_CharT2, _Alloc2>& __y);
1410
1411 template<class _CharT2, class _Alloc2>
1412 friend _Rope_iterator<_CharT2, _Alloc2>
1413 operator-(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1414
1415 template<class _CharT2, class _Alloc2>
1416 friend _Rope_iterator<_CharT2, _Alloc2>
1417 operator+(const _Rope_iterator<_CharT2, _Alloc2>& __x, ptrdiff_t __n);
1418
1419 template<class _CharT2, class _Alloc2>
1420 friend _Rope_iterator<_CharT2, _Alloc2>
1421 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT2, _Alloc2>& __x);
1422 };
1423
1424
1425 template <class _CharT, class _Alloc>
1426 struct _Rope_base
1427 : public _Alloc
1428 {
1429 typedef _Alloc allocator_type;
1430
1431 allocator_type
1432 get_allocator() const
1433 { return *static_cast<const _Alloc*>(this); }
1434
1435 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1436 // The one in _Base may not be visible due to template rules.
1437
1438 _Rope_base(_RopeRep* __t, const allocator_type&)
1439 : _M_tree_ptr(__t) {}
1440
1441 _Rope_base(const allocator_type&) {}
1442
1443 // The only data member of a rope:
1444 _RopeRep *_M_tree_ptr;
1445
1446 #define __ROPE_DEFINE_ALLOC(_Tp, __name) \
1447 typedef typename \
1448 _Alloc::template rebind<_Tp>::other __name##Alloc; \
1449 static _Tp* __name##_allocate(size_t __n) \
1450 { return __name##Alloc().allocate(__n); } \
1451 static void __name##_deallocate(_Tp *__p, size_t __n) \
1452 { __name##Alloc().deallocate(__p, __n); }
1453 __ROPE_DEFINE_ALLOCS(_Alloc)
1454 #undef __ROPE_DEFINE_ALLOC
1455
1456 protected:
1457 _Rope_base&
1458 operator=(const _Rope_base&);
1459
1460 _Rope_base(const _Rope_base&);
1461 };
1462
1463 /**
1464 * This is an SGI extension.
1465 * @ingroup SGIextensions
1466 * @doctodo
1467 */
1468 template <class _CharT, class _Alloc>
1469 class rope : public _Rope_base<_CharT, _Alloc>
1470 {
1471 public:
1472 typedef _CharT value_type;
1473 typedef ptrdiff_t difference_type;
1474 typedef size_t size_type;
1475 typedef _CharT const_reference;
1476 typedef const _CharT* const_pointer;
1477 typedef _Rope_iterator<_CharT, _Alloc> iterator;
1478 typedef _Rope_const_iterator<_CharT, _Alloc> const_iterator;
1479 typedef _Rope_char_ref_proxy<_CharT, _Alloc> reference;
1480 typedef _Rope_char_ptr_proxy<_CharT, _Alloc> pointer;
1481
1482 friend class _Rope_iterator<_CharT, _Alloc>;
1483 friend class _Rope_const_iterator<_CharT, _Alloc>;
1484 friend struct _Rope_RopeRep<_CharT, _Alloc>;
1485 friend class _Rope_iterator_base<_CharT, _Alloc>;
1486 friend class _Rope_char_ptr_proxy<_CharT, _Alloc>;
1487 friend class _Rope_char_ref_proxy<_CharT, _Alloc>;
1488 friend struct _Rope_RopeSubstring<_CharT, _Alloc>;
1489
1490 protected:
1491 typedef _Rope_base<_CharT, _Alloc> _Base;
1492 typedef typename _Base::allocator_type allocator_type;
1493 using _Base::_M_tree_ptr;
1494 using _Base::get_allocator;
1495 typedef __GC_CONST _CharT* _Cstrptr;
1496
1497 static _CharT _S_empty_c_str[1];
1498
1499 static bool
1500 _S_is0(_CharT __c)
1501 { return __c == _S_eos((_CharT*)0); }
1502
1503 enum { _S_copy_max = 23 };
1504 // For strings shorter than _S_copy_max, we copy to
1505 // concatenate.
1506
1507 typedef _Rope_RopeRep<_CharT, _Alloc> _RopeRep;
1508 typedef _Rope_RopeConcatenation<_CharT, _Alloc> _RopeConcatenation;
1509 typedef _Rope_RopeLeaf<_CharT, _Alloc> _RopeLeaf;
1510 typedef _Rope_RopeFunction<_CharT, _Alloc> _RopeFunction;
1511 typedef _Rope_RopeSubstring<_CharT, _Alloc> _RopeSubstring;
1512
1513 // Retrieve a character at the indicated position.
1514 static _CharT _S_fetch(_RopeRep* __r, size_type __pos);
1515
1516 #ifndef __GC
1517 // Obtain a pointer to the character at the indicated position.
1518 // The pointer can be used to change the character.
1519 // If such a pointer cannot be produced, as is frequently the
1520 // case, 0 is returned instead.
1521 // (Returns nonzero only if all nodes in the path have a refcount
1522 // of 1.)
1523 static _CharT* _S_fetch_ptr(_RopeRep* __r, size_type __pos);
1524 #endif
1525
1526 static bool
1527 _S_apply_to_pieces(// should be template parameter
1528 _Rope_char_consumer<_CharT>& __c,
1529 const _RopeRep* __r,
1530 size_t __begin, size_t __end);
1531 // begin and end are assumed to be in range.
1532
1533 #ifndef __GC
1534 static void
1535 _S_unref(_RopeRep* __t)
1536 { _RopeRep::_S_unref(__t); }
1537
1538 static void
1539 _S_ref(_RopeRep* __t)
1540 { _RopeRep::_S_ref(__t); }
1541
1542 #else /* __GC */
1543 static void _S_unref(_RopeRep*) {}
1544 static void _S_ref(_RopeRep*) {}
1545 #endif
1546
1547 #ifdef __GC
1548 typedef _Rope_RopeRep<_CharT, _Alloc>* _Self_destruct_ptr;
1549 #else
1550 typedef _Rope_self_destruct_ptr<_CharT, _Alloc> _Self_destruct_ptr;
1551 #endif
1552
1553 // _Result is counted in refcount.
1554 static _RopeRep* _S_substring(_RopeRep* __base,
1555 size_t __start, size_t __endp1);
1556
1557 static _RopeRep* _S_concat_char_iter(_RopeRep* __r,
1558 const _CharT* __iter, size_t __slen);
1559 // Concatenate rope and char ptr, copying __s.
1560 // Should really take an arbitrary iterator.
1561 // Result is counted in refcount.
1562 static _RopeRep* _S_destr_concat_char_iter(_RopeRep* __r,
1563 const _CharT* __iter,
1564 size_t __slen)
1565 // As above, but one reference to __r is about to be
1566 // destroyed. Thus the pieces may be recycled if all
1567 // relevant reference counts are 1.
1568 #ifdef __GC
1569 // We can't really do anything since refcounts are unavailable.
1570 { return _S_concat_char_iter(__r, __iter, __slen); }
1571 #else
1572 ;
1573 #endif
1574
1575 static _RopeRep* _S_concat(_RopeRep* __left, _RopeRep* __right);
1576 // General concatenation on _RopeRep. _Result
1577 // has refcount of 1. Adjusts argument refcounts.
1578
1579 public:
1580 void
1581 apply_to_pieces(size_t __begin, size_t __end,
1582 _Rope_char_consumer<_CharT>& __c) const
1583 { _S_apply_to_pieces(__c, this->_M_tree_ptr, __begin, __end); }
1584
1585 protected:
1586
1587 static size_t
1588 _S_rounded_up_size(size_t __n)
1589 { return _RopeLeaf::_S_rounded_up_size(__n); }
1590
1591 static size_t
1592 _S_allocated_capacity(size_t __n)
1593 {
1594 if (_S_is_basic_char_type((_CharT*)0))
1595 return _S_rounded_up_size(__n) - 1;
1596 else
1597 return _S_rounded_up_size(__n);
1598
1599 }
1600
1601 // Allocate and construct a RopeLeaf using the supplied allocator
1602 // Takes ownership of s instead of copying.
1603 static _RopeLeaf*
1604 _S_new_RopeLeaf(__GC_CONST _CharT *__s,
1605 size_t __size, allocator_type __a)
1606 {
1607 _RopeLeaf* __space = typename _Base::_LAlloc(__a).allocate(1);
1608 return new(__space) _RopeLeaf(__s, __size, __a);
1609 }
1610
1611 static _RopeConcatenation*
1612 _S_new_RopeConcatenation(_RopeRep* __left, _RopeRep* __right,
1613 allocator_type __a)
1614 {
1615 _RopeConcatenation* __space = typename _Base::_CAlloc(__a).allocate(1);
1616 return new(__space) _RopeConcatenation(__left, __right, __a);
1617 }
1618
1619 static _RopeFunction*
1620 _S_new_RopeFunction(char_producer<_CharT>* __f,
1621 size_t __size, bool __d, allocator_type __a)
1622 {
1623 _RopeFunction* __space = typename _Base::_FAlloc(__a).allocate(1);
1624 return new(__space) _RopeFunction(__f, __size, __d, __a);
1625 }
1626
1627 static _RopeSubstring*
1628 _S_new_RopeSubstring(_Rope_RopeRep<_CharT,_Alloc>* __b, size_t __s,
1629 size_t __l, allocator_type __a)
1630 {
1631 _RopeSubstring* __space = typename _Base::_SAlloc(__a).allocate(1);
1632 return new(__space) _RopeSubstring(__b, __s, __l, __a);
1633 }
1634
1635 static _RopeLeaf*
1636 _S_RopeLeaf_from_unowned_char_ptr(const _CharT *__s,
1637 size_t __size, allocator_type __a)
1638 #define __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __size, __a) \
1639 _S_RopeLeaf_from_unowned_char_ptr(__s, __size, __a)
1640 {
1641 if (0 == __size)
1642 return 0;
1643 _CharT* __buf = __a.allocate(_S_rounded_up_size(__size));
1644
1645 uninitialized_copy_n(__s, __size, __buf);
1646 _S_cond_store_eos(__buf[__size]);
1647 try
1648 { return _S_new_RopeLeaf(__buf, __size, __a); }
1649 catch(...)
1650 {
1651 _RopeRep::__STL_FREE_STRING(__buf, __size, __a);
1652 __throw_exception_again;
1653 }
1654 }
1655
1656 // Concatenation of nonempty strings.
1657 // Always builds a concatenation node.
1658 // Rebalances if the result is too deep.
1659 // Result has refcount 1.
1660 // Does not increment left and right ref counts even though
1661 // they are referenced.
1662 static _RopeRep*
1663 _S_tree_concat(_RopeRep* __left, _RopeRep* __right);
1664
1665 // Concatenation helper functions
1666 static _RopeLeaf*
1667 _S_leaf_concat_char_iter(_RopeLeaf* __r,
1668 const _CharT* __iter, size_t __slen);
1669 // Concatenate by copying leaf.
1670 // should take an arbitrary iterator
1671 // result has refcount 1.
1672 #ifndef __GC
1673 static _RopeLeaf*
1674 _S_destr_leaf_concat_char_iter(_RopeLeaf* __r,
1675 const _CharT* __iter, size_t __slen);
1676 // A version that potentially clobbers __r if __r->_M_ref_count == 1.
1677 #endif
1678
1679 private:
1680
1681 static size_t _S_char_ptr_len(const _CharT* __s);
1682 // slightly generalized strlen
1683
1684 rope(_RopeRep* __t, const allocator_type& __a = allocator_type())
1685 : _Base(__t, __a) { }
1686
1687
1688 // Copy __r to the _CharT buffer.
1689 // Returns __buffer + __r->_M_size.
1690 // Assumes that buffer is uninitialized.
1691 static _CharT* _S_flatten(_RopeRep* __r, _CharT* __buffer);
1692
1693 // Again, with explicit starting position and length.
1694 // Assumes that buffer is uninitialized.
1695 static _CharT* _S_flatten(_RopeRep* __r,
1696 size_t __start, size_t __len,
1697 _CharT* __buffer);
1698
1699 static const unsigned long
1700 _S_min_len[_Rope_constants::_S_max_rope_depth + 1];
1701
1702 static bool
1703 _S_is_balanced(_RopeRep* __r)
1704 { return (__r->_M_size >= _S_min_len[__r->_M_depth]); }
1705
1706 static bool
1707 _S_is_almost_balanced(_RopeRep* __r)
1708 { return (__r->_M_depth == 0
1709 || __r->_M_size >= _S_min_len[__r->_M_depth - 1]); }
1710
1711 static bool
1712 _S_is_roughly_balanced(_RopeRep* __r)
1713 { return (__r->_M_depth <= 1
1714 || __r->_M_size >= _S_min_len[__r->_M_depth - 2]); }
1715
1716 // Assumes the result is not empty.
1717 static _RopeRep*
1718 _S_concat_and_set_balanced(_RopeRep* __left, _RopeRep* __right)
1719 {
1720 _RopeRep* __result = _S_concat(__left, __right);
1721 if (_S_is_balanced(__result))
1722 __result->_M_is_balanced = true;
1723 return __result;
1724 }
1725
1726 // The basic rebalancing operation. Logically copies the
1727 // rope. The result has refcount of 1. The client will
1728 // usually decrement the reference count of __r.
1729 // The result is within height 2 of balanced by the above
1730 // definition.
1731 static _RopeRep* _S_balance(_RopeRep* __r);
1732
1733 // Add all unbalanced subtrees to the forest of balanceed trees.
1734 // Used only by balance.
1735 static void _S_add_to_forest(_RopeRep*__r, _RopeRep** __forest);
1736
1737 // Add __r to forest, assuming __r is already balanced.
1738 static void _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest);
1739
1740 // Print to stdout, exposing structure
1741 static void _S_dump(_RopeRep* __r, int __indent = 0);
1742
1743 // Return -1, 0, or 1 if __x < __y, __x == __y, or __x > __y resp.
1744 static int _S_compare(const _RopeRep* __x, const _RopeRep* __y);
1745
1746 public:
1747 bool
1748 empty() const
1749 { return 0 == this->_M_tree_ptr; }
1750
1751 // Comparison member function. This is public only for those
1752 // clients that need a ternary comparison. Others
1753 // should use the comparison operators below.
1754 int
1755 compare(const rope& __y) const
1756 { return _S_compare(this->_M_tree_ptr, __y._M_tree_ptr); }
1757
1758 rope(const _CharT* __s, const allocator_type& __a = allocator_type())
1759 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, _S_char_ptr_len(__s),
1760 __a), __a)
1761 { }
1762
1763 rope(const _CharT* __s, size_t __len,
1764 const allocator_type& __a = allocator_type())
1765 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __len, __a), __a)
1766 { }
1767
1768 // Should perhaps be templatized with respect to the iterator type
1769 // and use Sequence_buffer. (It should perhaps use sequence_buffer
1770 // even now.)
1771 rope(const _CharT *__s, const _CharT *__e,
1772 const allocator_type& __a = allocator_type())
1773 : _Base(__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __e - __s, __a), __a)
1774 { }
1775
1776 rope(const const_iterator& __s, const const_iterator& __e,
1777 const allocator_type& __a = allocator_type())
1778 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1779 __e._M_current_pos), __a)
1780 { }
1781
1782 rope(const iterator& __s, const iterator& __e,
1783 const allocator_type& __a = allocator_type())
1784 : _Base(_S_substring(__s._M_root, __s._M_current_pos,
1785 __e._M_current_pos), __a)
1786 { }
1787
1788 rope(_CharT __c, const allocator_type& __a = allocator_type())
1789 : _Base(__a)
1790 {
1791 _CharT* __buf = this->_Data_allocate(_S_rounded_up_size(1));
1792
1793 std::_Construct(__buf, __c);
1794 try
1795 { this->_M_tree_ptr = _S_new_RopeLeaf(__buf, 1, __a); }
1796 catch(...)
1797 {
1798 _RopeRep::__STL_FREE_STRING(__buf, 1, __a);
1799 __throw_exception_again;
1800 }
1801 }
1802
1803 rope(size_t __n, _CharT __c,
1804 const allocator_type& __a = allocator_type());
1805
1806 rope(const allocator_type& __a = allocator_type())
1807 : _Base(0, __a) {}
1808
1809 // Construct a rope from a function that can compute its members
1810 rope(char_producer<_CharT> *__fn, size_t __len, bool __delete_fn,
1811 const allocator_type& __a = allocator_type())
1812 : _Base(__a)
1813 {
1814 this->_M_tree_ptr = (0 == __len) ?
1815 0 : _S_new_RopeFunction(__fn, __len, __delete_fn, __a);
1816 }
1817
1818 rope(const rope& __x, const allocator_type& __a = allocator_type())
1819 : _Base(__x._M_tree_ptr, __a)
1820 { _S_ref(this->_M_tree_ptr); }
1821
1822 ~rope() throw()
1823 { _S_unref(this->_M_tree_ptr); }
1824
1825 rope&
1826 operator=(const rope& __x)
1827 {
1828 _RopeRep* __old = this->_M_tree_ptr;
1829 this->_M_tree_ptr = __x._M_tree_ptr;
1830 _S_ref(this->_M_tree_ptr);
1831 _S_unref(__old);
1832 return *this;
1833 }
1834
1835 void
1836 clear()
1837 {
1838 _S_unref(this->_M_tree_ptr);
1839 this->_M_tree_ptr = 0;
1840 }
1841
1842 void
1843 push_back(_CharT __x)
1844 {
1845 _RopeRep* __old = this->_M_tree_ptr;
1846 this->_M_tree_ptr
1847 = _S_destr_concat_char_iter(this->_M_tree_ptr, &__x, 1);
1848 _S_unref(__old);
1849 }
1850
1851 void
1852 pop_back()
1853 {
1854 _RopeRep* __old = this->_M_tree_ptr;
1855 this->_M_tree_ptr =
1856 _S_substring(this->_M_tree_ptr,
1857 0, this->_M_tree_ptr->_M_size - 1);
1858 _S_unref(__old);
1859 }
1860
1861 _CharT
1862 back() const
1863 { return _S_fetch(this->_M_tree_ptr, this->_M_tree_ptr->_M_size - 1); }
1864
1865 void
1866 push_front(_CharT __x)
1867 {
1868 _RopeRep* __old = this->_M_tree_ptr;
1869 _RopeRep* __left =
1870 __STL_ROPE_FROM_UNOWNED_CHAR_PTR(&__x, 1, this->get_allocator());
1871 try
1872 {
1873 this->_M_tree_ptr = _S_concat(__left, this->_M_tree_ptr);
1874 _S_unref(__old);
1875 _S_unref(__left);
1876 }
1877 catch(...)
1878 {
1879 _S_unref(__left);
1880 __throw_exception_again;
1881 }
1882 }
1883
1884 void
1885 pop_front()
1886 {
1887 _RopeRep* __old = this->_M_tree_ptr;
1888 this->_M_tree_ptr
1889 = _S_substring(this->_M_tree_ptr, 1, this->_M_tree_ptr->_M_size);
1890 _S_unref(__old);
1891 }
1892
1893 _CharT
1894 front() const
1895 { return _S_fetch(this->_M_tree_ptr, 0); }
1896
1897 void
1898 balance()
1899 {
1900 _RopeRep* __old = this->_M_tree_ptr;
1901 this->_M_tree_ptr = _S_balance(this->_M_tree_ptr);
1902 _S_unref(__old);
1903 }
1904
1905 void
1906 copy(_CharT* __buffer) const
1907 {
1908 _Destroy(__buffer, __buffer + size());
1909 _S_flatten(this->_M_tree_ptr, __buffer);
1910 }
1911
1912 // This is the copy function from the standard, but
1913 // with the arguments reordered to make it consistent with the
1914 // rest of the interface.
1915 // Note that this guaranteed not to compile if the draft standard
1916 // order is assumed.
1917 size_type
1918 copy(size_type __pos, size_type __n, _CharT* __buffer) const
1919 {
1920 size_t __size = size();
1921 size_t __len = (__pos + __n > __size? __size - __pos : __n);
1922
1923 _Destroy(__buffer, __buffer + __len);
1924 _S_flatten(this->_M_tree_ptr, __pos, __len, __buffer);
1925 return __len;
1926 }
1927
1928 // Print to stdout, exposing structure. May be useful for
1929 // performance debugging.
1930 void
1931 dump()
1932 { _S_dump(this->_M_tree_ptr); }
1933
1934 // Convert to 0 terminated string in new allocated memory.
1935 // Embedded 0s in the input do not terminate the copy.
1936 const _CharT* c_str() const;
1937
1938 // As above, but lso use the flattened representation as the
1939 // the new rope representation.
1940 const _CharT* replace_with_c_str();
1941
1942 // Reclaim memory for the c_str generated flattened string.
1943 // Intentionally undocumented, since it's hard to say when this
1944 // is safe for multiple threads.
1945 void
1946 delete_c_str ()
1947 {
1948 if (0 == this->_M_tree_ptr)
1949 return;
1950 if (_Rope_constants::_S_leaf == this->_M_tree_ptr->_M_tag &&
1951 ((_RopeLeaf*)this->_M_tree_ptr)->_M_data ==
1952 this->_M_tree_ptr->_M_c_string)
1953 {
1954 // Representation shared
1955 return;
1956 }
1957 #ifndef __GC
1958 this->_M_tree_ptr->_M_free_c_string();
1959 #endif
1960 this->_M_tree_ptr->_M_c_string = 0;
1961 }
1962
1963 _CharT
1964 operator[] (size_type __pos) const
1965 { return _S_fetch(this->_M_tree_ptr, __pos); }
1966
1967 _CharT
1968 at(size_type __pos) const
1969 {
1970 // if (__pos >= size()) throw out_of_range; // XXX
1971 return (*this)[__pos];
1972 }
1973
1974 const_iterator
1975 begin() const
1976 { return(const_iterator(this->_M_tree_ptr, 0)); }
1977
1978 // An easy way to get a const iterator from a non-const container.
1979 const_iterator
1980 const_begin() const
1981 { return(const_iterator(this->_M_tree_ptr, 0)); }
1982
1983 const_iterator
1984 end() const
1985 { return(const_iterator(this->_M_tree_ptr, size())); }
1986
1987 const_iterator
1988 const_end() const
1989 { return(const_iterator(this->_M_tree_ptr, size())); }
1990
1991 size_type
1992 size() const
1993 { return(0 == this->_M_tree_ptr? 0 : this->_M_tree_ptr->_M_size); }
1994
1995 size_type
1996 length() const
1997 { return size(); }
1998
1999 size_type
2000 max_size() const
2001 {
2002 return _S_min_len[_Rope_constants::_S_max_rope_depth - 1] - 1;
2003 // Guarantees that the result can be sufficirntly
2004 // balanced. Longer ropes will probably still work,
2005 // but it's harder to make guarantees.
2006 }
2007
2008 typedef reverse_iterator<const_iterator> const_reverse_iterator;
2009
2010 const_reverse_iterator
2011 rbegin() const
2012 { return const_reverse_iterator(end()); }
2013
2014 const_reverse_iterator
2015 const_rbegin() const
2016 { return const_reverse_iterator(end()); }
2017
2018 const_reverse_iterator
2019 rend() const
2020 { return const_reverse_iterator(begin()); }
2021
2022 const_reverse_iterator
2023 const_rend() const
2024 { return const_reverse_iterator(begin()); }
2025
2026 template<class _CharT2, class _Alloc2>
2027 friend rope<_CharT2, _Alloc2>
2028 operator+(const rope<_CharT2, _Alloc2>& __left,
2029 const rope<_CharT2, _Alloc2>& __right);
2030
2031 template<class _CharT2, class _Alloc2>
2032 friend rope<_CharT2, _Alloc2>
2033 operator+(const rope<_CharT2, _Alloc2>& __left, const _CharT2* __right);
2034
2035 template<class _CharT2, class _Alloc2>
2036 friend rope<_CharT2, _Alloc2>
2037 operator+(const rope<_CharT2, _Alloc2>& __left, _CharT2 __right);
2038 // The symmetric cases are intentionally omitted, since they're presumed
2039 // to be less common, and we don't handle them as well.
2040
2041 // The following should really be templatized.
2042 // The first argument should be an input iterator or
2043 // forward iterator with value_type _CharT.
2044 rope&
2045 append(const _CharT* __iter, size_t __n)
2046 {
2047 _RopeRep* __result =
2048 _S_destr_concat_char_iter(this->_M_tree_ptr, __iter, __n);
2049 _S_unref(this->_M_tree_ptr);
2050 this->_M_tree_ptr = __result;
2051 return *this;
2052 }
2053
2054 rope&
2055 append(const _CharT* __c_string)
2056 {
2057 size_t __len = _S_char_ptr_len(__c_string);
2058 append(__c_string, __len);
2059 return(*this);
2060 }
2061
2062 rope&
2063 append(const _CharT* __s, const _CharT* __e)
2064 {
2065 _RopeRep* __result =
2066 _S_destr_concat_char_iter(this->_M_tree_ptr, __s, __e - __s);
2067 _S_unref(this->_M_tree_ptr);
2068 this->_M_tree_ptr = __result;
2069 return *this;
2070 }
2071
2072 rope&
2073 append(const_iterator __s, const_iterator __e)
2074 {
2075 _Self_destruct_ptr __appendee(_S_substring(__s._M_root,
2076 __s._M_current_pos,
2077 __e._M_current_pos));
2078 _RopeRep* __result =
2079 _S_concat(this->_M_tree_ptr, (_RopeRep*)__appendee);
2080 _S_unref(this->_M_tree_ptr);
2081 this->_M_tree_ptr = __result;
2082 return *this;
2083 }
2084
2085 rope&
2086 append(_CharT __c)
2087 {
2088 _RopeRep* __result =
2089 _S_destr_concat_char_iter(this->_M_tree_ptr, &__c, 1);
2090 _S_unref(this->_M_tree_ptr);
2091 this->_M_tree_ptr = __result;
2092 return *this;
2093 }
2094
2095 rope&
2096 append()
2097 { return append(_CharT()); } // XXX why?
2098
2099 rope&
2100 append(const rope& __y)
2101 {
2102 _RopeRep* __result = _S_concat(this->_M_tree_ptr, __y._M_tree_ptr);
2103 _S_unref(this->_M_tree_ptr);
2104 this->_M_tree_ptr = __result;
2105 return *this;
2106 }
2107
2108 rope&
2109 append(size_t __n, _CharT __c)
2110 {
2111 rope<_CharT,_Alloc> __last(__n, __c);
2112 return append(__last);
2113 }
2114
2115 void
2116 swap(rope& __b)
2117 {
2118 _RopeRep* __tmp = this->_M_tree_ptr;
2119 this->_M_tree_ptr = __b._M_tree_ptr;
2120 __b._M_tree_ptr = __tmp;
2121 }
2122
2123 protected:
2124 // Result is included in refcount.
2125 static _RopeRep*
2126 replace(_RopeRep* __old, size_t __pos1,
2127 size_t __pos2, _RopeRep* __r)
2128 {
2129 if (0 == __old)
2130 {
2131 _S_ref(__r);
2132 return __r;
2133 }
2134 _Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
2135 _Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size));
2136 _RopeRep* __result;
2137
2138 if (0 == __r)
2139 __result = _S_concat(__left, __right);
2140 else
2141 {
2142 _Self_destruct_ptr __left_result(_S_concat(__left, __r));
2143 __result = _S_concat(__left_result, __right);
2144 }
2145 return __result;
2146 }
2147
2148 public:
2149 void
2150 insert(size_t __p, const rope& __r)
2151 {
2152 _RopeRep* __result =
2153 replace(this->_M_tree_ptr, __p, __p, __r._M_tree_ptr);
2154 _S_unref(this->_M_tree_ptr);
2155 this->_M_tree_ptr = __result;
2156 }
2157
2158 void
2159 insert(size_t __p, size_t __n, _CharT __c)
2160 {
2161 rope<_CharT,_Alloc> __r(__n,__c);
2162 insert(__p, __r);
2163 }
2164
2165 void
2166 insert(size_t __p, const _CharT* __i, size_t __n)
2167 {
2168 _Self_destruct_ptr __left(_S_substring(this->_M_tree_ptr, 0, __p));
2169 _Self_destruct_ptr __right(_S_substring(this->_M_tree_ptr,
2170 __p, size()));
2171 _Self_destruct_ptr __left_result(_S_concat_char_iter(__left, __i, __n));
2172 // _S_ destr_concat_char_iter should be safe here.
2173 // But as it stands it's probably not a win, since __left
2174 // is likely to have additional references.
2175 _RopeRep* __result = _S_concat(__left_result, __right);
2176 _S_unref(this->_M_tree_ptr);
2177 this->_M_tree_ptr = __result;
2178 }
2179
2180 void
2181 insert(size_t __p, const _CharT* __c_string)
2182 { insert(__p, __c_string, _S_char_ptr_len(__c_string)); }
2183
2184 void
2185 insert(size_t __p, _CharT __c)
2186 { insert(__p, &__c, 1); }
2187
2188 void
2189 insert(size_t __p)
2190 {
2191 _CharT __c = _CharT();
2192 insert(__p, &__c, 1);
2193 }
2194
2195 void
2196 insert(size_t __p, const _CharT* __i, const _CharT* __j)
2197 {
2198 rope __r(__i, __j);
2199 insert(__p, __r);
2200 }
2201
2202 void
2203 insert(size_t __p, const const_iterator& __i,
2204 const const_iterator& __j)
2205 {
2206 rope __r(__i, __j);
2207 insert(__p, __r);
2208 }
2209
2210 void
2211 insert(size_t __p, const iterator& __i,
2212 const iterator& __j)
2213 {
2214 rope __r(__i, __j);
2215 insert(__p, __r);
2216 }
2217
2218 // (position, length) versions of replace operations:
2219
2220 void
2221 replace(size_t __p, size_t __n, const rope& __r)
2222 {
2223 _RopeRep* __result =
2224 replace(this->_M_tree_ptr, __p, __p + __n, __r._M_tree_ptr);
2225 _S_unref(this->_M_tree_ptr);
2226 this->_M_tree_ptr = __result;
2227 }
2228
2229 void
2230 replace(size_t __p, size_t __n,
2231 const _CharT* __i, size_t __i_len)
2232 {
2233 rope __r(__i, __i_len);
2234 replace(__p, __n, __r);
2235 }
2236
2237 void
2238 replace(size_t __p, size_t __n, _CharT __c)
2239 {
2240 rope __r(__c);
2241 replace(__p, __n, __r);
2242 }
2243
2244 void
2245 replace(size_t __p, size_t __n, const _CharT* __c_string)
2246 {
2247 rope __r(__c_string);
2248 replace(__p, __n, __r);
2249 }
2250
2251 void
2252 replace(size_t __p, size_t __n,
2253 const _CharT* __i, const _CharT* __j)
2254 {
2255 rope __r(__i, __j);
2256 replace(__p, __n, __r);
2257 }
2258
2259 void
2260 replace(size_t __p, size_t __n,
2261 const const_iterator& __i, const const_iterator& __j)
2262 {
2263 rope __r(__i, __j);
2264 replace(__p, __n, __r);
2265 }
2266
2267 void
2268 replace(size_t __p, size_t __n,
2269 const iterator& __i, const iterator& __j)
2270 {
2271 rope __r(__i, __j);
2272 replace(__p, __n, __r);
2273 }
2274
2275 // Single character variants:
2276 void
2277 replace(size_t __p, _CharT __c)
2278 {
2279 iterator __i(this, __p);
2280 *__i = __c;
2281 }
2282
2283 void
2284 replace(size_t __p, const rope& __r)
2285 { replace(__p, 1, __r); }
2286
2287 void
2288 replace(size_t __p, const _CharT* __i, size_t __i_len)
2289 { replace(__p, 1, __i, __i_len); }
2290
2291 void
2292 replace(size_t __p, const _CharT* __c_string)
2293 { replace(__p, 1, __c_string); }
2294
2295 void
2296 replace(size_t __p, const _CharT* __i, const _CharT* __j)
2297 { replace(__p, 1, __i, __j); }
2298
2299 void
2300 replace(size_t __p, const const_iterator& __i,
2301 const const_iterator& __j)
2302 { replace(__p, 1, __i, __j); }
2303
2304 void
2305 replace(size_t __p, const iterator& __i,
2306 const iterator& __j)
2307 { replace(__p, 1, __i, __j); }
2308
2309 // Erase, (position, size) variant.
2310 void
2311 erase(size_t __p, size_t __n)
2312 {
2313 _RopeRep* __result = replace(this->_M_tree_ptr, __p,
2314 __p + __n, 0);
2315 _S_unref(this->_M_tree_ptr);
2316 this->_M_tree_ptr = __result;
2317 }
2318
2319 // Erase, single character
2320 void
2321 erase(size_t __p)
2322 { erase(__p, __p + 1); }
2323
2324 // Insert, iterator variants.
2325 iterator
2326 insert(const iterator& __p, const rope& __r)
2327 {
2328 insert(__p.index(), __r);
2329 return __p;
2330 }
2331
2332 iterator
2333 insert(const iterator& __p, size_t __n, _CharT __c)
2334 {
2335 insert(__p.index(), __n, __c);
2336 return __p;
2337 }
2338
2339 iterator insert(const iterator& __p, _CharT __c)
2340 {
2341 insert(__p.index(), __c);
2342 return __p;
2343 }
2344
2345 iterator
2346 insert(const iterator& __p )
2347 {
2348 insert(__p.index());
2349 return __p;
2350 }
2351
2352 iterator
2353 insert(const iterator& __p, const _CharT* c_string)
2354 {
2355 insert(__p.index(), c_string);
2356 return __p;
2357 }
2358
2359 iterator
2360 insert(const iterator& __p, const _CharT* __i, size_t __n)
2361 {
2362 insert(__p.index(), __i, __n);
2363 return __p;
2364 }
2365
2366 iterator
2367 insert(const iterator& __p, const _CharT* __i,
2368 const _CharT* __j)
2369 {
2370 insert(__p.index(), __i, __j);
2371 return __p;
2372 }
2373
2374 iterator
2375 insert(const iterator& __p,
2376 const const_iterator& __i, const const_iterator& __j)
2377 {
2378 insert(__p.index(), __i, __j);
2379 return __p;
2380 }
2381
2382 iterator
2383 insert(const iterator& __p,
2384 const iterator& __i, const iterator& __j)
2385 {
2386 insert(__p.index(), __i, __j);
2387 return __p;
2388 }
2389
2390 // Replace, range variants.
2391 void
2392 replace(const iterator& __p, const iterator& __q, const rope& __r)
2393 { replace(__p.index(), __q.index() - __p.index(), __r); }
2394
2395 void
2396 replace(const iterator& __p, const iterator& __q, _CharT __c)
2397 { replace(__p.index(), __q.index() - __p.index(), __c); }
2398
2399 void
2400 replace(const iterator& __p, const iterator& __q,
2401 const _CharT* __c_string)
2402 { replace(__p.index(), __q.index() - __p.index(), __c_string); }
2403
2404 void
2405 replace(const iterator& __p, const iterator& __q,
2406 const _CharT* __i, size_t __n)
2407 { replace(__p.index(), __q.index() - __p.index(), __i, __n); }
2408
2409 void
2410 replace(const iterator& __p, const iterator& __q,
2411 const _CharT* __i, const _CharT* __j)
2412 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2413
2414 void
2415 replace(const iterator& __p, const iterator& __q,
2416 const const_iterator& __i, const const_iterator& __j)
2417 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2418
2419 void
2420 replace(const iterator& __p, const iterator& __q,
2421 const iterator& __i, const iterator& __j)
2422 { replace(__p.index(), __q.index() - __p.index(), __i, __j); }
2423
2424 // Replace, iterator variants.
2425 void
2426 replace(const iterator& __p, const rope& __r)
2427 { replace(__p.index(), __r); }
2428
2429 void
2430 replace(const iterator& __p, _CharT __c)
2431 { replace(__p.index(), __c); }
2432
2433 void
2434 replace(const iterator& __p, const _CharT* __c_string)
2435 { replace(__p.index(), __c_string); }
2436
2437 void
2438 replace(const iterator& __p, const _CharT* __i, size_t __n)
2439 { replace(__p.index(), __i, __n); }
2440
2441 void
2442 replace(const iterator& __p, const _CharT* __i, const _CharT* __j)
2443 { replace(__p.index(), __i, __j); }
2444
2445 void
2446 replace(const iterator& __p, const_iterator __i, const_iterator __j)
2447 { replace(__p.index(), __i, __j); }
2448
2449 void
2450 replace(const iterator& __p, iterator __i, iterator __j)
2451 { replace(__p.index(), __i, __j); }
2452
2453 // Iterator and range variants of erase
2454 iterator
2455 erase(const iterator& __p, const iterator& __q)
2456 {
2457 size_t __p_index = __p.index();
2458 erase(__p_index, __q.index() - __p_index);
2459 return iterator(this, __p_index);
2460 }
2461
2462 iterator
2463 erase(const iterator& __p)
2464 {
2465 size_t __p_index = __p.index();
2466 erase(__p_index, 1);
2467 return iterator(this, __p_index);
2468 }
2469
2470 rope
2471 substr(size_t __start, size_t __len = 1) const
2472 {
2473 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2474 __start,
2475 __start + __len));
2476 }
2477
2478 rope
2479 substr(iterator __start, iterator __end) const
2480 {
2481 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2482 __start.index(),
2483 __end.index()));
2484 }
2485
2486 rope
2487 substr(iterator __start) const
2488 {
2489 size_t __pos = __start.index();
2490 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2491 __pos, __pos + 1));
2492 }
2493
2494 rope
2495 substr(const_iterator __start, const_iterator __end) const
2496 {
2497 // This might eventually take advantage of the cache in the
2498 // iterator.
2499 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2500 __start.index(),
2501 __end.index()));
2502 }
2503
2504 rope<_CharT, _Alloc>
2505 substr(const_iterator __start)
2506 {
2507 size_t __pos = __start.index();
2508 return rope<_CharT, _Alloc>(_S_substring(this->_M_tree_ptr,
2509 __pos, __pos + 1));
2510 }
2511
2512 static const size_type npos;
2513
2514 size_type find(_CharT __c, size_type __pos = 0) const;
2515
2516 size_type
2517 find(const _CharT* __s, size_type __pos = 0) const
2518 {
2519 size_type __result_pos;
2520 const_iterator __result =
2521 std::search(const_begin() + __pos, const_end(),
2522 __s, __s + _S_char_ptr_len(__s));
2523 __result_pos = __result.index();
2524 #ifndef __STL_OLD_ROPE_SEMANTICS
2525 if (__result_pos == size())
2526 __result_pos = npos;
2527 #endif
2528 return __result_pos;
2529 }
2530
2531 iterator
2532 mutable_begin()
2533 { return(iterator(this, 0)); }
2534
2535 iterator
2536 mutable_end()
2537 { return(iterator(this, size())); }
2538
2539 typedef reverse_iterator<iterator> reverse_iterator;
2540
2541 reverse_iterator
2542 mutable_rbegin()
2543 { return reverse_iterator(mutable_end()); }
2544
2545 reverse_iterator
2546 mutable_rend()
2547 { return reverse_iterator(mutable_begin()); }
2548
2549 reference
2550 mutable_reference_at(size_type __pos)
2551 { return reference(this, __pos); }
2552
2553 #ifdef __STD_STUFF
2554 reference
2555 operator[] (size_type __pos)
2556 { return _char_ref_proxy(this, __pos); }
2557
2558 reference
2559 at(size_type __pos)
2560 {
2561 // if (__pos >= size()) throw out_of_range; // XXX
2562 return (*this)[__pos];
2563 }
2564
2565 void resize(size_type __n, _CharT __c) {}
2566 void resize(size_type __n) {}
2567 void reserve(size_type __res_arg = 0) {}
2568
2569 size_type
2570 capacity() const
2571 { return max_size(); }
2572
2573 // Stuff below this line is dangerous because it's error prone.
2574 // I would really like to get rid of it.
2575 // copy function with funny arg ordering.
2576 size_type
2577 copy(_CharT* __buffer, size_type __n,
2578 size_type __pos = 0) const
2579 { return copy(__pos, __n, __buffer); }
2580
2581 iterator
2582 end()
2583 { return mutable_end(); }
2584
2585 iterator
2586 begin()
2587 { return mutable_begin(); }
2588
2589 reverse_iterator
2590 rend()
2591 { return mutable_rend(); }
2592
2593 reverse_iterator
2594 rbegin()
2595 { return mutable_rbegin(); }
2596
2597 #else
2598 const_iterator
2599 end()
2600 { return const_end(); }
2601
2602 const_iterator
2603 begin()
2604 { return const_begin(); }
2605
2606 const_reverse_iterator
2607 rend()
2608 { return const_rend(); }
2609
2610 const_reverse_iterator
2611 rbegin()
2612 { return const_rbegin(); }
2613
2614 #endif
2615 };
2616
2617 template <class _CharT, class _Alloc>
2618 const typename rope<_CharT, _Alloc>::size_type
2619 rope<_CharT, _Alloc>::npos = (size_type)(-1);
2620
2621 template <class _CharT, class _Alloc>
2622 inline bool operator==(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2623 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2624 { return (__x._M_current_pos == __y._M_current_pos
2625 && __x._M_root == __y._M_root); }
2626
2627 template <class _CharT, class _Alloc>
2628 inline bool operator<(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2629 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2630 { return (__x._M_current_pos < __y._M_current_pos); }
2631
2632 template <class _CharT, class _Alloc>
2633 inline bool operator!=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2634 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2635 { return !(__x == __y); }
2636
2637 template <class _CharT, class _Alloc>
2638 inline bool operator>(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2639 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2640 { return __y < __x; }
2641
2642 template <class _CharT, class _Alloc>
2643 inline bool
2644 operator<=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2645 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2646 { return !(__y < __x); }
2647
2648 template <class _CharT, class _Alloc>
2649 inline bool
2650 operator>=(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2651 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2652 { return !(__x < __y); }
2653
2654 template <class _CharT, class _Alloc>
2655 inline ptrdiff_t
2656 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x,
2657 const _Rope_const_iterator<_CharT, _Alloc>& __y)
2658 { return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; }
2659
2660 template <class _CharT, class _Alloc>
2661 inline _Rope_const_iterator<_CharT, _Alloc>
2662 operator-(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2663 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2664 __x._M_current_pos - __n); }
2665
2666 template <class _CharT, class _Alloc>
2667 inline _Rope_const_iterator<_CharT, _Alloc>
2668 operator+(const _Rope_const_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2669 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2670 __x._M_current_pos + __n); }
2671
2672 template <class _CharT, class _Alloc>
2673 inline _Rope_const_iterator<_CharT, _Alloc>
2674 operator+(ptrdiff_t __n, const _Rope_const_iterator<_CharT, _Alloc>& __x)
2675 { return _Rope_const_iterator<_CharT, _Alloc>(__x._M_root,
2676 __x._M_current_pos + __n); }
2677
2678 template <class _CharT, class _Alloc>
2679 inline bool
2680 operator==(const _Rope_iterator<_CharT, _Alloc>& __x,
2681 const _Rope_iterator<_CharT, _Alloc>& __y)
2682 {return (__x._M_current_pos == __y._M_current_pos
2683 && __x._M_root_rope == __y._M_root_rope); }
2684
2685 template <class _CharT, class _Alloc>
2686 inline bool
2687 operator<(const _Rope_iterator<_CharT, _Alloc>& __x,
2688 const _Rope_iterator<_CharT, _Alloc>& __y)
2689 { return (__x._M_current_pos < __y._M_current_pos); }
2690
2691 template <class _CharT, class _Alloc>
2692 inline bool
2693 operator!=(const _Rope_iterator<_CharT, _Alloc>& __x,
2694 const _Rope_iterator<_CharT, _Alloc>& __y)
2695 { return !(__x == __y); }
2696
2697 template <class _CharT, class _Alloc>
2698 inline bool
2699 operator>(const _Rope_iterator<_CharT, _Alloc>& __x,
2700 const _Rope_iterator<_CharT, _Alloc>& __y)
2701 { return __y < __x; }
2702
2703 template <class _CharT, class _Alloc>
2704 inline bool
2705 operator<=(const _Rope_iterator<_CharT, _Alloc>& __x,
2706 const _Rope_iterator<_CharT, _Alloc>& __y)
2707 { return !(__y < __x); }
2708
2709 template <class _CharT, class _Alloc>
2710 inline bool
2711 operator>=(const _Rope_iterator<_CharT, _Alloc>& __x,
2712 const _Rope_iterator<_CharT, _Alloc>& __y)
2713 { return !(__x < __y); }
2714
2715 template <class _CharT, class _Alloc>
2716 inline ptrdiff_t
2717 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2718 const _Rope_iterator<_CharT, _Alloc>& __y)
2719 { return ((ptrdiff_t)__x._M_current_pos
2720 - (ptrdiff_t)__y._M_current_pos); }
2721
2722 template <class _CharT, class _Alloc>
2723 inline _Rope_iterator<_CharT, _Alloc>
2724 operator-(const _Rope_iterator<_CharT, _Alloc>& __x,
2725 ptrdiff_t __n)
2726 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2727 __x._M_current_pos - __n); }
2728
2729 template <class _CharT, class _Alloc>
2730 inline _Rope_iterator<_CharT, _Alloc>
2731 operator+(const _Rope_iterator<_CharT, _Alloc>& __x, ptrdiff_t __n)
2732 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2733 __x._M_current_pos + __n); }
2734
2735 template <class _CharT, class _Alloc>
2736 inline _Rope_iterator<_CharT,_Alloc>
2737 operator+(ptrdiff_t __n, const _Rope_iterator<_CharT, _Alloc>& __x)
2738 { return _Rope_iterator<_CharT, _Alloc>(__x._M_root_rope,
2739 __x._M_current_pos + __n); }
2740
2741 template <class _CharT, class _Alloc>
2742 inline rope<_CharT,_Alloc>
2743 operator+(const rope<_CharT, _Alloc>& __left,
2744 const rope<_CharT, _Alloc>& __right)
2745 {
2746 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2747 _S_concat(__left._M_tree_ptr,
2748 __right._M_tree_ptr));
2749 // Inlining this should make it possible to keep __left and
2750 // __right in registers.
2751 }
2752
2753 template <class _CharT, class _Alloc>
2754 inline rope<_CharT, _Alloc>&
2755 operator+=(rope<_CharT, _Alloc>& __left,
2756 const rope<_CharT, _Alloc>& __right)
2757 {
2758 __left.append(__right);
2759 return __left;
2760 }
2761
2762 template <class _CharT, class _Alloc>
2763 inline rope<_CharT, _Alloc>
2764 operator+(const rope<_CharT, _Alloc>& __left,
2765 const _CharT* __right)
2766 {
2767 size_t __rlen = rope<_CharT,_Alloc>::_S_char_ptr_len(__right);
2768 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2769 _S_concat_char_iter(__left._M_tree_ptr,
2770 __right, __rlen));
2771 }
2772
2773 template <class _CharT, class _Alloc>
2774 inline rope<_CharT, _Alloc>&
2775 operator+=(rope<_CharT, _Alloc>& __left,
2776 const _CharT* __right)
2777 {
2778 __left.append(__right);
2779 return __left;
2780 }
2781
2782 template <class _CharT, class _Alloc>
2783 inline rope<_CharT, _Alloc>
2784 operator+(const rope<_CharT, _Alloc>& __left, _CharT __right)
2785 {
2786 return rope<_CharT, _Alloc>(rope<_CharT, _Alloc>::
2787 _S_concat_char_iter(__left._M_tree_ptr,
2788 &__right, 1));
2789 }
2790
2791 template <class _CharT, class _Alloc>
2792 inline rope<_CharT, _Alloc>&
2793 operator+=(rope<_CharT, _Alloc>& __left, _CharT __right)
2794 {
2795 __left.append(__right);
2796 return __left;
2797 }
2798
2799 template <class _CharT, class _Alloc>
2800 bool
2801 operator<(const rope<_CharT, _Alloc>& __left,
2802 const rope<_CharT, _Alloc>& __right)
2803 { return __left.compare(__right) < 0; }
2804
2805 template <class _CharT, class _Alloc>
2806 bool
2807 operator==(const rope<_CharT, _Alloc>& __left,
2808 const rope<_CharT, _Alloc>& __right)
2809 { return __left.compare(__right) == 0; }
2810
2811 template <class _CharT, class _Alloc>
2812 inline bool
2813 operator==(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2814 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2815 { return (__x._M_pos == __y._M_pos && __x._M_root == __y._M_root); }
2816
2817 template <class _CharT, class _Alloc>
2818 inline bool
2819 operator!=(const rope<_CharT, _Alloc>& __x,
2820 const rope<_CharT, _Alloc>& __y)
2821 { return !(__x == __y); }
2822
2823 template <class _CharT, class _Alloc>
2824 inline bool
2825 operator>(const rope<_CharT, _Alloc>& __x,
2826 const rope<_CharT, _Alloc>& __y)
2827 { return __y < __x; }
2828
2829 template <class _CharT, class _Alloc>
2830 inline bool
2831 operator<=(const rope<_CharT, _Alloc>& __x,
2832 const rope<_CharT, _Alloc>& __y)
2833 { return !(__y < __x); }
2834
2835 template <class _CharT, class _Alloc>
2836 inline bool
2837 operator>=(const rope<_CharT, _Alloc>& __x,
2838 const rope<_CharT, _Alloc>& __y)
2839 { return !(__x < __y); }
2840
2841 template <class _CharT, class _Alloc>
2842 inline bool
2843 operator!=(const _Rope_char_ptr_proxy<_CharT, _Alloc>& __x,
2844 const _Rope_char_ptr_proxy<_CharT, _Alloc>& __y)
2845 { return !(__x == __y); }
2846
2847 template<class _CharT, class _Traits, class _Alloc>
2848 std::basic_ostream<_CharT, _Traits>&
2849 operator<<(std::basic_ostream<_CharT, _Traits>& __o,
2850 const rope<_CharT, _Alloc>& __r);
2851
2852 typedef rope<char> crope;
2853 typedef rope<wchar_t> wrope;
2854
2855 inline crope::reference
2856 __mutable_reference_at(crope& __c, size_t __i)
2857 { return __c.mutable_reference_at(__i); }
2858
2859 inline wrope::reference
2860 __mutable_reference_at(wrope& __c, size_t __i)
2861 { return __c.mutable_reference_at(__i); }
2862
2863 template <class _CharT, class _Alloc>
2864 inline void
2865 swap(rope<_CharT, _Alloc>& __x, rope<_CharT, _Alloc>& __y)
2866 { __x.swap(__y); }
2867
2868 // Hash functions should probably be revisited later:
2869 template<>
2870 struct hash<crope>
2871 {
2872 size_t
2873 operator()(const crope& __str) const
2874 {
2875 size_t __size = __str.size();
2876
2877 if (0 == __size)
2878 return 0;
2879 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2880 }
2881 };
2882
2883
2884 template<>
2885 struct hash<wrope>
2886 {
2887 size_t
2888 operator()(const wrope& __str) const
2889 {
2890 size_t __size = __str.size();
2891
2892 if (0 == __size)
2893 return 0;
2894 return 13 * __str[0] + 5 * __str[__size - 1] + __size;
2895 }
2896 };
2897
2898 } // namespace __gnu_cxx
2899
2900 # include <ext/ropeimpl.h>
2901
2902 #endif