3 // Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
38 * @file pat_trie_/pat_trie_.hpp
39 * Contains an implementation class for a patricia tree.
48 #include <ext/pb_ds/exception.hpp>
49 #include <ext/pb_ds/tag_and_trait.hpp>
50 #include <ext/pb_ds/tree_policy.hpp>
51 #include <ext/pb_ds/detail/cond_dealtor.hpp>
52 #include <ext/pb_ds/detail/type_utils.hpp>
53 #include <ext/pb_ds/detail/types_traits.hpp>
54 #include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
55 #include <ext/pb_ds/detail/pat_trie_/synth_access_traits.hpp>
56 #include <ext/pb_ds/detail/pat_trie_/pat_trie_base.hpp>
58 #include <ext/pb_ds/detail/debug_map_base.hpp>
60 #include <debug/debug.h>
66 #ifdef PB_DS_DATA_TRUE_INDICATOR
67 #define PB_DS_PAT_TRIE_NAME pat_trie_map
70 #ifdef PB_DS_DATA_FALSE_INDICATOR
71 #define PB_DS_PAT_TRIE_NAME pat_trie_set
74 #define PB_DS_CLASS_T_DEC \
75 template<typename Key, typename Mapped, typename Node_And_It_Traits, \
78 #define PB_DS_CLASS_C_DEC \
79 PB_DS_PAT_TRIE_NAME<Key, Mapped, Node_And_It_Traits, _Alloc>
81 #define PB_DS_PAT_TRIE_TRAITS_BASE \
82 types_traits<Key, Mapped, _Alloc, false>
85 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
86 debug_map_base<Key, eq_by_less<Key, std::less<Key> >, \
87 typename _Alloc::template rebind<Key>::other::const_reference>
92 * @brief PATRICIA trie.
93 * @ingroup branch-detail
95 * This implementation loosely borrows ideas from:
96 * 1) Fast Mergeable Integer Maps, Okasaki, Gill 1998
97 * 2) Ptset: Sets of integers implemented as Patricia trees,
98 * Jean-Christophe Filliatr, 2000
100 template<typename Key, typename Mapped, typename Node_And_It_Traits,
102 class PB_DS_PAT_TRIE_NAME :
103 #ifdef _GLIBCXX_DEBUG
104 public PB_DS_DEBUG_MAP_BASE_C_DEC,
106 public Node_And_It_Traits::synth_access_traits,
107 public Node_And_It_Traits::node_update,
108 public PB_DS_PAT_TRIE_TRAITS_BASE,
112 typedef pat_trie_base base_type;
113 typedef PB_DS_PAT_TRIE_TRAITS_BASE traits_base;
114 typedef Node_And_It_Traits traits_type;
116 typedef typename traits_type::synth_access_traits synth_access_traits;
117 typedef typename synth_access_traits::const_iterator a_const_iterator;
119 typedef typename traits_type::node node;
120 typedef typename _Alloc::template rebind<node> __rebind_n;
121 typedef typename __rebind_n::other::const_pointer node_const_pointer;
122 typedef typename __rebind_n::other::pointer node_pointer;
124 typedef typename traits_type::head head;
125 typedef typename _Alloc::template rebind<head> __rebind_h;
126 typedef typename __rebind_h::other head_allocator;
127 typedef typename head_allocator::pointer head_pointer;
129 typedef typename traits_type::leaf leaf;
130 typedef typename _Alloc::template rebind<leaf> __rebind_l;
131 typedef typename __rebind_l::other leaf_allocator;
132 typedef typename leaf_allocator::pointer leaf_pointer;
133 typedef typename leaf_allocator::const_pointer leaf_const_pointer;
135 typedef typename traits_type::inode inode;
136 typedef typename inode::iterator inode_iterator;
137 typedef typename _Alloc::template rebind<inode> __rebind_in;
138 typedef typename __rebind_in::other __rebind_ina;
139 typedef typename __rebind_in::other inode_allocator;
140 typedef typename __rebind_ina::pointer inode_pointer;
141 typedef typename __rebind_ina::const_pointer inode_const_pointer;
144 /// Conditional deallocator.
149 bool m_no_action_dtor;
150 bool m_call_destructor;
153 cond_dealtor(leaf_pointer p_nd)
154 : m_p_nd(p_nd), m_no_action_dtor(false), m_call_destructor(false)
159 { m_no_action_dtor = true; }
162 set_call_destructor()
163 { m_call_destructor = true; }
167 if (m_no_action_dtor)
170 if (m_call_destructor)
173 s_leaf_allocator.deallocate(m_p_nd, 1);
178 /// Branch bag, for split-join.
182 typedef inode_pointer __inp;
183 typedef typename _Alloc::template rebind<__inp>::other __rebind_inp;
185 #ifdef _GLIBCXX_DEBUG
186 typedef std::_GLIBCXX_STD_C::list<__inp, __rebind_inp> bag_type;
188 typedef std::list<__inp, __rebind_inp> bag_type;
196 inode_pointer p_nd = s_inode_allocator.allocate(1);
199 m_bag.push_back(p_nd);
203 s_inode_allocator.deallocate(p_nd, 1);
204 __throw_exception_again;
211 _GLIBCXX_DEBUG_ASSERT(!m_bag.empty());
212 inode_pointer p_nd = *m_bag.begin();
219 while (!m_bag.empty())
221 inode_pointer p_nd = *m_bag.begin();
222 s_inode_allocator.deallocate(p_nd, 1);
229 { return m_bag.empty(); }
232 #ifdef _GLIBCXX_DEBUG
233 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
236 typedef typename traits_type::null_node_update_pointer null_node_update_pointer;
239 typedef pat_trie_tag container_category;
240 typedef _Alloc allocator_type;
241 typedef typename _Alloc::size_type size_type;
242 typedef typename _Alloc::difference_type difference_type;
244 typedef typename traits_base::key_type key_type;
245 typedef typename traits_base::key_pointer key_pointer;
246 typedef typename traits_base::key_const_pointer key_const_pointer;
247 typedef typename traits_base::key_reference key_reference;
248 typedef typename traits_base::key_const_reference key_const_reference;
249 typedef typename traits_base::mapped_type mapped_type;
250 typedef typename traits_base::mapped_pointer mapped_pointer;
251 typedef typename traits_base::mapped_const_pointer mapped_const_pointer;
252 typedef typename traits_base::mapped_reference mapped_reference;
253 typedef typename traits_base::mapped_const_reference mapped_const_reference;
254 typedef typename traits_base::value_type value_type;
255 typedef typename traits_base::pointer pointer;
256 typedef typename traits_base::const_pointer const_pointer;
257 typedef typename traits_base::reference reference;
258 typedef typename traits_base::const_reference const_reference;
260 typedef typename traits_type::access_traits access_traits;
261 typedef typename traits_type::const_iterator point_const_iterator;
262 typedef typename traits_type::iterator point_iterator;
263 typedef point_const_iterator const_iterator;
264 typedef point_iterator iterator;
266 typedef typename traits_type::reverse_iterator reverse_iterator;
267 typedef typename traits_type::const_reverse_iterator const_reverse_iterator;
268 typedef typename traits_type::node_const_iterator node_const_iterator;
269 typedef typename traits_type::node_iterator node_iterator;
270 typedef typename traits_type::node_update node_update;
272 PB_DS_PAT_TRIE_NAME();
274 PB_DS_PAT_TRIE_NAME(const access_traits&);
276 PB_DS_PAT_TRIE_NAME(const PB_DS_CLASS_C_DEC&);
279 swap(PB_DS_CLASS_C_DEC&);
281 ~PB_DS_PAT_TRIE_NAME();
296 get_access_traits() const;
302 get_node_update() const;
304 inline std::pair<point_iterator, bool>
305 insert(const_reference);
307 inline mapped_reference
308 operator[](key_const_reference r_key)
310 #ifdef PB_DS_DATA_TRUE_INDICATOR
311 return insert(std::make_pair(r_key, mapped_type())).first->second;
314 return traits_base::s_null_type;
318 inline point_iterator
319 find(key_const_reference);
321 inline point_const_iterator
322 find(key_const_reference) const;
324 inline point_iterator
325 lower_bound(key_const_reference);
327 inline point_const_iterator
328 lower_bound(key_const_reference) const;
330 inline point_iterator
331 upper_bound(key_const_reference);
333 inline point_const_iterator
334 upper_bound(key_const_reference) const;
340 erase(key_const_reference);
342 inline const_iterator
343 erase(const_iterator);
345 #ifdef PB_DS_DATA_TRUE_INDICATOR
350 inline const_reverse_iterator
351 erase(const_reverse_iterator);
353 #ifdef PB_DS_DATA_TRUE_INDICATOR
354 inline reverse_iterator
355 erase(reverse_iterator);
358 template<typename Pred>
363 join(PB_DS_CLASS_C_DEC&);
366 split(key_const_reference, PB_DS_CLASS_C_DEC&);
371 inline const_iterator
377 inline const_iterator
380 inline reverse_iterator
383 inline const_reverse_iterator
386 inline reverse_iterator
389 inline const_reverse_iterator
392 /// Returns a const node_iterator corresponding to the node at the
393 /// root of the tree.
394 inline node_const_iterator
397 /// Returns a node_iterator corresponding to the node at the
398 /// root of the tree.
402 /// Returns a const node_iterator corresponding to a node just
403 /// after a leaf of the tree.
404 inline node_const_iterator
407 /// Returns a node_iterator corresponding to a node just
408 /// after a leaf of the tree.
412 #ifdef PB_DS_PAT_TRIE_TRACE_
418 template<typename It>
420 copy_from_range(It, It);
423 value_swap(PB_DS_CLASS_C_DEC&);
426 recursive_copy_node(node_const_pointer);
433 apply_update(node_pointer, null_node_update_pointer);
435 template<typename Node_Update_>
437 apply_update(node_pointer, Node_Update_*);
440 join_prep(PB_DS_CLASS_C_DEC&, branch_bag&);
443 rec_join_prep(node_const_pointer, node_const_pointer, branch_bag&);
446 rec_join_prep(leaf_const_pointer, leaf_const_pointer, branch_bag&);
449 rec_join_prep(leaf_const_pointer, inode_const_pointer, branch_bag&);
452 rec_join_prep(inode_const_pointer, leaf_const_pointer, branch_bag&);
455 rec_join_prep(inode_const_pointer, inode_const_pointer, branch_bag&);
458 rec_join(node_pointer, node_pointer, size_type, branch_bag&);
461 rec_join(leaf_pointer, leaf_pointer, branch_bag&);
464 rec_join(leaf_pointer, inode_pointer, size_type, branch_bag&);
467 rec_join(inode_pointer, leaf_pointer, size_type, branch_bag&);
470 rec_join(inode_pointer, inode_pointer, branch_bag&);
473 keys_diff_ind(typename access_traits::const_iterator,
474 typename access_traits::const_iterator,
475 typename access_traits::const_iterator,
476 typename access_traits::const_iterator);
479 insert_branch(node_pointer, node_pointer, branch_bag&);
482 update_min_max_for_inserted_leaf(leaf_pointer);
485 erase_leaf(leaf_pointer);
488 actual_erase_leaf(leaf_pointer);
491 clear_imp(node_pointer);
494 erase_fixup(inode_pointer);
497 update_min_max_for_erased_leaf(leaf_pointer);
499 static inline a_const_iterator
500 pref_begin(node_const_pointer);
502 static inline a_const_iterator
503 pref_end(node_const_pointer);
506 find_imp(key_const_reference);
509 lower_bound_imp(key_const_reference);
512 upper_bound_imp(key_const_reference);
514 inline static leaf_const_pointer
515 leftmost_descendant(node_const_pointer);
517 inline static leaf_pointer
518 leftmost_descendant(node_pointer);
520 inline static leaf_const_pointer
521 rightmost_descendant(node_const_pointer);
523 inline static leaf_pointer
524 rightmost_descendant(node_pointer);
526 #ifdef _GLIBCXX_DEBUG
528 assert_valid(const char*, int) const;
531 assert_iterators(const char*, int) const;
534 assert_reverse_iterators(const char*, int) const;
537 recursive_count_leafs(node_const_pointer, const char*, int);
540 #ifdef PB_DS_PAT_TRIE_TRACE_
542 trace_node(node_const_pointer, size_type);
544 template<typename Metadata_>
546 trace_node_metadata(node_const_pointer, type_to_type<Metadata_>);
549 trace_node_metadata(node_const_pointer, type_to_type<null_type>);
553 split_prep(key_const_reference, PB_DS_CLASS_C_DEC&, branch_bag&);
556 rec_split(node_pointer, a_const_iterator, a_const_iterator,
557 PB_DS_CLASS_C_DEC&, branch_bag&);
560 split_insert_branch(size_type, a_const_iterator, inode_iterator,
561 size_type, branch_bag&);
563 static head_allocator s_head_allocator;
564 static inode_allocator s_inode_allocator;
565 static leaf_allocator s_leaf_allocator;
567 head_pointer m_p_head;
571 #define PB_DS_ASSERT_NODE_VALID(X) \
572 _GLIBCXX_DEBUG_ONLY(X->assert_valid(this, __FILE__, __LINE__);)
574 #define PB_DS_RECURSIVE_COUNT_LEAFS(X) \
575 recursive_count_leafs(X, __FILE__, __LINE__)
577 #include <ext/pb_ds/detail/pat_trie_/constructors_destructor_fn_imps.hpp>
578 #include <ext/pb_ds/detail/pat_trie_/iterators_fn_imps.hpp>
579 #include <ext/pb_ds/detail/pat_trie_/insert_join_fn_imps.hpp>
580 #include <ext/pb_ds/detail/pat_trie_/erase_fn_imps.hpp>
581 #include <ext/pb_ds/detail/pat_trie_/find_fn_imps.hpp>
582 #include <ext/pb_ds/detail/pat_trie_/info_fn_imps.hpp>
583 #include <ext/pb_ds/detail/pat_trie_/policy_access_fn_imps.hpp>
584 #include <ext/pb_ds/detail/pat_trie_/split_fn_imps.hpp>
585 #include <ext/pb_ds/detail/pat_trie_/debug_fn_imps.hpp>
586 #include <ext/pb_ds/detail/pat_trie_/trace_fn_imps.hpp>
587 #include <ext/pb_ds/detail/pat_trie_/update_fn_imps.hpp>
589 #undef PB_DS_RECURSIVE_COUNT_LEAFS
590 #undef PB_DS_ASSERT_NODE_VALID
591 #undef PB_DS_CLASS_C_DEC
592 #undef PB_DS_CLASS_T_DEC
593 #undef PB_DS_PAT_TRIE_NAME
594 #undef PB_DS_PAT_TRIE_TRAITS_BASE
595 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
596 } // namespace detail
597 } // namespace __gnu_pbds