You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2989 lines
87KB

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