3 // Copyright (C) 2005, 2006, 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 find_fn_imps.hpp
38 * Contains an implementation class for bin_search_tree_.
42 inline typename
PB_DS_CLASS_C_DEC::point_iterator
44 find(const_key_reference r_key
)
46 _GLIBCXX_DEBUG_ONLY(assert_valid();)
47 node_pointer p_nd
= find_imp(r_key
);
49 if (p_nd
== 0 || p_nd
->m_type
!= pat_trie_leaf_node_type
)
51 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key
);)
55 if (synth_e_access_traits::equal_keys(PB_DS_V2F(static_cast<leaf_pointer
>(p_nd
)->value()), r_key
))
57 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key
));
58 return iterator(p_nd
);
61 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key
);)
66 inline typename
PB_DS_CLASS_C_DEC::const_point_iterator
68 find(const_key_reference r_key
) const
70 _GLIBCXX_DEBUG_ONLY(assert_valid();)
72 const_node_pointer p_nd
= const_cast<PB_DS_CLASS_C_DEC
* >(this)->find_imp(r_key
);
74 if (p_nd
== 0 || p_nd
->m_type
!= pat_trie_leaf_node_type
)
76 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key
);)
80 if (synth_e_access_traits::equal_keys(PB_DS_V2F(static_cast<const_leaf_pointer
>(p_nd
)->value()), r_key
))
82 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key
));
83 return const_iterator(const_cast<node_pointer
>(p_nd
));
86 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key
);)
91 inline typename
PB_DS_CLASS_C_DEC::node_pointer
93 find_imp(const_key_reference r_key
)
98 typename
synth_e_access_traits::const_iterator b_it
=
99 synth_e_access_traits::begin(r_key
);
100 typename
synth_e_access_traits::const_iterator e_it
=
101 synth_e_access_traits::end(r_key
);
103 node_pointer p_nd
= m_p_head
->m_p_parent
;
104 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
106 while (p_nd
->m_type
!= pat_trie_leaf_node_type
)
108 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_type
== pat_trie_internal_node_type
);
109 node_pointer p_next_nd
= static_cast<internal_node_pointer
>(p_nd
)->get_child_node(b_it
, e_it
, this);
119 inline typename
PB_DS_CLASS_C_DEC::node_pointer
121 lower_bound_imp(const_key_reference r_key
)
126 node_pointer p_nd
= m_p_head
->m_p_parent
;
127 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
129 typename
PB_DS_CLASS_C_DEC::const_e_iterator b_it
=
130 synth_e_access_traits::begin(r_key
);
132 typename
PB_DS_CLASS_C_DEC::const_e_iterator e_it
=
133 synth_e_access_traits::end(r_key
);
135 size_type checked_ind
= 0;
138 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
140 if (!synth_e_access_traits::cmp_keys(PB_DS_V2F(static_cast<const_leaf_pointer
>(p_nd
)->value()), r_key
))
147 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_type
== pat_trie_internal_node_type
);
148 const size_type new_checked_ind
=
149 static_cast<internal_node_pointer
>(p_nd
)->get_e_ind();
152 static_cast<internal_node_pointer
>(p_nd
)->get_lower_bound_child_node( b_it
, e_it
, checked_ind
, this);
153 checked_ind
= new_checked_ind
;
158 inline typename
PB_DS_CLASS_C_DEC::point_iterator
160 lower_bound(const_key_reference r_key
)
161 { return point_iterator(lower_bound_imp(r_key
)); }
164 inline typename
PB_DS_CLASS_C_DEC::const_point_iterator
166 lower_bound(const_key_reference r_key
) const
168 return const_point_iterator(const_cast<PB_DS_CLASS_C_DEC
* >(this)->lower_bound_imp(r_key
));
172 inline typename
PB_DS_CLASS_C_DEC::point_iterator
174 upper_bound(const_key_reference r_key
)
176 point_iterator l_bound_it
= lower_bound(r_key
);
178 _GLIBCXX_DEBUG_ASSERT(l_bound_it
== end() ||
179 !synth_e_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it
),
182 if (l_bound_it
== end() ||
183 synth_e_access_traits::cmp_keys(r_key
, PB_DS_V2F(*l_bound_it
)))
190 inline typename
PB_DS_CLASS_C_DEC::const_point_iterator
192 upper_bound(const_key_reference r_key
) const
194 const_point_iterator l_bound_it
= lower_bound(r_key
);
196 _GLIBCXX_DEBUG_ASSERT(l_bound_it
== end() ||
197 !synth_e_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it
),
200 if (l_bound_it
== end() ||
201 synth_e_access_traits::cmp_keys(r_key
, PB_DS_V2F(*l_bound_it
)))
207 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
209 pref_begin(const_node_pointer p_nd
)
211 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
212 return (synth_e_access_traits::begin(PB_DS_V2F(static_cast<const_leaf_pointer
>(p_nd
)->value())));
214 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_type
== pat_trie_internal_node_type
);
215 return static_cast<const_internal_node_pointer
>(p_nd
)->pref_b_it();
219 inline typename
PB_DS_CLASS_C_DEC::const_e_iterator
221 pref_end(const_node_pointer p_nd
)
223 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
224 return (synth_e_access_traits::end(PB_DS_V2F(static_cast<const_leaf_pointer
>(p_nd
)->value())));
226 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_type
== pat_trie_internal_node_type
);
227 return static_cast<const_internal_node_pointer
>(p_nd
)->pref_e_it();
231 inline typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
233 leftmost_descendant(const_node_pointer p_nd
)
235 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
236 return static_cast<const_leaf_pointer
>(p_nd
);
237 return static_cast<const_internal_node_pointer
>(p_nd
)->leftmost_descendant();
241 inline typename
PB_DS_CLASS_C_DEC::leaf_pointer
243 leftmost_descendant(node_pointer p_nd
)
245 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
246 return static_cast<leaf_pointer
>(p_nd
);
247 return static_cast<internal_node_pointer
>(p_nd
)->leftmost_descendant();
251 inline typename
PB_DS_CLASS_C_DEC::const_leaf_pointer
253 rightmost_descendant(const_node_pointer p_nd
)
255 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
256 return static_cast<const_leaf_pointer
>(p_nd
);
257 return static_cast<const_internal_node_pointer
>(p_nd
)->rightmost_descendant();
261 inline typename
PB_DS_CLASS_C_DEC::leaf_pointer
263 rightmost_descendant(node_pointer p_nd
)
265 if (p_nd
->m_type
== pat_trie_leaf_node_type
)
266 return static_cast<leaf_pointer
>(p_nd
);
267 return static_cast<internal_node_pointer
>(p_nd
)->rightmost_descendant();