3 // Copyright (C) 2005, 2006, 2007, 2009 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 internal_node.hpp
38 * Contains an internal PB_DS_BASE_C_DEC for a patricia tree.
41 #ifndef PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
42 #define PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
44 #include <debug/debug.h>
50 #define PB_DS_CLASS_T_DEC \
51 template<typename Type_Traits, typename E_Access_Traits, \
52 typename Metadata, typename Allocator>
54 #define PB_DS_CLASS_C_DEC \
55 pat_trie_internal_node<Type_Traits, E_Access_Traits, Metadata, Allocator>
57 #define PB_DS_BASE_C_DEC \
58 pat_trie_node_base<Type_Traits, E_Access_Traits, Metadata, Allocator>
60 #define PB_DS_LEAF_C_DEC \
61 pat_trie_leaf<Type_Traits, E_Access_Traits, Metadata, Allocator>
63 template<typename Type_Traits
,
64 typename E_Access_Traits
,
67 struct pat_trie_internal_node
: public PB_DS_BASE_C_DEC
70 typedef PB_DS_BASE_C_DEC base_type
;
71 typedef Type_Traits type_traits
;
72 typedef typename
type_traits::value_type value_type
;
73 typedef typename
Allocator::size_type size_type
;
75 typedef E_Access_Traits e_access_traits
;
76 typedef typename
e_access_traits::const_iterator const_e_iterator
;
77 typedef typename
Allocator::template rebind
<e_access_traits
>::other access_rebind
;
78 typedef typename
access_rebind::const_pointer const_e_access_traits_pointer
;
80 typedef typename
Allocator::template rebind
<base_type
>::other base_rebind
;
81 typedef typename
base_rebind::pointer node_pointer
;
82 typedef typename
base_rebind::const_pointer const_node_pointer
;
84 typedef PB_DS_LEAF_C_DEC leaf
;
85 typedef typename
Allocator::template rebind
<leaf
>::other leaf_rebind
;
86 typedef typename
leaf_rebind::pointer leaf_pointer
;
87 typedef typename
leaf_rebind::const_pointer const_leaf_pointer
;
89 typedef typename
Allocator::template rebind
<pat_trie_internal_node
>::other internal_node_rebind
;
90 typedef typename
internal_node_rebind::pointer internal_node_pointer
;
91 typedef typename
internal_node_rebind::const_pointer const_internal_node_pointer
;
94 typedef typename
base_type::subtree_debug_info subtree_debug_info
;
96 virtual subtree_debug_info
97 assert_valid_imp(const_e_access_traits_pointer
) const;
101 get_pref_pos(const_e_iterator
, const_e_iterator
,
102 const_e_access_traits_pointer
) const;
105 typedef typename
Allocator::template rebind
<node_pointer
>::other node_pointer_rebind
;
106 typedef typename
node_pointer_rebind::pointer node_pointer_pointer
;
107 typedef typename
node_pointer_rebind::reference node_pointer_reference
;
111 arr_size
= E_Access_Traits::max_size
+ 1
113 PB_DS_STATIC_ASSERT(min_arr_size
, arr_size
>= 2);
115 #include <ext/pb_ds/detail/pat_trie_/const_child_iterator.hpp>
116 #include <ext/pb_ds/detail/pat_trie_/child_iterator.hpp>
118 pat_trie_internal_node(size_type
, const const_e_iterator
);
121 update_prefixes(const_e_access_traits_pointer
);
136 get_child_node(const_e_iterator
, const_e_iterator
,
137 const_e_access_traits_pointer
);
139 inline const_node_pointer
140 get_child_node(const_e_iterator
, const_e_iterator
,
141 const_e_access_traits_pointer
) const;
144 get_child_it(const_e_iterator
, const_e_iterator
,
145 const_e_access_traits_pointer
);
148 get_lower_bound_child_node(const_e_iterator
, const_e_iterator
,
149 size_type
, const_e_access_traits_pointer
);
152 add_child(node_pointer
, const_e_iterator
, const_e_iterator
,
153 const_e_access_traits_pointer
);
155 inline const_node_pointer
156 get_join_child(const_node_pointer
, const_e_access_traits_pointer
) const;
159 get_join_child(node_pointer
, const_e_access_traits_pointer
);
162 remove_child(node_pointer p_nd
);
165 remove_child(iterator it
);
168 replace_child(node_pointer
, const_e_iterator
, const_e_iterator
,
169 const_e_access_traits_pointer
);
171 inline const_e_iterator
174 inline const_e_iterator
181 should_be_mine(const_e_iterator
, const_e_iterator
, size_type
,
182 const_e_access_traits_pointer
) const;
185 leftmost_descendant();
188 leftmost_descendant() const;
191 rightmost_descendant();
194 rightmost_descendant() const;
196 #ifdef _GLIBCXX_DEBUG
202 pat_trie_internal_node(const pat_trie_internal_node
&);
205 get_begin_pos() const;
207 const size_type m_e_ind
;
208 const_e_iterator m_pref_b_it
;
209 const_e_iterator m_pref_e_it
;
210 node_pointer m_a_p_children
[arr_size
];
211 static leaf_rebind s_leaf_alloc
;
212 static internal_node_rebind s_internal_node_alloc
;
216 typename
PB_DS_CLASS_C_DEC::leaf_rebind
217 PB_DS_CLASS_C_DEC::s_leaf_alloc
;
220 typename
PB_DS_CLASS_C_DEC::internal_node_rebind
221 PB_DS_CLASS_C_DEC::s_internal_node_alloc
;
224 inline typename
PB_DS_CLASS_C_DEC::size_type
226 get_pref_pos(const_e_iterator b_it
, const_e_iterator e_it
,
227 const_e_access_traits_pointer p_traits
) const
229 if (static_cast<size_t>(std::distance(b_it
, e_it
)) <= m_e_ind
)
231 std::advance(b_it
, m_e_ind
);
232 return 1 + p_traits
->e_pos(*b_it
);
237 pat_trie_internal_node(size_type len
, const const_e_iterator it
) :
238 PB_DS_BASE_C_DEC(pat_trie_internal_node_type
),
239 m_e_ind(len
), m_pref_b_it(it
), m_pref_e_it(it
)
241 std::advance(m_pref_e_it
, m_e_ind
);
242 std::fill(m_a_p_children
, m_a_p_children
+ arr_size
,
243 static_cast<node_pointer
>(NULL
));
249 update_prefixes(const_e_access_traits_pointer p_traits
)
251 node_pointer p_first
= *begin();
252 if (p_first
->m_type
== pat_trie_leaf_node_type
)
254 const_leaf_pointer p
= static_cast<const_leaf_pointer
>(p_first
);
255 m_pref_b_it
= p_traits
->begin(e_access_traits::extract_key(p
->value()));
259 _GLIBCXX_DEBUG_ASSERT(p_first
->m_type
== pat_trie_internal_node_type
);
260 m_pref_b_it
= static_cast<internal_node_pointer
>(p_first
)->pref_b_it();
262 m_pref_e_it
= m_pref_b_it
;
263 std::advance(m_pref_e_it
, m_e_ind
);
267 typename
PB_DS_CLASS_C_DEC::const_iterator
271 typedef node_pointer_pointer pointer_type
;
272 pointer_type p
= const_cast<pointer_type
>(m_a_p_children
);
273 return const_iterator(p
+ get_begin_pos(), p
+ arr_size
);
277 typename
PB_DS_CLASS_C_DEC::iterator
281 return iterator(m_a_p_children
+ get_begin_pos(),
282 m_a_p_children
+ arr_size
);
286 typename
PB_DS_CLASS_C_DEC::const_iterator
290 typedef node_pointer_pointer pointer_type
;
291 pointer_type p
= const_cast<pointer_type
>(m_a_p_children
) + arr_size
;
292 return const_iterator(p
, p
);
296 typename
PB_DS_CLASS_C_DEC::iterator
299 { return iterator(m_a_p_children
+ arr_size
, m_a_p_children
+ arr_size
); }
302 inline typename
PB_DS_CLASS_C_DEC::node_pointer
304 get_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
305 const_e_access_traits_pointer p_traits
)
307 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
308 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
309 return m_a_p_children
[i
];
313 inline typename
PB_DS_CLASS_C_DEC::iterator
315 get_child_it(const_e_iterator b_it
, const_e_iterator e_it
,
316 const_e_access_traits_pointer p_traits
)
318 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
319 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
320 _GLIBCXX_DEBUG_ASSERT(m_a_p_children
[i
] != NULL
);
321 return iterator(m_a_p_children
+ i
, m_a_p_children
+ i
);
325 inline typename
PB_DS_CLASS_C_DEC::const_node_pointer
327 get_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
328 const_e_access_traits_pointer p_traits
) const
329 { return const_cast<node_pointer
>(get_child_node(b_it
, e_it
, p_traits
)); }
332 typename
PB_DS_CLASS_C_DEC::node_pointer
334 get_lower_bound_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
335 size_type checked_ind
,
336 const_e_access_traits_pointer p_traits
)
338 if (!should_be_mine(b_it
, e_it
, checked_ind
, p_traits
))
340 if (p_traits
->cmp_prefixes(b_it
, e_it
, m_pref_b_it
, m_pref_e_it
, true))
341 return leftmost_descendant();
342 return rightmost_descendant();
345 size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
346 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
348 if (m_a_p_children
[i
] != NULL
)
349 return m_a_p_children
[i
];
351 while (++i
< arr_size
)
352 if (m_a_p_children
[i
] != NULL
)
354 if (m_a_p_children
[i
]->m_type
== pat_trie_leaf_node_type
)
355 return m_a_p_children
[i
];
357 _GLIBCXX_DEBUG_ASSERT(m_a_p_children
[i
]->m_type
== pat_trie_internal_node_type
);
359 return static_cast<internal_node_pointer
>(m_a_p_children
[i
])->leftmost_descendant();
362 return rightmost_descendant();
366 inline typename
PB_DS_CLASS_C_DEC::node_pointer
368 add_child(node_pointer p_nd
, const_e_iterator b_it
, const_e_iterator e_it
,
369 const_e_access_traits_pointer p_traits
)
371 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
372 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
373 if (m_a_p_children
[i
] == NULL
)
375 m_a_p_children
[i
] = p_nd
;
376 p_nd
->m_p_parent
= this;
379 return m_a_p_children
[i
];
383 typename
PB_DS_CLASS_C_DEC::const_node_pointer
385 get_join_child(const_node_pointer p_nd
, const_e_access_traits_pointer p_traits
) const
387 node_pointer p
= const_cast<node_pointer
>(p_nd
);
388 return const_cast<internal_node_pointer
>(this)->get_join_child(p
, p_traits
);
392 typename
PB_DS_CLASS_C_DEC::node_pointer
394 get_join_child(node_pointer p_nd
, const_e_access_traits_pointer p_traits
)
397 const_e_iterator b_it
;
398 const_e_iterator e_it
;
399 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
401 typename
Type_Traits::const_key_reference r_key
=
402 e_access_traits::extract_key(static_cast<const_leaf_pointer
>(p_nd
)->value());
404 b_it
= p_traits
->begin(r_key
);
405 e_it
= p_traits
->end(r_key
);
409 b_it
= static_cast<internal_node_pointer
>(p_nd
)->pref_b_it();
410 e_it
= static_cast<internal_node_pointer
>(p_nd
)->pref_e_it();
412 i
= get_pref_pos(b_it
, e_it
, p_traits
);
413 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
414 return m_a_p_children
[i
];
420 remove_child(node_pointer p_nd
)
423 for (; i
< arr_size
; ++i
)
424 if (m_a_p_children
[i
] == p_nd
)
426 m_a_p_children
[i
] = NULL
;
429 _GLIBCXX_DEBUG_ASSERT(i
!= arr_size
);
433 typename
PB_DS_CLASS_C_DEC::iterator
435 remove_child(iterator it
)
439 * it
.m_p_p_cur
= NULL
;
446 replace_child(node_pointer p_nd
, const_e_iterator b_it
,
447 const_e_iterator e_it
,
448 const_e_access_traits_pointer p_traits
)
450 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
451 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
452 m_a_p_children
[i
] = p_nd
;
453 p_nd
->m_p_parent
= this;
457 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
460 { return m_pref_b_it
; }
463 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
466 { return m_pref_e_it
; }
469 inline typename
PB_DS_CLASS_C_DEC::size_type
477 should_be_mine(const_e_iterator b_it
, const_e_iterator e_it
,
478 size_type checked_ind
,
479 const_e_access_traits_pointer p_traits
) const
484 const size_type num_es
= std::distance(b_it
, e_it
);
485 if (num_es
< m_e_ind
)
488 const_e_iterator key_b_it
= b_it
;
489 std::advance(key_b_it
, checked_ind
);
490 const_e_iterator key_e_it
= b_it
;
491 std::advance(key_e_it
, m_e_ind
);
493 const_e_iterator value_b_it
= m_pref_b_it
;
494 std::advance(value_b_it
, checked_ind
);
495 const_e_iterator value_e_it
= m_pref_b_it
;
496 std::advance(value_e_it
, m_e_ind
);
498 return p_traits
->equal_prefixes(key_b_it
, key_e_it
, value_b_it
,
503 typename
PB_DS_CLASS_C_DEC::leaf_pointer
505 leftmost_descendant()
507 node_pointer p_pot
=* begin();
508 if (p_pot
->m_type
== pat_trie_leaf_node_type
)
509 return (static_cast<leaf_pointer
>(p_pot
));
510 _GLIBCXX_DEBUG_ASSERT(p_pot
->m_type
== pat_trie_internal_node_type
);
511 return static_cast<internal_node_pointer
>(p_pot
)->leftmost_descendant();
515 typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
517 leftmost_descendant() const
519 return const_cast<internal_node_pointer
>(this)->leftmost_descendant();
523 typename
PB_DS_CLASS_C_DEC::leaf_pointer
525 rightmost_descendant()
527 const size_type num_children
= std::distance(begin(), end());
528 _GLIBCXX_DEBUG_ASSERT(num_children
>= 2);
530 iterator it
= begin();
531 std::advance(it
, num_children
- 1);
532 node_pointer p_pot
=* it
;
533 if (p_pot
->m_type
== pat_trie_leaf_node_type
)
534 return static_cast<leaf_pointer
>(p_pot
);
535 _GLIBCXX_DEBUG_ASSERT(p_pot
->m_type
== pat_trie_internal_node_type
);
536 return static_cast<internal_node_pointer
>(p_pot
)->rightmost_descendant();
540 typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
542 rightmost_descendant() const
544 return const_cast<internal_node_pointer
>(this)->rightmost_descendant();
547 #ifdef _GLIBCXX_DEBUG
549 typename
PB_DS_CLASS_C_DEC::size_type
556 typename
PB_DS_CLASS_C_DEC::size_type
558 get_begin_pos() const
561 for (i
= 0; i
< arr_size
&& m_a_p_children
[i
] == NULL
; ++i
)
566 #ifdef _GLIBCXX_DEBUG
568 typename
PB_DS_CLASS_C_DEC::subtree_debug_info
570 assert_valid_imp(const_e_access_traits_pointer p_traits
) const
572 _GLIBCXX_DEBUG_ASSERT(base_type::m_type
== pat_trie_internal_node_type
);
573 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type
>(std::distance(pref_b_it(), pref_e_it())) == m_e_ind
);
574 _GLIBCXX_DEBUG_ASSERT(std::distance(begin(), end()) >= 2);
576 for (typename
pat_trie_internal_node::const_iterator it
= begin();
579 const_node_pointer p_nd
=* it
;
580 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_p_parent
== this);
581 subtree_debug_info child_ret
= p_nd
->assert_valid_imp(p_traits
);
583 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type
>(std::distance(child_ret
.first
, child_ret
.second
)) >= m_e_ind
);
584 _GLIBCXX_DEBUG_ASSERT(should_be_mine(child_ret
.first
, child_ret
.second
, 0, p_traits
));
585 _GLIBCXX_DEBUG_ASSERT(get_pref_pos(child_ret
.first
, child_ret
.second
, p_traits
) == static_cast<size_type
>(it
.m_p_p_cur
- m_a_p_children
));
587 return std::make_pair(pref_b_it(), pref_e_it());
591 #undef PB_DS_CLASS_T_DEC
592 #undef PB_DS_CLASS_C_DEC
593 #undef PB_DS_BASE_C_DEC
594 #undef PB_DS_LEAF_C_DEC
596 } // namespace detail
597 } // namespace __gnu_pbds