]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/point_iterators.hpp
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / point_iterators.hpp
CommitLineData
36fed23c 1// -*- C++ -*-
2
fbd26352 3// Copyright (C) 2005-2019 Free Software Foundation, Inc.
36fed23c 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
6bc9506f 8// Foundation; either version 3, or (at your option) any later
36fed23c 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
6bc9506f 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/>.
36fed23c 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/**
4f4a327e 37 * @file bin_search_tree_/point_iterators.hpp
36fed23c 38 * Contains an implementation class for bin_search_tree_.
39 */
40
41#ifndef PB_DS_BIN_SEARCH_TREE_FIND_ITERATORS_HPP
42#define PB_DS_BIN_SEARCH_TREE_FIND_ITERATORS_HPP
43
36fed23c 44#include <ext/pb_ds/tag_and_trait.hpp>
2b31bec4 45#include <debug/debug.h>
36fed23c 46
b34535d7 47namespace __gnu_pbds
36fed23c 48{
49 namespace detail
50 {
51
52#define PB_DS_TREE_CONST_IT_C_DEC \
53 bin_search_tree_const_it_< \
54 Node_Pointer, \
55 Value_Type, \
56 Pointer, \
57 Const_Pointer, \
58 Reference, \
59 Const_Reference, \
60 Is_Forward_Iterator, \
4f4a327e 61 _Alloc>
36fed23c 62
63#define PB_DS_TREE_CONST_ODIR_IT_C_DEC \
64 bin_search_tree_const_it_< \
65 Node_Pointer, \
66 Value_Type, \
67 Pointer, \
68 Const_Pointer, \
69 Reference, \
70 Const_Reference, \
71 !Is_Forward_Iterator, \
4f4a327e 72 _Alloc>
36fed23c 73
74#define PB_DS_TREE_IT_C_DEC \
75 bin_search_tree_it_< \
76 Node_Pointer, \
77 Value_Type, \
78 Pointer, \
79 Const_Pointer, \
80 Reference, \
81 Const_Reference, \
82 Is_Forward_Iterator, \
4f4a327e 83 _Alloc>
36fed23c 84
85#define PB_DS_TREE_ODIR_IT_C_DEC \
86 bin_search_tree_it_< \
87 Node_Pointer, \
88 Value_Type, \
89 Pointer, \
90 Const_Pointer, \
91 Reference, \
92 Const_Reference, \
93 !Is_Forward_Iterator, \
4f4a327e 94 _Alloc>
36fed23c 95
4f4a327e 96 /// Const iterator.
36fed23c 97 template<typename Node_Pointer,
98 typename Value_Type,
99 typename Pointer,
100 typename Const_Pointer,
101 typename Reference,
102 typename Const_Reference,
103 bool Is_Forward_Iterator,
4f4a327e 104 typename _Alloc>
36fed23c 105 class bin_search_tree_const_it_
106 {
36fed23c 107 public:
4f4a327e 108 typedef std::bidirectional_iterator_tag iterator_category;
109 typedef typename _Alloc::difference_type difference_type;
110 typedef Value_Type value_type;
111 typedef Pointer pointer;
112 typedef Const_Pointer const_pointer;
113 typedef Reference reference;
114 typedef Const_Reference const_reference;
36fed23c 115
116 inline
e4bb1925 117 bin_search_tree_const_it_(const Node_Pointer p_nd = 0)
2b31bec4 118 : m_p_nd(const_cast<Node_Pointer>(p_nd))
36fed23c 119 { }
120
121 inline
2b31bec4 122 bin_search_tree_const_it_(const PB_DS_TREE_CONST_ODIR_IT_C_DEC& other)
123 : m_p_nd(other.m_p_nd)
36fed23c 124 { }
125
126 inline
127 PB_DS_TREE_CONST_IT_C_DEC&
2b31bec4 128 operator=(const PB_DS_TREE_CONST_IT_C_DEC& other)
36fed23c 129 {
130 m_p_nd = other.m_p_nd;
2b31bec4 131 return *this;
36fed23c 132 }
133
134 inline
135 PB_DS_TREE_CONST_IT_C_DEC&
2b31bec4 136 operator=(const PB_DS_TREE_CONST_ODIR_IT_C_DEC& other)
36fed23c 137 {
138 m_p_nd = other.m_p_nd;
2b31bec4 139 return *this;
36fed23c 140 }
141
142 inline const_pointer
143 operator->() const
144 {
e4bb1925 145 _GLIBCXX_DEBUG_ASSERT(m_p_nd != 0);
2b31bec4 146 return &m_p_nd->m_value;
36fed23c 147 }
148
149 inline const_reference
150 operator*() const
151 {
e4bb1925 152 _GLIBCXX_DEBUG_ASSERT(m_p_nd != 0);
2b31bec4 153 return m_p_nd->m_value;
36fed23c 154 }
155
156 inline bool
2b31bec4 157 operator==(const PB_DS_TREE_CONST_IT_C_DEC & other) const
158 { return m_p_nd == other.m_p_nd; }
36fed23c 159
160 inline bool
2b31bec4 161 operator==(const PB_DS_TREE_CONST_ODIR_IT_C_DEC & other) const
162 { return m_p_nd == other.m_p_nd; }
36fed23c 163
164 inline bool
2b31bec4 165 operator!=(const PB_DS_TREE_CONST_IT_C_DEC& other) const
166 { return m_p_nd != other.m_p_nd; }
36fed23c 167
168 inline bool
2b31bec4 169 operator!=(const PB_DS_TREE_CONST_ODIR_IT_C_DEC& other) const
170 { return m_p_nd != other.m_p_nd; }
36fed23c 171
172 inline PB_DS_TREE_CONST_IT_C_DEC&
173 operator++()
174 {
e4bb1925 175 _GLIBCXX_DEBUG_ASSERT(m_p_nd != 0);
36fed23c 176 inc(integral_constant<int,Is_Forward_Iterator>());
2b31bec4 177 return *this;
36fed23c 178 }
179
180 inline PB_DS_TREE_CONST_IT_C_DEC
181 operator++(int)
182 {
2b31bec4 183 PB_DS_TREE_CONST_IT_C_DEC ret_it(m_p_nd);
36fed23c 184 operator++();
2b31bec4 185 return ret_it;
36fed23c 186 }
187
188 inline PB_DS_TREE_CONST_IT_C_DEC&
189 operator--()
190 {
191 dec(integral_constant<int,Is_Forward_Iterator>());
2b31bec4 192 return *this;
36fed23c 193 }
194
195 inline PB_DS_TREE_CONST_IT_C_DEC
196 operator--(int)
197 {
2b31bec4 198 PB_DS_TREE_CONST_IT_C_DEC ret_it(m_p_nd);
36fed23c 199 operator--();
2b31bec4 200 return ret_it;
36fed23c 201 }
202
203 protected:
204 inline void
205 inc(false_type)
2b31bec4 206 { dec(true_type()); }
36fed23c 207
208 void
209 inc(true_type)
210 {
211 if (m_p_nd->special()&&
212 m_p_nd->m_p_parent->m_p_parent == m_p_nd)
213 {
214 m_p_nd = m_p_nd->m_p_left;
36fed23c 215 return;
216 }
217
e4bb1925 218 if (m_p_nd->m_p_right != 0)
36fed23c 219 {
220 m_p_nd = m_p_nd->m_p_right;
e4bb1925 221 while (m_p_nd->m_p_left != 0)
36fed23c 222 m_p_nd = m_p_nd->m_p_left;
36fed23c 223 return;
224 }
225
226 Node_Pointer p_y = m_p_nd->m_p_parent;
36fed23c 227 while (m_p_nd == p_y->m_p_right)
228 {
229 m_p_nd = p_y;
36fed23c 230 p_y = p_y->m_p_parent;
231 }
232
233 if (m_p_nd->m_p_right != p_y)
234 m_p_nd = p_y;
235 }
236
237 inline void
238 dec(false_type)
2b31bec4 239 { inc(true_type()); }
36fed23c 240
241 void
242 dec(true_type)
243 {
2b31bec4 244 if (m_p_nd->special() && m_p_nd->m_p_parent->m_p_parent == m_p_nd)
36fed23c 245 {
246 m_p_nd = m_p_nd->m_p_right;
36fed23c 247 return;
248 }
249
e4bb1925 250 if (m_p_nd->m_p_left != 0)
36fed23c 251 {
252 Node_Pointer p_y = m_p_nd->m_p_left;
e4bb1925 253 while (p_y->m_p_right != 0)
36fed23c 254 p_y = p_y->m_p_right;
36fed23c 255 m_p_nd = p_y;
36fed23c 256 return;
257 }
258
259 Node_Pointer p_y = m_p_nd->m_p_parent;
36fed23c 260 while (m_p_nd == p_y->m_p_left)
261 {
262 m_p_nd = p_y;
36fed23c 263 p_y = p_y->m_p_parent;
264 }
36fed23c 265 if (m_p_nd->m_p_left != p_y)
266 m_p_nd = p_y;
267 }
268
269 public:
270 Node_Pointer m_p_nd;
271 };
272
4f4a327e 273 /// Iterator.
36fed23c 274 template<typename Node_Pointer,
275 typename Value_Type,
276 typename Pointer,
277 typename Const_Pointer,
278 typename Reference,
279 typename Const_Reference,
280 bool Is_Forward_Iterator,
4f4a327e 281 typename _Alloc>
282 class bin_search_tree_it_ : public PB_DS_TREE_CONST_IT_C_DEC
36fed23c 283 {
36fed23c 284 public:
36fed23c 285 inline
e4bb1925 286 bin_search_tree_it_(const Node_Pointer p_nd = 0)
2b31bec4 287 : PB_DS_TREE_CONST_IT_C_DEC((Node_Pointer)p_nd)
36fed23c 288 { }
289
290 inline
2b31bec4 291 bin_search_tree_it_(const PB_DS_TREE_ODIR_IT_C_DEC& other)
292 : PB_DS_TREE_CONST_IT_C_DEC(other.m_p_nd)
36fed23c 293 { }
294
295 inline
296 PB_DS_TREE_IT_C_DEC&
297 operator=(const PB_DS_TREE_IT_C_DEC& other)
298 {
299 base_it_type::m_p_nd = other.m_p_nd;
2b31bec4 300 return *this;
36fed23c 301 }
302
303 inline
304 PB_DS_TREE_IT_C_DEC&
305 operator=(const PB_DS_TREE_ODIR_IT_C_DEC& other)
306 {
307 base_it_type::m_p_nd = other.m_p_nd;
2b31bec4 308 return *this;
36fed23c 309 }
310
311 inline typename PB_DS_TREE_CONST_IT_C_DEC::pointer
312 operator->() const
313 {
e4bb1925 314 _GLIBCXX_DEBUG_ASSERT(base_it_type::m_p_nd != 0);
2b31bec4 315 return &base_it_type::m_p_nd->m_value;
36fed23c 316 }
317
318 inline typename PB_DS_TREE_CONST_IT_C_DEC::reference
319 operator*() const
320 {
e4bb1925 321 _GLIBCXX_DEBUG_ASSERT(base_it_type::m_p_nd != 0);
2b31bec4 322 return base_it_type::m_p_nd->m_value;
36fed23c 323 }
324
325 inline PB_DS_TREE_IT_C_DEC&
326 operator++()
327 {
2b31bec4 328 PB_DS_TREE_CONST_IT_C_DEC:: operator++();
329 return *this;
36fed23c 330 }
331
332 inline PB_DS_TREE_IT_C_DEC
333 operator++(int)
334 {
2b31bec4 335 PB_DS_TREE_IT_C_DEC ret_it(base_it_type::m_p_nd);
36fed23c 336 operator++();
2b31bec4 337 return ret_it;
36fed23c 338 }
339
340 inline PB_DS_TREE_IT_C_DEC&
341 operator--()
342 {
2b31bec4 343 PB_DS_TREE_CONST_IT_C_DEC:: operator--();
344 return *this;
36fed23c 345 }
346
347 inline PB_DS_TREE_IT_C_DEC
348 operator--(int)
349 {
2b31bec4 350 PB_DS_TREE_IT_C_DEC ret_it(base_it_type::m_p_nd);
36fed23c 351 operator--();
2b31bec4 352 return ret_it;
36fed23c 353 }
354
355 protected:
356 typedef PB_DS_TREE_CONST_IT_C_DEC base_it_type;
357 };
358
359#undef PB_DS_TREE_CONST_IT_C_DEC
36fed23c 360#undef PB_DS_TREE_CONST_ODIR_IT_C_DEC
36fed23c 361#undef PB_DS_TREE_IT_C_DEC
36fed23c 362#undef PB_DS_TREE_ODIR_IT_C_DEC
363
36fed23c 364 } // namespace detail
b34535d7 365} // namespace __gnu_pbds
36fed23c 366
2b31bec4 367#endif