📄 stl_tree.h
字号:
if (comp) if (j == begin()) return pair<iterator,bool>(__insert(x, y, v), true); else --j; if (key_compare(key(j.node), KeyOfValue()(v))) return pair<iterator,bool>(__insert(x, y, v), true); return pair<iterator,bool>(j, false);}template <class Key, class Val, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Val, KeyOfValue, Compare, Alloc>::iterator rb_tree<Key, Val, KeyOfValue, Compare, Alloc>::insert_unique(iterator position, const Val& v) { if (position.node == header->left) // begin() if (size() > 0 && key_compare(KeyOfValue()(v), key(position.node))) return __insert(position.node, position.node, v); // first argument just needs to be non-null else return insert_unique(v).first; else if (position.node == header) // end() if (key_compare(key(rightmost()), KeyOfValue()(v))) return __insert(0, rightmost(), v); else return insert_unique(v).first; else { iterator before = position; --before; if (key_compare(key(before.node), KeyOfValue()(v)) && key_compare(KeyOfValue()(v), key(position.node))) if (right(before.node) == 0) return __insert(0, before.node, v); else return __insert(position.node, position.node, v); // first argument just needs to be non-null else return insert_unique(v).first; }}template <class Key, class Val, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Val, KeyOfValue, Compare, Alloc>::iterator rb_tree<Key, Val, KeyOfValue, Compare, Alloc>::insert_equal(iterator position, const Val& v) { if (position.node == header->left) // begin() if (size() > 0 && key_compare(KeyOfValue()(v), key(position.node))) return __insert(position.node, position.node, v); // first argument just needs to be non-null else return insert_equal(v); else if (position.node == header) // end() if (!key_compare(KeyOfValue()(v), key(rightmost()))) return __insert(0, rightmost(), v); else return insert_equal(v); else { iterator before = position; --before; if (!key_compare(KeyOfValue()(v), key(before.node)) && !key_compare(key(position.node), KeyOfValue()(v))) if (right(before.node) == 0) return __insert(0, before.node, v); else return __insert(position.node, position.node, v); // first argument just needs to be non-null else return insert_equal(v); }}#ifdef __STL_MEMBER_TEMPLATES template <class K, class V, class KoV, class Cmp, class Al> template<class II>void rb_tree<K, V, KoV, Cmp, Al>::insert_equal(II first, II last) { for ( ; first != last; ++first) insert_equal(*first);}template <class K, class V, class KoV, class Cmp, class Al> template<class II>void rb_tree<K, V, KoV, Cmp, Al>::insert_unique(II first, II last) { for ( ; first != last; ++first) insert_unique(*first);}#else /* __STL_MEMBER_TEMPLATES */template <class K, class V, class KoV, class Cmp, class Al>voidrb_tree<K, V, KoV, Cmp, Al>::insert_equal(const V* first, const V* last) { for ( ; first != last; ++first) insert_equal(*first);}template <class K, class V, class KoV, class Cmp, class Al>voidrb_tree<K, V, KoV, Cmp, Al>::insert_equal(const_iterator first, const_iterator last) { for ( ; first != last; ++first) insert_equal(*first);}template <class K, class V, class KoV, class Cmp, class A>void rb_tree<K, V, KoV, Cmp, A>::insert_unique(const V* first, const V* last) { for ( ; first != last; ++first) insert_unique(*first);}template <class K, class V, class KoV, class Cmp, class A>void rb_tree<K, V, KoV, Cmp, A>::insert_unique(const_iterator first, const_iterator last) { for ( ; first != last; ++first) insert_unique(*first);}#endif /* __STL_MEMBER_TEMPLATES */ template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>inline voidrb_tree<Key, Value, KeyOfValue, Compare, Alloc>::erase(iterator position) { link_type y = (link_type) __rb_tree_rebalance_for_erase(position.node, header->parent, header->left, header->right); destroy_node(y); --node_count;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::size_type rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::erase(const Key& x) { pair<iterator,iterator> p = equal_range(x); size_type n = 0; distance(p.first, p.second, n); erase(p.first, p.second); return n;}template <class K, class V, class KeyOfValue, class Compare, class Alloc>typename rb_tree<K, V, KeyOfValue, Compare, Alloc>::link_type rb_tree<K, V, KeyOfValue, Compare, Alloc>::__copy(link_type x, link_type p) { // structural copy. x and p must be non-null. link_type top = clone_node(x); top->parent = p; __STL_TRY { if (x->right) top->right = __copy(right(x), top); p = top; x = left(x); while (x != 0) { link_type y = clone_node(x); p->left = y; y->parent = p; if (x->right) y->right = __copy(right(x), y); p = y; x = left(x); } } __STL_UNWIND(__erase(top)); return top;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>void rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::__erase(link_type x) { // erase without rebalancing while (x != 0) { __erase(right(x)); link_type y = left(x); destroy_node(x); x = y; }}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>void rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::erase(iterator first, iterator last) { if (first == begin() && last == end()) clear(); else while (first != last) erase(first++);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>void rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::erase(const Key* first, const Key* last) { while (first != last) erase(*first++);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::find(const Key& k) { link_type y = header; // Last node which is not less than k. link_type x = root(); // Current node. while (x != 0) if (!key_compare(key(x), k)) y = x, x = left(x); else x = right(x); iterator j = iterator(y); return (j == end() || key_compare(k, key(j.node))) ? end() : j;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::const_iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::find(const Key& k) const { link_type y = header; /* Last node which is not less than k. */ link_type x = root(); /* Current node. */ while (x != 0) { if (!key_compare(key(x), k)) y = x, x = left(x); else x = right(x); } const_iterator j = const_iterator(y); return (j == end() || key_compare(k, key(j.node))) ? end() : j;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::size_type rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::count(const Key& k) const { pair<const_iterator, const_iterator> p = equal_range(k); size_type n = 0; distance(p.first, p.second, n); return n;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::lower_bound(const Key& k) { link_type y = header; /* Last node which is not less than k. */ link_type x = root(); /* Current node. */ while (x != 0) if (!key_compare(key(x), k)) y = x, x = left(x); else x = right(x); return iterator(y);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::const_iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::lower_bound(const Key& k) const { link_type y = header; /* Last node which is not less than k. */ link_type x = root(); /* Current node. */ while (x != 0) if (!key_compare(key(x), k)) y = x, x = left(x); else x = right(x); return const_iterator(y);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::upper_bound(const Key& k) { link_type y = header; /* Last node which is greater than k. */ link_type x = root(); /* Current node. */ while (x != 0) if (key_compare(k, key(x))) y = x, x = left(x); else x = right(x); return iterator(y);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::const_iterator rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::upper_bound(const Key& k) const { link_type y = header; /* Last node which is greater than k. */ link_type x = root(); /* Current node. */ while (x != 0) if (key_compare(k, key(x))) y = x, x = left(x); else x = right(x); return const_iterator(y);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>inline pair<typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator, typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator>rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::equal_range(const Key& k) { return pair<iterator, iterator>(lower_bound(k), upper_bound(k));}template <class Key, class Value, class KoV, class Compare, class Alloc>inline pair<typename rb_tree<Key, Value, KoV, Compare, Alloc>::const_iterator, typename rb_tree<Key, Value, KoV, Compare, Alloc>::const_iterator>rb_tree<Key, Value, KoV, Compare, Alloc>::equal_range(const Key& k) const { return pair<const_iterator,const_iterator>(lower_bound(k), upper_bound(k));}inline int __black_count(__rb_tree_node_base* node, __rb_tree_node_base* root){ if (node == 0) return 0; else { int bc = node->color == __rb_tree_black ? 1 : 0; if (node == root) return bc; else return bc + __black_count(node->parent, root); }}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>bool rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::__rb_verify() const{ if (node_count == 0 || begin() == end()) return node_count == 0 && begin() == end() && header->left == header && header->right == header; int len = __black_count(leftmost(), root()); for (const_iterator it = begin(); it != end(); ++it) { link_type x = (link_type) it.node; link_type L = left(x); link_type R = right(x); if (x->color == __rb_tree_red) if ((L && L->color == __rb_tree_red) || (R && R->color == __rb_tree_red)) return false; if (L && key_compare(key(x), key(L))) return false; if (R && key_compare(key(R), key(x))) return false; if (!L && !R && __black_count(x, root()) != len) return false; } if (leftmost() != __rb_tree_node_base::minimum(root())) return false; if (rightmost() != __rb_tree_node_base::maximum(root())) return false; return true;}__STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_TREE_H */// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -