STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bin_search_tree_.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2013 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 
41 #include <ext/pb_ds/exception.hpp>
48 #ifdef _GLIBCXX_DEBUG
50 #endif
51 #include <utility>
52 #include <functional>
53 #include <debug/debug.h>
54 
55 namespace __gnu_pbds
56 {
57  namespace detail
58  {
59 #ifdef PB_DS_DATA_TRUE_INDICATOR
60 #define PB_DS_BIN_TREE_NAME bin_search_tree_map
61 #endif
62 
63 #ifdef PB_DS_DATA_FALSE_INDICATOR
64 #define PB_DS_BIN_TREE_NAME bin_search_tree_set
65 #endif
66 
67 #define PB_DS_CLASS_T_DEC \
68  template<typename Key, typename Mapped, typename Cmp_Fn, \
69  typename Node_And_It_Traits, typename _Alloc>
70 
71 #define PB_DS_CLASS_C_DEC \
72  PB_DS_BIN_TREE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
73 
74 #define PB_DS_BIN_TREE_TRAITS_BASE \
75  types_traits<Key, Mapped, _Alloc, false>
76 
77 #ifdef _GLIBCXX_DEBUG
78 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
79  debug_map_base<Key, eq_by_less<Key, Cmp_Fn>, \
80  typename _Alloc::template rebind<Key>::other::const_reference>
81 #endif
82 
83 #ifdef PB_DS_TREE_TRACE
84 #define PB_DS_TREE_TRACE_BASE_C_DEC \
85  tree_trace_base<typename Node_And_It_Traits::node_const_iterator, \
86  typename Node_And_It_Traits::node_iterator, \
87  Cmp_Fn, true, _Alloc>
88 #endif
89 
90 
91  /*
92  * @brief Binary search tree (BST).
93  *
94  * This implementation uses an idea from the SGI STL (using a @a
95  * header node which is needed for efficient iteration).
96  */
97  template<typename Key, typename Mapped, typename Cmp_Fn,
98  typename Node_And_It_Traits, typename _Alloc>
100 #ifdef _GLIBCXX_DEBUG
101  public PB_DS_DEBUG_MAP_BASE_C_DEC,
102 #endif
103 #ifdef PB_DS_TREE_TRACE
104  public PB_DS_TREE_TRACE_BASE_C_DEC,
105 #endif
106  public Cmp_Fn,
108  public Node_And_It_Traits::node_update
109  {
110  typedef Node_And_It_Traits traits_type;
111 
112  protected:
114 
115  typedef
116  typename _Alloc::template rebind<typename traits_type::node>::other
118 
119  typedef typename node_allocator::value_type node;
120  typedef typename node_allocator::pointer node_pointer;
121 
122  typedef typename traits_type::null_node_update_pointer
124 
125  private:
127 
128 #ifdef _GLIBCXX_DEBUG
129  typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
130 #endif
131 
132  public:
133  typedef typename _Alloc::size_type size_type;
134  typedef typename _Alloc::difference_type difference_type;
135  typedef typename traits_base::key_type key_type;
136  typedef typename traits_base::key_pointer key_pointer;
137  typedef typename traits_base::key_const_pointer key_const_pointer;
138  typedef typename traits_base::key_reference key_reference;
139  typedef typename traits_base::key_const_reference key_const_reference;
140 
141 #ifdef PB_DS_DATA_TRUE_INDICATOR
142  typedef typename traits_base::mapped_type mapped_type;
143  typedef typename traits_base::mapped_pointer mapped_pointer;
144  typedef typename traits_base::mapped_const_pointer mapped_const_pointer;
145  typedef typename traits_base::mapped_reference mapped_reference;
146  typedef typename traits_base::mapped_const_reference mapped_const_reference;
147 #endif
148 
149  typedef typename traits_base::value_type value_type;
150  typedef typename traits_base::pointer pointer;
151  typedef typename traits_base::const_pointer const_pointer;
152  typedef typename traits_base::reference reference;
153  typedef typename traits_base::const_reference const_reference;
154  typedef typename traits_type::point_const_iterator point_const_iterator;
155 
157  typedef typename traits_type::point_iterator point_iterator;
159 
160  typedef typename traits_type::const_reverse_iterator const_reverse_iterator;
161 
162  typedef typename traits_type::reverse_iterator reverse_iterator;
163  typedef typename traits_type::node_const_iterator node_const_iterator;
164  typedef typename traits_type::node_iterator node_iterator;
165  typedef typename traits_type::node_update node_update;
166 
167  typedef Cmp_Fn cmp_fn;
168  typedef _Alloc allocator_type;
169 
171 
172  PB_DS_BIN_TREE_NAME(const Cmp_Fn&);
173 
174  PB_DS_BIN_TREE_NAME(const Cmp_Fn&, const node_update&);
175 
177 
178  void
180 
182 
183  inline bool
184  empty() const;
185 
186  inline size_type
187  size() const;
188 
189  inline size_type
190  max_size() const;
191 
192  Cmp_Fn&
193  get_cmp_fn();
194 
195  const Cmp_Fn&
196  get_cmp_fn() const;
197 
198  inline point_iterator
200 
201  inline point_const_iterator
203 
204  inline point_iterator
206 
207  inline point_const_iterator
209 
210  inline point_iterator
212 
213  inline point_const_iterator
214  find(key_const_reference) const;
215 
216  inline iterator
217  begin();
218 
219  inline const_iterator
220  begin() const;
221 
222  inline iterator
223  end();
224 
225  inline const_iterator
226  end() const;
227 
228  inline reverse_iterator
229  rbegin();
230 
232  rbegin() const;
233 
234  inline reverse_iterator
235  rend();
236 
238  rend() const;
239 
242  inline node_const_iterator
243  node_begin() const;
244 
247  inline node_iterator
248  node_begin();
249 
252  inline node_const_iterator
253  node_end() const;
254 
257  inline node_iterator
258  node_end();
259 
260  void
261  clear();
262 
263  protected:
264  void
266 
267  void
269 
270  inline iterator
272 
273  inline iterator
275 
276  inline node_pointer
278 
279  inline node_pointer
281 
282  inline void
284 
285  inline std::pair<node_pointer, bool>
287 
288  inline void
290 
291  static void
293 
294  inline std::pair<point_iterator, bool>
296 
297  inline void
299 
300  inline void
302 
303  inline void
305 
306  inline void
308 
309  template<typename Node_Update_>
310  inline void
311  apply_update(node_pointer, Node_Update_*);
312 
313  inline void
315 
316  template<typename Node_Update_>
317  inline void
318  update_to_top(node_pointer, Node_Update_*);
319 
320  bool
322 
323  void
325 
326  bool
328 
329  void
331 
332  size_type
334 
335 #ifdef _GLIBCXX_DEBUG
336  void
337  assert_valid(const char*, int) const;
338 
339  void
340  structure_only_assert_valid(const char*, int) const;
341 
342  void
343  assert_node_consistent(const node_pointer, const char*, int) const;
344 #endif
345 
346  private:
347 #ifdef _GLIBCXX_DEBUG
348  void
349  assert_iterators(const char*, int) const;
350 
351  void
352  assert_consistent_with_debug_base(const char*, int) const;
353 
354  void
355  assert_node_consistent_with_left(const node_pointer,
356  const char*, int) const;
357 
358  void
359  assert_node_consistent_with_right(const node_pointer,
360  const char*, int) const;
361 
362  void
363  assert_consistent_with_debug_base(const node_pointer,
364  const char*, int) const;
365 
366  void
367  assert_min(const char*, int) const;
368 
369  void
370  assert_min_imp(const node_pointer, const char*, int) const;
371 
372  void
373  assert_max(const char*, int) const;
374 
375  void
376  assert_max_imp(const node_pointer, const char*, int) const;
377 
378  void
379  assert_size(const char*, int) const;
380 
381  typedef std::pair<const_pointer, const_pointer> node_consistent_t;
382 
383  node_consistent_t
384  assert_node_consistent_(const node_pointer, const char*, int) const;
385 #endif
386 
387  void
388  initialize();
389 
392 
393  protected:
397  };
398 
399 #define PB_DS_STRUCT_ONLY_ASSERT_VALID(X) \
400  _GLIBCXX_DEBUG_ONLY(X.structure_only_assert_valid(__FILE__, __LINE__);)
401 
402 #define PB_DS_ASSERT_NODE_CONSISTENT(_Node) \
403  _GLIBCXX_DEBUG_ONLY(assert_node_consistent(_Node, __FILE__, __LINE__);)
404 
415 
416 #undef PB_DS_ASSERT_NODE_CONSISTENT
417 #undef PB_DS_STRUCT_ONLY_ASSERT_VALID
418 #undef PB_DS_CLASS_C_DEC
419 #undef PB_DS_CLASS_T_DEC
420 #undef PB_DS_BIN_TREE_NAME
421 #undef PB_DS_BIN_TREE_TRAITS_BASE
422 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
423 
424 #ifdef PB_DS_TREE_TRACE
425 #undef PB_DS_TREE_TRACE_BASE_C_DEC
426 #endif
427  } // namespace detail
428 } // namespace __gnu_pbds
_Alloc allocator_type
Definition: bin_search_tree_.hpp:168
traits_type::node_const_iterator node_const_iterator
Definition: bin_search_tree_.hpp:163
traits_type::node_update node_update
Definition: bin_search_tree_.hpp:165
traits_base::pointer pointer
Definition: bin_search_tree_.hpp:150
void update_to_top(node_pointer, null_node_update_pointer)
void split_finish(PB_DS_CLASS_C_DEC &)
_Alloc::size_type size_type
Definition: bin_search_tree_.hpp:133
static node_allocator s_node_allocator
Definition: bin_search_tree_.hpp:396
point_const_iterator const_iterator
Definition: bin_search_tree_.hpp:156
traits_base::const_pointer const_pointer
Definition: bin_search_tree_.hpp:151
node_pointer get_new_node_for_leaf_insert(const_reference, false_type)
void update_min_max_for_erased_node(node_pointer)
node_pointer m_p_head
Definition: bin_search_tree_.hpp:394
traits_base::reference reference
Definition: bin_search_tree_.hpp:152
point_iterator find(key_const_reference)
#define PB_DS_CLASS_C_DEC
Definition: bin_search_tree_.hpp:71
node_const_iterator node_begin() const
size_type recursive_count(node_pointer) const
node_const_iterator node_end() const
point_iterator upper_bound(key_const_reference)
node_pointer recursive_copy_node(const node_pointer)
iterator insert_imp_empty(const_reference)
std::pair< node_pointer, bool > erase(node_pointer)
traits_type::null_node_update_pointer null_node_update_pointer
Definition: bin_search_tree_.hpp:123
traits_base::key_const_pointer key_const_pointer
Definition: bin_search_tree_.hpp:137
#define PB_DS_BIN_TREE_TRAITS_BASE
Definition: bin_search_tree_.hpp:74
traits_type::reverse_iterator reverse_iterator
Definition: bin_search_tree_.hpp:162
traits_base::const_reference const_reference
Definition: bin_search_tree_.hpp:153
traits_type::node_iterator node_iterator
Definition: bin_search_tree_.hpp:164
traits_type::point_const_iterator point_const_iterator
Definition: bin_search_tree_.hpp:154
bool join_prep(PB_DS_CLASS_C_DEC &)
void value_swap(PB_DS_CLASS_C_DEC &)
std::tr1::integral_constant< int, 1 > true_type
Definition: type_utils.hpp:70
Conditional dey destructor, cc_hash.
Definition: cond_key_dtor_entry_dealtor.hpp:47
traits_type::point_iterator point_iterator
Definition: bin_search_tree_.hpp:157
Definition: bin_search_tree_.hpp:99
PB_DS_BIN_TREE_TRAITS_BASE traits_base
Definition: bin_search_tree_.hpp:113
_Alloc::template rebind< typename traits_type::node >::other node_allocator
Definition: bin_search_tree_.hpp:117
point_iterator lower_bound(key_const_reference)
traits_base::value_type value_type
Definition: bin_search_tree_.hpp:149
node_allocator::pointer node_pointer
Definition: bin_search_tree_.hpp:120
iterator insert_leaf_new(const_reference, node_pointer, bool)
cond_dealtor< node, _Alloc > cond_dealtor_t
Definition: bin_search_tree_.hpp:126
void swap(PB_DS_CLASS_C_DEC &)
traits_base::key_pointer key_pointer
Definition: bin_search_tree_.hpp:136
traits_base::key_const_reference key_const_reference
Definition: bin_search_tree_.hpp:139
point_iterator iterator
Definition: bin_search_tree_.hpp:158
_Alloc::difference_type difference_type
Definition: bin_search_tree_.hpp:134
size_type m_size
Definition: bin_search_tree_.hpp:395
node_allocator::value_type node
Definition: bin_search_tree_.hpp:119
Cmp_Fn cmp_fn
Definition: bin_search_tree_.hpp:167
traits_base::key_reference key_reference
Definition: bin_search_tree_.hpp:138
void apply_update(node_pointer, null_node_update_pointer)
std::pair< point_iterator, bool > insert_leaf(const_reference)
static void clear_imp(node_pointer)
void join_finish(PB_DS_CLASS_C_DEC &)
traits_type::const_reverse_iterator const_reverse_iterator
Definition: bin_search_tree_.hpp:160
bool split_prep(key_const_reference, PB_DS_CLASS_C_DEC &)
traits_base::key_type key_type
Definition: bin_search_tree_.hpp:135
std::tr1::integral_constant< int, 0 > false_type
Definition: type_utils.hpp:71
Node_And_It_Traits traits_type
Definition: bin_search_tree_.hpp:110