]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/pb_ds/detail/ov_tree_map_/node_iterators.hpp
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/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, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
10 // version.
11
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
35 // warranty.
36
37 /**
38 * @file ov_tree_map_/node_iterators.hpp
39 * Contains an implementation class for ov_tree_.
40 */
41
42 #ifndef PB_DS_OV_TREE_NODE_ITERATORS_HPP
43 #define PB_DS_OV_TREE_NODE_ITERATORS_HPP
44
45 #include <ext/pb_ds/tag_and_trait.hpp>
46 #include <ext/pb_ds/detail/type_utils.hpp>
47 #include <debug/debug.h>
48
49 namespace __gnu_pbds
50 {
51 namespace detail
52 {
53 #define PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC \
54 ov_tree_node_const_it_<Value_Type, Metadata_Type, _Alloc>
55
56 /// Const node reference.
57 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
58 class ov_tree_node_const_it_
59 {
60
61 protected:
62 typedef
63 typename _Alloc::template rebind<
64 Value_Type>::other::pointer
65 pointer;
66
67 typedef
68 typename _Alloc::template rebind<
69 Value_Type>::other::const_pointer
70 const_pointer;
71
72 typedef
73 typename _Alloc::template rebind<
74 Metadata_Type>::other::const_pointer
75 const_metadata_pointer;
76
77 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC this_type;
78
79 protected:
80
81 template<typename Ptr>
82 inline static Ptr
83 mid_pointer(Ptr p_begin, Ptr p_end)
84 {
85 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
86 return (p_begin + (p_end - p_begin) / 2);
87 }
88
89 public:
90
91 typedef trivial_iterator_tag iterator_category;
92
93 typedef trivial_iterator_difference_type difference_type;
94
95 typedef
96 typename _Alloc::template rebind<
97 Value_Type>::other::const_pointer
98 value_type;
99
100 typedef
101 typename _Alloc::template rebind<
102 typename remove_const<
103 Value_Type>::type>::other::const_pointer
104 reference;
105
106 typedef
107 typename _Alloc::template rebind<
108 typename remove_const<
109 Value_Type>::type>::other::const_pointer
110 const_reference;
111
112 typedef Metadata_Type metadata_type;
113
114 typedef
115 typename _Alloc::template rebind<
116 metadata_type>::other::const_reference
117 metadata_const_reference;
118
119 public:
120 inline
121 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)
122 { }
123
124 inline const_reference
125 operator*() const
126 { return m_p_value; }
127
128 inline metadata_const_reference
129 get_metadata() const
130 {
131 enum
132 {
133 has_metadata = !is_same<Metadata_Type, null_type>::value
134 };
135
136 PB_DS_STATIC_ASSERT(should_have_metadata, has_metadata);
137 _GLIBCXX_DEBUG_ASSERT(m_p_metadata != 0);
138 return *m_p_metadata;
139 }
140
141 /// Returns the node iterator associated with the left node.
142 inline this_type
143 get_l_child() const
144 {
145 if (m_p_begin_value == m_p_value)
146 return (this_type(m_p_begin_value, m_p_begin_value, m_p_begin_value));
147
148 const_metadata_pointer p_begin_metadata =
149 m_p_metadata - (m_p_value - m_p_begin_value);
150
151 return (this_type(mid_pointer(m_p_begin_value, m_p_value),
152 m_p_begin_value,
153 m_p_value,
154 mid_pointer(p_begin_metadata, m_p_metadata)));
155 }
156
157 /// Returns the node iterator associated with the right node.
158 inline this_type
159 get_r_child() const
160 {
161 if (m_p_value == m_p_end_value)
162 return (this_type(m_p_end_value, m_p_end_value, m_p_end_value));
163
164 const_metadata_pointer p_end_metadata =
165 m_p_metadata + (m_p_end_value - m_p_value);
166
167 return (this_type(mid_pointer(m_p_value + 1, m_p_end_value),
168 m_p_value + 1,
169 m_p_end_value,(m_p_metadata == 0) ?
170 0 : mid_pointer(m_p_metadata + 1, p_end_metadata)));
171 }
172
173 inline bool
174 operator==(const this_type& other) const
175 {
176 const bool is_end = m_p_begin_value == m_p_end_value;
177 const bool is_other_end = other.m_p_begin_value == other.m_p_end_value;
178
179 if (is_end)
180 return (is_other_end);
181
182 if (is_other_end)
183 return (is_end);
184
185 return m_p_value == other.m_p_value;
186 }
187
188 inline bool
189 operator!=(const this_type& other) const
190 { return !operator==(other); }
191
192 public:
193 pointer m_p_value;
194 pointer m_p_begin_value;
195 pointer m_p_end_value;
196
197 const_metadata_pointer m_p_metadata;
198 };
199
200 #define PB_DS_OV_TREE_NODE_ITERATOR_C_DEC \
201 ov_tree_node_it_<Value_Type, Metadata_Type, _Alloc>
202
203 /// Node reference.
204 template<typename Value_Type, typename Metadata_Type, typename _Alloc>
205 class ov_tree_node_it_ : public PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
206 {
207 private:
208 typedef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC this_type;
209
210 typedef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC base_type;
211
212 typedef typename base_type::pointer pointer;
213
214 typedef typename base_type::const_pointer const_pointer;
215
216 typedef
217 typename base_type::const_metadata_pointer
218 const_metadata_pointer;
219
220 public:
221 typedef trivial_iterator_tag iterator_category;
222
223 typedef trivial_iterator_difference_type difference_type;
224
225 typedef
226 typename _Alloc::template rebind<
227 Value_Type>::other::pointer
228 value_type;
229
230 typedef
231 typename _Alloc::template rebind<
232 typename remove_const<
233 Value_Type>::type>::other::pointer
234 reference;
235
236 typedef
237 typename _Alloc::template rebind<
238 typename remove_const<
239 Value_Type>::type>::other::pointer
240 const_reference;
241
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, base_type::m_p_end_value,
273 base_type::m_p_end_value);
274
275 const_metadata_pointer p_end_metadata =
276 base_type::m_p_metadata + (base_type::m_p_end_value - base_type::m_p_value);
277
278 return (this_type(base_type::mid_pointer(base_type::m_p_value + 1, base_type::m_p_end_value),
279 base_type::m_p_value + 1,
280 base_type::m_p_end_value,(base_type::m_p_metadata == 0)?
281 0 : base_type::mid_pointer(base_type::m_p_metadata + 1, p_end_metadata)));
282 }
283
284 };
285
286 #undef PB_DS_OV_TREE_NODE_ITERATOR_C_DEC
287 #undef PB_DS_OV_TREE_CONST_NODE_ITERATOR_C_DEC
288
289 } // namespace detail
290 } // namespace __gnu_pbds
291
292 #endif