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
43 * @file internal_node.hpp
44 * Contains an internal PB_DS_BASE_C_DEC for a patricia tree.
47 #ifndef PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
48 #define PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
50 #include <debug/debug.h>
56 #define PB_DS_CLASS_T_DEC \
57 template<typename Type_Traits, typename E_Access_Traits, \
58 typename Metadata, typename Allocator>
60 #define PB_DS_CLASS_C_DEC \
61 pat_trie_internal_node<Type_Traits, E_Access_Traits, Metadata, Allocator>
63 #define PB_DS_BASE_C_DEC \
64 pat_trie_node_base<Type_Traits, E_Access_Traits, Metadata, Allocator>
66 #define PB_DS_LEAF_C_DEC \
67 pat_trie_leaf<Type_Traits, E_Access_Traits, Metadata, Allocator>
69 template<typename Type_Traits
,
70 typename E_Access_Traits
,
73 struct pat_trie_internal_node
: public PB_DS_BASE_C_DEC
76 typedef PB_DS_BASE_C_DEC base_type
;
77 typedef Type_Traits type_traits
;
78 typedef typename
type_traits::value_type value_type
;
79 typedef typename
Allocator::size_type size_type
;
81 typedef E_Access_Traits e_access_traits
;
82 typedef typename
e_access_traits::const_iterator const_e_iterator
;
83 typedef typename
Allocator::template rebind
<e_access_traits
>::other access_rebind
;
84 typedef typename
access_rebind::const_pointer const_e_access_traits_pointer
;
86 typedef typename
Allocator::template rebind
<base_type
>::other base_rebind
;
87 typedef typename
base_rebind::pointer node_pointer
;
88 typedef typename
base_rebind::const_pointer const_node_pointer
;
90 typedef PB_DS_LEAF_C_DEC leaf
;
91 typedef typename
Allocator::template rebind
<leaf
>::other leaf_rebind
;
92 typedef typename
leaf_rebind::pointer leaf_pointer
;
93 typedef typename
leaf_rebind::const_pointer const_leaf_pointer
;
95 typedef typename
Allocator::template rebind
<pat_trie_internal_node
>::other internal_node_rebind
;
96 typedef typename
internal_node_rebind::pointer internal_node_pointer
;
97 typedef typename
internal_node_rebind::const_pointer const_internal_node_pointer
;
100 typedef typename
base_type::subtree_debug_info subtree_debug_info
;
102 virtual subtree_debug_info
103 assert_valid_imp(const_e_access_traits_pointer
) const;
107 get_pref_pos(const_e_iterator
, const_e_iterator
,
108 const_e_access_traits_pointer
) const;
111 typedef typename
Allocator::template rebind
<node_pointer
>::other node_pointer_rebind
;
112 typedef typename
node_pointer_rebind::pointer node_pointer_pointer
;
113 typedef typename
node_pointer_rebind::reference node_pointer_reference
;
117 arr_size
= E_Access_Traits::max_size
+ 1
119 PB_DS_STATIC_ASSERT(min_arr_size
, arr_size
>= 2);
121 #include <ext/pb_ds/detail/pat_trie_/const_child_iterator.hpp>
122 #include <ext/pb_ds/detail/pat_trie_/child_iterator.hpp>
124 pat_trie_internal_node(size_type
, const const_e_iterator
);
127 update_prefixes(const_e_access_traits_pointer
);
142 get_child_node(const_e_iterator
, const_e_iterator
,
143 const_e_access_traits_pointer
);
145 inline const_node_pointer
146 get_child_node(const_e_iterator
, const_e_iterator
,
147 const_e_access_traits_pointer
) const;
150 get_child_it(const_e_iterator
, const_e_iterator
,
151 const_e_access_traits_pointer
);
154 get_lower_bound_child_node(const_e_iterator
, const_e_iterator
,
155 size_type
, const_e_access_traits_pointer
);
158 add_child(node_pointer
, const_e_iterator
, const_e_iterator
,
159 const_e_access_traits_pointer
);
161 inline const_node_pointer
162 get_join_child(const_node_pointer
, const_e_access_traits_pointer
) const;
165 get_join_child(node_pointer
, const_e_access_traits_pointer
);
168 remove_child(node_pointer p_nd
);
171 remove_child(iterator it
);
174 replace_child(node_pointer
, const_e_iterator
, const_e_iterator
,
175 const_e_access_traits_pointer
);
177 inline const_e_iterator
180 inline const_e_iterator
187 should_be_mine(const_e_iterator
, const_e_iterator
, size_type
,
188 const_e_access_traits_pointer
) const;
191 leftmost_descendant();
194 leftmost_descendant() const;
197 rightmost_descendant();
200 rightmost_descendant() const;
202 #ifdef _GLIBCXX_DEBUG
208 pat_trie_internal_node(const pat_trie_internal_node
&);
211 get_begin_pos() const;
213 const size_type m_e_ind
;
214 const_e_iterator m_pref_b_it
;
215 const_e_iterator m_pref_e_it
;
216 node_pointer m_a_p_children
[arr_size
];
217 static leaf_rebind s_leaf_alloc
;
218 static internal_node_rebind s_internal_node_alloc
;
222 typename
PB_DS_CLASS_C_DEC::leaf_rebind
223 PB_DS_CLASS_C_DEC::s_leaf_alloc
;
226 typename
PB_DS_CLASS_C_DEC::internal_node_rebind
227 PB_DS_CLASS_C_DEC::s_internal_node_alloc
;
230 inline typename
PB_DS_CLASS_C_DEC::size_type
232 get_pref_pos(const_e_iterator b_it
, const_e_iterator e_it
,
233 const_e_access_traits_pointer p_traits
) const
235 if (static_cast<size_t>(std::distance(b_it
, e_it
)) <= m_e_ind
)
237 std::advance(b_it
, m_e_ind
);
238 return 1 + p_traits
->e_pos(*b_it
);
243 pat_trie_internal_node(size_type len
, const const_e_iterator it
) :
244 PB_DS_BASE_C_DEC(pat_trie_internal_node_type
),
245 m_e_ind(len
), m_pref_b_it(it
), m_pref_e_it(it
)
247 std::advance(m_pref_e_it
, m_e_ind
);
248 std::fill(m_a_p_children
, m_a_p_children
+ arr_size
,
249 static_cast<node_pointer
>(NULL
));
255 update_prefixes(const_e_access_traits_pointer p_traits
)
257 node_pointer p_first
= *begin();
258 if (p_first
->m_type
== pat_trie_leaf_node_type
)
260 const_leaf_pointer p
= static_cast<const_leaf_pointer
>(p_first
);
261 m_pref_b_it
= p_traits
->begin(e_access_traits::extract_key(p
->value()));
265 _GLIBCXX_DEBUG_ASSERT(p_first
->m_type
== pat_trie_internal_node_type
);
266 m_pref_b_it
= static_cast<internal_node_pointer
>(p_first
)->pref_b_it();
268 m_pref_e_it
= m_pref_b_it
;
269 std::advance(m_pref_e_it
, m_e_ind
);
273 typename
PB_DS_CLASS_C_DEC::const_iterator
277 typedef node_pointer_pointer pointer_type
;
278 pointer_type p
= const_cast<pointer_type
>(m_a_p_children
);
279 return const_iterator(p
+ get_begin_pos(), p
+ arr_size
);
283 typename
PB_DS_CLASS_C_DEC::iterator
287 return iterator(m_a_p_children
+ get_begin_pos(),
288 m_a_p_children
+ arr_size
);
292 typename
PB_DS_CLASS_C_DEC::const_iterator
296 typedef node_pointer_pointer pointer_type
;
297 pointer_type p
= const_cast<pointer_type
>(m_a_p_children
) + arr_size
;
298 return const_iterator(p
, p
);
302 typename
PB_DS_CLASS_C_DEC::iterator
305 { return iterator(m_a_p_children
+ arr_size
, m_a_p_children
+ arr_size
); }
308 inline typename
PB_DS_CLASS_C_DEC::node_pointer
310 get_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
311 const_e_access_traits_pointer p_traits
)
313 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
314 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
315 return m_a_p_children
[i
];
319 inline typename
PB_DS_CLASS_C_DEC::iterator
321 get_child_it(const_e_iterator b_it
, const_e_iterator e_it
,
322 const_e_access_traits_pointer p_traits
)
324 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
325 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
326 _GLIBCXX_DEBUG_ASSERT(m_a_p_children
[i
] != NULL
);
327 return iterator(m_a_p_children
+ i
, m_a_p_children
+ i
);
331 inline typename
PB_DS_CLASS_C_DEC::const_node_pointer
333 get_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
334 const_e_access_traits_pointer p_traits
) const
335 { return const_cast<node_pointer
>(get_child_node(b_it
, e_it
, p_traits
)); }
338 typename
PB_DS_CLASS_C_DEC::node_pointer
340 get_lower_bound_child_node(const_e_iterator b_it
, const_e_iterator e_it
,
341 size_type checked_ind
,
342 const_e_access_traits_pointer p_traits
)
344 if (!should_be_mine(b_it
, e_it
, checked_ind
, p_traits
))
346 if (p_traits
->cmp_prefixes(b_it
, e_it
, m_pref_b_it
, m_pref_e_it
, true))
347 return leftmost_descendant();
348 return rightmost_descendant();
351 size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
352 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
354 if (m_a_p_children
[i
] != NULL
)
355 return m_a_p_children
[i
];
357 while (++i
< arr_size
)
358 if (m_a_p_children
[i
] != NULL
)
360 if (m_a_p_children
[i
]->m_type
== pat_trie_leaf_node_type
)
361 return m_a_p_children
[i
];
363 _GLIBCXX_DEBUG_ASSERT(m_a_p_children
[i
]->m_type
== pat_trie_internal_node_type
);
365 return static_cast<internal_node_pointer
>(m_a_p_children
[i
])->leftmost_descendant();
368 return rightmost_descendant();
372 inline typename
PB_DS_CLASS_C_DEC::node_pointer
374 add_child(node_pointer p_nd
, const_e_iterator b_it
, const_e_iterator e_it
,
375 const_e_access_traits_pointer p_traits
)
377 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
378 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
379 if (m_a_p_children
[i
] == NULL
)
381 m_a_p_children
[i
] = p_nd
;
382 p_nd
->m_p_parent
= this;
385 return m_a_p_children
[i
];
389 typename
PB_DS_CLASS_C_DEC::const_node_pointer
391 get_join_child(const_node_pointer p_nd
, const_e_access_traits_pointer p_traits
) const
393 node_pointer p
= const_cast<node_pointer
>(p_nd
);
394 return const_cast<internal_node_pointer
>(this)->get_join_child(p
, p_traits
);
398 typename
PB_DS_CLASS_C_DEC::node_pointer
400 get_join_child(node_pointer p_nd
, const_e_access_traits_pointer p_traits
)
403 const_e_iterator b_it
;
404 const_e_iterator e_it
;
405 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
407 typename
Type_Traits::const_key_reference r_key
=
408 e_access_traits::extract_key(static_cast<const_leaf_pointer
>(p_nd
)->value());
410 b_it
= p_traits
->begin(r_key
);
411 e_it
= p_traits
->end(r_key
);
415 b_it
= static_cast<internal_node_pointer
>(p_nd
)->pref_b_it();
416 e_it
= static_cast<internal_node_pointer
>(p_nd
)->pref_e_it();
418 i
= get_pref_pos(b_it
, e_it
, p_traits
);
419 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
420 return m_a_p_children
[i
];
426 remove_child(node_pointer p_nd
)
429 for (; i
< arr_size
; ++i
)
430 if (m_a_p_children
[i
] == p_nd
)
432 m_a_p_children
[i
] = NULL
;
435 _GLIBCXX_DEBUG_ASSERT(i
!= arr_size
);
439 typename
PB_DS_CLASS_C_DEC::iterator
441 remove_child(iterator it
)
445 * it
.m_p_p_cur
= NULL
;
452 replace_child(node_pointer p_nd
, const_e_iterator b_it
,
453 const_e_iterator e_it
,
454 const_e_access_traits_pointer p_traits
)
456 const size_type i
= get_pref_pos(b_it
, e_it
, p_traits
);
457 _GLIBCXX_DEBUG_ASSERT(i
< arr_size
);
458 m_a_p_children
[i
] = p_nd
;
459 p_nd
->m_p_parent
= this;
463 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
466 { return m_pref_b_it
; }
469 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
472 { return m_pref_e_it
; }
475 inline typename
PB_DS_CLASS_C_DEC::size_type
483 should_be_mine(const_e_iterator b_it
, const_e_iterator e_it
,
484 size_type checked_ind
,
485 const_e_access_traits_pointer p_traits
) const
490 const size_type num_es
= std::distance(b_it
, e_it
);
491 if (num_es
< m_e_ind
)
494 const_e_iterator key_b_it
= b_it
;
495 std::advance(key_b_it
, checked_ind
);
496 const_e_iterator key_e_it
= b_it
;
497 std::advance(key_e_it
, m_e_ind
);
499 const_e_iterator value_b_it
= m_pref_b_it
;
500 std::advance(value_b_it
, checked_ind
);
501 const_e_iterator value_e_it
= m_pref_b_it
;
502 std::advance(value_e_it
, m_e_ind
);
504 return p_traits
->equal_prefixes(key_b_it
, key_e_it
, value_b_it
,
509 typename
PB_DS_CLASS_C_DEC::leaf_pointer
511 leftmost_descendant()
513 node_pointer p_pot
=* begin();
514 if (p_pot
->m_type
== pat_trie_leaf_node_type
)
515 return (static_cast<leaf_pointer
>(p_pot
));
516 _GLIBCXX_DEBUG_ASSERT(p_pot
->m_type
== pat_trie_internal_node_type
);
517 return static_cast<internal_node_pointer
>(p_pot
)->leftmost_descendant();
521 typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
523 leftmost_descendant() const
525 return const_cast<internal_node_pointer
>(this)->leftmost_descendant();
529 typename
PB_DS_CLASS_C_DEC::leaf_pointer
531 rightmost_descendant()
533 const size_type num_children
= std::distance(begin(), end());
534 _GLIBCXX_DEBUG_ASSERT(num_children
>= 2);
536 iterator it
= begin();
537 std::advance(it
, num_children
- 1);
538 node_pointer p_pot
=* it
;
539 if (p_pot
->m_type
== pat_trie_leaf_node_type
)
540 return static_cast<leaf_pointer
>(p_pot
);
541 _GLIBCXX_DEBUG_ASSERT(p_pot
->m_type
== pat_trie_internal_node_type
);
542 return static_cast<internal_node_pointer
>(p_pot
)->rightmost_descendant();
546 typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
548 rightmost_descendant() const
550 return const_cast<internal_node_pointer
>(this)->rightmost_descendant();
553 #ifdef _GLIBCXX_DEBUG
555 typename
PB_DS_CLASS_C_DEC::size_type
562 typename
PB_DS_CLASS_C_DEC::size_type
564 get_begin_pos() const
567 for (i
= 0; i
< arr_size
&& m_a_p_children
[i
] == NULL
; ++i
)
572 #ifdef _GLIBCXX_DEBUG
574 typename
PB_DS_CLASS_C_DEC::subtree_debug_info
576 assert_valid_imp(const_e_access_traits_pointer p_traits
) const
578 _GLIBCXX_DEBUG_ASSERT(base_type::m_type
== pat_trie_internal_node_type
);
579 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type
>(std::distance(pref_b_it(), pref_e_it())) == m_e_ind
);
580 _GLIBCXX_DEBUG_ASSERT(std::distance(begin(), end()) >= 2);
582 for (typename
pat_trie_internal_node::const_iterator it
= begin();
585 const_node_pointer p_nd
=* it
;
586 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_p_parent
== this);
587 subtree_debug_info child_ret
= p_nd
->assert_valid_imp(p_traits
);
589 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type
>(std::distance(child_ret
.first
, child_ret
.second
)) >= m_e_ind
);
590 _GLIBCXX_DEBUG_ASSERT(should_be_mine(child_ret
.first
, child_ret
.second
, 0, p_traits
));
591 _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
));
593 return std::make_pair(pref_b_it(), pref_e_it());
597 #undef PB_DS_CLASS_T_DEC
598 #undef PB_DS_CLASS_C_DEC
599 #undef PB_DS_BASE_C_DEC
600 #undef PB_DS_LEAF_C_DEC
602 } // namespace detail
603 } // namespace __gnu_pbds