]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/ext/pb_ds/detail/pat_trie_/internal_node.hpp
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / ext / pb_ds / detail / pat_trie_ / internal_node.hpp
1 // -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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.
41
42 /**
43  * @file internal_node.hpp
44  * Contains an internal PB_DS_BASE_C_DEC for a patricia tree.
45  */
46
47 #ifndef PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
48 #define PB_DS_PAT_TRIE_INTERNAL_NODE_HPP
49
50 #include <debug/debug.h>
51
52 namespace __gnu_pbds
53 {
54   namespace detail
55   {
56 #define PB_DS_CLASS_T_DEC \
57     template<typename Type_Traits, typename E_Access_Traits,  \
58              typename Metadata, typename Allocator>
59
60 #define PB_DS_CLASS_C_DEC \
61     pat_trie_internal_node<Type_Traits, E_Access_Traits, Metadata, Allocator>
62
63 #define PB_DS_BASE_C_DEC \
64     pat_trie_node_base<Type_Traits, E_Access_Traits, Metadata, Allocator>
65
66 #define PB_DS_LEAF_C_DEC \
67     pat_trie_leaf<Type_Traits, E_Access_Traits, Metadata, Allocator>
68
69     template<typename Type_Traits,
70              typename E_Access_Traits,
71              typename Metadata,
72              typename Allocator>
73     struct pat_trie_internal_node : public PB_DS_BASE_C_DEC
74     {
75     private:
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;
80
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;
85
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;
89
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;
94
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;
98
99 #ifdef _GLIBCXX_DEBUG
100       typedef typename base_type::subtree_debug_info subtree_debug_info;
101
102       virtual subtree_debug_info
103       assert_valid_imp(const_e_access_traits_pointer) const;
104 #endif 
105
106       inline size_type
107       get_pref_pos(const_e_iterator, const_e_iterator, 
108                    const_e_access_traits_pointer) const;
109
110     public:
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;
114
115       enum
116         {
117           arr_size = E_Access_Traits::max_size + 1
118         };
119       PB_DS_STATIC_ASSERT(min_arr_size, arr_size >= 2);
120
121 #include <ext/pb_ds/detail/pat_trie_/const_child_iterator.hpp>
122 #include <ext/pb_ds/detail/pat_trie_/child_iterator.hpp>
123
124       pat_trie_internal_node(size_type, const const_e_iterator);
125
126       void
127       update_prefixes(const_e_access_traits_pointer);
128
129       const_iterator
130       begin() const;
131
132       iterator
133       begin();
134
135       const_iterator
136       end() const;
137
138       iterator
139       end();
140
141       inline node_pointer
142       get_child_node(const_e_iterator, const_e_iterator, 
143                      const_e_access_traits_pointer);
144
145       inline const_node_pointer
146       get_child_node(const_e_iterator, const_e_iterator, 
147                      const_e_access_traits_pointer) const;
148
149       inline iterator
150       get_child_it(const_e_iterator, const_e_iterator, 
151                    const_e_access_traits_pointer);
152
153       inline node_pointer
154       get_lower_bound_child_node(const_e_iterator, const_e_iterator, 
155                                  size_type, const_e_access_traits_pointer);
156
157       inline node_pointer
158       add_child(node_pointer, const_e_iterator, const_e_iterator, 
159                 const_e_access_traits_pointer);
160
161       inline const_node_pointer
162       get_join_child(const_node_pointer, const_e_access_traits_pointer) const;
163
164       inline node_pointer
165       get_join_child(node_pointer, const_e_access_traits_pointer);
166
167       void
168       remove_child(node_pointer p_nd);
169
170       iterator
171       remove_child(iterator it);
172
173       void
174       replace_child(node_pointer, const_e_iterator, const_e_iterator, 
175                     const_e_access_traits_pointer);
176
177       inline const_e_iterator
178       pref_b_it() const;
179
180       inline const_e_iterator
181       pref_e_it() const;
182
183       inline size_type
184       get_e_ind() const;
185
186       bool
187       should_be_mine(const_e_iterator, const_e_iterator, size_type, 
188                      const_e_access_traits_pointer) const;
189
190       leaf_pointer
191       leftmost_descendant();
192
193       const_leaf_pointer
194       leftmost_descendant() const;
195
196       leaf_pointer
197       rightmost_descendant();
198
199       const_leaf_pointer
200       rightmost_descendant() const;
201
202 #ifdef _GLIBCXX_DEBUG
203       size_type
204       e_ind() const;
205 #endif 
206
207     private:
208       pat_trie_internal_node(const pat_trie_internal_node&);
209
210       size_type
211       get_begin_pos() const;
212
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;
219     };
220
221     PB_DS_CLASS_T_DEC
222     typename PB_DS_CLASS_C_DEC::leaf_rebind
223     PB_DS_CLASS_C_DEC::s_leaf_alloc;
224
225     PB_DS_CLASS_T_DEC
226     typename PB_DS_CLASS_C_DEC::internal_node_rebind
227     PB_DS_CLASS_C_DEC::s_internal_node_alloc;
228
229     PB_DS_CLASS_T_DEC
230     inline typename PB_DS_CLASS_C_DEC::size_type
231     PB_DS_CLASS_C_DEC::
232     get_pref_pos(const_e_iterator b_it, const_e_iterator e_it, 
233                  const_e_access_traits_pointer p_traits) const
234     {
235       if (static_cast<size_t>(std::distance(b_it, e_it)) <= m_e_ind)
236         return 0;
237       std::advance(b_it, m_e_ind);
238       return 1 + p_traits->e_pos(*b_it);
239     }
240
241     PB_DS_CLASS_T_DEC
242     PB_DS_CLASS_C_DEC::
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)
246     {
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));
250     }
251
252     PB_DS_CLASS_T_DEC
253     void
254     PB_DS_CLASS_C_DEC::
255     update_prefixes(const_e_access_traits_pointer p_traits)
256     {
257       node_pointer p_first = *begin();
258       if (p_first->m_type == pat_trie_leaf_node_type)
259         {
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()));
262         }
263       else
264         {
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();
267         }
268       m_pref_e_it = m_pref_b_it;
269       std::advance(m_pref_e_it, m_e_ind);
270     }
271
272     PB_DS_CLASS_T_DEC
273     typename PB_DS_CLASS_C_DEC::const_iterator
274     PB_DS_CLASS_C_DEC::
275     begin() const
276     {
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);
280     }
281
282     PB_DS_CLASS_T_DEC
283     typename PB_DS_CLASS_C_DEC::iterator
284     PB_DS_CLASS_C_DEC::
285     begin()
286     {
287       return iterator(m_a_p_children + get_begin_pos(), 
288                       m_a_p_children + arr_size);
289     }
290
291     PB_DS_CLASS_T_DEC
292     typename PB_DS_CLASS_C_DEC::const_iterator
293     PB_DS_CLASS_C_DEC::
294     end() const
295     {
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);
299     }
300
301     PB_DS_CLASS_T_DEC
302     typename PB_DS_CLASS_C_DEC::iterator
303     PB_DS_CLASS_C_DEC::
304     end()
305     { return iterator(m_a_p_children + arr_size, m_a_p_children + arr_size); }
306
307     PB_DS_CLASS_T_DEC
308     inline typename PB_DS_CLASS_C_DEC::node_pointer
309     PB_DS_CLASS_C_DEC::
310     get_child_node(const_e_iterator b_it, const_e_iterator e_it, 
311                    const_e_access_traits_pointer p_traits)
312     {
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];
316     }
317
318     PB_DS_CLASS_T_DEC
319     inline typename PB_DS_CLASS_C_DEC::iterator
320     PB_DS_CLASS_C_DEC::
321     get_child_it(const_e_iterator b_it, const_e_iterator e_it, 
322                  const_e_access_traits_pointer p_traits)
323     {
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);
328     }
329
330     PB_DS_CLASS_T_DEC
331     inline typename PB_DS_CLASS_C_DEC::const_node_pointer
332     PB_DS_CLASS_C_DEC::
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)); }
336
337     PB_DS_CLASS_T_DEC
338     typename PB_DS_CLASS_C_DEC::node_pointer
339     PB_DS_CLASS_C_DEC::
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)
343     {
344       if (!should_be_mine(b_it, e_it, checked_ind, p_traits))
345         {
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();
349         }
350
351       size_type i = get_pref_pos(b_it, e_it, p_traits);
352       _GLIBCXX_DEBUG_ASSERT(i < arr_size);
353
354       if (m_a_p_children[i] != NULL)
355         return m_a_p_children[i];
356
357       while (++i < arr_size)
358         if (m_a_p_children[i] != NULL)
359           {
360             if (m_a_p_children[i]->m_type == pat_trie_leaf_node_type)
361               return m_a_p_children[i];
362
363             _GLIBCXX_DEBUG_ASSERT(m_a_p_children[i]->m_type == pat_trie_internal_node_type);
364
365             return static_cast<internal_node_pointer>(m_a_p_children[i])->leftmost_descendant();
366           }
367
368       return rightmost_descendant();
369     }
370
371     PB_DS_CLASS_T_DEC
372     inline typename PB_DS_CLASS_C_DEC::node_pointer
373     PB_DS_CLASS_C_DEC::
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)
376     {
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)
380         {
381           m_a_p_children[i] = p_nd;
382           p_nd->m_p_parent = this;
383           return p_nd;
384         }
385       return m_a_p_children[i];
386     }
387
388     PB_DS_CLASS_T_DEC
389     typename PB_DS_CLASS_C_DEC::const_node_pointer
390     PB_DS_CLASS_C_DEC::
391     get_join_child(const_node_pointer p_nd, const_e_access_traits_pointer p_traits) const
392     {
393       node_pointer p = const_cast<node_pointer>(p_nd);
394       return const_cast<internal_node_pointer>(this)->get_join_child(p, p_traits);
395     }
396
397     PB_DS_CLASS_T_DEC
398     typename PB_DS_CLASS_C_DEC::node_pointer
399     PB_DS_CLASS_C_DEC::
400     get_join_child(node_pointer p_nd, const_e_access_traits_pointer p_traits)
401     {
402       size_type i;
403       const_e_iterator b_it;
404       const_e_iterator e_it;
405       if (p_nd->m_type == pat_trie_leaf_node_type)
406         {
407           typename Type_Traits::const_key_reference r_key =
408             e_access_traits::extract_key(static_cast<const_leaf_pointer>(p_nd)->value());
409
410           b_it = p_traits->begin(r_key);
411           e_it = p_traits->end(r_key);
412         }
413       else
414         {
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();
417         }
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];
421     }
422
423     PB_DS_CLASS_T_DEC
424     void
425     PB_DS_CLASS_C_DEC::
426     remove_child(node_pointer p_nd)
427     {
428       size_type i = 0;
429       for (; i < arr_size; ++i)
430         if (m_a_p_children[i] == p_nd)
431           {
432             m_a_p_children[i] = NULL;
433             return;
434           }
435       _GLIBCXX_DEBUG_ASSERT(i != arr_size);
436     }
437
438     PB_DS_CLASS_T_DEC
439     typename PB_DS_CLASS_C_DEC::iterator
440     PB_DS_CLASS_C_DEC::
441     remove_child(iterator it)
442     {
443       iterator ret = it;
444       ++ret;
445       * it.m_p_p_cur = NULL;
446       return ret;
447     }
448
449     PB_DS_CLASS_T_DEC
450     void
451     PB_DS_CLASS_C_DEC::
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)
455     {
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;
460     }
461
462     PB_DS_CLASS_T_DEC
463     inline typename PB_DS_CLASS_C_DEC::const_e_iterator
464     PB_DS_CLASS_C_DEC::
465     pref_b_it() const
466     { return m_pref_b_it; }
467
468     PB_DS_CLASS_T_DEC
469     inline typename PB_DS_CLASS_C_DEC::const_e_iterator
470     PB_DS_CLASS_C_DEC::
471     pref_e_it() const
472     { return m_pref_e_it; }
473
474     PB_DS_CLASS_T_DEC
475     inline typename PB_DS_CLASS_C_DEC::size_type
476     PB_DS_CLASS_C_DEC::
477     get_e_ind() const
478     { return m_e_ind; }
479
480     PB_DS_CLASS_T_DEC
481     bool
482     PB_DS_CLASS_C_DEC::
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
486     {
487       if (m_e_ind == 0)
488         return true;
489
490       const size_type num_es = std::distance(b_it, e_it);
491       if (num_es < m_e_ind)
492         return false;
493
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);
498
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);
503
504       return p_traits->equal_prefixes(key_b_it, key_e_it, value_b_it, 
505                                       value_e_it);
506     }
507
508     PB_DS_CLASS_T_DEC
509     typename PB_DS_CLASS_C_DEC::leaf_pointer
510     PB_DS_CLASS_C_DEC::
511     leftmost_descendant()
512     {
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();
518     }
519
520     PB_DS_CLASS_T_DEC
521     typename PB_DS_CLASS_C_DEC::const_leaf_pointer
522     PB_DS_CLASS_C_DEC::
523     leftmost_descendant() const
524     {
525       return const_cast<internal_node_pointer>(this)->leftmost_descendant();
526     }
527
528     PB_DS_CLASS_T_DEC
529     typename PB_DS_CLASS_C_DEC::leaf_pointer
530     PB_DS_CLASS_C_DEC::
531     rightmost_descendant()
532     {
533       const size_type num_children = std::distance(begin(), end());
534       _GLIBCXX_DEBUG_ASSERT(num_children >= 2);
535
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();
543     }
544
545     PB_DS_CLASS_T_DEC
546     typename PB_DS_CLASS_C_DEC::const_leaf_pointer
547     PB_DS_CLASS_C_DEC::
548     rightmost_descendant() const
549     {
550       return const_cast<internal_node_pointer>(this)->rightmost_descendant();
551     }
552
553 #ifdef _GLIBCXX_DEBUG
554     PB_DS_CLASS_T_DEC
555     typename PB_DS_CLASS_C_DEC::size_type
556     PB_DS_CLASS_C_DEC::
557     e_ind() const
558     { return m_e_ind; }
559 #endif 
560
561     PB_DS_CLASS_T_DEC
562     typename PB_DS_CLASS_C_DEC::size_type
563     PB_DS_CLASS_C_DEC::
564     get_begin_pos() const
565     {
566       size_type i;
567       for (i = 0; i < arr_size && m_a_p_children[i] == NULL; ++i)
568         ;
569       return i;
570     }
571
572 #ifdef _GLIBCXX_DEBUG
573     PB_DS_CLASS_T_DEC
574     typename PB_DS_CLASS_C_DEC::subtree_debug_info
575     PB_DS_CLASS_C_DEC::
576     assert_valid_imp(const_e_access_traits_pointer p_traits) const
577     {
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);
581
582       for (typename pat_trie_internal_node::const_iterator it = begin();
583            it != end(); ++it)
584         {
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);
588
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));
592         }
593       return std::make_pair(pref_b_it(), pref_e_it());
594     }
595 #endif 
596
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
601
602   } // namespace detail
603 } // namespace __gnu_pbds
604
605 #endif