re PR libstdc++/37144 (A bug in include/ext/pb_ds/detail/pat_trie_/constructors_destr...
[gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / ov_tree_map_ / node_iterators.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
4 //
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
9 // version.
10
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.
15
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.
19
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/>.
24
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
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
34 // warranty.
35
36 /**
37 * @file ov_tree_map_/node_iterators.hpp
38 * Contains an implementation class for ov_tree_.
39 */
40
41 #ifndef PB_DS_OV_TREE_NODE_ITERATORS_HPP
42 #define PB_DS_OV_TREE_NODE_ITERATORS_HPP
43
44 #include <ext/pb_ds/tag_and_trait.hpp>
45 #include <ext/pb_ds/detail/type_utils.hpp>
46 #include <debug/debug.h>
47
48 namespace __gnu_pbds
49 {
50 namespace detail
51 {
52 #define PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC \
53 ov_tree_node_const_it_<Value_Type, Metadata_Type, _Alloc>
54
55 /// Const node reference.
56 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
57 class ov_tree_node_const_it_
58 {
59
60 protected:
61 typedef
62 typename _Alloc::template rebind<
63 Value_Type>::other::pointer
64 pointer;
65
66 typedef
67 typename _Alloc::template rebind<
68 Value_Type>::other::const_pointer
69 const_pointer;
70
71 typedef
72 typename _Alloc::template rebind<
73 Metadata_Type>::other::const_pointer
74 const_metadata_pointer;
75
76 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC this_type;
77
78 protected:
79
80 template<typename Ptr>
81 inline static Ptr
82 mid_pointer(Ptr p_begin, Ptr p_end)
83 {
84 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
85 return (p_begin + (p_end - p_begin) / 2);
86 }
87
88 public:
89
90 typedef trivial_iterator_tag iterator_category;
91
92 typedef trivial_iterator_difference_type difference_type;
93
94 typedef
95 typename _Alloc::template rebind<
96 Value_Type>::other::const_pointer
97 value_type;
98
99 typedef
100 typename _Alloc::template rebind<
101 typename remove_const<
102 Value_Type>::type>::other::const_pointer
103 reference;
104
105 typedef
106 typename _Alloc::template rebind<
107 typename remove_const<
108 Value_Type>::type>::other::const_pointer
109 const_reference;
110
111 typedef Metadata_Type metadata_type;
112
113 typedef
114 typename _Alloc::template rebind<
115 metadata_type>::other::const_reference
116 metadata_const_reference;
117
118 public:
119 inline
120 ov_tree_node_const_it_(const_pointer p_nd = 0, const_pointer p_begin_nd = 0, const_pointer p_end_nd = 0, const_metadata_pointer p_metadata = 0) : m_p_value(const_cast<pointer>(p_nd)), m_p_begin_value(const_cast<pointer>(p_begin_nd)), m_p_end_value(const_cast<pointer>(p_end_nd)), m_p_metadata(p_metadata)
121 { }
122
123 inline const_reference
124 operator*() const
125 { return m_p_value; }
126
127 inline metadata_const_reference
128 get_metadata() const
129 {
130 enum
131 {
132 has_metadata = !is_same<Metadata_Type, null_type>::value
133 };
134
135 PB_DS_STATIC_ASSERT(should_have_metadata, has_metadata);
136 _GLIBCXX_DEBUG_ASSERT(m_p_metadata != 0);
137 return *m_p_metadata;
138 }
139
140 inline this_type
141 get_l_child() const
142 {
143 if (m_p_begin_value == m_p_value)
144 return (this_type(m_p_begin_value, m_p_begin_value, m_p_begin_value));
145
146 const_metadata_pointer p_begin_metadata =
147 m_p_metadata - (m_p_value - m_p_begin_value);
148
149 return (this_type(mid_pointer(m_p_begin_value, m_p_value),
150 m_p_begin_value,
151 m_p_value,
152 mid_pointer(p_begin_metadata, m_p_metadata)));
153 }
154
155 inline this_type
156 get_r_child() const
157 {
158 if (m_p_value == m_p_end_value)
159 return (this_type(m_p_end_value, m_p_end_value, m_p_end_value));
160
161 const_metadata_pointer p_end_metadata =
162 m_p_metadata + (m_p_end_value - m_p_value);
163
164 return (this_type(mid_pointer(m_p_value + 1, m_p_end_value),
165 m_p_value + 1,
166 m_p_end_value,(m_p_metadata == 0) ?
167 0 : mid_pointer(m_p_metadata + 1, p_end_metadata)));
168 }
169
170 inline bool
171 operator==(const this_type& other) const
172 {
173 const bool is_end = m_p_begin_value == m_p_end_value;
174 const bool is_other_end = other.m_p_begin_value == other.m_p_end_value;
175
176 if (is_end)
177 return (is_other_end);
178
179 if (is_other_end)
180 return (is_end);
181
182 return m_p_value == other.m_p_value;
183 }
184
185 inline bool
186 operator!=(const this_type& other) const
187 { return !operator==(other); }
188
189 public:
190 pointer m_p_value;
191 pointer m_p_begin_value;
192 pointer m_p_end_value;
193
194 const_metadata_pointer m_p_metadata;
195 };
196
197 #define PB_DS_OV_TREE_NODE_ITERATOR_C_DEC \
198 ov_tree_node_it_<Value_Type, Metadata_Type, _Alloc>
199
200 /// Node reference.
201 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
202 class ov_tree_node_it_ : public PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
203 {
204
205 private:
206 typedef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC this_type;
207
208 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC base_type;
209
210 typedef typename base_type::pointer pointer;
211
212 typedef typename base_type::const_pointer const_pointer;
213
214 typedef
215 typename base_type::const_metadata_pointer
216 const_metadata_pointer;
217
218 public:
219
220 typedef trivial_iterator_tag iterator_category;
221
222 typedef trivial_iterator_difference_type difference_type;
223
224 typedef
225 typename _Alloc::template rebind<
226 Value_Type>::other::pointer
227 value_type;
228
229 typedef
230 typename _Alloc::template rebind<
231 typename remove_const<
232 Value_Type>::type>::other::pointer
233 reference;
234
235 typedef
236 typename _Alloc::template rebind<
237 typename remove_const<
238 Value_Type>::type>::other::pointer
239 const_reference;
240
241 public:
242 inline
243 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)
244 { }
245
246 // Access.
247 inline reference
248 operator*() const
249 { return reference(base_type::m_p_value); }
250
251 // Returns the node reference associated with the left node.
252 inline ov_tree_node_it_
253 get_l_child() const
254 {
255 if (base_type::m_p_begin_value == base_type::m_p_value)
256 return (this_type(base_type::m_p_begin_value, base_type::m_p_begin_value, base_type::m_p_begin_value));
257
258 const_metadata_pointer p_begin_metadata =
259 base_type::m_p_metadata - (base_type::m_p_value - base_type::m_p_begin_value);
260
261 return (this_type(base_type::mid_pointer(base_type::m_p_begin_value, base_type::m_p_value),
262 base_type::m_p_begin_value,
263 base_type::m_p_value,
264 base_type::mid_pointer(p_begin_metadata, base_type::m_p_metadata)));
265 }
266
267 // Returns the node reference associated with the right node.
268 inline ov_tree_node_it_
269 get_r_child() const
270 {
271 if (base_type::m_p_value == base_type::m_p_end_value)
272 return this_type(base_type::m_p_end_value,
273 base_type::m_p_end_value,
274 base_type::m_p_end_value);
275
276 const_metadata_pointer p_end_metadata =
277 base_type::m_p_metadata + (base_type::m_p_end_value - base_type::m_p_value);
278
279 return (this_type(base_type::mid_pointer(base_type::m_p_value + 1, base_type::m_p_end_value),
280 base_type::m_p_value + 1,
281 base_type::m_p_end_value,(base_type::m_p_metadata == 0)?
282 0 : base_type::mid_pointer(base_type::m_p_metadata + 1, p_end_metadata)));
283 }
284
285 };
286
287 #undef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC
288 #undef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
289
290 } // namespace detail
291 } // namespace __gnu_pbds
292
293 #endif