3 // Copyright (C) 2005-2021 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
37 * @file pat_trie_/pat_trie_.hpp
38 * Contains an implementation class for a patricia tree.
47 #include <ext/pb_ds/exception.hpp>
48 #include <ext/pb_ds/tag_and_trait.hpp>
49 #include <ext/pb_ds/tree_policy.hpp>
50 #include <ext/pb_ds/detail/cond_dealtor.hpp>
51 #include <ext/pb_ds/detail/type_utils.hpp>
52 #include <ext/pb_ds/detail/types_traits.hpp>
53 #include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
54 #include <ext/pb_ds/detail/pat_trie_/synth_access_traits.hpp>
55 #include <ext/pb_ds/detail/pat_trie_/pat_trie_base.hpp>
57 #include <ext/pb_ds/detail/debug_map_base.hpp>
59 #include <debug/debug.h>
65 #ifdef PB_DS_DATA_TRUE_INDICATOR
66 #define PB_DS_PAT_TRIE_NAME pat_trie_map
69 #ifdef PB_DS_DATA_FALSE_INDICATOR
70 #define PB_DS_PAT_TRIE_NAME pat_trie_set
73 #define PB_DS_CLASS_T_DEC \
74 template<typename Key, typename Mapped, typename Node_And_It_Traits, \
77 #define PB_DS_CLASS_C_DEC \
78 PB_DS_PAT_TRIE_NAME<Key, Mapped, Node_And_It_Traits, _Alloc>
80 #define PB_DS_PAT_TRIE_TRAITS_BASE \
81 types_traits<Key, Mapped, _Alloc, false>
84 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
85 debug_map_base<Key, eq_by_less<Key, std::less<Key> >, \
86 typename rebind_traits<_Alloc, Key>::const_reference>
91 * @brief PATRICIA trie.
92 * @ingroup branch-detail
94 * This implementation loosely borrows ideas from:
95 * 1) Fast Mergeable Integer Maps, Okasaki, Gill 1998
96 * 2) Ptset: Sets of integers implemented as Patricia trees,
97 * Jean-Christophe Filliatr, 2000
99 template<typename Key, typename Mapped, typename Node_And_It_Traits,
101 class PB_DS_PAT_TRIE_NAME :
102 #ifdef _GLIBCXX_DEBUG
103 public PB_DS_DEBUG_MAP_BASE_C_DEC,
105 public Node_And_It_Traits::synth_access_traits,
106 public Node_And_It_Traits::node_update,
107 public PB_DS_PAT_TRIE_TRAITS_BASE,
111 typedef pat_trie_base base_type;
112 typedef PB_DS_PAT_TRIE_TRAITS_BASE traits_base;
113 typedef Node_And_It_Traits traits_type;
115 typedef typename traits_type::synth_access_traits synth_access_traits;
116 typedef typename synth_access_traits::const_iterator a_const_iterator;
118 typedef typename traits_type::node node;
119 typedef rebind_traits<_Alloc, node> __rebind_n;
120 typedef typename __rebind_n::const_pointer node_const_pointer;
121 typedef typename __rebind_n::pointer node_pointer;
123 typedef typename traits_type::head head;
124 typedef rebind_traits<_Alloc, head> __rebind_h;
125 typedef typename __rebind_h::allocator_type head_allocator;
126 typedef typename __rebind_h::pointer head_pointer;
128 typedef typename traits_type::leaf leaf;
129 typedef rebind_traits<_Alloc, leaf> __rebind_l;
130 typedef typename __rebind_l::allocator_type leaf_allocator;
131 typedef typename __rebind_l::pointer leaf_pointer;
132 typedef typename __rebind_l::const_pointer leaf_const_pointer;
134 typedef typename traits_type::inode inode;
135 typedef typename inode::iterator inode_iterator;
136 typedef rebind_traits<_Alloc, inode> __rebind_in;
137 typedef typename __rebind_in::allocator_type inode_allocator;
138 typedef typename __rebind_in::pointer inode_pointer;
139 typedef typename __rebind_in::const_pointer inode_const_pointer;
142 /// Conditional deallocator.
147 bool m_no_action_dtor;
148 bool m_call_destructor;
151 cond_dealtor(leaf_pointer p_nd)
152 : m_p_nd(p_nd), m_no_action_dtor(false), m_call_destructor(false)
157 { m_no_action_dtor = true; }
160 set_call_destructor()
161 { m_call_destructor = true; }
165 if (m_no_action_dtor)
168 if (m_call_destructor)
171 s_leaf_allocator.deallocate(m_p_nd, 1);
176 /// Branch bag, for split-join.
180 typedef inode_pointer __inp;
181 typedef typename rebind_traits<_Alloc, __inp>::allocator_type
184 #ifdef _GLIBCXX_DEBUG
185 typedef std::_GLIBCXX_STD_C::list<__inp, __rebind_inp> bag_type;
187 typedef std::list<__inp, __rebind_inp> bag_type;
195 inode_pointer p_nd = s_inode_allocator.allocate(1);
198 m_bag.push_back(p_nd);
202 s_inode_allocator.deallocate(p_nd, 1);
203 __throw_exception_again;
210 _GLIBCXX_DEBUG_ASSERT(!m_bag.empty());
211 inode_pointer p_nd = *m_bag.begin();
218 while (!m_bag.empty())
220 inode_pointer p_nd = *m_bag.begin();
221 s_inode_allocator.deallocate(p_nd, 1);
226 _GLIBCXX_NODISCARD inline bool
228 { return m_bag.empty(); }
231 #ifdef _GLIBCXX_DEBUG
232 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
235 typedef typename traits_type::null_node_update_pointer null_node_update_pointer;
238 typedef pat_trie_tag container_category;
239 typedef _Alloc allocator_type;
240 typedef typename _Alloc::size_type size_type;
241 typedef typename _Alloc::difference_type difference_type;
243 typedef typename traits_base::key_type key_type;
244 typedef typename traits_base::key_pointer key_pointer;
245 typedef typename traits_base::key_const_pointer key_const_pointer;
246 typedef typename traits_base::key_reference key_reference;
247 typedef typename traits_base::key_const_reference key_const_reference;
248 typedef typename traits_base::mapped_type mapped_type;
249 typedef typename traits_base::mapped_pointer mapped_pointer;
250 typedef typename traits_base::mapped_const_pointer mapped_const_pointer;
251 typedef typename traits_base::mapped_reference mapped_reference;
252 typedef typename traits_base::mapped_const_reference mapped_const_reference;
253 typedef typename traits_base::value_type value_type;
254 typedef typename traits_base::pointer pointer;
255 typedef typename traits_base::const_pointer const_pointer;
256 typedef typename traits_base::reference reference;
257 typedef typename traits_base::const_reference const_reference;
259 typedef typename traits_type::access_traits access_traits;
260 typedef typename traits_type::const_iterator point_const_iterator;
261 typedef typename traits_type::iterator point_iterator;
262 typedef point_const_iterator const_iterator;
263 typedef point_iterator iterator;
265 typedef typename traits_type::reverse_iterator reverse_iterator;
266 typedef typename traits_type::const_reverse_iterator const_reverse_iterator;
267 typedef typename traits_type::node_const_iterator node_const_iterator;
268 typedef typename traits_type::node_iterator node_iterator;
269 typedef typename traits_type::node_update node_update;
271 PB_DS_PAT_TRIE_NAME();
273 PB_DS_PAT_TRIE_NAME(const access_traits&);
275 PB_DS_PAT_TRIE_NAME(const PB_DS_CLASS_C_DEC&);
278 swap(PB_DS_CLASS_C_DEC&);
280 ~PB_DS_PAT_TRIE_NAME();
282 _GLIBCXX_NODISCARD inline bool
295 get_access_traits() const;
301 get_node_update() const;
303 inline std::pair<point_iterator, bool>
304 insert(const_reference);
306 inline mapped_reference
307 operator[](key_const_reference r_key)
309 #ifdef PB_DS_DATA_TRUE_INDICATOR
310 return insert(std::make_pair(r_key, mapped_type())).first->second;
313 return traits_base::s_null_type;
317 inline point_iterator
318 find(key_const_reference);
320 inline point_const_iterator
321 find(key_const_reference) const;
323 inline point_iterator
324 lower_bound(key_const_reference);
326 inline point_const_iterator
327 lower_bound(key_const_reference) const;
329 inline point_iterator
330 upper_bound(key_const_reference);
332 inline point_const_iterator
333 upper_bound(key_const_reference) const;
339 erase(key_const_reference);
341 inline const_iterator
342 erase(const_iterator);
344 #ifdef PB_DS_DATA_TRUE_INDICATOR
349 inline const_reverse_iterator
350 erase(const_reverse_iterator);
352 #ifdef PB_DS_DATA_TRUE_INDICATOR
353 inline reverse_iterator
354 erase(reverse_iterator);
357 template<typename Pred>
362 join(PB_DS_CLASS_C_DEC&);
365 split(key_const_reference, PB_DS_CLASS_C_DEC&);
370 inline const_iterator
376 inline const_iterator
379 inline reverse_iterator
382 inline const_reverse_iterator
385 inline reverse_iterator
388 inline const_reverse_iterator
391 /// Returns a const node_iterator corresponding to the node at the
392 /// root of the tree.
393 inline node_const_iterator
396 /// Returns a node_iterator corresponding to the node at the
397 /// root of the tree.
401 /// Returns a const node_iterator corresponding to a node just
402 /// after a leaf of the tree.
403 inline node_const_iterator
406 /// Returns a node_iterator corresponding to a node just
407 /// after a leaf of the tree.
411 #ifdef PB_DS_PAT_TRIE_TRACE_
417 template<typename It>
419 copy_from_range(It, It);
422 value_swap(PB_DS_CLASS_C_DEC&);
425 recursive_copy_node(node_const_pointer);
432 apply_update(node_pointer, null_node_update_pointer);
434 template<typename Node_Update_>
436 apply_update(node_pointer, Node_Update_*);
439 join_prep(PB_DS_CLASS_C_DEC&, branch_bag&);
442 rec_join_prep(node_const_pointer, node_const_pointer, branch_bag&);
445 rec_join_prep(leaf_const_pointer, leaf_const_pointer, branch_bag&);
448 rec_join_prep(leaf_const_pointer, inode_const_pointer, branch_bag&);
451 rec_join_prep(inode_const_pointer, leaf_const_pointer, branch_bag&);
454 rec_join_prep(inode_const_pointer, inode_const_pointer, branch_bag&);
457 rec_join(node_pointer, node_pointer, size_type, branch_bag&);
460 rec_join(leaf_pointer, leaf_pointer, branch_bag&);
463 rec_join(leaf_pointer, inode_pointer, size_type, branch_bag&);
466 rec_join(inode_pointer, leaf_pointer, size_type, branch_bag&);
469 rec_join(inode_pointer, inode_pointer, branch_bag&);
472 keys_diff_ind(typename access_traits::const_iterator,
473 typename access_traits::const_iterator,
474 typename access_traits::const_iterator,
475 typename access_traits::const_iterator);
478 insert_branch(node_pointer, node_pointer, branch_bag&);
481 update_min_max_for_inserted_leaf(leaf_pointer);
484 erase_leaf(leaf_pointer);
487 actual_erase_leaf(leaf_pointer);
490 clear_imp(node_pointer);
493 erase_fixup(inode_pointer);
496 update_min_max_for_erased_leaf(leaf_pointer);
498 static inline a_const_iterator
499 pref_begin(node_const_pointer);
501 static inline a_const_iterator
502 pref_end(node_const_pointer);
505 find_imp(key_const_reference);
508 lower_bound_imp(key_const_reference);
511 upper_bound_imp(key_const_reference);
513 inline static leaf_const_pointer
514 leftmost_descendant(node_const_pointer);
516 inline static leaf_pointer
517 leftmost_descendant(node_pointer);
519 inline static leaf_const_pointer
520 rightmost_descendant(node_const_pointer);
522 inline static leaf_pointer
523 rightmost_descendant(node_pointer);
525 #ifdef _GLIBCXX_DEBUG
527 assert_valid(const char*, int) const;
530 assert_iterators(const char*, int) const;
533 assert_reverse_iterators(const char*, int) const;
536 recursive_count_leafs(node_const_pointer, const char*, int);
539 #ifdef PB_DS_PAT_TRIE_TRACE_
541 trace_node(node_const_pointer, size_type);
543 template<typename Metadata_>
545 trace_node_metadata(node_const_pointer, type_to_type<Metadata_>);
548 trace_node_metadata(node_const_pointer, type_to_type<null_type>);
552 split_prep(key_const_reference, PB_DS_CLASS_C_DEC&, branch_bag&);
555 rec_split(node_pointer, a_const_iterator, a_const_iterator,
556 PB_DS_CLASS_C_DEC&, branch_bag&);
559 split_insert_branch(size_type, a_const_iterator, inode_iterator,
560 size_type, branch_bag&);
562 static head_allocator s_head_allocator;
563 static inode_allocator s_inode_allocator;
564 static leaf_allocator s_leaf_allocator;
566 head_pointer m_p_head;
570 #define PB_DS_ASSERT_NODE_VALID(X) \
571 _GLIBCXX_DEBUG_ONLY(X->assert_valid(this, __FILE__, __LINE__);)
573 #define PB_DS_RECURSIVE_COUNT_LEAFS(X) \
574 recursive_count_leafs(X, __FILE__, __LINE__)
576 #include <ext/pb_ds/detail/pat_trie_/constructors_destructor_fn_imps.hpp>
577 #include <ext/pb_ds/detail/pat_trie_/iterators_fn_imps.hpp>
578 #include <ext/pb_ds/detail/pat_trie_/insert_join_fn_imps.hpp>
579 #include <ext/pb_ds/detail/pat_trie_/erase_fn_imps.hpp>
580 #include <ext/pb_ds/detail/pat_trie_/find_fn_imps.hpp>
581 #include <ext/pb_ds/detail/pat_trie_/info_fn_imps.hpp>
582 #include <ext/pb_ds/detail/pat_trie_/policy_access_fn_imps.hpp>
583 #include <ext/pb_ds/detail/pat_trie_/split_fn_imps.hpp>
584 #include <ext/pb_ds/detail/pat_trie_/debug_fn_imps.hpp>
585 #include <ext/pb_ds/detail/pat_trie_/trace_fn_imps.hpp>
586 #include <ext/pb_ds/detail/pat_trie_/update_fn_imps.hpp>
588 #undef PB_DS_RECURSIVE_COUNT_LEAFS
589 #undef PB_DS_ASSERT_NODE_VALID
590 #undef PB_DS_CLASS_C_DEC
591 #undef PB_DS_CLASS_T_DEC
592 #undef PB_DS_PAT_TRIE_NAME
593 #undef PB_DS_PAT_TRIE_TRAITS_BASE
594 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
595 } // namespace detail
596 } // namespace __gnu_pbds