libstdc++
rb_tree_.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file rb_tree_map_/rb_tree_.hpp
00038  * Contains an implementation for Red Black trees.
00039  */
00040 
00041 #include <ext/pb_ds/detail/standard_policies.hpp>
00042 #include <utility>
00043 #include <vector>
00044 #include <assert.h>
00045 #include <debug/debug.h>
00046 
00047 namespace __gnu_pbds
00048 {
00049   namespace detail
00050   {
00051 #define PB_DS_CLASS_T_DEC \
00052     template<typename Key, typename Mapped, typename Cmp_Fn, \
00053          typename Node_And_It_Traits, typename _Alloc>
00054 
00055 #ifdef PB_DS_DATA_TRUE_INDICATOR
00056 # define PB_DS_RB_TREE_NAME rb_tree_map
00057 # define PB_DS_RB_TREE_BASE_NAME bin_search_tree_map
00058 #endif
00059 
00060 #ifdef PB_DS_DATA_FALSE_INDICATOR
00061 # define PB_DS_RB_TREE_NAME rb_tree_set
00062 # define PB_DS_RB_TREE_BASE_NAME bin_search_tree_set
00063 #endif
00064 
00065 #define PB_DS_CLASS_C_DEC \
00066     PB_DS_RB_TREE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
00067 
00068 #define PB_DS_RB_TREE_BASE \
00069     PB_DS_RB_TREE_BASE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
00070 
00071 
00072     /**
00073      *  @brief Red-Black tree.
00074      *  @ingroup branch-detail
00075      *
00076      *  This implementation uses an idea from the SGI STL (using a
00077      *  @a header node which is needed for efficient iteration).
00078      */
00079     template<typename Key,
00080          typename Mapped,
00081          typename Cmp_Fn,
00082          typename Node_And_It_Traits,
00083          typename _Alloc>
00084     class PB_DS_RB_TREE_NAME : public PB_DS_RB_TREE_BASE
00085     {
00086     private:
00087       typedef PB_DS_RB_TREE_BASE                 base_type;
00088       typedef typename base_type::node_pointer       node_pointer;
00089 
00090     public:
00091       typedef rb_tree_tag                container_category;
00092       typedef Cmp_Fn                     cmp_fn;
00093       typedef _Alloc                     allocator_type;
00094       typedef typename _Alloc::size_type         size_type;
00095       typedef typename _Alloc::difference_type       difference_type;
00096       typedef typename base_type::key_type       key_type;
00097       typedef typename base_type::key_pointer        key_pointer;
00098       typedef typename base_type::key_const_pointer      key_const_pointer;
00099       typedef typename base_type::key_reference      key_reference;
00100       typedef typename base_type::key_const_reference    key_const_reference;
00101       typedef typename base_type::mapped_type        mapped_type;
00102       typedef typename base_type::mapped_pointer     mapped_pointer;
00103       typedef typename base_type::mapped_const_pointer   mapped_const_pointer;
00104       typedef typename base_type::mapped_reference   mapped_reference;
00105       typedef typename base_type::mapped_const_reference mapped_const_reference;
00106       typedef typename base_type::value_type         value_type;
00107       typedef typename base_type::pointer        pointer;
00108       typedef typename base_type::const_pointer      const_pointer;
00109       typedef typename base_type::reference          reference;
00110       typedef typename base_type::const_reference    const_reference;
00111       typedef typename base_type::point_iterator     point_iterator;
00112       typedef typename base_type::const_iterator     point_const_iterator;
00113       typedef typename base_type::iterator       iterator;
00114       typedef typename base_type::const_iterator     const_iterator;
00115       typedef typename base_type::reverse_iterator   reverse_iterator;
00116       typedef typename base_type::const_reverse_iterator const_reverse_iterator;
00117       typedef typename base_type::node_update        node_update;
00118 
00119       PB_DS_RB_TREE_NAME();
00120 
00121       PB_DS_RB_TREE_NAME(const Cmp_Fn&);
00122 
00123       PB_DS_RB_TREE_NAME(const Cmp_Fn&, const node_update&);
00124 
00125       PB_DS_RB_TREE_NAME(const PB_DS_CLASS_C_DEC&);
00126 
00127       void
00128       swap(PB_DS_CLASS_C_DEC&);
00129 
00130       template<typename It>
00131       void
00132       copy_from_range(It, It);
00133 
00134       inline std::pair<point_iterator, bool>
00135       insert(const_reference);
00136 
00137       inline mapped_reference
00138       operator[](key_const_reference r_key)
00139       {
00140 #ifdef PB_DS_DATA_TRUE_INDICATOR
00141     _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
00142     std::pair<point_iterator, bool> ins_pair =
00143     base_type::insert_leaf(value_type(r_key, mapped_type()));
00144 
00145     if (ins_pair.second == true)
00146       {
00147         ins_pair.first.m_p_nd->m_red = true;
00148         _GLIBCXX_DEBUG_ONLY(this->structure_only_assert_valid(__FILE__, __LINE__);)
00149         insert_fixup(ins_pair.first.m_p_nd);
00150       }
00151     _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
00152     return ins_pair.first.m_p_nd->m_value.second;
00153 #else
00154     insert(r_key);
00155     return base_type::s_null_type;
00156 #endif
00157       }
00158 
00159       inline bool
00160       erase(key_const_reference);
00161 
00162       inline iterator
00163       erase(iterator);
00164 
00165       inline reverse_iterator
00166       erase(reverse_iterator);
00167 
00168       template<typename Pred>
00169       inline size_type
00170       erase_if(Pred);
00171 
00172       void
00173       join(PB_DS_CLASS_C_DEC&);
00174 
00175       void
00176       split(key_const_reference, PB_DS_CLASS_C_DEC&);
00177 
00178     private:
00179 
00180 #ifdef _GLIBCXX_DEBUG
00181       void
00182       assert_valid(const char*, int) const;
00183 
00184       size_type
00185       assert_node_consistent(const node_pointer, const char*, int) const;
00186 #endif
00187 
00188       inline static bool
00189       is_effectively_black(const node_pointer);
00190 
00191       void
00192       initialize();
00193 
00194       void
00195       insert_fixup(node_pointer);
00196 
00197       void
00198       erase_node(node_pointer);
00199 
00200       void
00201       remove_node(node_pointer);
00202 
00203       void
00204       remove_fixup(node_pointer, node_pointer);
00205 
00206       void
00207       split_imp(node_pointer, PB_DS_CLASS_C_DEC&);
00208 
00209       inline node_pointer
00210       split_min();
00211 
00212       std::pair<node_pointer, node_pointer>
00213       split_min_imp();
00214 
00215       void
00216       join_imp(node_pointer, node_pointer);
00217 
00218       std::pair<node_pointer, node_pointer>
00219       find_join_pos_right(node_pointer, size_type, size_type);
00220 
00221       std::pair<node_pointer, node_pointer>
00222       find_join_pos_left(node_pointer, size_type, size_type);
00223 
00224       inline size_type
00225       black_height(node_pointer);
00226 
00227       void
00228       split_at_node(node_pointer, PB_DS_CLASS_C_DEC&);
00229     };
00230 
00231 #define PB_DS_STRUCT_ONLY_ASSERT_VALID(X)               \
00232   _GLIBCXX_DEBUG_ONLY(X.structure_only_assert_valid(__FILE__, __LINE__);)
00233 
00234 #include <ext/pb_ds/detail/rb_tree_map_/constructors_destructor_fn_imps.hpp>
00235 #include <ext/pb_ds/detail/rb_tree_map_/insert_fn_imps.hpp>
00236 #include <ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp>
00237 #include <ext/pb_ds/detail/rb_tree_map_/debug_fn_imps.hpp>
00238 #include <ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp>
00239 #include <ext/pb_ds/detail/rb_tree_map_/info_fn_imps.hpp>
00240 
00241 #undef PB_DS_STRUCT_ONLY_ASSERT_VALID
00242 #undef PB_DS_CLASS_T_DEC
00243 #undef PB_DS_CLASS_C_DEC
00244 #undef PB_DS_RB_TREE_NAME
00245 #undef PB_DS_RB_TREE_BASE_NAME
00246 #undef PB_DS_RB_TREE_BASE
00247   } // namespace detail
00248 } // namespace __gnu_pbds