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 node_iterators.hpp
44 * Contains an implementation class for pat_trie_.
47 #ifndef PB_DS_PAT_TRIE_NODE_ITERATORS_HPP
48 #define PB_DS_PAT_TRIE_NODE_ITERATORS_HPP
50 #include <debug/debug.h>
57 #define PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC \
58 pat_trie_const_node_it_< \
68 #define PB_DS_PAT_TRIE_NODE_ITERATOR_C_DEC \
79 // Const node iterator.
80 template<typename Node,
86 class E_Access_Traits,
88 class pat_trie_const_node_it_
92 typename Allocator::template rebind<
97 typename Allocator::template rebind<
98 Leaf>::other::const_pointer
102 typename Allocator::template rebind<
103 Leaf>::other::pointer
107 typename Allocator::template rebind<
108 Internal_Node>::other::pointer
109 internal_node_pointer;
112 typename Allocator::template rebind<
113 Internal_Node>::other::const_pointer
114 const_internal_node_pointer;
117 typename Allocator::template rebind<
118 E_Access_Traits>::other::const_pointer
119 const_e_access_traits_pointer;
122 inline typename E_Access_Traits::const_iterator
125 if (m_p_nd->m_type == pat_trie_leaf_node_type)
126 return (m_p_traits->begin(
127 m_p_traits->extract_key(
128 static_cast<const_leaf_pointer>(m_p_nd)->value())));
130 _GLIBCXX_DEBUG_ASSERT(m_p_nd->m_type == pat_trie_internal_node_type);
132 return (static_cast<const_internal_node_pointer>(m_p_nd)->pref_b_it());
135 inline typename E_Access_Traits::const_iterator
138 if (m_p_nd->m_type == pat_trie_leaf_node_type)
139 return (m_p_traits->end(
140 m_p_traits->extract_key(
141 static_cast<const_leaf_pointer>(m_p_nd)->value())));
143 _GLIBCXX_DEBUG_ASSERT(m_p_nd->m_type == pat_trie_internal_node_type);
145 return (static_cast<const_internal_node_pointer>(m_p_nd)->pref_e_it());
151 typedef typename Allocator::size_type size_type;
154 typedef trivial_iterator_tag iterator_category;
157 typedef trivial_iterator_difference_type difference_type;
159 // __Iterator's value type.
160 typedef Const_Iterator value_type;
162 // __Iterator's reference type.
163 typedef value_type reference;
165 // __Iterator's __const reference type.
166 typedef value_type const_reference;
168 // Element access traits.
169 typedef E_Access_Traits e_access_traits;
171 // A key's element __const iterator.
172 typedef typename e_access_traits::const_iterator const_e_iterator;
175 typedef typename Node::metadata_type metadata_type;
177 // Const metadata reference type.
179 typename Allocator::template rebind<
180 metadata_type>::other::const_reference
181 const_metadata_reference;
183 // Default constructor.
186 pat_trie_const_node_it_()
189 pat_trie_const_node_it_(node_pointer p_nd = NULL,
190 const_e_access_traits_pointer p_traits = NULL)
191 : m_p_nd(const_cast<node_pointer>(p_nd)), m_p_traits(p_traits)
194 // Subtree valid prefix.
195 inline std::pair<const_e_iterator, const_e_iterator>
197 { return std::make_pair(pref_begin(), pref_end()); }
199 // Const access; returns the __const iterator* associated with
201 inline const_reference
204 _GLIBCXX_DEBUG_ASSERT(num_children() == 0);
205 return Const_Iterator(m_p_nd);
209 inline const_metadata_reference
211 { return m_p_nd->get_metadata(); }
213 // Returns the number of children in the corresponding node.
217 if (m_p_nd->m_type == pat_trie_leaf_node_type)
219 _GLIBCXX_DEBUG_ASSERT(m_p_nd->m_type == pat_trie_internal_node_type);
220 return std::distance(static_cast<internal_node_pointer>(m_p_nd)->begin(), static_cast<internal_node_pointer>(m_p_nd)->end());
223 // Returns a __const node __iterator to the corresponding node's
225 PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC
226 get_child(size_type i) const
228 _GLIBCXX_DEBUG_ASSERT(m_p_nd->m_type == pat_trie_internal_node_type);
229 typename Internal_Node::iterator it =
230 static_cast<internal_node_pointer>(m_p_nd)->begin();
233 return PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC(*it, m_p_traits);
236 // Compares content to a different iterator object.
238 operator==(const PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC& other) const
239 { return (m_p_nd == other.m_p_nd); }
241 // Compares content (negatively) to a different iterator object.
243 operator!=(const PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC& other) const
244 { return m_p_nd != other.m_p_nd; }
248 friend class PB_DS_CLASS_C_DEC;
253 const_e_access_traits_pointer m_p_traits;
257 template<typename Node,
261 class Const_Iterator,
263 class E_Access_Traits,
265 class pat_trie_node_it_ :
266 public PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC
271 typename Allocator::template rebind<
272 Node>::other::pointer
275 typedef Iterator iterator;
277 typedef PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC base_type;
280 typename base_type::const_e_access_traits_pointer
281 const_e_access_traits_pointer;
283 typedef typename base_type::internal_node_pointer internal_node_pointer;
289 typename PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC::size_type
292 // __Iterator's value type.
293 typedef Iterator value_type;
295 // __Iterator's reference type.
296 typedef value_type reference;
298 // __Iterator's __const reference type.
299 typedef value_type const_reference;
301 // Default constructor.
304 pat_trie_node_it_() ;
308 pat_trie_node_it_(node_pointer p_nd = NULL, const_e_access_traits_pointer p_traits = NULL) : base_type(p_nd, p_traits)
311 // Access; returns the iterator* associated with the current leaf.
315 _GLIBCXX_DEBUG_ASSERT(base_type::num_children() == 0);
316 return Iterator(base_type::m_p_nd);
320 // Returns a node __iterator to the corresponding node's i-th child.
321 PB_DS_PAT_TRIE_NODE_ITERATOR_C_DEC
322 get_child(size_type i) const
324 _GLIBCXX_DEBUG_ASSERT(base_type::m_p_nd->m_type == pat_trie_internal_node_type);
326 typename Internal_Node::iterator it =
327 static_cast<internal_node_pointer>(base_type::m_p_nd)->begin();
330 return PB_DS_PAT_TRIE_NODE_ITERATOR_C_DEC(*it, base_type::m_p_traits);
334 friend class PB_DS_CLASS_C_DEC;
337 #undef PB_DS_PAT_TRIE_CONST_NODE_ITERATOR_C_DEC
338 #undef PB_DS_PAT_TRIE_NODE_ITERATOR_C_DEC
340 } // namespace detail