3 // Copyright (C) 2005-2021 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 ov_tree_map_/node_iterators.hpp
38 * Contains an implementation class for ov_tree_.
41 #ifndef PB_DS_OV_TREE_NODE_ITERATORS_HPP
42 #define PB_DS_OV_TREE_NODE_ITERATORS_HPP
44 #include <ext/pb_ds/tag_and_trait.hpp>
45 #include <ext/pb_ds/detail/type_utils.hpp>
46 #include <debug/debug.h>
52 #define PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC \
53 ov_tree_node_const_it_<Value_Type, Metadata_Type, _Alloc>
55 /// Const node reference.
56 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
57 class ov_tree_node_const_it_
61 typedef typename rebind_traits<_Alloc, Value_Type>::pointer
64 typedef typename rebind_traits<_Alloc, Value_Type>::const_pointer
67 typedef typename rebind_traits<_Alloc, Metadata_Type>::const_pointer
68 const_metadata_pointer;
70 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC this_type;
74 template<typename Ptr>
76 mid_pointer(Ptr p_begin, Ptr p_end)
78 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
79 return (p_begin + (p_end - p_begin) / 2);
84 typedef trivial_iterator_tag iterator_category;
86 typedef trivial_iterator_difference_type difference_type;
88 typedef typename rebind_traits<_Alloc, Value_Type>::const_pointer
91 typedef typename rebind_traits<_Alloc,
92 typename remove_const<Value_Type>::type>::const_pointer
95 typedef typename rebind_traits<_Alloc,
96 typename remove_const<Value_Type>::type>::const_pointer
99 typedef Metadata_Type metadata_type;
101 typedef typename rebind_traits<_Alloc, metadata_type>::const_reference
102 metadata_const_reference;
106 ov_tree_node_const_it_(const_pointer p_nd = 0,
107 const_pointer p_begin_nd = 0,
108 const_pointer p_end_nd = 0,
109 const_metadata_pointer p_metadata = 0)
110 : m_p_value(const_cast<pointer>(p_nd)),
111 m_p_begin_value(const_cast<pointer>(p_begin_nd)),
112 m_p_end_value(const_cast<pointer>(p_end_nd)),
113 m_p_metadata(p_metadata)
116 inline const_reference
118 { return m_p_value; }
120 inline metadata_const_reference
125 has_metadata = !is_same<Metadata_Type, null_type>::value
128 PB_DS_STATIC_ASSERT(should_have_metadata, has_metadata);
129 _GLIBCXX_DEBUG_ASSERT(m_p_metadata != 0);
130 return *m_p_metadata;
133 /// Returns the node iterator associated with the left node.
137 if (m_p_begin_value == m_p_value)
138 return (this_type(m_p_begin_value, m_p_begin_value, m_p_begin_value));
140 const_metadata_pointer p_begin_metadata =
141 m_p_metadata - (m_p_value - m_p_begin_value);
143 return (this_type(mid_pointer(m_p_begin_value, m_p_value),
146 mid_pointer(p_begin_metadata, m_p_metadata)));
149 /// Returns the node iterator associated with the right node.
153 if (m_p_value == m_p_end_value)
154 return (this_type(m_p_end_value, m_p_end_value, m_p_end_value));
156 const_metadata_pointer p_end_metadata =
157 m_p_metadata + (m_p_end_value - m_p_value);
159 return (this_type(mid_pointer(m_p_value + 1, m_p_end_value),
161 m_p_end_value,(m_p_metadata == 0) ?
162 0 : mid_pointer(m_p_metadata + 1, p_end_metadata)));
166 operator==(const this_type& other) const
168 const bool is_end = m_p_begin_value == m_p_end_value;
169 const bool is_other_end = other.m_p_begin_value == other.m_p_end_value;
172 return (is_other_end);
177 return m_p_value == other.m_p_value;
181 operator!=(const this_type& other) const
182 { return !operator==(other); }
186 pointer m_p_begin_value;
187 pointer m_p_end_value;
189 const_metadata_pointer m_p_metadata;
192 #define PB_DS_OV_TREE_NODE_ITERATOR_C_DEC \
193 ov_tree_node_it_<Value_Type, Metadata_Type, _Alloc>
196 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
197 class ov_tree_node_it_ : public PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
200 typedef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC this_type;
202 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC base_type;
204 typedef typename base_type::pointer pointer;
206 typedef typename base_type::const_pointer const_pointer;
209 typename base_type::const_metadata_pointer
210 const_metadata_pointer;
213 typedef trivial_iterator_tag iterator_category;
215 typedef trivial_iterator_difference_type difference_type;
217 typedef typename rebind_traits<_Alloc, Value_Type>::pointer
220 typedef typename rebind_traits<_Alloc,
221 typename remove_const<Value_Type>::type>::pointer
224 typedef typename rebind_traits<_Alloc,
225 typename remove_const<Value_Type>::type>::pointer
229 ov_tree_node_it_(const_pointer p_nd = 0, const_pointer p_begin_nd = 0, const_pointer p_end_nd = 0, const_metadata_pointer p_metadata = 0) : base_type(p_nd, p_begin_nd, p_end_nd, p_metadata)
235 { return reference(base_type::m_p_value); }
237 /// Returns the node reference associated with the left node.
238 inline ov_tree_node_it_
241 if (base_type::m_p_begin_value == base_type::m_p_value)
242 return (this_type(base_type::m_p_begin_value, base_type::m_p_begin_value, base_type::m_p_begin_value));
244 const_metadata_pointer p_begin_metadata =
245 base_type::m_p_metadata - (base_type::m_p_value - base_type::m_p_begin_value);
247 return (this_type(base_type::mid_pointer(base_type::m_p_begin_value, base_type::m_p_value),
248 base_type::m_p_begin_value,
249 base_type::m_p_value,
250 base_type::mid_pointer(p_begin_metadata, base_type::m_p_metadata)));
253 /// Returns the node reference associated with the right node.
254 inline ov_tree_node_it_
257 if (base_type::m_p_value == base_type::m_p_end_value)
258 return this_type(base_type::m_p_end_value, base_type::m_p_end_value,
259 base_type::m_p_end_value);
261 const_metadata_pointer p_end_metadata =
262 base_type::m_p_metadata + (base_type::m_p_end_value - base_type::m_p_value);
264 return (this_type(base_type::mid_pointer(base_type::m_p_value + 1, base_type::m_p_end_value),
265 base_type::m_p_value + 1,
266 base_type::m_p_end_value,(base_type::m_p_metadata == 0)?
267 0 : base_type::mid_pointer(base_type::m_p_metadata + 1, p_end_metadata)));
272 #undef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC
273 #undef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
275 } // namespace detail
276 } // namespace __gnu_pbds