📄 stl_tree.h
字号:
if ((w->left == 0 || w->left->color == __rb_tree_black) && (w->right == 0 || w->right->color == __rb_tree_black)) { w->color = __rb_tree_red; x = x_parent; x_parent = x_parent->parent; } else { if (w->right == 0 || w->right->color == __rb_tree_black) { if (w->left) w->left->color = __rb_tree_black; w->color = __rb_tree_red; __rb_tree_rotate_right(w, root); w = x_parent->right; } w->color = x_parent->color; x_parent->color = __rb_tree_black; if (w->right) w->right->color = __rb_tree_black; __rb_tree_rotate_left(x_parent, root); break; } } else { // same as above, with right <-> left. __rb_tree_node_base* w = x_parent->left; if (w->color == __rb_tree_red) { w->color = __rb_tree_black; x_parent->color = __rb_tree_red; __rb_tree_rotate_right(x_parent, root); w = x_parent->left; } if ((w->right == 0 || w->right->color == __rb_tree_black) && (w->left == 0 || w->left->color == __rb_tree_black)) { w->color = __rb_tree_red; x = x_parent; x_parent = x_parent->parent; } else { if (w->left == 0 || w->left->color == __rb_tree_black) { if (w->right) w->right->color = __rb_tree_black; w->color = __rb_tree_red; __rb_tree_rotate_left(w, root); w = x_parent->left; } w->color = x_parent->color; x_parent->color = __rb_tree_black; if (w->left) w->left->color = __rb_tree_black; __rb_tree_rotate_right(x_parent, root); break; } } if (x) x->color = __rb_tree_black; } return y;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc = alloc>class rb_tree {protected: typedef void* void_pointer; typedef __rb_tree_node_base* base_ptr; typedef __rb_tree_node<Value> rb_tree_node; typedef simple_alloc<rb_tree_node, Alloc> rb_tree_node_allocator; typedef __rb_tree_color_type color_type;public: typedef Key key_type; typedef Value value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; typedef rb_tree_node* link_type; typedef size_t size_type; typedef ptrdiff_t difference_type;protected: link_type get_node() { return rb_tree_node_allocator::allocate(); } void put_node(link_type p) { rb_tree_node_allocator::deallocate(p); } link_type create_node(const value_type& x) { link_type tmp = get_node(); __STL_TRY { construct(&tmp->value_field, x); } __STL_UNWIND(put_node(tmp)); return tmp; } link_type clone_node(link_type x) { link_type tmp = create_node(x->value_field); tmp->color = x->color; tmp->left = 0; tmp->right = 0; return tmp; } void destroy_node(link_type p) { destroy(&p->value_field); put_node(p); }protected: size_type node_count; // keeps track of size of tree link_type header; Compare key_compare; link_type& root() const { return (link_type&) header->parent; } link_type& leftmost() const { return (link_type&) header->left; } link_type& rightmost() const { return (link_type&) header->right; } static link_type& left(link_type x) { return (link_type&)(x->left); } static link_type& right(link_type x) { return (link_type&)(x->right); } static link_type& parent(link_type x) { return (link_type&)(x->parent); } static reference value(link_type x) { return x->value_field; } static const Key& key(link_type x) { return KeyOfValue()(value(x)); } static color_type& color(link_type x) { return (color_type&)(x->color); } static link_type& left(base_ptr x) { return (link_type&)(x->left); } static link_type& right(base_ptr x) { return (link_type&)(x->right); } static link_type& parent(base_ptr x) { return (link_type&)(x->parent); } static reference value(base_ptr x) { return ((link_type)x)->value_field; } static const Key& key(base_ptr x) { return KeyOfValue()(value(link_type(x)));} static color_type& color(base_ptr x) { return (color_type&)(link_type(x)->color); } static link_type minimum(link_type x) { return (link_type) __rb_tree_node_base::minimum(x); } static link_type maximum(link_type x) { return (link_type) __rb_tree_node_base::maximum(x); }public: typedef __rb_tree_iterator<value_type, reference, pointer> iterator; typedef __rb_tree_iterator<value_type, const_reference, const_pointer> const_iterator;#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION typedef reverse_iterator<const_iterator> const_reverse_iterator; typedef reverse_iterator<iterator> reverse_iterator;#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ typedef reverse_bidirectional_iterator<iterator, value_type, reference, difference_type> reverse_iterator; typedef reverse_bidirectional_iterator<const_iterator, value_type, const_reference, difference_type> const_reverse_iterator;#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ private: iterator __insert(base_ptr x, base_ptr y, const value_type& v); link_type __copy(link_type x, link_type p); void __erase(link_type x); void init() { header = get_node(); color(header) = __rb_tree_red; // used to distinguish header from // root, in iterator.operator++ root() = 0; leftmost() = header; rightmost() = header; }public: // allocation/deallocation rb_tree(const Compare& comp = Compare()) : node_count(0), key_compare(comp) { init(); } rb_tree(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x) : node_count(0), key_compare(x.key_compare) { header = get_node(); color(header) = __rb_tree_red; if (x.root() == 0) { root() = 0; leftmost() = header; rightmost() = header; } else { __STL_TRY { root() = __copy(x.root(), header); } __STL_UNWIND(put_node(header)); leftmost() = minimum(root()); rightmost() = maximum(root()); } node_count = x.node_count; } ~rb_tree() { clear(); put_node(header); } rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& operator=(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x);public: // accessors: Compare key_comp() const { return key_compare; } iterator begin() { return leftmost(); } const_iterator begin() const { return leftmost(); } iterator end() { return header; } const_iterator end() const { return header; } reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } bool empty() const { return node_count == 0; } size_type size() const { return node_count; } size_type max_size() const { return size_type(-1); } void swap(rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& t) { __STD::swap(header, t.header); __STD::swap(node_count, t.node_count); __STD::swap(key_compare, t.key_compare); } public: // insert/erase pair<iterator,bool> insert_unique(const value_type& x); iterator insert_equal(const value_type& x); iterator insert_unique(iterator position, const value_type& x); iterator insert_equal(iterator position, const value_type& x);#ifdef __STL_MEMBER_TEMPLATES template <class InputIterator> void insert_unique(InputIterator first, InputIterator last); template <class InputIterator> void insert_equal(InputIterator first, InputIterator last);#else /* __STL_MEMBER_TEMPLATES */ void insert_unique(const_iterator first, const_iterator last); void insert_unique(const value_type* first, const value_type* last); void insert_equal(const_iterator first, const_iterator last); void insert_equal(const value_type* first, const value_type* last);#endif /* __STL_MEMBER_TEMPLATES */ void erase(iterator position); size_type erase(const key_type& x); void erase(iterator first, iterator last); void erase(const key_type* first, const key_type* last); void clear() { if (node_count != 0) { __erase(root()); leftmost() = header; root() = 0; rightmost() = header; node_count = 0; } } public: // set operations: iterator find(const key_type& x); const_iterator find(const key_type& x) const; size_type count(const key_type& x) const; iterator lower_bound(const key_type& x); const_iterator lower_bound(const key_type& x) const; iterator upper_bound(const key_type& x); const_iterator upper_bound(const key_type& x) const; pair<iterator,iterator> equal_range(const key_type& x); pair<const_iterator, const_iterator> equal_range(const key_type& x) const;public: // Debugging. bool __rb_verify() const;};template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>inline bool operator==(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x, const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& y) { return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>inline bool operator<(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x, const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& y) { return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());}#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDERtemplate <class Key, class Value, class KeyOfValue, class Compare, class Alloc>inline void swap(rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x, rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& y) { x.swap(y);}#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::operator=(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x) { if (this != &x) { // Note that Key may be a constant type. clear(); node_count = 0; key_compare = x.key_compare; if (x.root() == 0) { root() = 0; leftmost() = header; rightmost() = header; } else { root() = __copy(x.root(), header); leftmost() = minimum(root()); rightmost() = maximum(root()); node_count = x.node_count; } } return *this;}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iteratorrb_tree<Key, Value, KeyOfValue, Compare, Alloc>::__insert(base_ptr x_, base_ptr y_, const Value& v) { link_type x = (link_type) x_; link_type y = (link_type) y_; link_type z; if (y == header || x != 0 || key_compare(KeyOfValue()(v), key(y))) { z = create_node(v); left(y) = z; // also makes leftmost() = z when y == header if (y == header) { root() = z; rightmost() = z; } else if (y == leftmost()) leftmost() = z; // maintain leftmost() pointing to min node } else { z = create_node(v); right(y) = z; if (y == rightmost()) rightmost() = z; // maintain rightmost() pointing to max node } parent(z) = y; left(z) = 0; right(z) = 0; __rb_tree_rebalance(z, header->parent); ++node_count; return iterator(z);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iteratorrb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_equal(const Value& v){ link_type y = header; link_type x = root(); while (x != 0) { y = x; x = key_compare(KeyOfValue()(v), key(x)) ? left(x) : right(x); } return __insert(x, y, v);}template <class Key, class Value, class KeyOfValue, class Compare, class Alloc>pair<typename rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::iterator, bool>rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_unique(const Value& v){ link_type y = header; link_type x = root(); bool comp = true; while (x != 0) { y = x; comp = key_compare(KeyOfValue()(v), key(x)); x = comp ? left(x) : right(x); } iterator j = iterator(y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -