3 // Copyright (C) 2005, 2006, 2007 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
44 * Contains an implementation class for a patricia tree.
48 * This implementation loosely borrows ideas from:
49 * 1) "Fast Mergeable Integer Maps", Okasaki, Gill 1998
50 * 2) "Ptset: Sets of integers implemented as Patricia trees",
51 * Jean-Christophe Filliatr, 2000
54 #include <ext/pb_ds/detail/pat_trie_/synth_e_access_traits.hpp>
55 #include <ext/pb_ds/detail/pat_trie_/node_base.hpp>
56 #include <ext/pb_ds/exception.hpp>
57 #include <ext/pb_ds/tag_and_trait.hpp>
58 #include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
59 #include <ext/pb_ds/detail/types_traits.hpp>
60 #include <ext/pb_ds/tree_policy.hpp>
61 #include <ext/pb_ds/detail/cond_dealtor.hpp>
62 #include <ext/pb_ds/detail/type_utils.hpp>
70 #include <ext/pb_ds/detail/debug_map_base.hpp>
72 #include <debug/debug.h>
78 #define PB_DS_CLASS_T_DEC \
79 template<typename Key, typename Mapped, typename Node_And_It_Traits, \
82 #ifdef PB_DS_DATA_TRUE_INDICATOR
83 #define PB_DS_CLASS_NAME pat_trie_data_
86 #ifdef PB_DS_DATA_FALSE_INDICATOR
87 #define PB_DS_CLASS_NAME pat_trie_no_data_
90 #define PB_DS_CLASS_C_DEC \
91 PB_DS_CLASS_NAME<Key, Mapped, Node_And_It_Traits, Allocator>
93 #define PB_DS_TYPES_TRAITS_C_DEC \
94 types_traits<Key, Mapped, Allocator, false>
97 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
98 debug_map_base<Key, eq_by_less<Key, \
99 std::less<Key> >, typename Allocator::template rebind<Key>::other::const_reference>
102 #ifdef PB_DS_DATA_TRUE_INDICATOR
103 #define PB_DS_V2F(X) (X).first
104 #define PB_DS_V2S(X) (X).second
105 #define PB_DS_EP2VP(X)& ((X)->m_value)
108 #ifdef PB_DS_DATA_FALSE_INDICATOR
109 #define PB_DS_V2F(X) (X)
110 #define PB_DS_V2S(X) Mapped_Data()
111 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
115 * class description = PATRICIA trie implementation.">
117 template<typename Key
,
119 typename Node_And_It_Traits
,
121 class PB_DS_CLASS_NAME
:
122 #ifdef _GLIBCXX_DEBUG
123 public PB_DS_DEBUG_MAP_BASE_C_DEC
,
125 public Node_And_It_Traits::synth_e_access_traits
,
126 public Node_And_It_Traits::node_update
,
127 public PB_DS_TYPES_TRAITS_C_DEC
130 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base
;
132 typedef typename
Node_And_It_Traits::synth_e_access_traits synth_e_access_traits
;
133 typedef typename
Allocator::template rebind
<synth_e_access_traits
>::other::const_pointer const_e_access_traits_pointer
;
134 typedef typename
synth_e_access_traits::const_iterator const_e_iterator
;
136 typedef typename
Node_And_It_Traits::node node
;
137 typedef typename
Allocator::template rebind
<node
>::other::const_pointer const_node_pointer
;
139 typedef typename
Allocator::template rebind
<node
>::other::pointer node_pointer
;
141 typedef typename
Node_And_It_Traits::head head
;
142 typedef typename
Allocator::template rebind
<head
>::other head_allocator
;
143 typedef typename
head_allocator::pointer head_pointer
;
145 typedef typename
Node_And_It_Traits::leaf leaf
;
146 typedef typename
Allocator::template rebind
<leaf
>::other leaf_allocator
;
147 typedef typename
leaf_allocator::const_pointer const_leaf_pointer
;
148 typedef typename
leaf_allocator::pointer leaf_pointer
;
150 typedef typename
Node_And_It_Traits::internal_node internal_node
;
151 typedef typename
Allocator::template rebind
<internal_node
>::other internal_node_allocator
;
152 typedef typename
internal_node_allocator::const_pointer const_internal_node_pointer
;
153 typedef typename
internal_node_allocator::pointer internal_node_pointer
;
155 #include <ext/pb_ds/detail/pat_trie_/cond_dtor_entry_dealtor.hpp>
157 #ifdef _GLIBCXX_DEBUG
158 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base
;
161 #include <ext/pb_ds/detail/pat_trie_/split_join_branch_bag.hpp>
163 typedef typename
Node_And_It_Traits::null_node_update_pointer null_node_update_pointer
;
166 typedef pat_trie_tag container_category
;
167 typedef Allocator allocator
;
168 typedef typename
Allocator::size_type size_type
;
169 typedef typename
Allocator::difference_type difference_type
;
171 typedef typename
traits_base::key_type key_type
;
172 typedef typename
traits_base::key_pointer key_pointer
;
173 typedef typename
traits_base::const_key_pointer const_key_pointer
;
174 typedef typename
traits_base::key_reference key_reference
;
175 typedef typename
traits_base::const_key_reference const_key_reference
;
176 typedef typename
traits_base::mapped_type mapped_type
;
177 typedef typename
traits_base::mapped_pointer mapped_pointer
;
178 typedef typename
traits_base::const_mapped_pointer const_mapped_pointer
;
179 typedef typename
traits_base::mapped_reference mapped_reference
;
180 typedef typename
traits_base::const_mapped_reference const_mapped_reference
;
181 typedef typename
traits_base::value_type value_type
;
182 typedef typename
traits_base::pointer pointer
;
183 typedef typename
traits_base::const_pointer const_pointer
;
184 typedef typename
traits_base::reference reference
;
185 typedef typename
traits_base::const_reference const_reference
;
187 typedef typename
Node_And_It_Traits::const_iterator const_point_iterator
;
188 typedef typename
Node_And_It_Traits::iterator point_iterator
;
189 typedef const_point_iterator const_iterator
;
190 typedef point_iterator iterator
;
192 typedef typename
Node_And_It_Traits::const_reverse_iterator const_reverse_iterator
;
193 typedef typename
Node_And_It_Traits::reverse_iterator reverse_iterator
;
194 typedef typename
Node_And_It_Traits::const_node_iterator const_node_iterator
;
195 typedef typename
Node_And_It_Traits::node_iterator node_iterator
;
196 typedef typename
Node_And_It_Traits::e_access_traits e_access_traits
;
197 typedef typename
Node_And_It_Traits::node_update node_update
;
201 PB_DS_CLASS_NAME(const e_access_traits
&);
203 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC
&);
206 swap(PB_DS_CLASS_C_DEC
&);
220 get_e_access_traits();
222 const e_access_traits
&
223 get_e_access_traits() const;
229 get_node_update() const;
231 inline std::pair
<point_iterator
, bool>
232 insert(const_reference
);
234 inline mapped_reference
235 operator[](const_key_reference r_key
)
237 #ifdef PB_DS_DATA_TRUE_INDICATOR
238 return insert(std::make_pair(r_key
, mapped_type())).first
->second
;
241 return traits_base::s_null_mapped
;
245 inline point_iterator
246 find(const_key_reference
);
248 inline const_point_iterator
249 find(const_key_reference
) const;
251 inline point_iterator
252 lower_bound(const_key_reference
);
254 inline const_point_iterator
255 lower_bound(const_key_reference
) const;
257 inline point_iterator
258 upper_bound(const_key_reference
);
260 inline const_point_iterator
261 upper_bound(const_key_reference
) const;
267 erase(const_key_reference
);
269 inline const_iterator
270 erase(const_iterator
);
272 #ifdef PB_DS_DATA_TRUE_INDICATOR
277 inline const_reverse_iterator
278 erase(const_reverse_iterator
);
280 #ifdef PB_DS_DATA_TRUE_INDICATOR
281 inline reverse_iterator
282 erase(reverse_iterator
);
285 template<typename Pred
>
290 join(PB_DS_CLASS_C_DEC
&);
293 split(const_key_reference
, PB_DS_CLASS_C_DEC
&);
298 inline const_iterator
304 inline const_iterator
307 inline reverse_iterator
310 inline const_reverse_iterator
313 inline reverse_iterator
316 inline const_reverse_iterator
319 inline const_node_iterator
325 inline const_node_iterator
331 #ifdef PB_DS_PAT_TRIE_TRACE_
338 template<typename It
>
340 copy_from_range(It
, It
);
343 value_swap(PB_DS_CLASS_C_DEC
&);
346 recursive_copy_node(const_node_pointer
);
354 apply_update(node_pointer
, null_node_update_pointer
);
356 template<typename Node_Update_
>
358 apply_update(node_pointer
, Node_Update_
*);
361 join_prep(PB_DS_CLASS_C_DEC
&, split_join_branch_bag
&);
364 rec_join_prep(const_node_pointer
, const_node_pointer
,
365 split_join_branch_bag
&);
368 rec_join_prep(const_leaf_pointer
, const_leaf_pointer
,
369 split_join_branch_bag
&);
372 rec_join_prep(const_leaf_pointer
, const_internal_node_pointer
,
373 split_join_branch_bag
&);
376 rec_join_prep(const_internal_node_pointer
, const_leaf_pointer
,
377 split_join_branch_bag
&);
380 rec_join_prep(const_internal_node_pointer
, const_internal_node_pointer
,
381 split_join_branch_bag
&);
384 rec_join(node_pointer
, node_pointer
, size_type
, split_join_branch_bag
&);
387 rec_join(leaf_pointer
, leaf_pointer
, split_join_branch_bag
&);
390 rec_join(leaf_pointer
, internal_node_pointer
, size_type
,
391 split_join_branch_bag
&);
394 rec_join(internal_node_pointer
, leaf_pointer
, size_type
,
395 split_join_branch_bag
&);
398 rec_join(internal_node_pointer
, internal_node_pointer
,
399 split_join_branch_bag
&);
402 keys_diff_ind(typename
e_access_traits::const_iterator
, typename
e_access_traits::const_iterator
, typename
e_access_traits::const_iterator
, typename
e_access_traits::const_iterator
);
404 internal_node_pointer
405 insert_branch(node_pointer
, node_pointer
, split_join_branch_bag
&);
408 update_min_max_for_inserted_leaf(leaf_pointer
);
411 erase_leaf(leaf_pointer
);
414 actual_erase_leaf(leaf_pointer
);
417 clear_imp(node_pointer
);
420 erase_fixup(internal_node_pointer
);
423 update_min_max_for_erased_leaf(leaf_pointer
);
425 static inline const_e_iterator
426 pref_begin(const_node_pointer
);
428 static inline const_e_iterator
429 pref_end(const_node_pointer
);
432 find_imp(const_key_reference
);
435 lower_bound_imp(const_key_reference
);
438 upper_bound_imp(const_key_reference
);
440 inline static const_leaf_pointer
441 leftmost_descendant(const_node_pointer
);
443 inline static leaf_pointer
444 leftmost_descendant(node_pointer
);
446 inline static const_leaf_pointer
447 rightmost_descendant(const_node_pointer
);
449 inline static leaf_pointer
450 rightmost_descendant(node_pointer
);
452 #ifdef _GLIBCXX_DEBUG
454 assert_valid() const;
457 assert_iterators() const;
460 assert_reverse_iterators() const;
463 recursive_count_leafs(const_node_pointer
);
466 #ifdef PB_DS_PAT_TRIE_TRACE_
468 trace_node(const_node_pointer
, size_type
);
470 template<typename Metadata_
>
472 trace_node_metadata(const_node_pointer
, type_to_type
<Metadata_
>);
475 trace_node_metadata(const_node_pointer
, type_to_type
<null_node_metadata
>);
479 split_prep(const_key_reference
, PB_DS_CLASS_C_DEC
&,
480 split_join_branch_bag
&);
483 rec_split(node_pointer
, const_e_iterator
, const_e_iterator
,
484 PB_DS_CLASS_C_DEC
&, split_join_branch_bag
&);
487 split_insert_branch(size_type
, const_e_iterator
,
488 typename
internal_node::iterator
,
489 size_type
, split_join_branch_bag
&);
491 static head_allocator s_head_allocator
;
492 static internal_node_allocator s_internal_node_allocator
;
493 static leaf_allocator s_leaf_allocator
;
495 head_pointer m_p_head
;
499 #include <ext/pb_ds/detail/pat_trie_/constructors_destructor_fn_imps.hpp>
500 #include <ext/pb_ds/detail/pat_trie_/iterators_fn_imps.hpp>
501 #include <ext/pb_ds/detail/pat_trie_/insert_join_fn_imps.hpp>
502 #include <ext/pb_ds/detail/pat_trie_/erase_fn_imps.hpp>
503 #include <ext/pb_ds/detail/pat_trie_/find_fn_imps.hpp>
504 #include <ext/pb_ds/detail/pat_trie_/info_fn_imps.hpp>
505 #include <ext/pb_ds/detail/pat_trie_/policy_access_fn_imps.hpp>
506 #include <ext/pb_ds/detail/pat_trie_/split_fn_imps.hpp>
507 #include <ext/pb_ds/detail/pat_trie_/debug_fn_imps.hpp>
508 #include <ext/pb_ds/detail/pat_trie_/trace_fn_imps.hpp>
509 #include <ext/pb_ds/detail/pat_trie_/update_fn_imps.hpp>
511 #undef PB_DS_CLASS_C_DEC
512 #undef PB_DS_CLASS_T_DEC
513 #undef PB_DS_CLASS_NAME
514 #undef PB_DS_TYPES_TRAITS_C_DEC
515 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
520 } // namespace detail
521 } // namespace __gnu_pbds