1 // -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the terms 7 // of the GNU General Public License as published by the Free Software 8 // Foundation; either version 3, or (at your option) any later 9 // version. 10 11 // This library is distributed in the hope that it will be useful, but 12 // WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 26 27 // Permission to use, copy, modify, sell, and distribute this software 28 // is hereby granted without fee, provided that the above copyright 29 // notice appears in all copies, and that both that copyright notice 30 // and this permission notice appear in supporting documentation. None 31 // of the above authors, nor IBM Haifa Research Laboratories, make any 32 // representation about the suitability of this software for any 33 // purpose. It is provided "as is" without express or implied 34 // warranty. 35 36 /** 37 * @file rb_tree_.hpp 38 * Contains an implementation for rb_tree_. 39 */ 40 /* 41 * This implementation uses an idea from the SGI STL (using a "header" node 42 * which is needed for efficient iteration). 43 */ 44 45 #ifdef PB_DS_DATA_TRUE_INDICATOR 46 #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_TRUE_INDICATOR 47 #define PB_DS_BIN_SEARCH_TREE_HPP__DATA_TRUE_INDICATOR 48 #include <ext/pb_ds/detail/bin_search_tree_/bin_search_tree_.hpp> 49 #endif 50 #endif 51 52 #ifdef PB_DS_DATA_FALSE_INDICATOR 53 #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_FALSE_INDICATOR 54 #define PB_DS_BIN_SEARCH_TREE_HPP__DATA_FALSE_INDICATOR 55 #include <ext/pb_ds/detail/bin_search_tree_/bin_search_tree_.hpp> 56 #endif 57 #endif 58 59 #include <ext/pb_ds/detail/standard_policies.hpp> 60 #include <ext/pb_ds/detail/basic_types.hpp> 61 #include <utility> 62 #include <vector> 63 #include <assert.h> 64 #include <debug/debug.h> 65 66 namespace __gnu_pbds 67 { 68 namespace detail 69 { 70 #define PB_DS_CLASS_T_DEC \ 71 template<typename Key, typename Mapped, typename Cmp_Fn, \ 72 typename Node_And_It_Traits, typename Allocator> 73 74 #ifdef PB_DS_DATA_TRUE_INDICATOR 75 #define PB_DS_CLASS_NAME rb_tree_data_ 76 #endif 77 78 #ifdef PB_DS_DATA_TRUE_INDICATOR 79 #define PB_DS_BASE_CLASS_NAME bin_search_tree_data_ 80 #endif 81 82 #ifdef PB_DS_DATA_FALSE_INDICATOR 83 #define PB_DS_CLASS_NAME rb_tree_no_data_ 84 #endif 85 86 #ifdef PB_DS_DATA_FALSE_INDICATOR 87 #define PB_DS_BASE_CLASS_NAME bin_search_tree_no_data_ 88 #endif 89 90 #define PB_DS_CLASS_C_DEC \ 91 PB_DS_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator> 92 93 #define PB_DS_BASE_C_DEC \ 94 PB_DS_BASE_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator> 95 96 #ifdef PB_DS_DATA_TRUE_INDICATOR 97 #define PB_DS_V2F(X) (X).first 98 #define PB_DS_V2S(X) (X).second 99 #define PB_DS_EP2VP(X)& ((X)->m_value) 100 #endif 101 102 #ifdef PB_DS_DATA_FALSE_INDICATOR 103 #define PB_DS_V2F(X) (X) 104 #define PB_DS_V2S(X) Mapped_Data() 105 #define PB_DS_EP2VP(X)& ((X)->m_value.first) 106 #endif 107 108 template<typename Key, 109 typename Mapped, 110 typename Cmp_Fn, 111 typename Node_And_It_Traits, 112 typename Allocator> 113 class PB_DS_CLASS_NAME : public PB_DS_BASE_C_DEC 114 { 115 private: 116 typedef PB_DS_BASE_C_DEC base_type; 117 typedef typename base_type::node_pointer node_pointer; 118 119 public: 120 typedef Cmp_Fn cmp_fn; 121 typedef Allocator allocator_type; 122 typedef typename Allocator::size_type size_type; 123 typedef typename Allocator::difference_type difference_type; 124 typedef typename base_type::key_type key_type; 125 typedef typename base_type::key_pointer key_pointer; 126 typedef typename base_type::const_key_pointer const_key_pointer; 127 typedef typename base_type::key_reference key_reference; 128 typedef typename base_type::const_key_reference const_key_reference; 129 typedef typename base_type::mapped_type mapped_type; 130 typedef typename base_type::mapped_pointer mapped_pointer; 131 typedef typename base_type::const_mapped_pointer const_mapped_pointer; 132 typedef typename base_type::mapped_reference mapped_reference; 133 typedef typename base_type::const_mapped_reference const_mapped_reference; 134 typedef typename base_type::value_type value_type; 135 typedef typename base_type::pointer pointer; 136 typedef typename base_type::const_pointer const_pointer; 137 typedef typename base_type::reference reference; 138 typedef typename base_type::const_reference const_reference; 139 typedef typename base_type::point_iterator point_iterator; 140 typedef typename base_type::const_iterator const_point_iterator; 141 typedef typename base_type::iterator iterator; 142 typedef typename base_type::const_iterator const_iterator; 143 typedef typename base_type::reverse_iterator reverse_iterator; 144 typedef typename base_type::const_reverse_iterator const_reverse_iterator; 145 typedef typename base_type::node_update node_update; 146 147 148 PB_DS_CLASS_NAME(); 149 150 PB_DS_CLASS_NAME(const Cmp_Fn&); 151 152 PB_DS_CLASS_NAME(const Cmp_Fn&, const node_update&); 153 154 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&); 155 156 void 157 swap(PB_DS_CLASS_C_DEC&); 158 159 template<typename It> 160 void 161 copy_from_range(It, It); 162 163 inline std::pair<point_iterator, bool> 164 insert(const_reference); 165 166 inline mapped_reference 167 operator[](const_key_reference r_key) 168 { 169 #ifdef PB_DS_DATA_TRUE_INDICATOR 170 _GLIBCXX_DEBUG_ONLY(assert_valid();) 171 std::pair<point_iterator, bool> ins_pair = 172 base_type::insert_leaf(value_type(r_key, mapped_type())); 173 174 if (ins_pair.second == true) 175 { 176 ins_pair.first.m_p_nd->m_red = true; 177 _GLIBCXX_DEBUG_ONLY(this->structure_only_assert_valid();) 178 insert_fixup(ins_pair.first.m_p_nd); 179 } 180 _GLIBCXX_DEBUG_ONLY(assert_valid();) 181 return ins_pair.first.m_p_nd->m_value.second; 182 #else 183 insert(r_key); 184 return base_type::s_null_mapped; 185 #endif 186 } 187 188 inline bool 189 erase(const_key_reference); 190 191 inline iterator 192 erase(iterator); 193 194 inline reverse_iterator 195 erase(reverse_iterator); 196 197 template<typename Pred> 198 inline size_type 199 erase_if(Pred); 200 201 void 202 join(PB_DS_CLASS_C_DEC&); 203 204 void 205 split(const_key_reference, PB_DS_CLASS_C_DEC&); 206 207 protected: 208 209 private: 210 211 #ifdef _GLIBCXX_DEBUG 212 void 213 assert_valid() const; 214 215 size_type 216 assert_node_consistent(const node_pointer) const; 217 #endif 218 219 inline static bool 220 is_effectively_black(const node_pointer); 221 222 void 223 initialize(); 224 225 void 226 insert_fixup(node_pointer); 227 228 void 229 erase_node(node_pointer); 230 231 void 232 remove_node(node_pointer); 233 234 void 235 remove_fixup(node_pointer, node_pointer); 236 237 void 238 split_imp(node_pointer, PB_DS_CLASS_C_DEC&); 239 240 inline node_pointer 241 split_min(); 242 243 std::pair<node_pointer, node_pointer> 244 split_min_imp(); 245 246 void 247 join_imp(node_pointer, node_pointer); 248 249 std::pair<node_pointer, node_pointer> 250 find_join_pos_right(node_pointer, size_type, size_type); 251 252 std::pair<node_pointer, node_pointer> 253 find_join_pos_left(node_pointer, size_type, size_type); 254 255 inline size_type 256 black_height(node_pointer); 257 258 void 259 split_at_node(node_pointer, PB_DS_CLASS_C_DEC&); 260 }; 261 262 #include <ext/pb_ds/detail/rb_tree_map_/constructors_destructor_fn_imps.hpp> 263 #include <ext/pb_ds/detail/rb_tree_map_/insert_fn_imps.hpp> 264 #include <ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp> 265 #include <ext/pb_ds/detail/rb_tree_map_/debug_fn_imps.hpp> 266 #include <ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp> 267 #include <ext/pb_ds/detail/rb_tree_map_/info_fn_imps.hpp> 268 269 #undef PB_DS_CLASS_T_DEC 270 #undef PB_DS_CLASS_C_DEC 271 #undef PB_DS_CLASS_NAME 272 #undef PB_DS_BASE_CLASS_NAME 273 #undef PB_DS_BASE_C_DEC 274 #undef PB_DS_V2F 275 #undef PB_DS_EP2VP 276 #undef PB_DS_V2S 277 278 } // namespace detail 279 } // namespace __gnu_pbds 280 281