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
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 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
70 typedef static_assert_dumclass<sizeof(static_assert<(bool)(E)>)> UNIQUE##static_assert_type
72 template<typename Type_Traits,
73 typename E_Access_Traits,
76 struct pat_trie_internal_node : public PB_DS_BASE_C_DEC
79 typedef PB_DS_BASE_C_DEC base_type;
80 typedef Type_Traits type_traits;
81 typedef typename type_traits::value_type value_type;
82 typedef typename Allocator::size_type size_type;
84 typedef E_Access_Traits e_access_traits;
85 typedef typename e_access_traits::const_iterator const_e_iterator;
86 typedef typename Allocator::template rebind<e_access_traits>::other access_rebind;
87 typedef typename access_rebind::const_pointer const_e_access_traits_pointer;
89 typedef typename Allocator::template rebind<base_type>::other base_rebind;
90 typedef typename base_rebind::pointer node_pointer;
91 typedef typename base_rebind::const_pointer const_node_pointer;
93 typedef PB_DS_LEAF_C_DEC leaf;
94 typedef typename Allocator::template rebind<leaf>::other leaf_rebind;
95 typedef typename leaf_rebind::pointer leaf_pointer;
96 typedef typename leaf_rebind::const_pointer const_leaf_pointer;
98 typedef typename Allocator::template rebind<pat_trie_internal_node>::other internal_node_rebind;
99 typedef typename internal_node_rebind::pointer internal_node_pointer;
100 typedef typename internal_node_rebind::const_pointer const_internal_node_pointer;
102 #ifdef _GLIBCXX_DEBUG
103 typedef typename base_type::subtree_debug_info subtree_debug_info;
105 virtual subtree_debug_info
106 assert_valid_imp(const_e_access_traits_pointer) const;
110 get_pref_pos(const_e_iterator, const_e_iterator,
111 const_e_access_traits_pointer) const;
114 typedef typename Allocator::template rebind<node_pointer>::other node_pointer_rebind;
115 typedef typename node_pointer_rebind::pointer node_pointer_pointer;
116 typedef typename node_pointer_rebind::reference node_pointer_reference;
120 arr_size = E_Access_Traits::max_size + 1
122 PB_DS_STATIC_ASSERT(min_arr_size, arr_size >= 2);
124 #include <ext/pb_ds/detail/pat_trie_/const_child_iterator.hpp>
125 #include <ext/pb_ds/detail/pat_trie_/child_iterator.hpp>
127 pat_trie_internal_node(size_type, const const_e_iterator);
130 update_prefixes(const_e_access_traits_pointer);
145 get_child_node(const_e_iterator, const_e_iterator,
146 const_e_access_traits_pointer);
148 inline const_node_pointer
149 get_child_node(const_e_iterator, const_e_iterator,
150 const_e_access_traits_pointer) const;
153 get_child_it(const_e_iterator, const_e_iterator,
154 const_e_access_traits_pointer);
157 get_lower_bound_child_node(const_e_iterator, const_e_iterator,
158 size_type, const_e_access_traits_pointer);
161 add_child(node_pointer, const_e_iterator, const_e_iterator,
162 const_e_access_traits_pointer);
164 inline const_node_pointer
165 get_join_child(const_node_pointer, const_e_access_traits_pointer) const;
168 get_join_child(node_pointer, const_e_access_traits_pointer);
171 remove_child(node_pointer p_nd);
174 remove_child(iterator it);
177 replace_child(node_pointer, const_e_iterator, const_e_iterator,
178 const_e_access_traits_pointer);
180 inline const_e_iterator
183 inline const_e_iterator
190 should_be_mine(const_e_iterator, const_e_iterator, size_type,
191 const_e_access_traits_pointer) const;
194 leftmost_descendant();
197 leftmost_descendant() const;
200 rightmost_descendant();
203 rightmost_descendant() const;
205 #ifdef _GLIBCXX_DEBUG
211 pat_trie_internal_node(const pat_trie_internal_node&);
214 get_begin_pos() const;
216 const size_type m_e_ind;
217 const_e_iterator m_pref_b_it;
218 const_e_iterator m_pref_e_it;
219 node_pointer m_a_p_children[arr_size];
220 static leaf_rebind s_leaf_alloc;
221 static internal_node_rebind s_internal_node_alloc;
225 typename PB_DS_CLASS_C_DEC::leaf_rebind
226 PB_DS_CLASS_C_DEC::s_leaf_alloc;
229 typename PB_DS_CLASS_C_DEC::internal_node_rebind
230 PB_DS_CLASS_C_DEC::s_internal_node_alloc;
233 inline typename PB_DS_CLASS_C_DEC::size_type
235 get_pref_pos(const_e_iterator b_it, const_e_iterator e_it,
236 const_e_access_traits_pointer p_traits) const
238 if (static_cast<size_t>(std::distance(b_it, e_it)) <= m_e_ind)
240 std::advance(b_it, m_e_ind);
241 return 1 + p_traits->e_pos(*b_it);
246 pat_trie_internal_node(size_type len, const const_e_iterator it) :
247 PB_DS_BASE_C_DEC(pat_trie_internal_node_type),
248 m_e_ind(len), m_pref_b_it(it), m_pref_e_it(it)
250 std::advance(m_pref_e_it, m_e_ind);
251 std::fill(m_a_p_children, m_a_p_children + arr_size,
252 static_cast<node_pointer>(NULL));
258 update_prefixes(const_e_access_traits_pointer p_traits)
260 node_pointer p_first = *begin();
261 if (p_first->m_type == pat_trie_leaf_node_type)
263 const_leaf_pointer p = static_cast<const_leaf_pointer>(p_first);
264 m_pref_b_it = p_traits->begin(e_access_traits::extract_key(p->value()));
268 _GLIBCXX_DEBUG_ASSERT(p_first->m_type == pat_trie_internal_node_type);
269 m_pref_b_it = static_cast<internal_node_pointer>(p_first)->pref_b_it();
271 m_pref_e_it = m_pref_b_it;
272 std::advance(m_pref_e_it, m_e_ind);
276 typename PB_DS_CLASS_C_DEC::const_iterator
280 typedef node_pointer_pointer pointer_type;
281 pointer_type p = const_cast<pointer_type>(m_a_p_children);
282 return const_iterator(p + get_begin_pos(), p + arr_size);
286 typename PB_DS_CLASS_C_DEC::iterator
290 return iterator(m_a_p_children + get_begin_pos(),
291 m_a_p_children + arr_size);
295 typename PB_DS_CLASS_C_DEC::const_iterator
299 typedef node_pointer_pointer pointer_type;
300 pointer_type p = const_cast<pointer_type>(m_a_p_children) + arr_size;
301 return const_iterator(p, p);
305 typename PB_DS_CLASS_C_DEC::iterator
308 { return iterator(m_a_p_children + arr_size, m_a_p_children + arr_size); }
311 inline typename PB_DS_CLASS_C_DEC::node_pointer
313 get_child_node(const_e_iterator b_it, const_e_iterator e_it,
314 const_e_access_traits_pointer p_traits)
316 const size_type i = get_pref_pos(b_it, e_it, p_traits);
317 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
318 return m_a_p_children[i];
322 inline typename PB_DS_CLASS_C_DEC::iterator
324 get_child_it(const_e_iterator b_it, const_e_iterator e_it,
325 const_e_access_traits_pointer p_traits)
327 const size_type i = get_pref_pos(b_it, e_it, p_traits);
328 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
329 _GLIBCXX_DEBUG_ASSERT(m_a_p_children[i] != NULL);
330 return iterator(m_a_p_children + i, m_a_p_children + i);
334 inline typename PB_DS_CLASS_C_DEC::const_node_pointer
336 get_child_node(const_e_iterator b_it, const_e_iterator e_it,
337 const_e_access_traits_pointer p_traits) const
338 { return const_cast<node_pointer>(get_child_node(b_it, e_it, p_traits)); }
341 typename PB_DS_CLASS_C_DEC::node_pointer
343 get_lower_bound_child_node(const_e_iterator b_it, const_e_iterator e_it,
344 size_type checked_ind,
345 const_e_access_traits_pointer p_traits)
347 if (!should_be_mine(b_it, e_it, checked_ind, p_traits))
349 if (p_traits->cmp_prefixes(b_it, e_it, m_pref_b_it, m_pref_e_it, true))
350 return leftmost_descendant();
351 return rightmost_descendant();
354 size_type i = get_pref_pos(b_it, e_it, p_traits);
355 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
357 if (m_a_p_children[i] != NULL)
358 return m_a_p_children[i];
360 while (++i < arr_size)
361 if (m_a_p_children[i] != NULL)
363 if (m_a_p_children[i]->m_type == pat_trie_leaf_node_type)
364 return m_a_p_children[i];
366 _GLIBCXX_DEBUG_ASSERT(m_a_p_children[i]->m_type == pat_trie_internal_node_type);
368 return static_cast<internal_node_pointer>(m_a_p_children[i])->leftmost_descendant();
371 return rightmost_descendant();
375 inline typename PB_DS_CLASS_C_DEC::node_pointer
377 add_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it,
378 const_e_access_traits_pointer p_traits)
380 const size_type i = get_pref_pos(b_it, e_it, p_traits);
381 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
382 if (m_a_p_children[i] == NULL)
384 m_a_p_children[i] = p_nd;
385 p_nd->m_p_parent = this;
388 return m_a_p_children[i];
392 typename PB_DS_CLASS_C_DEC::const_node_pointer
394 get_join_child(const_node_pointer p_nd, const_e_access_traits_pointer p_traits) const
396 node_pointer p = const_cast<node_pointer>(p_nd);
397 return const_cast<internal_node_pointer>(this)->get_join_child(p, p_traits);
401 typename PB_DS_CLASS_C_DEC::node_pointer
403 get_join_child(node_pointer p_nd, const_e_access_traits_pointer p_traits)
406 const_e_iterator b_it;
407 const_e_iterator e_it;
408 if (p_nd->m_type == pat_trie_leaf_node_type)
410 typename Type_Traits::const_key_reference r_key =
411 e_access_traits::extract_key(static_cast<const_leaf_pointer>(p_nd)->value());
413 b_it = p_traits->begin(r_key);
414 e_it = p_traits->end(r_key);
418 b_it = static_cast<internal_node_pointer>(p_nd)->pref_b_it();
419 e_it = static_cast<internal_node_pointer>(p_nd)->pref_e_it();
421 i = get_pref_pos(b_it, e_it, p_traits);
422 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
423 return m_a_p_children[i];
429 remove_child(node_pointer p_nd)
432 for (; i < arr_size; ++i)
433 if (m_a_p_children[i] == p_nd)
435 m_a_p_children[i] = NULL;
438 _GLIBCXX_DEBUG_ASSERT(i != arr_size);
442 typename PB_DS_CLASS_C_DEC::iterator
444 remove_child(iterator it)
448 * it.m_p_p_cur = NULL;
455 replace_child(node_pointer p_nd, const_e_iterator b_it,
456 const_e_iterator e_it,
457 const_e_access_traits_pointer p_traits)
459 const size_type i = get_pref_pos(b_it, e_it, p_traits);
460 _GLIBCXX_DEBUG_ASSERT(i < arr_size);
461 m_a_p_children[i] = p_nd;
462 p_nd->m_p_parent = this;
466 inline typename PB_DS_CLASS_C_DEC::const_e_iterator
469 { return m_pref_b_it; }
472 inline typename PB_DS_CLASS_C_DEC::const_e_iterator
475 { return m_pref_e_it; }
478 inline typename PB_DS_CLASS_C_DEC::size_type
486 should_be_mine(const_e_iterator b_it, const_e_iterator e_it,
487 size_type checked_ind,
488 const_e_access_traits_pointer p_traits) const
493 const size_type num_es = std::distance(b_it, e_it);
494 if (num_es < m_e_ind)
497 const_e_iterator key_b_it = b_it;
498 std::advance(key_b_it, checked_ind);
499 const_e_iterator key_e_it = b_it;
500 std::advance(key_e_it, m_e_ind);
502 const_e_iterator value_b_it = m_pref_b_it;
503 std::advance(value_b_it, checked_ind);
504 const_e_iterator value_e_it = m_pref_b_it;
505 std::advance(value_e_it, m_e_ind);
507 return p_traits->equal_prefixes(key_b_it, key_e_it, value_b_it,
512 typename PB_DS_CLASS_C_DEC::leaf_pointer
514 leftmost_descendant()
516 node_pointer p_pot =* begin();
517 if (p_pot->m_type == pat_trie_leaf_node_type)
518 return (static_cast<leaf_pointer>(p_pot));
519 _GLIBCXX_DEBUG_ASSERT(p_pot->m_type == pat_trie_internal_node_type);
520 return static_cast<internal_node_pointer>(p_pot)->leftmost_descendant();
524 typename PB_DS_CLASS_C_DEC::const_leaf_pointer
526 leftmost_descendant() const
528 return const_cast<internal_node_pointer>(this)->leftmost_descendant();
532 typename PB_DS_CLASS_C_DEC::leaf_pointer
534 rightmost_descendant()
536 const size_type num_children = std::distance(begin(), end());
537 _GLIBCXX_DEBUG_ASSERT(num_children >= 2);
539 iterator it = begin();
540 std::advance(it, num_children - 1);
541 node_pointer p_pot =* it;
542 if (p_pot->m_type == pat_trie_leaf_node_type)
543 return static_cast<leaf_pointer>(p_pot);
544 _GLIBCXX_DEBUG_ASSERT(p_pot->m_type == pat_trie_internal_node_type);
545 return static_cast<internal_node_pointer>(p_pot)->rightmost_descendant();
549 typename PB_DS_CLASS_C_DEC::const_leaf_pointer
551 rightmost_descendant() const
553 return const_cast<internal_node_pointer>(this)->rightmost_descendant();
556 #ifdef _GLIBCXX_DEBUG
558 typename PB_DS_CLASS_C_DEC::size_type
565 typename PB_DS_CLASS_C_DEC::size_type
567 get_begin_pos() const
570 for (i = 0; i < arr_size && m_a_p_children[i] == NULL; ++i)
575 #ifdef _GLIBCXX_DEBUG
577 typename PB_DS_CLASS_C_DEC::subtree_debug_info
579 assert_valid_imp(const_e_access_traits_pointer p_traits) const
581 _GLIBCXX_DEBUG_ASSERT(base_type::m_type == pat_trie_internal_node_type);
582 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type>(std::distance(pref_b_it(), pref_e_it())) == m_e_ind);
583 _GLIBCXX_DEBUG_ASSERT(std::distance(begin(), end()) >= 2);
585 for (typename pat_trie_internal_node::const_iterator it = begin();
588 const_node_pointer p_nd =* it;
589 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_parent == this);
590 subtree_debug_info child_ret = p_nd->assert_valid_imp(p_traits);
592 _GLIBCXX_DEBUG_ASSERT(static_cast<size_type>(std::distance(child_ret.first, child_ret.second)) >= m_e_ind);
593 _GLIBCXX_DEBUG_ASSERT(should_be_mine(child_ret.first, child_ret.second, 0, p_traits));
594 _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));
596 return std::make_pair(pref_b_it(), pref_e_it());
600 #undef PB_DS_CLASS_T_DEC
601 #undef PB_DS_CLASS_C_DEC
602 #undef PB_DS_BASE_C_DEC
603 #undef PB_DS_LEAF_C_DEC
604 #undef PB_DS_STATIC_ASSERT
606 } // namespace detail