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 find_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
48 inline typename PB_DS_CLASS_C_DEC::point_iterator
50 find(const_key_reference r_key)
52 _GLIBCXX_DEBUG_ONLY(assert_valid();)
53 node_pointer p_nd = find_imp(r_key);
55 if (p_nd == NULL || p_nd->m_type != pat_trie_leaf_node_type)
57 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key);)
61 if (synth_e_access_traits::equal_keys(PB_DS_V2F(static_cast<leaf_pointer>(p_nd)->value()), r_key))
63 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
64 return iterator(p_nd);
67 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key);)
72 inline typename PB_DS_CLASS_C_DEC::const_point_iterator
74 find(const_key_reference r_key) const
76 _GLIBCXX_DEBUG_ONLY(assert_valid();)
78 const_node_pointer p_nd = const_cast<PB_DS_CLASS_C_DEC* >(this)->find_imp(r_key);
80 if (p_nd == NULL || p_nd->m_type != pat_trie_leaf_node_type)
82 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key);)
86 if (synth_e_access_traits::equal_keys(PB_DS_V2F(static_cast<const_leaf_pointer>(p_nd)->value()), r_key))
88 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
89 return const_iterator(const_cast<node_pointer>(p_nd));
92 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key);)
97 inline typename PB_DS_CLASS_C_DEC::node_pointer
99 find_imp(const_key_reference r_key)
104 typename synth_e_access_traits::const_iterator b_it =
105 synth_e_access_traits::begin(r_key);
106 typename synth_e_access_traits::const_iterator e_it =
107 synth_e_access_traits::end(r_key);
109 node_pointer p_nd = m_p_head->m_p_parent;
110 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL);
112 while (p_nd->m_type != pat_trie_leaf_node_type)
114 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
115 node_pointer p_next_nd = static_cast<internal_node_pointer>(p_nd)->get_child_node(b_it, e_it, this);
117 if (p_next_nd == NULL)
125 inline typename PB_DS_CLASS_C_DEC::node_pointer
127 lower_bound_imp(const_key_reference r_key)
132 node_pointer p_nd = m_p_head->m_p_parent;
133 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL);
135 typename PB_DS_CLASS_C_DEC::const_e_iterator b_it =
136 synth_e_access_traits::begin(r_key);
138 typename PB_DS_CLASS_C_DEC::const_e_iterator e_it =
139 synth_e_access_traits::end(r_key);
141 size_type checked_ind = 0;
144 if (p_nd->m_type == pat_trie_leaf_node_type)
146 if (!synth_e_access_traits::cmp_keys(PB_DS_V2F(static_cast<const_leaf_pointer>(p_nd)->value()), r_key))
153 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
154 const size_type new_checked_ind =
155 static_cast<internal_node_pointer>(p_nd)->get_e_ind();
158 static_cast<internal_node_pointer>(p_nd)->get_lower_bound_child_node( b_it, e_it, checked_ind, this);
159 checked_ind = new_checked_ind;
164 inline typename PB_DS_CLASS_C_DEC::point_iterator
166 lower_bound(const_key_reference r_key)
167 { return point_iterator(lower_bound_imp(r_key)); }
170 inline typename PB_DS_CLASS_C_DEC::const_point_iterator
172 lower_bound(const_key_reference r_key) const
174 return const_point_iterator(const_cast<PB_DS_CLASS_C_DEC* >(this)->lower_bound_imp(r_key));
178 inline typename PB_DS_CLASS_C_DEC::point_iterator
180 upper_bound(const_key_reference r_key)
182 point_iterator l_bound_it = lower_bound(r_key);
184 _GLIBCXX_DEBUG_ASSERT(l_bound_it == end() ||
185 !synth_e_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it),
188 if (l_bound_it == end() ||
189 synth_e_access_traits::cmp_keys(r_key, PB_DS_V2F(*l_bound_it)))
196 inline typename PB_DS_CLASS_C_DEC::const_point_iterator
198 upper_bound(const_key_reference r_key) const
200 const_point_iterator l_bound_it = lower_bound(r_key);
202 _GLIBCXX_DEBUG_ASSERT(l_bound_it == end() ||
203 !synth_e_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it),
206 if (l_bound_it == end() ||
207 synth_e_access_traits::cmp_keys(r_key, PB_DS_V2F(*l_bound_it)))
213 inline typename PB_DS_CLASS_C_DEC::const_e_iterator
215 pref_begin(const_node_pointer p_nd)
217 if (p_nd->m_type == pat_trie_leaf_node_type)
218 return (synth_e_access_traits::begin(PB_DS_V2F(static_cast<const_leaf_pointer>(p_nd)->value())));
220 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
221 return static_cast<const_internal_node_pointer>(p_nd)->pref_b_it();
225 inline typename PB_DS_CLASS_C_DEC::const_e_iterator
227 pref_end(const_node_pointer p_nd)
229 if (p_nd->m_type == pat_trie_leaf_node_type)
230 return (synth_e_access_traits::end(PB_DS_V2F(static_cast<const_leaf_pointer>(p_nd)->value())));
232 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
233 return static_cast<const_internal_node_pointer>(p_nd)->pref_e_it();
237 inline typename PB_DS_CLASS_C_DEC::const_leaf_pointer
239 leftmost_descendant(const_node_pointer p_nd)
241 if (p_nd->m_type == pat_trie_leaf_node_type)
242 return static_cast<const_leaf_pointer>(p_nd);
243 return static_cast<const_internal_node_pointer>(p_nd)->leftmost_descendant();
247 inline typename PB_DS_CLASS_C_DEC::leaf_pointer
249 leftmost_descendant(node_pointer p_nd)
251 if (p_nd->m_type == pat_trie_leaf_node_type)
252 return static_cast<leaf_pointer>(p_nd);
253 return static_cast<internal_node_pointer>(p_nd)->leftmost_descendant();
257 inline typename PB_DS_CLASS_C_DEC::const_leaf_pointer
259 rightmost_descendant(const_node_pointer p_nd)
261 if (p_nd->m_type == pat_trie_leaf_node_type)
262 return static_cast<const_leaf_pointer>(p_nd);
263 return static_cast<const_internal_node_pointer>(p_nd)->rightmost_descendant();
267 inline typename PB_DS_CLASS_C_DEC::leaf_pointer
269 rightmost_descendant(node_pointer p_nd)
271 if (p_nd->m_type == pat_trie_leaf_node_type)
272 return static_cast<leaf_pointer>(p_nd);
273 return static_cast<internal_node_pointer>(p_nd)->rightmost_descendant();