📄 _rope.h
字号:
_RopeRep* __left =
_S_RopeLeaf_from_unowned_char_ptr(&__x, 1, _M_tree_ptr);
_STLP_TRY {
_M_tree_ptr._M_data = _S_concat_rep(__left, _M_tree_ptr._M_data);
_S_unref(__old);
_S_unref(__left);
}
_STLP_UNWIND(_S_unref(__left))
}
void pop_front() {
_RopeRep* __old = _M_tree_ptr._M_data;
_M_tree_ptr._M_data = _S_substring(_M_tree_ptr._M_data, 1, _M_tree_ptr._M_data->_M_size._M_data);
_S_unref(__old);
}
_CharT front() const {
return _S_fetch(_M_tree_ptr._M_data, 0);
}
void balance() {
_RopeRep* __old = _M_tree_ptr._M_data;
_M_tree_ptr._M_data = _S_balance(_M_tree_ptr._M_data);
_S_unref(__old);
}
void copy(_CharT* __buffer) const {
_STLP_STD::_Destroy_Range(__buffer, __buffer + size());
_S_flatten(_M_tree_ptr._M_data, __buffer);
}
/*
* This is the copy function from the standard, but
* with the arguments reordered to make it consistent with the
* rest of the interface.
* Note that this guaranteed not to compile if the draft standard
* order is assumed.
*/
size_type copy(size_type __pos, size_type __n, _CharT* __buffer) const {
size_t _p_size = size();
size_t __len = (__pos + __n > _p_size? _p_size - __pos : __n);
_STLP_STD::_Destroy_Range(__buffer, __buffer + __len);
_S_flatten(_M_tree_ptr._M_data, __pos, __len, __buffer);
return __len;
}
# ifdef _STLP_DEBUG
// Print to stdout, exposing structure. May be useful for
// performance debugging.
void dump() {
_S_dump(_M_tree_ptr._M_data);
}
# endif
// Convert to 0 terminated string in new allocated memory.
// Embedded 0s in the input do not terminate the copy.
const _CharT* c_str() const;
// As above, but also use the flattened representation as the
// the new rope representation.
const _CharT* replace_with_c_str();
// Reclaim memory for the c_str generated flattened string.
// Intentionally undocumented, since it's hard to say when this
// is safe for multiple threads.
void delete_c_str () {
if (0 == _M_tree_ptr._M_data) return;
if (_RopeRep::_S_leaf == _M_tree_ptr._M_data->_M_tag &&
((_RopeLeaf*)_M_tree_ptr._M_data)->_M_data ==
_M_tree_ptr._M_data->_M_c_string) {
// Representation shared
return;
}
_M_tree_ptr._M_data->_M_free_c_string();
_M_tree_ptr._M_data->_M_c_string = 0;
}
_CharT operator[] (size_type __pos) const {
return _S_fetch(_M_tree_ptr._M_data, __pos);
}
_CharT at(size_type __pos) const {
if (__pos >= size()) _M_throw_out_of_range();
return (*this)[__pos];
}
const_iterator begin() const {
return(const_iterator(_M_tree_ptr._M_data, 0));
}
// An easy way to get a const iterator from a non-const container.
const_iterator const_begin() const {
return(const_iterator(_M_tree_ptr._M_data, 0));
}
const_iterator end() const {
return(const_iterator(_M_tree_ptr._M_data, size()));
}
const_iterator const_end() const {
return(const_iterator(_M_tree_ptr._M_data, size()));
}
size_type size() const {
return(0 == _M_tree_ptr._M_data? 0 : _M_tree_ptr._M_data->_M_size._M_data);
}
size_type length() const {
return size();
}
size_type max_size() const {
return _S_min_len[__ROPE_MAX_DEPTH-1] - 1;
// Guarantees that the result can be sufficiently
// balanced. Longer ropes will probably still work,
// but it's harder to make guarantees.
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator const_rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
const_reverse_iterator const_rend() const {
return const_reverse_iterator(begin());
}
// The symmetric cases are intentionally omitted, since they're presumed
// to be less common, and we don't handle them as well.
// The following should really be templatized.
// The first argument should be an input iterator or
// forward iterator with value_type _CharT.
_Self& append(const _CharT* __iter, size_t __n) {
_M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __iter, __n));
return *this;
}
_Self& append(const _CharT* __c_string) {
size_t __len = _S_char_ptr_len(__c_string);
append(__c_string, __len);
return *this;
}
_Self& append(const _CharT* __s, const _CharT* __e) {
_M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, __s, __e - __s));
return *this;
}
_Self& append(const_iterator __s, const_iterator __e) {
_STLP_ASSERT(__s._M_root == __e._M_root)
_STLP_ASSERT(get_allocator() == __s._M_root->get_allocator())
_Self_destruct_ptr __appendee(_S_substring(__s._M_root, __s._M_current_pos, __e._M_current_pos));
_M_reset(_S_concat_rep(_M_tree_ptr._M_data, (_RopeRep*)__appendee));
return *this;
}
_Self& append(_CharT __c) {
_M_reset(_S_destr_concat_char_iter(_M_tree_ptr._M_data, &__c, 1));
return *this;
}
_Self& append() { return append(_CharT()); } // XXX why?
_Self& append(const _Self& __y) {
_STLP_ASSERT(__y.get_allocator() == get_allocator())
_M_reset(_S_concat_rep(_M_tree_ptr._M_data, __y._M_tree_ptr._M_data));
return *this;
}
_Self& append(size_t __n, _CharT __c) {
rope<_CharT,_Alloc> __last(__n, __c);
return append(__last);
}
void swap(_Self& __b) {
_M_tree_ptr.swap(__b._M_tree_ptr);
}
protected:
// Result is included in refcount.
static _RopeRep* replace(_RopeRep* __old, size_t __pos1,
size_t __pos2, _RopeRep* __r) {
if (0 == __old) { _S_ref(__r); return __r; }
_Self_destruct_ptr __left(_S_substring(__old, 0, __pos1));
_Self_destruct_ptr __right(_S_substring(__old, __pos2, __old->_M_size._M_data));
_STLP_MPWFIX_TRY //*TY 06/01/2000 -
_RopeRep* __result;
if (0 == __r) {
__result = _S_concat_rep(__left, __right);
} else {
_STLP_ASSERT(__old->get_allocator() == __r->get_allocator())
_Self_destruct_ptr __left_result(_S_concat_rep(__left, __r));
__result = _S_concat_rep(__left_result, __right);
}
return __result;
_STLP_MPWFIX_CATCH //*TY 06/01/2000 -
}
public:
void insert(size_t __p, const _Self& __r) {
if (__p > size()) _M_throw_out_of_range();
_STLP_ASSERT(get_allocator() == __r.get_allocator())
_M_reset(replace(_M_tree_ptr._M_data, __p, __p, __r._M_tree_ptr._M_data));
}
void insert(size_t __p, size_t __n, _CharT __c) {
rope<_CharT,_Alloc> __r(__n,__c);
insert(__p, __r);
}
void insert(size_t __p, const _CharT* __i, size_t __n) {
if (__p > size()) _M_throw_out_of_range();
_Self_destruct_ptr __left(_S_substring(_M_tree_ptr._M_data, 0, __p));
_Self_destruct_ptr __right(_S_substring(_M_tree_ptr._M_data, __p, size()));
_Self_destruct_ptr __left_result(
_S_concat_char_iter(__left, __i, __n));
// _S_ destr_concat_char_iter should be safe here.
// But as it stands it's probably not a win, since __left
// is likely to have additional references.
_M_reset(_S_concat_rep(__left_result, __right));
}
void insert(size_t __p, const _CharT* __c_string) {
insert(__p, __c_string, _S_char_ptr_len(__c_string));
}
void insert(size_t __p, _CharT __c) {
insert(__p, &__c, 1);
}
void insert(size_t __p) {
_CharT __c = _CharT();
insert(__p, &__c, 1);
}
void insert(size_t __p, const _CharT* __i, const _CharT* __j) {
_Self __r(__i, __j);
insert(__p, __r);
}
void insert(size_t __p, const const_iterator& __i,
const const_iterator& __j) {
_Self __r(__i, __j);
insert(__p, __r);
}
void insert(size_t __p, const iterator& __i,
const iterator& __j) {
_Self __r(__i, __j);
insert(__p, __r);
}
// (position, length) versions of replace operations:
void replace(size_t __p, size_t __n, const _Self& __r) {
if (__p > size()) _M_throw_out_of_range();
_M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, __r._M_tree_ptr._M_data));
}
void replace(size_t __p, size_t __n,
const _CharT* __i, size_t __i_len) {
_Self __r(__i, __i_len);
replace(__p, __n, __r);
}
void replace(size_t __p, size_t __n, _CharT __c) {
_Self __r(__c);
replace(__p, __n, __r);
}
void replace(size_t __p, size_t __n, const _CharT* __c_string) {
_Self __r(__c_string);
replace(__p, __n, __r);
}
void replace(size_t __p, size_t __n,
const _CharT* __i, const _CharT* __j) {
_Self __r(__i, __j);
replace(__p, __n, __r);
}
void replace(size_t __p, size_t __n,
const const_iterator& __i, const const_iterator& __j) {
_Self __r(__i, __j);
replace(__p, __n, __r);
}
void replace(size_t __p, size_t __n,
const iterator& __i, const iterator& __j) {
_Self __r(__i, __j);
replace(__p, __n, __r);
}
// Single character variants:
void replace(size_t __p, _CharT __c) {
if (__p > size()) _M_throw_out_of_range();
iterator __i(this, __p);
*__i = __c;
}
void replace(size_t __p, const _Self& __r) {
replace(__p, 1, __r);
}
void replace(size_t __p, const _CharT* __i, size_t __i_len) {
replace(__p, 1, __i, __i_len);
}
void replace(size_t __p, const _CharT* __c_string) {
replace(__p, 1, __c_string);
}
void replace(size_t __p, const _CharT* __i, const _CharT* __j) {
replace(__p, 1, __i, __j);
}
void replace(size_t __p, const const_iterator& __i,
const const_iterator& __j) {
replace(__p, 1, __i, __j);
}
void replace(size_t __p, const iterator& __i,
const iterator& __j) {
replace(__p, 1, __i, __j);
}
// Erase, (position, size) variant.
void erase(size_t __p, size_t __n) {
if (__p > size()) _M_throw_out_of_range();
_M_reset(replace(_M_tree_ptr._M_data, __p, __p + __n, 0));
}
// Erase, single character
void erase(size_t __p) {
erase(__p, __p + 1);
}
// Insert, iterator variants.
iterator insert(const iterator& __p, const _Self& __r)
{ insert(__p.index(), __r); return __p; }
iterator insert(const iterator& __p, size_t __n, _CharT __c)
{ insert(__p.index(), __n, __c); return __p; }
iterator insert(const iterator& __p, _CharT __c)
{ insert(__p.index(), __c); return __p; }
iterator insert(const iterator& __p )
{ insert(__p.index()); return __p; }
iterator insert(const iterator& __p, const _CharT* c_string)
{ insert(__p.index(), c_string); return __p; }
iterator insert(const iterator& __p, const _CharT* __i, size_t __n)
{ insert(__p.index(), __i, __n); return __p; }
iterator insert(const iterator& __p, const _CharT* __i,
const _CharT* __j)
{ insert(__p.index(), __i, __j); return __p; }
iterator insert(const iterator& __p,
const const_iterator& __i, const const_iterator& __j)
{ insert(__p.index(), __i, __j); return __p; }
iterator insert(const iterator& __p,
const iterator& __i, const iterator& __j)
{ insert(__p.index(), __i, __j); return __p; }
// Replace, range variants.
void replace(const iterator& __p, const iterator& __q,
const _Self& __r)
{ replace(__p.index(), __q.index() - __p.index(), __r); }
void replace(const iterator& __p, const iterator& __q, _CharT __c)
{ replace(__p.index(), __q.index() - __p.index(), __c); }
void replace(const iterator& __p, const iterator&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -