3 // Copyright (C) 2005-2024 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 pat_trie_/find_fn_imps.hpp
38 * Contains an implementation class for pat_trie.
41 #ifdef PB_DS_CLASS_C_DEC
44 inline typename PB_DS_CLASS_C_DEC::point_iterator
46 find(key_const_reference r_key)
48 PB_DS_ASSERT_VALID((*this))
49 node_pointer p_nd = find_imp(r_key);
51 if (p_nd == 0 || p_nd->m_type != leaf_node)
53 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
57 if (synth_access_traits::equal_keys(PB_DS_V2F(static_cast<leaf_pointer>(p_nd)->value()), r_key))
59 PB_DS_CHECK_KEY_EXISTS(r_key)
60 return iterator(p_nd);
63 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
68 inline typename PB_DS_CLASS_C_DEC::point_const_iterator
70 find(key_const_reference r_key) const
72 PB_DS_ASSERT_VALID((*this))
74 node_const_pointer p_nd = const_cast<PB_DS_CLASS_C_DEC* >(this)->find_imp(r_key);
76 if (p_nd == 0 || p_nd->m_type != leaf_node)
78 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
82 if (synth_access_traits::equal_keys(PB_DS_V2F(static_cast<leaf_const_pointer>(p_nd)->value()), r_key))
84 PB_DS_CHECK_KEY_EXISTS(r_key)
85 return const_iterator(const_cast<node_pointer>(p_nd));
88 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
93 inline typename PB_DS_CLASS_C_DEC::node_pointer
95 find_imp(key_const_reference r_key)
100 typename synth_access_traits::const_iterator b_it =
101 synth_access_traits::begin(r_key);
102 typename synth_access_traits::const_iterator e_it =
103 synth_access_traits::end(r_key);
105 node_pointer p_nd = m_p_head->m_p_parent;
106 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
108 while (p_nd->m_type != leaf_node)
110 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == i_node);
111 node_pointer p_next_nd = static_cast<inode_pointer>(p_nd)->get_child_node(b_it, e_it, this);
121 inline typename PB_DS_CLASS_C_DEC::node_pointer
123 lower_bound_imp(key_const_reference r_key)
128 node_pointer p_nd = m_p_head->m_p_parent;
129 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
131 typename PB_DS_CLASS_C_DEC::a_const_iterator b_it =
132 synth_access_traits::begin(r_key);
134 typename PB_DS_CLASS_C_DEC::a_const_iterator e_it =
135 synth_access_traits::end(r_key);
137 size_type checked_ind = 0;
140 if (p_nd->m_type == leaf_node)
142 if (!synth_access_traits::cmp_keys(PB_DS_V2F(static_cast<leaf_const_pointer>(p_nd)->value()), r_key))
149 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == i_node);
150 const size_type new_checked_ind =
151 static_cast<inode_pointer>(p_nd)->get_e_ind();
154 static_cast<inode_pointer>(p_nd)->get_lower_bound_child_node( b_it, e_it, checked_ind, this);
155 checked_ind = new_checked_ind;
160 inline typename PB_DS_CLASS_C_DEC::point_iterator
162 lower_bound(key_const_reference r_key)
163 { return point_iterator(lower_bound_imp(r_key)); }
166 inline typename PB_DS_CLASS_C_DEC::point_const_iterator
168 lower_bound(key_const_reference r_key) const
170 return point_const_iterator(const_cast<PB_DS_CLASS_C_DEC* >(this)->lower_bound_imp(r_key));
174 inline typename PB_DS_CLASS_C_DEC::point_iterator
176 upper_bound(key_const_reference r_key)
178 point_iterator l_bound_it = lower_bound(r_key);
180 _GLIBCXX_DEBUG_ASSERT(l_bound_it == end() ||
181 !synth_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it),
184 if (l_bound_it == end() ||
185 synth_access_traits::cmp_keys(r_key, PB_DS_V2F(*l_bound_it)))
192 inline typename PB_DS_CLASS_C_DEC::point_const_iterator
194 upper_bound(key_const_reference r_key) const
196 point_const_iterator l_bound_it = lower_bound(r_key);
198 _GLIBCXX_DEBUG_ASSERT(l_bound_it == end() ||
199 !synth_access_traits::cmp_keys(PB_DS_V2F(*l_bound_it),
202 if (l_bound_it == end() ||
203 synth_access_traits::cmp_keys(r_key, PB_DS_V2F(*l_bound_it)))
209 inline typename PB_DS_CLASS_C_DEC::a_const_iterator
211 pref_begin(node_const_pointer p_nd)
213 if (p_nd->m_type == leaf_node)
214 return (synth_access_traits::begin(PB_DS_V2F(static_cast<leaf_const_pointer>(p_nd)->value())));
216 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == i_node);
217 return static_cast<inode_const_pointer>(p_nd)->pref_b_it();
221 inline typename PB_DS_CLASS_C_DEC::a_const_iterator
223 pref_end(node_const_pointer p_nd)
225 if (p_nd->m_type == leaf_node)
226 return (synth_access_traits::end(PB_DS_V2F(static_cast<leaf_const_pointer>(p_nd)->value())));
228 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == i_node);
229 return static_cast<inode_const_pointer>(p_nd)->pref_e_it();
233 inline typename PB_DS_CLASS_C_DEC::leaf_const_pointer
235 leftmost_descendant(node_const_pointer p_nd)
237 if (p_nd->m_type == leaf_node)
238 return static_cast<leaf_const_pointer>(p_nd);
239 return static_cast<inode_const_pointer>(p_nd)->leftmost_descendant();
243 inline typename PB_DS_CLASS_C_DEC::leaf_pointer
245 leftmost_descendant(node_pointer p_nd)
247 if (p_nd->m_type == leaf_node)
248 return static_cast<leaf_pointer>(p_nd);
249 return static_cast<inode_pointer>(p_nd)->leftmost_descendant();
253 inline typename PB_DS_CLASS_C_DEC::leaf_const_pointer
255 rightmost_descendant(node_const_pointer p_nd)
257 if (p_nd->m_type == leaf_node)
258 return static_cast<leaf_const_pointer>(p_nd);
259 return static_cast<inode_const_pointer>(p_nd)->rightmost_descendant();
263 inline typename PB_DS_CLASS_C_DEC::leaf_pointer
265 rightmost_descendant(node_pointer p_nd)
267 if (p_nd->m_type == leaf_node)
268 return static_cast<leaf_pointer>(p_nd);
269 return static_cast<inode_pointer>(p_nd)->rightmost_descendant();