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
39 * Contains an implementation class for a patricia tree.
43 * This implementation loosely borrows ideas from:
44 * 1) Fast Mergeable Integer Maps, Okasaki, Gill 1998
45 * 2) Ptset: Sets of integers implemented as Patricia trees,
46 * Jean-Christophe Filliatr, 2000
49 #include <ext/pb_ds/detail/pat_trie_/synth_e_access_traits.hpp>
50 #include <ext/pb_ds/detail/pat_trie_/node_base.hpp>
51 #include <ext/pb_ds/exception.hpp>
52 #include <ext/pb_ds/tag_and_trait.hpp>
53 #include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
54 #include <ext/pb_ds/detail/types_traits.hpp>
55 #include <ext/pb_ds/tree_policy.hpp>
56 #include <ext/pb_ds/detail/cond_dealtor.hpp>
57 #include <ext/pb_ds/detail/type_utils.hpp>
65 #include <ext/pb_ds/detail/debug_map_base.hpp>
67 #include <debug/debug.h>
73 #define PB_DS_CLASS_T_DEC \
74 template<typename Key, typename Mapped, typename Node_And_It_Traits, \
77 #ifdef PB_DS_DATA_TRUE_INDICATOR
78 #define PB_DS_CLASS_NAME pat_trie_data_
81 #ifdef PB_DS_DATA_FALSE_INDICATOR
82 #define PB_DS_CLASS_NAME pat_trie_no_data_
85 #define PB_DS_CLASS_C_DEC \
86 PB_DS_CLASS_NAME<Key, Mapped, Node_And_It_Traits, Allocator>
88 #define PB_DS_TYPES_TRAITS_C_DEC \
89 types_traits<Key, Mapped, Allocator, false>
92 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
93 debug_map_base<Key, eq_by_less<Key, \
94 std::less<Key> >, typename Allocator::template rebind<Key>::other::const_reference>
97 #ifdef PB_DS_DATA_TRUE_INDICATOR
98 #define PB_DS_V2F(X) (X).first
99 #define PB_DS_V2S(X) (X).second
100 #define PB_DS_EP2VP(X)& ((X)->m_value)
103 #ifdef PB_DS_DATA_FALSE_INDICATOR
104 #define PB_DS_V2F(X) (X)
105 #define PB_DS_V2S(X) Mapped_Data()
106 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
110 * class description = PATRICIA trie implementation.">
112 template<typename Key
,
114 typename Node_And_It_Traits
,
116 class PB_DS_CLASS_NAME
:
117 #ifdef _GLIBCXX_DEBUG
118 public PB_DS_DEBUG_MAP_BASE_C_DEC
,
120 public Node_And_It_Traits::synth_e_access_traits
,
121 public Node_And_It_Traits::node_update
,
122 public PB_DS_TYPES_TRAITS_C_DEC
125 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base
;
127 typedef typename
Node_And_It_Traits::synth_e_access_traits synth_e_access_traits
;
128 typedef typename
Allocator::template rebind
<synth_e_access_traits
>::other::const_pointer const_e_access_traits_pointer
;
129 typedef typename
synth_e_access_traits::const_iterator const_e_iterator
;
131 typedef typename
Node_And_It_Traits::node node
;
132 typedef typename
Allocator::template rebind
<node
>::other::const_pointer const_node_pointer
;
134 typedef typename
Allocator::template rebind
<node
>::other::pointer node_pointer
;
136 typedef typename
Node_And_It_Traits::head head
;
137 typedef typename
Allocator::template rebind
<head
>::other head_allocator
;
138 typedef typename
head_allocator::pointer head_pointer
;
140 typedef typename
Node_And_It_Traits::leaf leaf
;
141 typedef typename
Allocator::template rebind
<leaf
>::other leaf_allocator
;
142 typedef typename
leaf_allocator::const_pointer const_leaf_pointer
;
143 typedef typename
leaf_allocator::pointer leaf_pointer
;
145 typedef typename
Node_And_It_Traits::internal_node internal_node
;
146 typedef typename
Allocator::template rebind
<internal_node
>::other internal_node_allocator
;
147 typedef typename
internal_node_allocator::const_pointer const_internal_node_pointer
;
148 typedef typename
internal_node_allocator::pointer internal_node_pointer
;
150 #include <ext/pb_ds/detail/pat_trie_/cond_dtor_entry_dealtor.hpp>
152 #ifdef _GLIBCXX_DEBUG
153 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base
;
156 #include <ext/pb_ds/detail/pat_trie_/split_join_branch_bag.hpp>
158 typedef typename
Node_And_It_Traits::null_node_update_pointer null_node_update_pointer
;
161 typedef pat_trie_tag container_category
;
162 typedef Allocator allocator_type
;
163 typedef typename
Allocator::size_type size_type
;
164 typedef typename
Allocator::difference_type difference_type
;
166 typedef typename
traits_base::key_type key_type
;
167 typedef typename
traits_base::key_pointer key_pointer
;
168 typedef typename
traits_base::const_key_pointer const_key_pointer
;
169 typedef typename
traits_base::key_reference key_reference
;
170 typedef typename
traits_base::const_key_reference const_key_reference
;
171 typedef typename
traits_base::mapped_type mapped_type
;
172 typedef typename
traits_base::mapped_pointer mapped_pointer
;
173 typedef typename
traits_base::const_mapped_pointer const_mapped_pointer
;
174 typedef typename
traits_base::mapped_reference mapped_reference
;
175 typedef typename
traits_base::const_mapped_reference const_mapped_reference
;
176 typedef typename
traits_base::value_type value_type
;
177 typedef typename
traits_base::pointer pointer
;
178 typedef typename
traits_base::const_pointer const_pointer
;
179 typedef typename
traits_base::reference reference
;
180 typedef typename
traits_base::const_reference const_reference
;
182 typedef typename
Node_And_It_Traits::const_iterator const_point_iterator
;
183 typedef typename
Node_And_It_Traits::iterator point_iterator
;
184 typedef const_point_iterator const_iterator
;
185 typedef point_iterator iterator
;
187 typedef typename
Node_And_It_Traits::const_reverse_iterator const_reverse_iterator
;
188 typedef typename
Node_And_It_Traits::reverse_iterator reverse_iterator
;
189 typedef typename
Node_And_It_Traits::const_node_iterator const_node_iterator
;
190 typedef typename
Node_And_It_Traits::node_iterator node_iterator
;
191 typedef typename
Node_And_It_Traits::e_access_traits e_access_traits
;
192 typedef typename
Node_And_It_Traits::node_update node_update
;
196 PB_DS_CLASS_NAME(const e_access_traits
&);
198 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC
&);
201 swap(PB_DS_CLASS_C_DEC
&);
215 get_e_access_traits();
217 const e_access_traits
&
218 get_e_access_traits() const;
224 get_node_update() const;
226 inline std::pair
<point_iterator
, bool>
227 insert(const_reference
);
229 inline mapped_reference
230 operator[](const_key_reference r_key
)
232 #ifdef PB_DS_DATA_TRUE_INDICATOR
233 return insert(std::make_pair(r_key
, mapped_type())).first
->second
;
236 return traits_base::s_null_mapped
;
240 inline point_iterator
241 find(const_key_reference
);
243 inline const_point_iterator
244 find(const_key_reference
) const;
246 inline point_iterator
247 lower_bound(const_key_reference
);
249 inline const_point_iterator
250 lower_bound(const_key_reference
) const;
252 inline point_iterator
253 upper_bound(const_key_reference
);
255 inline const_point_iterator
256 upper_bound(const_key_reference
) const;
262 erase(const_key_reference
);
264 inline const_iterator
265 erase(const_iterator
);
267 #ifdef PB_DS_DATA_TRUE_INDICATOR
272 inline const_reverse_iterator
273 erase(const_reverse_iterator
);
275 #ifdef PB_DS_DATA_TRUE_INDICATOR
276 inline reverse_iterator
277 erase(reverse_iterator
);
280 template<typename Pred
>
285 join(PB_DS_CLASS_C_DEC
&);
288 split(const_key_reference
, PB_DS_CLASS_C_DEC
&);
293 inline const_iterator
299 inline const_iterator
302 inline reverse_iterator
305 inline const_reverse_iterator
308 inline reverse_iterator
311 inline const_reverse_iterator
314 inline const_node_iterator
320 inline const_node_iterator
326 #ifdef PB_DS_PAT_TRIE_TRACE_
333 template<typename It
>
335 copy_from_range(It
, It
);
338 value_swap(PB_DS_CLASS_C_DEC
&);
341 recursive_copy_node(const_node_pointer
);
349 apply_update(node_pointer
, null_node_update_pointer
);
351 template<typename Node_Update_
>
353 apply_update(node_pointer
, Node_Update_
*);
356 join_prep(PB_DS_CLASS_C_DEC
&, split_join_branch_bag
&);
359 rec_join_prep(const_node_pointer
, const_node_pointer
,
360 split_join_branch_bag
&);
363 rec_join_prep(const_leaf_pointer
, const_leaf_pointer
,
364 split_join_branch_bag
&);
367 rec_join_prep(const_leaf_pointer
, const_internal_node_pointer
,
368 split_join_branch_bag
&);
371 rec_join_prep(const_internal_node_pointer
, const_leaf_pointer
,
372 split_join_branch_bag
&);
375 rec_join_prep(const_internal_node_pointer
, const_internal_node_pointer
,
376 split_join_branch_bag
&);
379 rec_join(node_pointer
, node_pointer
, size_type
, split_join_branch_bag
&);
382 rec_join(leaf_pointer
, leaf_pointer
, split_join_branch_bag
&);
385 rec_join(leaf_pointer
, internal_node_pointer
, size_type
,
386 split_join_branch_bag
&);
389 rec_join(internal_node_pointer
, leaf_pointer
, size_type
,
390 split_join_branch_bag
&);
393 rec_join(internal_node_pointer
, internal_node_pointer
,
394 split_join_branch_bag
&);
397 keys_diff_ind(typename
e_access_traits::const_iterator
,
398 typename
e_access_traits::const_iterator
,
399 typename
e_access_traits::const_iterator
,
400 typename
e_access_traits::const_iterator
);
402 internal_node_pointer
403 insert_branch(node_pointer
, node_pointer
, split_join_branch_bag
&);
406 update_min_max_for_inserted_leaf(leaf_pointer
);
409 erase_leaf(leaf_pointer
);
412 actual_erase_leaf(leaf_pointer
);
415 clear_imp(node_pointer
);
418 erase_fixup(internal_node_pointer
);
421 update_min_max_for_erased_leaf(leaf_pointer
);
423 static inline const_e_iterator
424 pref_begin(const_node_pointer
);
426 static inline const_e_iterator
427 pref_end(const_node_pointer
);
430 find_imp(const_key_reference
);
433 lower_bound_imp(const_key_reference
);
436 upper_bound_imp(const_key_reference
);
438 inline static const_leaf_pointer
439 leftmost_descendant(const_node_pointer
);
441 inline static leaf_pointer
442 leftmost_descendant(node_pointer
);
444 inline static const_leaf_pointer
445 rightmost_descendant(const_node_pointer
);
447 inline static leaf_pointer
448 rightmost_descendant(node_pointer
);
450 #ifdef _GLIBCXX_DEBUG
452 assert_valid(const char* file
, int line
) const;
455 assert_iterators(const char* file
, int line
) const;
458 assert_reverse_iterators(const char* file
, int line
) const;
461 recursive_count_leafs(const_node_pointer
,
462 const char* file
, int line
);
465 #ifdef PB_DS_PAT_TRIE_TRACE_
467 trace_node(const_node_pointer
, size_type
);
469 template<typename Metadata_
>
471 trace_node_metadata(const_node_pointer
, type_to_type
<Metadata_
>);
474 trace_node_metadata(const_node_pointer
, type_to_type
<null_node_metadata
>);
478 split_prep(const_key_reference
, PB_DS_CLASS_C_DEC
&,
479 split_join_branch_bag
&);
482 rec_split(node_pointer
, const_e_iterator
, const_e_iterator
,
483 PB_DS_CLASS_C_DEC
&, split_join_branch_bag
&);
486 split_insert_branch(size_type
, const_e_iterator
,
487 typename
internal_node::iterator
,
488 size_type
, split_join_branch_bag
&);
490 static head_allocator s_head_allocator
;
491 static internal_node_allocator s_internal_node_allocator
;
492 static leaf_allocator s_leaf_allocator
;
494 head_pointer m_p_head
;
498 #define PB_DS_ASSERT_VALID(X) \
499 _GLIBCXX_DEBUG_ONLY(X.assert_valid(__FILE__, __LINE__);)
501 #define PB_DS_ASSERT_NODE_VALID(X) \
502 _GLIBCXX_DEBUG_ONLY(X->assert_valid(this, __FILE__, __LINE__);)
504 #define PB_DS_RECURSIVE_COUNT_LEAFS(X) \
505 recursive_count_leafs(X, __FILE__, __LINE__)
507 #define PB_DS_CHECK_KEY_EXISTS(_Key) \
508 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(_Key, __FILE__, __LINE__);)
510 #define PB_DS_CHECK_KEY_DOES_NOT_EXIST(_Key) \
511 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(_Key, \
512 __FILE__, __LINE__);)
514 #define PB_DS_DEBUG_VERIFY(_Cond) \
515 _GLIBCXX_DEBUG_VERIFY_AT(_Cond, \
516 _M_message(#_Cond" assertion from %1;:%2;") \
517 ._M_string(__FILE__)._M_integer(__LINE__) \
520 #include <ext/pb_ds/detail/pat_trie_/constructors_destructor_fn_imps.hpp>
521 #include <ext/pb_ds/detail/pat_trie_/iterators_fn_imps.hpp>
522 #include <ext/pb_ds/detail/pat_trie_/insert_join_fn_imps.hpp>
523 #include <ext/pb_ds/detail/pat_trie_/erase_fn_imps.hpp>
524 #include <ext/pb_ds/detail/pat_trie_/find_fn_imps.hpp>
525 #include <ext/pb_ds/detail/pat_trie_/info_fn_imps.hpp>
526 #include <ext/pb_ds/detail/pat_trie_/policy_access_fn_imps.hpp>
527 #include <ext/pb_ds/detail/pat_trie_/split_fn_imps.hpp>
528 #include <ext/pb_ds/detail/pat_trie_/debug_fn_imps.hpp>
529 #include <ext/pb_ds/detail/pat_trie_/trace_fn_imps.hpp>
530 #include <ext/pb_ds/detail/pat_trie_/update_fn_imps.hpp>
532 #undef PB_DS_DEBUG_VERIFY
533 #undef PB_DS_CHECK_KEY_DOES_NOT_EXIST
534 #undef PB_DS_CHECK_KEY_EXISTS
535 #undef PB_DS_RECURSIVE_COUNT_LEAFS
536 #undef PB_DS_ASSERT_NODE_VALID
537 #undef PB_DS_ASSERT_VALID
538 #undef PB_DS_CLASS_C_DEC
539 #undef PB_DS_CLASS_T_DEC
540 #undef PB_DS_CLASS_NAME
541 #undef PB_DS_TYPES_TRAITS_C_DEC
542 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
547 } // namespace detail
548 } // namespace __gnu_pbds