3 // Copyright (C) 2005, 2006 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/map_debug_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_MAP_DEBUG_BASE_C_DEC \
98 map_debug_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)
114 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
115 typedef static_assert_dumclass<sizeof(static_assert<(bool)(E)>)> \
116 UNIQUE##static_assert_type
119 * class description = PATRICIA trie implementation.">
121 template<typename Key,
123 typename Node_And_It_Traits,
125 class PB_DS_CLASS_NAME :
126 #ifdef _GLIBCXX_DEBUG
127 public PB_DS_MAP_DEBUG_BASE_C_DEC,
129 public Node_And_It_Traits::synth_e_access_traits,
130 public Node_And_It_Traits::node_update,
131 public PB_DS_TYPES_TRAITS_C_DEC
134 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
136 typedef typename Node_And_It_Traits::synth_e_access_traits synth_e_access_traits;
137 typedef typename Allocator::template rebind<synth_e_access_traits>::other::const_pointer const_e_access_traits_pointer;
138 typedef typename synth_e_access_traits::const_iterator const_e_iterator;
140 typedef typename Node_And_It_Traits::node node;
141 typedef typename Allocator::template rebind<node>::other::const_pointer const_node_pointer;
143 typedef typename Allocator::template rebind<node>::other::pointer node_pointer;
145 typedef typename Node_And_It_Traits::head head;
146 typedef typename Allocator::template rebind<head>::other head_allocator;
147 typedef typename head_allocator::pointer head_pointer;
149 typedef typename Node_And_It_Traits::leaf leaf;
150 typedef typename Allocator::template rebind<leaf>::other leaf_allocator;
151 typedef typename leaf_allocator::const_pointer const_leaf_pointer;
152 typedef typename leaf_allocator::pointer leaf_pointer;
154 typedef typename Node_And_It_Traits::internal_node internal_node;
155 typedef typename Allocator::template rebind<internal_node>::other internal_node_allocator;
156 typedef typename internal_node_allocator::const_pointer const_internal_node_pointer;
157 typedef typename internal_node_allocator::pointer internal_node_pointer;
159 #include <ext/pb_ds/detail/pat_trie_/cond_dtor_entry_dealtor.hpp>
161 #ifdef _GLIBCXX_DEBUG
162 typedef PB_DS_MAP_DEBUG_BASE_C_DEC map_debug_base;
165 #include <ext/pb_ds/detail/pat_trie_/split_join_branch_bag.hpp>
167 typedef typename Node_And_It_Traits::null_node_update_pointer null_node_update_pointer;
170 typedef pat_trie_tag container_category;
171 typedef Allocator allocator;
172 typedef typename Allocator::size_type size_type;
173 typedef typename Allocator::difference_type difference_type;
175 typedef typename traits_base::key_type key_type;
176 typedef typename traits_base::key_pointer key_pointer;
177 typedef typename traits_base::const_key_pointer const_key_pointer;
178 typedef typename traits_base::key_reference key_reference;
179 typedef typename traits_base::const_key_reference const_key_reference;
180 typedef typename traits_base::mapped_type mapped_type;
181 typedef typename traits_base::mapped_pointer mapped_pointer;
182 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
183 typedef typename traits_base::mapped_reference mapped_reference;
184 typedef typename traits_base::const_mapped_reference const_mapped_reference;
185 typedef typename traits_base::value_type value_type;
186 typedef typename traits_base::pointer pointer;
187 typedef typename traits_base::const_pointer const_pointer;
188 typedef typename traits_base::reference reference;
189 typedef typename traits_base::const_reference const_reference;
191 typedef typename Node_And_It_Traits::const_iterator const_point_iterator;
192 typedef typename Node_And_It_Traits::iterator point_iterator;
193 typedef const_point_iterator const_iterator;
194 typedef point_iterator iterator;
196 typedef typename Node_And_It_Traits::const_reverse_iterator const_reverse_iterator;
197 typedef typename Node_And_It_Traits::reverse_iterator reverse_iterator;
198 typedef typename Node_And_It_Traits::const_node_iterator const_node_iterator;
199 typedef typename Node_And_It_Traits::node_iterator node_iterator;
200 typedef typename Node_And_It_Traits::e_access_traits e_access_traits;
201 typedef typename Node_And_It_Traits::node_update node_update;
205 PB_DS_CLASS_NAME(const e_access_traits&);
207 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
210 swap(PB_DS_CLASS_C_DEC&);
224 get_e_access_traits();
226 const e_access_traits&
227 get_e_access_traits() const;
233 get_node_update() const;
235 inline std::pair<point_iterator, bool>
236 insert(const_reference);
238 inline mapped_reference
239 operator[](const_key_reference r_key)
241 #ifdef PB_DS_DATA_TRUE_INDICATOR
242 return insert(std::make_pair(r_key, mapped_type())).first->second;
245 return traits_base::s_null_mapped;
249 inline point_iterator
250 find(const_key_reference);
252 inline const_point_iterator
253 find(const_key_reference) const;
255 inline point_iterator
256 lower_bound(const_key_reference);
258 inline const_point_iterator
259 lower_bound(const_key_reference) const;
261 inline point_iterator
262 upper_bound(const_key_reference);
264 inline const_point_iterator
265 upper_bound(const_key_reference) const;
271 erase(const_key_reference);
273 inline const_iterator
274 erase(const_iterator);
276 #ifdef PB_DS_DATA_TRUE_INDICATOR
281 inline const_reverse_iterator
282 erase(const_reverse_iterator);
284 #ifdef PB_DS_DATA_TRUE_INDICATOR
285 inline reverse_iterator
286 erase(reverse_iterator);
289 template<typename Pred>
294 join(PB_DS_CLASS_C_DEC&);
297 split(const_key_reference, PB_DS_CLASS_C_DEC&);
302 inline const_iterator
308 inline const_iterator
311 inline reverse_iterator
314 inline const_reverse_iterator
317 inline reverse_iterator
320 inline const_reverse_iterator
323 inline const_node_iterator
329 inline const_node_iterator
335 #ifdef PB_DS_PAT_TRIE_TRACE_
342 template<typename It>
344 copy_from_range(It, It);
347 value_swap(PB_DS_CLASS_C_DEC&);
350 recursive_copy_node(const_node_pointer);
358 apply_update(node_pointer, null_node_update_pointer);
360 template<typename Node_Update_>
362 apply_update(node_pointer, Node_Update_*);
365 join_prep(PB_DS_CLASS_C_DEC&, split_join_branch_bag&);
368 rec_join_prep(const_node_pointer, const_node_pointer,
369 split_join_branch_bag&);
372 rec_join_prep(const_leaf_pointer, const_leaf_pointer,
373 split_join_branch_bag&);
376 rec_join_prep(const_leaf_pointer, const_internal_node_pointer,
377 split_join_branch_bag&);
380 rec_join_prep(const_internal_node_pointer, const_leaf_pointer,
381 split_join_branch_bag&);
384 rec_join_prep(const_internal_node_pointer, const_internal_node_pointer,
385 split_join_branch_bag&);
388 rec_join(node_pointer, node_pointer, size_type, split_join_branch_bag&);
391 rec_join(leaf_pointer, leaf_pointer, split_join_branch_bag&);
394 rec_join(leaf_pointer, internal_node_pointer, size_type,
395 split_join_branch_bag&);
398 rec_join(internal_node_pointer, leaf_pointer, size_type,
399 split_join_branch_bag&);
402 rec_join(internal_node_pointer, internal_node_pointer,
403 split_join_branch_bag&);
406 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);
408 internal_node_pointer
409 insert_branch(node_pointer, node_pointer, split_join_branch_bag&);
412 update_min_max_for_inserted_leaf(leaf_pointer);
415 erase_leaf(leaf_pointer);
418 actual_erase_leaf(leaf_pointer);
421 clear_imp(node_pointer);
424 erase_fixup(internal_node_pointer);
427 update_min_max_for_erased_leaf(leaf_pointer);
429 static inline const_e_iterator
430 pref_begin(const_node_pointer);
432 static inline const_e_iterator
433 pref_end(const_node_pointer);
436 find_imp(const_key_reference);
439 lower_bound_imp(const_key_reference);
442 upper_bound_imp(const_key_reference);
444 inline static const_leaf_pointer
445 leftmost_descendant(const_node_pointer);
447 inline static leaf_pointer
448 leftmost_descendant(node_pointer);
450 inline static const_leaf_pointer
451 rightmost_descendant(const_node_pointer);
453 inline static leaf_pointer
454 rightmost_descendant(node_pointer);
456 #ifdef _GLIBCXX_DEBUG
458 assert_valid() const;
461 assert_iterators() const;
464 assert_reverse_iterators() const;
467 recursive_count_leafs(const_node_pointer);
470 #ifdef PB_DS_PAT_TRIE_TRACE_
472 trace_node(const_node_pointer, size_type);
474 template<typename Metadata_>
476 trace_node_metadata(const_node_pointer, type_to_type<Metadata_>);
479 trace_node_metadata(const_node_pointer, type_to_type<null_node_metadata>);
483 split_prep(const_key_reference, PB_DS_CLASS_C_DEC&,
484 split_join_branch_bag&);
487 rec_split(node_pointer, const_e_iterator, const_e_iterator,
488 PB_DS_CLASS_C_DEC&, split_join_branch_bag&);
491 split_insert_branch(size_type, const_e_iterator,
492 typename internal_node::iterator,
493 size_type, split_join_branch_bag&);
495 static head_allocator s_head_allocator;
496 static internal_node_allocator s_internal_node_allocator;
497 static leaf_allocator s_leaf_allocator;
499 head_pointer m_p_head;
503 #include <ext/pb_ds/detail/pat_trie_/constructors_destructor_fn_imps.hpp>
504 #include <ext/pb_ds/detail/pat_trie_/iterators_fn_imps.hpp>
505 #include <ext/pb_ds/detail/pat_trie_/insert_join_fn_imps.hpp>
506 #include <ext/pb_ds/detail/pat_trie_/erase_fn_imps.hpp>
507 #include <ext/pb_ds/detail/pat_trie_/find_fn_imps.hpp>
508 #include <ext/pb_ds/detail/pat_trie_/info_fn_imps.hpp>
509 #include <ext/pb_ds/detail/pat_trie_/policy_access_fn_imps.hpp>
510 #include <ext/pb_ds/detail/pat_trie_/split_fn_imps.hpp>
511 #include <ext/pb_ds/detail/pat_trie_/debug_fn_imps.hpp>
512 #include <ext/pb_ds/detail/pat_trie_/trace_fn_imps.hpp>
513 #include <ext/pb_ds/detail/pat_trie_/update_fn_imps.hpp>
515 #undef PB_DS_CLASS_C_DEC
516 #undef PB_DS_CLASS_T_DEC
517 #undef PB_DS_CLASS_NAME
518 #undef PB_DS_TYPES_TRAITS_C_DEC
519 #undef PB_DS_MAP_DEBUG_BASE_C_DEC
523 #undef PB_DS_STATIC_ASSERT
525 } // namespace detail