Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / ext / pb_ds / detail / pat_trie_ / pat_trie_.hpp
blob00a00d34bcec3c0f67bd097705154b44fa888ba8
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 //
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
9 // version.
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
29 // Public License.
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
40 // warranty.
42 /**
43 * @file pat_trie_.hpp
44 * Contains an implementation class for a patricia tree.
47 /**
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
52 **/
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>
63 #include <iterator>
64 #include <utility>
65 #include <algorithm>
66 #include <functional>
67 #include <assert.h>
68 #include <list>
69 #ifdef _GLIBCXX_DEBUG
70 #include <ext/pb_ds/detail/debug_map_base.hpp>
71 #endif
72 #include <debug/debug.h>
74 namespace __gnu_pbds
76 namespace detail
78 #define PB_DS_CLASS_T_DEC \
79 template<typename Key, typename Mapped, typename Node_And_It_Traits, \
80 typename Allocator>
82 #ifdef PB_DS_DATA_TRUE_INDICATOR
83 #define PB_DS_CLASS_NAME pat_trie_data_
84 #endif
86 #ifdef PB_DS_DATA_FALSE_INDICATOR
87 #define PB_DS_CLASS_NAME pat_trie_no_data_
88 #endif
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>
96 #ifdef _GLIBCXX_DEBUG
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>
100 #endif
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)
106 #endif
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)
112 #endif
115 * class description = PATRICIA trie implementation.">
117 template<typename Key,
118 typename Mapped,
119 typename Node_And_It_Traits,
120 typename Allocator>
121 class PB_DS_CLASS_NAME :
122 #ifdef _GLIBCXX_DEBUG
123 public PB_DS_DEBUG_MAP_BASE_C_DEC,
124 #endif
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
129 private:
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;
159 #endif
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;
165 public:
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;
199 PB_DS_CLASS_NAME();
201 PB_DS_CLASS_NAME(const e_access_traits&);
203 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
205 void
206 swap(PB_DS_CLASS_C_DEC&);
208 ~PB_DS_CLASS_NAME();
210 inline bool
211 empty() const;
213 inline size_type
214 size() const;
216 inline size_type
217 max_size() const;
219 e_access_traits&
220 get_e_access_traits();
222 const e_access_traits&
223 get_e_access_traits() const;
225 node_update&
226 get_node_update();
228 const node_update&
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;
239 #else
240 insert(r_key);
241 return traits_base::s_null_mapped;
242 #endif
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;
263 void
264 clear();
266 inline bool
267 erase(const_key_reference);
269 inline const_iterator
270 erase(const_iterator);
272 #ifdef PB_DS_DATA_TRUE_INDICATOR
273 inline iterator
274 erase(iterator);
275 #endif
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);
283 #endif
285 template<typename Pred>
286 inline size_type
287 erase_if(Pred);
289 void
290 join(PB_DS_CLASS_C_DEC&);
292 void
293 split(const_key_reference, PB_DS_CLASS_C_DEC&);
295 inline iterator
296 begin();
298 inline const_iterator
299 begin() const;
301 inline iterator
302 end();
304 inline const_iterator
305 end() const;
307 inline reverse_iterator
308 rbegin();
310 inline const_reverse_iterator
311 rbegin() const;
313 inline reverse_iterator
314 rend();
316 inline const_reverse_iterator
317 rend() const;
319 inline const_node_iterator
320 node_begin() const;
322 inline node_iterator
323 node_begin();
325 inline const_node_iterator
326 node_end() const;
328 inline node_iterator
329 node_end();
331 #ifdef PB_DS_PAT_TRIE_TRACE_
332 void
333 trace() const;
334 #endif
336 protected:
338 template<typename It>
339 void
340 copy_from_range(It, It);
342 void
343 value_swap(PB_DS_CLASS_C_DEC&);
345 node_pointer
346 recursive_copy_node(const_node_pointer);
348 private:
350 void
351 initialize();
353 inline void
354 apply_update(node_pointer, null_node_update_pointer);
356 template<typename Node_Update_>
357 inline void
358 apply_update(node_pointer, Node_Update_*);
360 bool
361 join_prep(PB_DS_CLASS_C_DEC&, split_join_branch_bag&);
363 void
364 rec_join_prep(const_node_pointer, const_node_pointer,
365 split_join_branch_bag&);
367 void
368 rec_join_prep(const_leaf_pointer, const_leaf_pointer,
369 split_join_branch_bag&);
371 void
372 rec_join_prep(const_leaf_pointer, const_internal_node_pointer,
373 split_join_branch_bag&);
375 void
376 rec_join_prep(const_internal_node_pointer, const_leaf_pointer,
377 split_join_branch_bag&);
379 void
380 rec_join_prep(const_internal_node_pointer, const_internal_node_pointer,
381 split_join_branch_bag&);
383 node_pointer
384 rec_join(node_pointer, node_pointer, size_type, split_join_branch_bag&);
386 node_pointer
387 rec_join(leaf_pointer, leaf_pointer, split_join_branch_bag&);
389 node_pointer
390 rec_join(leaf_pointer, internal_node_pointer, size_type,
391 split_join_branch_bag&);
393 node_pointer
394 rec_join(internal_node_pointer, leaf_pointer, size_type,
395 split_join_branch_bag&);
397 node_pointer
398 rec_join(internal_node_pointer, internal_node_pointer,
399 split_join_branch_bag&);
401 size_type
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&);
407 void
408 update_min_max_for_inserted_leaf(leaf_pointer);
410 void
411 erase_leaf(leaf_pointer);
413 inline void
414 actual_erase_leaf(leaf_pointer);
416 void
417 clear_imp(node_pointer);
419 void
420 erase_fixup(internal_node_pointer);
422 void
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);
431 inline node_pointer
432 find_imp(const_key_reference);
434 inline node_pointer
435 lower_bound_imp(const_key_reference);
437 inline node_pointer
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
453 void
454 assert_valid() const;
456 void
457 assert_iterators() const;
459 void
460 assert_reverse_iterators() const;
462 static size_type
463 recursive_count_leafs(const_node_pointer);
464 #endif
466 #ifdef PB_DS_PAT_TRIE_TRACE_
467 static void
468 trace_node(const_node_pointer, size_type);
470 template<typename Metadata_>
471 static void
472 trace_node_metadata(const_node_pointer, type_to_type<Metadata_>);
474 static void
475 trace_node_metadata(const_node_pointer, type_to_type<null_node_metadata>);
476 #endif
478 leaf_pointer
479 split_prep(const_key_reference, PB_DS_CLASS_C_DEC&,
480 split_join_branch_bag&);
482 node_pointer
483 rec_split(node_pointer, const_e_iterator, const_e_iterator,
484 PB_DS_CLASS_C_DEC&, split_join_branch_bag&);
486 void
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;
496 size_type m_size;
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
516 #undef PB_DS_V2F
517 #undef PB_DS_EP2VP
518 #undef PB_DS_V2S
520 } // namespace detail
521 } // namespace __gnu_pbds