]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/intrusive/list.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / intrusive / list.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga 2006-2014
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/intrusive for documentation.
11 //
12 /////////////////////////////////////////////////////////////////////////////
13
14 #ifndef BOOST_INTRUSIVE_LIST_HPP
15 #define BOOST_INTRUSIVE_LIST_HPP
16
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/intrusive/intrusive_fwd.hpp>
19 #include <boost/intrusive/detail/assert.hpp>
20 #include <boost/intrusive/list_hook.hpp>
21 #include <boost/intrusive/circular_list_algorithms.hpp>
22 #include <boost/intrusive/pointer_traits.hpp>
23 #include <boost/intrusive/detail/mpl.hpp>
24 #include <boost/intrusive/link_mode.hpp>
25 #include <boost/intrusive/detail/get_value_traits.hpp>
26 #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
27 #include <boost/intrusive/detail/default_header_holder.hpp>
28 #include <boost/intrusive/detail/reverse_iterator.hpp>
29 #include <boost/intrusive/detail/uncast.hpp>
30 #include <boost/intrusive/detail/list_iterator.hpp>
31 #include <boost/intrusive/detail/array_initializer.hpp>
32 #include <boost/intrusive/detail/exception_disposer.hpp>
33 #include <boost/intrusive/detail/equal_to_value.hpp>
34 #include <boost/intrusive/detail/key_nodeptr_comp.hpp>
35 #include <boost/intrusive/detail/simple_disposers.hpp>
36 #include <boost/intrusive/detail/size_holder.hpp>
37 #include <boost/intrusive/detail/algorithm.hpp>
38
39 #include <boost/move/utility_core.hpp>
40 #include <boost/static_assert.hpp>
41
42 #include <boost/intrusive/detail/value_functors.hpp>
43 #include <cstddef> //std::size_t, etc.
44
45 #if defined(BOOST_HAS_PRAGMA_ONCE)
46 # pragma once
47 #endif
48
49 namespace boost {
50 namespace intrusive {
51
52 /// @cond
53
54 struct default_list_hook_applier
55 { template <class T> struct apply{ typedef typename T::default_list_hook type; }; };
56
57 template<>
58 struct is_default_hook_tag<default_list_hook_applier>
59 { static const bool value = true; };
60
61 struct list_defaults
62 {
63 typedef default_list_hook_applier proto_value_traits;
64 static const bool constant_time_size = true;
65 typedef std::size_t size_type;
66 typedef void header_holder_type;
67 };
68
69 /// @endcond
70
71 //! The class template list is an intrusive container that mimics most of the
72 //! interface of std::list as described in the C++ standard.
73 //!
74 //! The template parameter \c T is the type to be managed by the container.
75 //! The user can specify additional options and if no options are provided
76 //! default options are used.
77 //!
78 //! The container supports the following options:
79 //! \c base_hook<>/member_hook<>/value_traits<>,
80 //! \c constant_time_size<> and \c size_type<>.
81 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
82 template<class T, class ...Options>
83 #else
84 template <class ValueTraits, class SizeType, bool ConstantTimeSize, typename HeaderHolder>
85 #endif
86 class list_impl
87 {
88 //Public typedefs
89 public:
90 typedef ValueTraits value_traits;
91 typedef typename value_traits::pointer pointer;
92 typedef typename value_traits::const_pointer const_pointer;
93 typedef typename pointer_traits<pointer>::element_type value_type;
94 typedef typename pointer_traits<pointer>::reference reference;
95 typedef typename pointer_traits<const_pointer>::reference const_reference;
96 typedef typename pointer_traits<pointer>::difference_type difference_type;
97 typedef SizeType size_type;
98 typedef list_iterator<value_traits, false> iterator;
99 typedef list_iterator<value_traits, true> const_iterator;
100 typedef boost::intrusive::reverse_iterator<iterator> reverse_iterator;
101 typedef boost::intrusive::reverse_iterator<const_iterator> const_reverse_iterator;
102 typedef typename value_traits::node_traits node_traits;
103 typedef typename node_traits::node node;
104 typedef typename node_traits::node_ptr node_ptr;
105 typedef typename node_traits::const_node_ptr const_node_ptr;
106 typedef circular_list_algorithms<node_traits> node_algorithms;
107 typedef typename detail::get_header_holder_type
108 < value_traits, HeaderHolder >::type header_holder_type;
109
110 static const bool constant_time_size = ConstantTimeSize;
111 static const bool stateful_value_traits = detail::is_stateful_value_traits<value_traits>::value;
112 static const bool has_container_from_iterator =
113 detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value;
114
115 /// @cond
116
117 private:
118 typedef detail::size_holder<constant_time_size, size_type> size_traits;
119
120 //noncopyable
121 BOOST_MOVABLE_BUT_NOT_COPYABLE(list_impl)
122
123 static const bool safemode_or_autounlink = is_safe_autounlink<value_traits::link_mode>::value;
124
125 //Constant-time size is incompatible with auto-unlink hooks!
126 BOOST_STATIC_ASSERT(!(constant_time_size &&
127 ((int)value_traits::link_mode == (int)auto_unlink)
128 ));
129
130 node_ptr get_root_node()
131 { return data_.root_plus_size_.m_header.get_node(); }
132
133 const_node_ptr get_root_node() const
134 { return data_.root_plus_size_.m_header.get_node(); }
135
136 struct root_plus_size : public size_traits
137 {
138 header_holder_type m_header;
139 };
140
141 struct data_t : public value_traits
142 {
143 typedef typename list_impl::value_traits value_traits;
144 explicit data_t(const value_traits &val_traits)
145 : value_traits(val_traits)
146 {}
147
148 root_plus_size root_plus_size_;
149 } data_;
150
151 BOOST_INTRUSIVE_FORCEINLINE size_traits &priv_size_traits() BOOST_NOEXCEPT
152 { return data_.root_plus_size_; }
153
154 BOOST_INTRUSIVE_FORCEINLINE const size_traits &priv_size_traits() const BOOST_NOEXCEPT
155 { return data_.root_plus_size_; }
156
157 BOOST_INTRUSIVE_FORCEINLINE const value_traits &priv_value_traits() const BOOST_NOEXCEPT
158 { return data_; }
159
160 BOOST_INTRUSIVE_FORCEINLINE value_traits &priv_value_traits() BOOST_NOEXCEPT
161 { return data_; }
162
163 typedef typename boost::intrusive::value_traits_pointers
164 <ValueTraits>::const_value_traits_ptr const_value_traits_ptr;
165
166 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr priv_value_traits_ptr() const BOOST_NOEXCEPT
167 { return pointer_traits<const_value_traits_ptr>::pointer_to(this->priv_value_traits()); }
168
169 /// @endcond
170
171 public:
172
173 //! <b>Effects</b>: constructs an empty list.
174 //!
175 //! <b>Complexity</b>: Constant
176 //!
177 //! <b>Throws</b>: If value_traits::node_traits::node
178 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
179 list_impl()
180 : data_(value_traits())
181 {
182 this->priv_size_traits().set_size(size_type(0));
183 node_algorithms::init_header(this->get_root_node());
184 }
185
186 //! <b>Effects</b>: constructs an empty list.
187 //!
188 //! <b>Complexity</b>: Constant
189 //!
190 //! <b>Throws</b>: If value_traits::node_traits::node
191 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
192 explicit list_impl(const value_traits &v_traits)
193 : data_(v_traits)
194 {
195 this->priv_size_traits().set_size(size_type(0));
196 node_algorithms::init_header(this->get_root_node());
197 }
198
199 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
200 //!
201 //! <b>Effects</b>: Constructs a list equal to the range [first,last).
202 //!
203 //! <b>Complexity</b>: Linear in distance(b, e). No copy constructors are called.
204 //!
205 //! <b>Throws</b>: If value_traits::node_traits::node
206 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
207 template<class Iterator>
208 list_impl(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
209 : data_(v_traits)
210 {
211 //nothrow, no need to rollback to release elements on exception
212 this->priv_size_traits().set_size(size_type(0));
213 node_algorithms::init_header(this->get_root_node());
214 //nothrow, no need to rollback to release elements on exception
215 this->insert(this->cend(), b, e);
216 }
217
218 //! <b>Effects</b>: Constructs a container moving resources from another container.
219 //! Internal value traits are move constructed and
220 //! nodes belonging to x (except the node representing the "end") are linked to *this.
221 //!
222 //! <b>Complexity</b>: Constant.
223 //!
224 //! <b>Throws</b>: If value_traits::node_traits::node's
225 //! move constructor throws (this does not happen with predefined Boost.Intrusive hooks)
226 //! or the move constructor of value traits throws.
227 list_impl(BOOST_RV_REF(list_impl) x)
228 : data_(::boost::move(x.priv_value_traits()))
229 {
230 this->priv_size_traits().set_size(size_type(0));
231 node_algorithms::init_header(this->get_root_node());
232 //nothrow, no need to rollback to release elements on exception
233 this->swap(x);
234 }
235
236 //! <b>Effects</b>: Equivalent to swap
237 //!
238 list_impl& operator=(BOOST_RV_REF(list_impl) x)
239 { this->swap(x); return *this; }
240
241 //! <b>Effects</b>: If it's not a safe-mode or an auto-unlink value_type
242 //! the destructor does nothing
243 //! (ie. no code is generated). Otherwise it detaches all elements from this.
244 //! In this case the objects in the list are not deleted (i.e. no destructors
245 //! are called), but the hooks according to the ValueTraits template parameter
246 //! are set to their default value.
247 //!
248 //! <b>Throws</b>: Nothing.
249 //!
250 //! <b>Complexity</b>: Linear to the number of elements in the list, if
251 //! it's a safe-mode or auto-unlink value . Otherwise constant.
252 ~list_impl()
253 {
254 BOOST_IF_CONSTEXPR(is_safe_autounlink<ValueTraits::link_mode>::value){
255 this->clear();
256 node_algorithms::init(this->get_root_node());
257 }
258 }
259
260 //! <b>Requires</b>: value must be an lvalue.
261 //!
262 //! <b>Effects</b>: Inserts the value in the back of the list.
263 //! No copy constructors are called.
264 //!
265 //! <b>Throws</b>: Nothing.
266 //!
267 //! <b>Complexity</b>: Constant.
268 //!
269 //! <b>Note</b>: Does not affect the validity of iterators and references.
270 void push_back(reference value) BOOST_NOEXCEPT
271 {
272 node_ptr to_insert = priv_value_traits().to_node_ptr(value);
273 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
274 node_algorithms::link_before(this->get_root_node(), to_insert);
275 this->priv_size_traits().increment();
276 }
277
278 //! <b>Requires</b>: value must be an lvalue.
279 //!
280 //! <b>Effects</b>: Inserts the value in the front of the list.
281 //! No copy constructors are called.
282 //!
283 //! <b>Throws</b>: Nothing.
284 //!
285 //! <b>Complexity</b>: Constant.
286 //!
287 //! <b>Note</b>: Does not affect the validity of iterators and references.
288 void push_front(reference value) BOOST_NOEXCEPT
289 {
290 node_ptr to_insert = priv_value_traits().to_node_ptr(value);
291 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
292 node_algorithms::link_before(node_traits::get_next(this->get_root_node()), to_insert);
293 this->priv_size_traits().increment();
294 }
295
296 //! <b>Effects</b>: Erases the last element of the list.
297 //! No destructors are called.
298 //!
299 //! <b>Throws</b>: Nothing.
300 //!
301 //! <b>Complexity</b>: Constant.
302 //!
303 //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
304 void pop_back() BOOST_NOEXCEPT
305 { return this->pop_back_and_dispose(detail::null_disposer()); }
306
307 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
308 //!
309 //! <b>Effects</b>: Erases the last element of the list.
310 //! No destructors are called.
311 //! Disposer::operator()(pointer) is called for the removed element.
312 //!
313 //! <b>Throws</b>: Nothing.
314 //!
315 //! <b>Complexity</b>: Constant.
316 //!
317 //! <b>Note</b>: Invalidates the iterators to the erased element.
318 template<class Disposer>
319 void pop_back_and_dispose(Disposer disposer) BOOST_NOEXCEPT
320 {
321 node_ptr to_erase = node_traits::get_previous(this->get_root_node());
322 node_algorithms::unlink(to_erase);
323 this->priv_size_traits().decrement();
324 BOOST_IF_CONSTEXPR(safemode_or_autounlink)
325 node_algorithms::init(to_erase);
326 disposer(priv_value_traits().to_value_ptr(to_erase));
327 }
328
329 //! <b>Effects</b>: Erases the first element of the list.
330 //! No destructors are called.
331 //!
332 //! <b>Throws</b>: Nothing.
333 //!
334 //! <b>Complexity</b>: Constant.
335 //!
336 //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
337 void pop_front() BOOST_NOEXCEPT
338 { return this->pop_front_and_dispose(detail::null_disposer()); }
339
340 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
341 //!
342 //! <b>Effects</b>: Erases the first element of the list.
343 //! No destructors are called.
344 //! Disposer::operator()(pointer) is called for the removed element.
345 //!
346 //! <b>Throws</b>: Nothing.
347 //!
348 //! <b>Complexity</b>: Constant.
349 //!
350 //! <b>Note</b>: Invalidates the iterators to the erased element.
351 template<class Disposer>
352 void pop_front_and_dispose(Disposer disposer) BOOST_NOEXCEPT
353 {
354 node_ptr to_erase = node_traits::get_next(this->get_root_node());
355 node_algorithms::unlink(to_erase);
356 this->priv_size_traits().decrement();
357 BOOST_IF_CONSTEXPR(safemode_or_autounlink)
358 node_algorithms::init(to_erase);
359 disposer(priv_value_traits().to_value_ptr(to_erase));
360 }
361
362 //! <b>Effects</b>: Returns a reference to the first element of the list.
363 //!
364 //! <b>Throws</b>: Nothing.
365 //!
366 //! <b>Complexity</b>: Constant.
367 BOOST_INTRUSIVE_FORCEINLINE reference front() BOOST_NOEXCEPT
368 { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
369
370 //! <b>Effects</b>: Returns a const_reference to the first element of the list.
371 //!
372 //! <b>Throws</b>: Nothing.
373 //!
374 //! <b>Complexity</b>: Constant.
375 BOOST_INTRUSIVE_FORCEINLINE const_reference front() const BOOST_NOEXCEPT
376 { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
377
378 //! <b>Effects</b>: Returns a reference to the last element of the list.
379 //!
380 //! <b>Throws</b>: Nothing.
381 //!
382 //! <b>Complexity</b>: Constant.
383 BOOST_INTRUSIVE_FORCEINLINE reference back() BOOST_NOEXCEPT
384 { return *priv_value_traits().to_value_ptr(node_traits::get_previous(this->get_root_node())); }
385
386 //! <b>Effects</b>: Returns a const_reference to the last element of the list.
387 //!
388 //! <b>Throws</b>: Nothing.
389 //!
390 //! <b>Complexity</b>: Constant.
391 BOOST_INTRUSIVE_FORCEINLINE const_reference back() const BOOST_NOEXCEPT
392 { return *priv_value_traits().to_value_ptr(detail::uncast(node_traits::get_previous(this->get_root_node()))); }
393
394 //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
395 //!
396 //! <b>Throws</b>: Nothing.
397 //!
398 //! <b>Complexity</b>: Constant.
399 BOOST_INTRUSIVE_FORCEINLINE iterator begin() BOOST_NOEXCEPT
400 { return iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
401
402 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
403 //!
404 //! <b>Throws</b>: Nothing.
405 //!
406 //! <b>Complexity</b>: Constant.
407 BOOST_INTRUSIVE_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT
408 { return this->cbegin(); }
409
410 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
411 //!
412 //! <b>Throws</b>: Nothing.
413 //!
414 //! <b>Complexity</b>: Constant.
415 BOOST_INTRUSIVE_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT
416 { return const_iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); }
417
418 //! <b>Effects</b>: Returns an iterator to the end of the list.
419 //!
420 //! <b>Throws</b>: Nothing.
421 //!
422 //! <b>Complexity</b>: Constant.
423 BOOST_INTRUSIVE_FORCEINLINE iterator end() BOOST_NOEXCEPT
424 { return iterator(this->get_root_node(), this->priv_value_traits_ptr()); }
425
426 //! <b>Effects</b>: Returns a const_iterator to the end of the list.
427 //!
428 //! <b>Throws</b>: Nothing.
429 //!
430 //! <b>Complexity</b>: Constant.
431 BOOST_INTRUSIVE_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT
432 { return this->cend(); }
433
434 //! <b>Effects</b>: Returns a constant iterator to the end of the list.
435 //!
436 //! <b>Throws</b>: Nothing.
437 //!
438 //! <b>Complexity</b>: Constant.
439 BOOST_INTRUSIVE_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT
440 { return const_iterator(detail::uncast(this->get_root_node()), this->priv_value_traits_ptr()); }
441
442 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
443 //! of the reversed list.
444 //!
445 //! <b>Throws</b>: Nothing.
446 //!
447 //! <b>Complexity</b>: Constant.
448 BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT
449 { return reverse_iterator(this->end()); }
450
451 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
452 //! of the reversed list.
453 //!
454 //! <b>Throws</b>: Nothing.
455 //!
456 //! <b>Complexity</b>: Constant.
457 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT
458 { return this->crbegin(); }
459
460 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
461 //! of the reversed list.
462 //!
463 //! <b>Throws</b>: Nothing.
464 //!
465 //! <b>Complexity</b>: Constant.
466 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT
467 { return const_reverse_iterator(end()); }
468
469 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
470 //! of the reversed list.
471 //!
472 //! <b>Throws</b>: Nothing.
473 //!
474 //! <b>Complexity</b>: Constant.
475 BOOST_INTRUSIVE_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT
476 { return reverse_iterator(begin()); }
477
478 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
479 //! of the reversed list.
480 //!
481 //! <b>Throws</b>: Nothing.
482 //!
483 //! <b>Complexity</b>: Constant.
484 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT
485 { return this->crend(); }
486
487 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
488 //! of the reversed list.
489 //!
490 //! <b>Throws</b>: Nothing.
491 //!
492 //! <b>Complexity</b>: Constant.
493 BOOST_INTRUSIVE_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT
494 { return const_reverse_iterator(this->begin()); }
495
496 //! <b>Precondition</b>: end_iterator must be a valid end iterator
497 //! of list.
498 //!
499 //! <b>Effects</b>: Returns a const reference to the list associated to the end iterator
500 //!
501 //! <b>Throws</b>: Nothing.
502 //!
503 //! <b>Complexity</b>: Constant.
504 BOOST_INTRUSIVE_FORCEINLINE static list_impl &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
505 { return list_impl::priv_container_from_end_iterator(end_iterator); }
506
507 //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
508 //! of list.
509 //!
510 //! <b>Effects</b>: Returns a const reference to the list associated to the end iterator
511 //!
512 //! <b>Throws</b>: Nothing.
513 //!
514 //! <b>Complexity</b>: Constant.
515 BOOST_INTRUSIVE_FORCEINLINE static const list_impl &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
516 { return list_impl::priv_container_from_end_iterator(end_iterator); }
517
518 //! <b>Effects</b>: Returns the number of the elements contained in the list.
519 //!
520 //! <b>Throws</b>: Nothing.
521 //!
522 //! <b>Complexity</b>: Linear to the number of elements contained in the list.
523 //! if constant-time size option is disabled. Constant time otherwise.
524 //!
525 //! <b>Note</b>: Does not affect the validity of iterators and references.
526 BOOST_INTRUSIVE_FORCEINLINE size_type size() const BOOST_NOEXCEPT
527 {
528 BOOST_IF_CONSTEXPR(constant_time_size)
529 return this->priv_size_traits().get_size();
530 else
531 return node_algorithms::count(this->get_root_node()) - 1;
532 }
533
534 //! <b>Effects</b>: Returns true if the list contains no elements.
535 //!
536 //! <b>Throws</b>: Nothing.
537 //!
538 //! <b>Complexity</b>: Constant.
539 //!
540 //! <b>Note</b>: Does not affect the validity of iterators and references.
541 BOOST_INTRUSIVE_FORCEINLINE bool empty() const BOOST_NOEXCEPT
542 { return node_algorithms::unique(this->get_root_node()); }
543
544 //! <b>Effects</b>: Swaps the elements of x and *this.
545 //!
546 //! <b>Throws</b>: Nothing.
547 //!
548 //! <b>Complexity</b>: Constant.
549 //!
550 //! <b>Note</b>: Does not affect the validity of iterators and references.
551 void swap(list_impl& other) BOOST_NOEXCEPT
552 {
553 node_algorithms::swap_nodes(this->get_root_node(), other.get_root_node());
554 this->priv_size_traits().swap(other.priv_size_traits());
555 }
556
557 //! <b>Effects</b>: Moves backwards all the elements, so that the first
558 //! element becomes the second, the second becomes the third...
559 //! the last element becomes the first one.
560 //!
561 //! <b>Throws</b>: Nothing.
562 //!
563 //! <b>Complexity</b>: Linear to the number of shifts.
564 //!
565 //! <b>Note</b>: Does not affect the validity of iterators and references.
566 BOOST_INTRUSIVE_FORCEINLINE void shift_backwards(size_type n = 1) BOOST_NOEXCEPT
567 { node_algorithms::move_forward(this->get_root_node(), n); }
568
569 //! <b>Effects</b>: Moves forward all the elements, so that the second
570 //! element becomes the first, the third becomes the second...
571 //! the first element becomes the last one.
572 //!
573 //! <b>Throws</b>: Nothing.
574 //!
575 //! <b>Complexity</b>: Linear to the number of shifts.
576 //!
577 //! <b>Note</b>: Does not affect the validity of iterators and references.
578 BOOST_INTRUSIVE_FORCEINLINE void shift_forward(size_type n = 1) BOOST_NOEXCEPT
579 { node_algorithms::move_backwards(this->get_root_node(), n); }
580
581 //! <b>Effects</b>: Erases the element pointed by i of the list.
582 //! No destructors are called.
583 //!
584 //! <b>Returns</b>: the first element remaining beyond the removed element,
585 //! or end() if no such element exists.
586 //!
587 //! <b>Throws</b>: Nothing.
588 //!
589 //! <b>Complexity</b>: Constant.
590 //!
591 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
592 //! erased element.
593 BOOST_INTRUSIVE_FORCEINLINE iterator erase(const_iterator i) BOOST_NOEXCEPT
594 { return this->erase_and_dispose(i, detail::null_disposer()); }
595
596 //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
597 //!
598 //! <b>Effects</b>: Erases the element range pointed by b and e
599 //! No destructors are called.
600 //!
601 //! <b>Returns</b>: the first element remaining beyond the removed elements,
602 //! or end() if no such element exists.
603 //!
604 //! <b>Throws</b>: Nothing.
605 //!
606 //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
607 //! or auto-unlink value, or constant-time size is enabled. Constant-time otherwise.
608 //!
609 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
610 //! erased elements.
611 BOOST_INTRUSIVE_FORCEINLINE iterator erase(const_iterator b, const_iterator e) BOOST_NOEXCEPT
612 {
613 BOOST_IF_CONSTEXPR(safemode_or_autounlink || constant_time_size){
614 return this->erase_and_dispose(b, e, detail::null_disposer());
615 }
616 else{
617 node_algorithms::unlink(b.pointed_node(), e.pointed_node());
618 return e.unconst();
619 }
620 }
621
622 //! <b>Requires</b>: b and e must be valid iterators to elements in *this.
623 //! n must be distance(b, e).
624 //!
625 //! <b>Effects</b>: Erases the element range pointed by b and e
626 //! No destructors are called.
627 //!
628 //! <b>Returns</b>: the first element remaining beyond the removed elements,
629 //! or end() if no such element exists.
630 //!
631 //! <b>Throws</b>: Nothing.
632 //!
633 //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
634 //! or auto-unlink value is enabled. Constant-time otherwise.
635 //!
636 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
637 //! erased elements.
638 iterator erase(const_iterator b, const_iterator e, size_type n) BOOST_NOEXCEPT
639 {
640 BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(b.pointed_node(), e.pointed_node()) == n);
641 (void)n;
642 BOOST_IF_CONSTEXPR(safemode_or_autounlink){
643 return this->erase_and_dispose(b, e, detail::null_disposer());
644 }
645 else{
646 BOOST_IF_CONSTEXPR(constant_time_size){
647 this->priv_size_traits().decrease(n);
648 }
649 node_algorithms::unlink(b.pointed_node(), e.pointed_node());
650 return e.unconst();
651 }
652 }
653
654 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
655 //!
656 //! <b>Effects</b>: Erases the element pointed by i of the list.
657 //! No destructors are called.
658 //! Disposer::operator()(pointer) is called for the removed element.
659 //!
660 //! <b>Returns</b>: the first element remaining beyond the removed element,
661 //! or end() if no such element exists.
662 //!
663 //! <b>Throws</b>: Nothing.
664 //!
665 //! <b>Complexity</b>: Constant.
666 //!
667 //! <b>Note</b>: Invalidates the iterators to the erased element.
668 template <class Disposer>
669 iterator erase_and_dispose(const_iterator i, Disposer disposer) BOOST_NOEXCEPT
670 {
671 node_ptr to_erase(i.pointed_node());
672 ++i;
673 node_algorithms::unlink(to_erase);
674 this->priv_size_traits().decrement();
675 BOOST_IF_CONSTEXPR(safemode_or_autounlink)
676 node_algorithms::init(to_erase);
677 disposer(this->priv_value_traits().to_value_ptr(to_erase));
678 return i.unconst();
679 }
680
681 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
682 template<class Disposer>
683 iterator erase_and_dispose(iterator i, Disposer disposer) BOOST_NOEXCEPT
684 { return this->erase_and_dispose(const_iterator(i), disposer); }
685 #endif
686
687 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
688 //!
689 //! <b>Effects</b>: Erases the element range pointed by b and e
690 //! No destructors are called.
691 //! Disposer::operator()(pointer) is called for the removed elements.
692 //!
693 //! <b>Returns</b>: the first element remaining beyond the removed elements,
694 //! or end() if no such element exists.
695 //!
696 //! <b>Throws</b>: Nothing.
697 //!
698 //! <b>Complexity</b>: Linear to the number of elements erased.
699 //!
700 //! <b>Note</b>: Invalidates the iterators to the erased elements.
701 template <class Disposer>
702 iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) BOOST_NOEXCEPT
703 {
704 node_ptr bp(b.pointed_node()), ep(e.pointed_node());
705 node_algorithms::unlink(bp, ep);
706 while(bp != ep){
707 node_ptr to_erase(bp);
708 bp = node_traits::get_next(bp);
709 BOOST_IF_CONSTEXPR(safemode_or_autounlink)
710 node_algorithms::init(to_erase);
711 disposer(priv_value_traits().to_value_ptr(to_erase));
712 this->priv_size_traits().decrement();
713 }
714 return e.unconst();
715 }
716
717 //! <b>Effects</b>: Erases all the elements of the container.
718 //! No destructors are called.
719 //!
720 //! <b>Throws</b>: Nothing.
721 //!
722 //! <b>Complexity</b>: Linear to the number of elements of the list.
723 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
724 //!
725 //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased elements.
726 void clear() BOOST_NOEXCEPT
727 {
728 BOOST_IF_CONSTEXPR(safemode_or_autounlink){
729 this->clear_and_dispose(detail::null_disposer());
730 }
731 else{
732 node_algorithms::init_header(this->get_root_node());
733 this->priv_size_traits().set_size(size_type(0));
734 }
735 }
736
737 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
738 //!
739 //! <b>Effects</b>: Erases all the elements of the container.
740 //! No destructors are called.
741 //! Disposer::operator()(pointer) is called for the removed elements.
742 //!
743 //! <b>Throws</b>: Nothing.
744 //!
745 //! <b>Complexity</b>: Linear to the number of elements of the list.
746 //!
747 //! <b>Note</b>: Invalidates the iterators to the erased elements.
748 template <class Disposer>
749 void clear_and_dispose(Disposer disposer) BOOST_NOEXCEPT
750 {
751 const_iterator it(this->begin()), itend(this->end());
752 while(it != itend){
753 node_ptr to_erase(it.pointed_node());
754 ++it;
755 BOOST_IF_CONSTEXPR(safemode_or_autounlink)
756 node_algorithms::init(to_erase);
757 disposer(priv_value_traits().to_value_ptr(to_erase));
758 }
759 node_algorithms::init_header(this->get_root_node());
760 this->priv_size_traits().set_size(0);
761 }
762
763 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
764 //! Cloner should yield to nodes equivalent to the original nodes.
765 //!
766 //! <b>Effects</b>: Erases all the elements from *this
767 //! calling Disposer::operator()(pointer), clones all the
768 //! elements from src calling Cloner::operator()(const_reference )
769 //! and inserts them on *this.
770 //!
771 //! If cloner throws, all cloned elements are unlinked and disposed
772 //! calling Disposer::operator()(pointer).
773 //!
774 //! <b>Complexity</b>: Linear to erased plus inserted elements.
775 //!
776 //! <b>Throws</b>: If cloner throws. Basic guarantee.
777 template <class Cloner, class Disposer>
778 void clone_from(const list_impl &src, Cloner cloner, Disposer disposer)
779 {
780 this->clear_and_dispose(disposer);
781 detail::exception_disposer<list_impl, Disposer>
782 rollback(*this, disposer);
783 const_iterator b(src.begin()), e(src.end());
784 for(; b != e; ++b){
785 this->push_back(*cloner(*b));
786 }
787 rollback.release();
788 }
789
790 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
791 //! Cloner should yield to nodes equivalent to the original nodes.
792 //!
793 //! <b>Effects</b>: Erases all the elements from *this
794 //! calling Disposer::operator()(pointer), clones all the
795 //! elements from src calling Cloner::operator()(reference)
796 //! and inserts them on *this.
797 //!
798 //! If cloner throws, all cloned elements are unlinked and disposed
799 //! calling Disposer::operator()(pointer).
800 //!
801 //! <b>Complexity</b>: Linear to erased plus inserted elements.
802 //!
803 //! <b>Throws</b>: If cloner throws. Basic guarantee.
804 template <class Cloner, class Disposer>
805 void clone_from(BOOST_RV_REF(list_impl) src, Cloner cloner, Disposer disposer)
806 {
807 this->clear_and_dispose(disposer);
808 detail::exception_disposer<list_impl, Disposer>
809 rollback(*this, disposer);
810 iterator b(src.begin()), e(src.end());
811 for(; b != e; ++b){
812 this->push_back(*cloner(*b));
813 }
814 rollback.release();
815 }
816
817 //! <b>Requires</b>: value must be an lvalue and p must be a valid iterator of *this.
818 //!
819 //! <b>Effects</b>: Inserts the value before the position pointed by p.
820 //!
821 //! <b>Returns</b>: An iterator to the inserted element.
822 //!
823 //! <b>Throws</b>: Nothing.
824 //!
825 //! <b>Complexity</b>: Constant time. No copy constructors are called.
826 //!
827 //! <b>Note</b>: Does not affect the validity of iterators and references.
828 iterator insert(const_iterator p, reference value) BOOST_NOEXCEPT
829 {
830 node_ptr to_insert = this->priv_value_traits().to_node_ptr(value);
831 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::inited(to_insert));
832 node_algorithms::link_before(p.pointed_node(), to_insert);
833 this->priv_size_traits().increment();
834 return iterator(to_insert, this->priv_value_traits_ptr());
835 }
836
837 //! <b>Requires</b>: Dereferencing iterator must yield
838 //! an lvalue of type value_type and p must be a valid iterator of *this.
839 //!
840 //! <b>Effects</b>: Inserts the range pointed by b and e before the position p.
841 //! No copy constructors are called.
842 //!
843 //! <b>Throws</b>: Nothing.
844 //!
845 //! <b>Complexity</b>: Linear to the number of elements inserted.
846 //!
847 //! <b>Note</b>: Does not affect the validity of iterators and references.
848 template<class Iterator>
849 void insert(const_iterator p, Iterator b, Iterator e) BOOST_NOEXCEPT
850 {
851 for (; b != e; ++b)
852 this->insert(p, *b);
853 }
854
855 //! <b>Requires</b>: Dereferencing iterator must yield
856 //! an lvalue of type value_type.
857 //!
858 //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
859 //! No destructors or copy constructors are called.
860 //!
861 //! <b>Throws</b>: Nothing.
862 //!
863 //! <b>Complexity</b>: Linear to the number of elements inserted plus
864 //! linear to the elements contained in the list if it's a safe-mode
865 //! or auto-unlink value.
866 //! Linear to the number of elements inserted in the list otherwise.
867 //!
868 //! <b>Note</b>: Invalidates the iterators (but not the references)
869 //! to the erased elements.
870 template<class Iterator>
871 void assign(Iterator b, Iterator e) BOOST_NOEXCEPT
872 {
873 this->clear();
874 this->insert(this->cend(), b, e);
875 }
876
877 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
878 //!
879 //! <b>Requires</b>: Dereferencing iterator must yield
880 //! an lvalue of type value_type.
881 //!
882 //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
883 //! No destructors or copy constructors are called.
884 //! Disposer::operator()(pointer) is called for the removed elements.
885 //!
886 //! <b>Throws</b>: Nothing.
887 //!
888 //! <b>Complexity</b>: Linear to the number of elements inserted plus
889 //! linear to the elements contained in the list.
890 //!
891 //! <b>Note</b>: Invalidates the iterators (but not the references)
892 //! to the erased elements.
893 template<class Iterator, class Disposer>
894 void dispose_and_assign(Disposer disposer, Iterator b, Iterator e) BOOST_NOEXCEPT
895 {
896 this->clear_and_dispose(disposer);
897 this->insert(this->cend(), b, e);
898 }
899
900 //! <b>Requires</b>: p must be a valid iterator of *this.
901 //!
902 //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
903 //! the element pointed by p. No destructors or copy constructors are called.
904 //!
905 //! <b>Throws</b>: Nothing.
906 //!
907 //! <b>Complexity</b>: Constant.
908 //!
909 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of
910 //! this list. Iterators of this list and all the references are not invalidated.
911 void splice(const_iterator p, list_impl& x) BOOST_NOEXCEPT
912 {
913 if(!x.empty()){
914 node_algorithms::transfer
915 (p.pointed_node(), x.begin().pointed_node(), x.end().pointed_node());
916 size_traits &thist = this->priv_size_traits();
917 size_traits &xt = x.priv_size_traits();
918 thist.increase(xt.get_size());
919 xt.set_size(size_type(0));
920 }
921 }
922
923 //! <b>Requires</b>: p must be a valid iterator of *this.
924 //! new_ele must point to an element contained in list x.
925 //!
926 //! <b>Effects</b>: Transfers the value pointed by new_ele, from list x to this list,
927 //! before the element pointed by p. No destructors or copy constructors are called.
928 //! If p == new_ele or p == ++new_ele, this function is a null operation.
929 //!
930 //! <b>Throws</b>: Nothing.
931 //!
932 //! <b>Complexity</b>: Constant.
933 //!
934 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
935 //! list. Iterators of this list and all the references are not invalidated.
936 void splice(const_iterator p, list_impl&x, const_iterator new_ele) BOOST_NOEXCEPT
937 {
938 node_algorithms::transfer(p.pointed_node(), new_ele.pointed_node());
939 x.priv_size_traits().decrement();
940 this->priv_size_traits().increment();
941 }
942
943 //! <b>Requires</b>: p must be a valid iterator of *this.
944 //! f and e must point to elements contained in list x.
945 //!
946 //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
947 //! before the element pointed by p. No destructors or copy constructors are called.
948 //!
949 //! <b>Throws</b>: Nothing.
950 //!
951 //! <b>Complexity</b>: Linear to the number of elements transferred
952 //! if constant-time size option is enabled. Constant-time otherwise.
953 //!
954 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
955 //! list. Iterators of this list and all the references are not invalidated.
956 void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e) BOOST_NOEXCEPT
957 {
958 BOOST_IF_CONSTEXPR(constant_time_size)
959 this->splice(p, x, f, e, node_algorithms::distance(f.pointed_node(), e.pointed_node()));
960 else
961 this->splice(p, x, f, e, 1);//intrusive::iterator_distance is a dummy value
962 }
963
964 //! <b>Requires</b>: p must be a valid iterator of *this.
965 //! f and e must point to elements contained in list x.
966 //! n == distance(f, e)
967 //!
968 //! <b>Effects</b>: Transfers the range pointed by f and e from list x to this list,
969 //! before the element pointed by p. No destructors or copy constructors are called.
970 //!
971 //! <b>Throws</b>: Nothing.
972 //!
973 //! <b>Complexity</b>: Constant.
974 //!
975 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
976 //! list. Iterators of this list and all the references are not invalidated.
977 void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e, size_type n) BOOST_NOEXCEPT
978 {
979 if(n){
980 BOOST_IF_CONSTEXPR(constant_time_size){
981 BOOST_INTRUSIVE_INVARIANT_ASSERT(n == node_algorithms::distance(f.pointed_node(), e.pointed_node()));
982 node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
983 size_traits &thist = this->priv_size_traits();
984 size_traits &xt = x.priv_size_traits();
985 thist.increase(n);
986 xt.decrease(n);
987 }
988 else{
989 node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node());
990 }
991 }
992 }
993
994 //! <b>Effects</b>: This function sorts the list *this according to operator <.
995 //! The sort is stable, that is, the relative order of equivalent elements is preserved.
996 //!
997 //! <b>Throws</b>: If value_traits::node_traits::node
998 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
999 //! or operator < throws. Basic guarantee.
1000 //!
1001 //! <b>Notes</b>: Iterators and references are not invalidated.
1002 //!
1003 //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
1004 //! is the list's size.
1005 void sort()
1006 { this->sort(value_less<value_type>()); }
1007
1008 //! <b>Requires</b>: p must be a comparison function that induces a strict weak ordering
1009 //!
1010 //! <b>Effects</b>: This function sorts the list *this according to p. The sort is
1011 //! stable, that is, the relative order of equivalent elements is preserved.
1012 //!
1013 //! <b>Throws</b>: If value_traits::node_traits::node
1014 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1015 //! or the predicate throws. Basic guarantee.
1016 //!
1017 //! <b>Notes</b>: This won't throw if list_base_hook<> or
1018 //! list_member_hook are used.
1019 //! Iterators and references are not invalidated.
1020 //!
1021 //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
1022 //! is the list's size.
1023 template<class Predicate>
1024 void sort(Predicate p)
1025 {
1026 if(node_traits::get_next(this->get_root_node())
1027 != node_traits::get_previous(this->get_root_node())){
1028 list_impl carry(this->priv_value_traits());
1029 detail::array_initializer<list_impl, 64> counter(this->priv_value_traits());
1030 int fill = 0;
1031 while(!this->empty()){
1032 carry.splice(carry.cbegin(), *this, this->cbegin());
1033 int i = 0;
1034 while(i < fill && !counter[i].empty()) {
1035 counter[i].merge(carry, p);
1036 carry.swap(counter[i++]);
1037 }
1038 carry.swap(counter[i]);
1039 if(i == fill)
1040 ++fill;
1041 }
1042 for (int i = 1; i < fill; ++i)
1043 counter[i].merge(counter[i-1], p);
1044 this->swap(counter[fill-1]);
1045 }
1046 }
1047
1048 //! <b>Effects</b>: This function removes all of x's elements and inserts them
1049 //! in order into *this according to operator <. The merge is stable;
1050 //! that is, if an element from *this is equivalent to one from x, then the element
1051 //! from *this will precede the one from x.
1052 //!
1053 //! <b>Throws</b>: If operator < throws. Basic guarantee.
1054 //!
1055 //! <b>Complexity</b>: This function is linear time: it performs at most
1056 //! size() + x.size() - 1 comparisons.
1057 //!
1058 //! <b>Note</b>: Iterators and references are not invalidated
1059 void merge(list_impl& x)
1060 { this->merge(x, value_less<value_type>()); }
1061
1062 //! <b>Requires</b>: p must be a comparison function that induces a strict weak
1063 //! ordering and both *this and x must be sorted according to that ordering
1064 //! The lists x and *this must be distinct.
1065 //!
1066 //! <b>Effects</b>: This function removes all of x's elements and inserts them
1067 //! in order into *this. The merge is stable; that is, if an element from *this is
1068 //! equivalent to one from x, then the element from *this will precede the one from x.
1069 //!
1070 //! <b>Throws</b>: If the predicate throws. Basic guarantee.
1071 //!
1072 //! <b>Complexity</b>: This function is linear time: it performs at most
1073 //! size() + x.size() - 1 comparisons.
1074 //!
1075 //! <b>Note</b>: Iterators and references are not invalidated.
1076 template<class Predicate>
1077 void merge(list_impl& x, Predicate p)
1078 {
1079 const_iterator e(this->cend()), ex(x.cend());
1080 const_iterator b(this->cbegin());
1081 while(!x.empty()){
1082 const_iterator ix(x.cbegin());
1083 while (b != e && !p(*ix, *b)){
1084 ++b;
1085 }
1086 if(b == e){
1087 //Now transfer the rest to the end of the container
1088 this->splice(e, x);
1089 break;
1090 }
1091 else{
1092 size_type n(0);
1093 do{
1094 ++ix; ++n;
1095 } while(ix != ex && p(*ix, *b));
1096 this->splice(b, x, x.begin(), ix, n);
1097 }
1098 }
1099 }
1100
1101 //! <b>Effects</b>: Reverses the order of elements in the list.
1102 //!
1103 //! <b>Throws</b>: Nothing.
1104 //!
1105 //! <b>Complexity</b>: This function is linear time.
1106 //!
1107 //! <b>Note</b>: Iterators and references are not invalidated
1108 void reverse() BOOST_NOEXCEPT
1109 { node_algorithms::reverse(this->get_root_node()); }
1110
1111 //! <b>Effects</b>: Removes all the elements that compare equal to value.
1112 //! No destructors are called.
1113 //!
1114 //! <b>Throws</b>: If operator == throws. Basic guarantee.
1115 //!
1116 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1117 //!
1118 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1119 //! and iterators to elements that are not removed remain valid.
1120 void remove(const_reference value) BOOST_NOEXCEPT
1121 { this->remove_if(detail::equal_to_value<const_reference>(value)); }
1122
1123 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1124 //!
1125 //! <b>Effects</b>: Removes all the elements that compare equal to value.
1126 //! Disposer::operator()(pointer) is called for every removed element.
1127 //!
1128 //! <b>Throws</b>: If operator == throws. Basic guarantee.
1129 //!
1130 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1131 //!
1132 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1133 //! and iterators to elements that are not removed remain valid.
1134 template<class Disposer>
1135 void remove_and_dispose(const_reference value, Disposer disposer) BOOST_NOEXCEPT
1136 { this->remove_and_dispose_if(detail::equal_to_value<const_reference>(value), disposer); }
1137
1138 //! <b>Effects</b>: Removes all the elements for which a specified
1139 //! predicate is satisfied. No destructors are called.
1140 //!
1141 //! <b>Throws</b>: If pred throws. Basic guarantee.
1142 //!
1143 //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
1144 //!
1145 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1146 //! and iterators to elements that are not removed remain valid.
1147 template<class Pred>
1148 void remove_if(Pred pred)
1149 {
1150 const node_ptr root_node = this->get_root_node();
1151 typename node_algorithms::stable_partition_info info;
1152 node_algorithms::stable_partition
1153 (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
1154 //Invariants preserved by stable_partition so erase can be safely called
1155 //The first element might have changed so calculate it again
1156 this->erase( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
1157 , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
1158 , info.num_1st_partition);
1159 }
1160
1161 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1162 //!
1163 //! <b>Effects</b>: Removes all the elements for which a specified
1164 //! predicate is satisfied.
1165 //! Disposer::operator()(pointer) is called for every removed element.
1166 //!
1167 //! <b>Throws</b>: If pred throws. Basic guarantee.
1168 //!
1169 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1170 //!
1171 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1172 //! and iterators to elements that are not removed remain valid.
1173 template<class Pred, class Disposer>
1174 void remove_and_dispose_if(Pred pred, Disposer disposer)
1175 {
1176 const node_ptr root_node = this->get_root_node();
1177 typename node_algorithms::stable_partition_info info;
1178 node_algorithms::stable_partition
1179 (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp<Pred, value_traits>(pred, &this->priv_value_traits()), info);
1180 //Invariants preserved by stable_partition so erase can be safely called
1181 //The first element might have changed so calculate it again
1182 this->erase_and_dispose( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr())
1183 , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr())
1184 , disposer);
1185 }
1186
1187 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1188 //! elements that are equal from the list. No destructors are called.
1189 //!
1190 //! <b>Throws</b>: If std::equal_to<value_type throws. Basic guarantee.
1191 //!
1192 //! <b>Complexity</b>: Linear time (size()-1 comparisons calls to pred()).
1193 //!
1194 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1195 //! and iterators to elements that are not removed remain valid.
1196 void unique()
1197 { this->unique_and_dispose(std::equal_to<value_type>(), detail::null_disposer()); }
1198
1199 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1200 //! elements that satisfy some binary predicate from the list.
1201 //! No destructors are called.
1202 //!
1203 //! <b>Throws</b>: If pred throws. Basic guarantee.
1204 //!
1205 //! <b>Complexity</b>: Linear time (size()-1 comparisons equality comparisons).
1206 //!
1207 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1208 //! and iterators to elements that are not removed remain valid.
1209 template<class BinaryPredicate>
1210 void unique(BinaryPredicate pred)
1211 { this->unique_and_dispose(pred, detail::null_disposer()); }
1212
1213 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1214 //!
1215 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1216 //! elements that are equal from the list.
1217 //! Disposer::operator()(pointer) is called for every removed element.
1218 //!
1219 //! <b>Throws</b>: If std::equal_to<value_type throws. Basic guarantee.
1220 //!
1221 //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
1222 //!
1223 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1224 //! and iterators to elements that are not removed remain valid.
1225 template<class Disposer>
1226 void unique_and_dispose(Disposer disposer)
1227 { this->unique_and_dispose(std::equal_to<value_type>(), disposer); }
1228
1229 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1230 //!
1231 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1232 //! elements that satisfy some binary predicate from the list.
1233 //! Disposer::operator()(pointer) is called for every removed element.
1234 //!
1235 //! <b>Throws</b>: If pred throws. Basic guarantee.
1236 //!
1237 //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
1238 //!
1239 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1240 //! and iterators to elements that are not removed remain valid.
1241 template<class BinaryPredicate, class Disposer>
1242 void unique_and_dispose(BinaryPredicate pred, Disposer disposer)
1243 {
1244 const_iterator itend(this->cend());
1245 const_iterator cur(this->cbegin());
1246
1247 if(cur != itend){
1248 const_iterator after(cur);
1249 ++after;
1250 while(after != itend){
1251 if(pred(*cur, *after)){
1252 after = this->erase_and_dispose(after, disposer);
1253 }
1254 else{
1255 cur = after;
1256 ++after;
1257 }
1258 }
1259 }
1260 }
1261
1262 //! <b>Requires</b>: value must be a reference to a value inserted in a list.
1263 //!
1264 //! <b>Effects</b>: This function returns a const_iterator pointing to the element
1265 //!
1266 //! <b>Throws</b>: Nothing.
1267 //!
1268 //! <b>Complexity</b>: Constant time.
1269 //!
1270 //! <b>Note</b>: Iterators and references are not invalidated.
1271 //! This static function is available only if the <i>value traits</i>
1272 //! is stateless.
1273 static iterator s_iterator_to(reference value) BOOST_NOEXCEPT
1274 {
1275 BOOST_STATIC_ASSERT((!stateful_value_traits));
1276 BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(value)));
1277 return iterator(value_traits::to_node_ptr(value), const_value_traits_ptr());
1278 }
1279
1280 //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
1281 //!
1282 //! <b>Effects</b>: This function returns an iterator pointing to the element.
1283 //!
1284 //! <b>Throws</b>: Nothing.
1285 //!
1286 //! <b>Complexity</b>: Constant time.
1287 //!
1288 //! <b>Note</b>: Iterators and references are not invalidated.
1289 //! This static function is available only if the <i>value traits</i>
1290 //! is stateless.
1291 static const_iterator s_iterator_to(const_reference value) BOOST_NOEXCEPT
1292 {
1293 BOOST_STATIC_ASSERT((!stateful_value_traits));
1294 reference r =*detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
1295 BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(r)));
1296 return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr());
1297 }
1298
1299 //! <b>Requires</b>: value must be a reference to a value inserted in a list.
1300 //!
1301 //! <b>Effects</b>: This function returns a const_iterator pointing to the element
1302 //!
1303 //! <b>Throws</b>: Nothing.
1304 //!
1305 //! <b>Complexity</b>: Constant time.
1306 //!
1307 //! <b>Note</b>: Iterators and references are not invalidated.
1308 iterator iterator_to(reference value) BOOST_NOEXCEPT
1309 {
1310 BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(value)));
1311 return iterator(this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr());
1312 }
1313
1314 //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
1315 //!
1316 //! <b>Effects</b>: This function returns an iterator pointing to the element.
1317 //!
1318 //! <b>Throws</b>: Nothing.
1319 //!
1320 //! <b>Complexity</b>: Constant time.
1321 //!
1322 //! <b>Note</b>: Iterators and references are not invalidated.
1323 const_iterator iterator_to(const_reference value) const BOOST_NOEXCEPT
1324 {
1325 reference r = *detail::uncast(pointer_traits<const_pointer>::pointer_to(value));
1326 BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(r)));
1327 return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr());
1328 }
1329
1330 //! <b>Effects</b>: Asserts the integrity of the container.
1331 //!
1332 //! <b>Complexity</b>: Linear time.
1333 //!
1334 //! <b>Note</b>: The method has no effect when asserts are turned off (e.g., with NDEBUG).
1335 //! Experimental function, interface might change in future versions.
1336 void check() const
1337 {
1338 const_node_ptr header_ptr = get_root_node();
1339 // header's next and prev are never null
1340 BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr));
1341 BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(header_ptr));
1342 // header's next and prev either both point to header (empty list) or neither does
1343 BOOST_INTRUSIVE_INVARIANT_ASSERT((node_traits::get_next(header_ptr) == header_ptr)
1344 == (node_traits::get_previous(header_ptr) == header_ptr));
1345 if (node_traits::get_next(header_ptr) == header_ptr)
1346 {
1347 BOOST_IF_CONSTEXPR(constant_time_size)
1348 BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == 0);
1349 return;
1350 }
1351 size_t node_count = 0;
1352 const_node_ptr p = header_ptr;
1353 while (true)
1354 {
1355 const_node_ptr next_p = node_traits::get_next(p);
1356 BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p);
1357 BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(next_p) == p);
1358 p = next_p;
1359 if (p == header_ptr) break;
1360 ++node_count;
1361 }
1362 BOOST_IF_CONSTEXPR(constant_time_size)
1363 BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == node_count);
1364 }
1365
1366 friend bool operator==(const list_impl &x, const list_impl &y)
1367 {
1368 if(constant_time_size && x.size() != y.size()){
1369 return false;
1370 }
1371 return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend());
1372 }
1373
1374 BOOST_INTRUSIVE_FORCEINLINE friend bool operator!=(const list_impl &x, const list_impl &y)
1375 { return !(x == y); }
1376
1377 BOOST_INTRUSIVE_FORCEINLINE friend bool operator<(const list_impl &x, const list_impl &y)
1378 { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
1379
1380 BOOST_INTRUSIVE_FORCEINLINE friend bool operator>(const list_impl &x, const list_impl &y)
1381 { return y < x; }
1382
1383 BOOST_INTRUSIVE_FORCEINLINE friend bool operator<=(const list_impl &x, const list_impl &y)
1384 { return !(y < x); }
1385
1386 BOOST_INTRUSIVE_FORCEINLINE friend bool operator>=(const list_impl &x, const list_impl &y)
1387 { return !(x < y); }
1388
1389 BOOST_INTRUSIVE_FORCEINLINE friend void swap(list_impl &x, list_impl &y) BOOST_NOEXCEPT
1390 { x.swap(y); }
1391
1392 /// @cond
1393
1394 private:
1395 static list_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) BOOST_NOEXCEPT
1396 {
1397 BOOST_STATIC_ASSERT((has_container_from_iterator));
1398 node_ptr p = end_iterator.pointed_node();
1399 header_holder_type* h = header_holder_type::get_holder(p);
1400 root_plus_size* r = detail::parent_from_member
1401 < root_plus_size, header_holder_type>(h, &root_plus_size::m_header);
1402 data_t *d = detail::parent_from_member<data_t, root_plus_size>
1403 ( r, &data_t::root_plus_size_);
1404 list_impl *s = detail::parent_from_member<list_impl, data_t>(d, &list_impl::data_);
1405 return *s;
1406 }
1407 /// @endcond
1408 };
1409
1410
1411 //! Helper metafunction to define a \c list that yields to the same type when the
1412 //! same options (either explicitly or implicitly) are used.
1413 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1414 template<class T, class ...Options>
1415 #else
1416 template<class T, class O1 = void, class O2 = void, class O3 = void, class O4 = void>
1417 #endif
1418 struct make_list
1419 {
1420 /// @cond
1421 typedef typename pack_options
1422 < list_defaults,
1423 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1424 O1, O2, O3, O4
1425 #else
1426 Options...
1427 #endif
1428 >::type packed_options;
1429
1430 typedef typename detail::get_value_traits
1431 <T, typename packed_options::proto_value_traits>::type value_traits;
1432 typedef list_impl
1433 <
1434 value_traits,
1435 typename packed_options::size_type,
1436 packed_options::constant_time_size,
1437 typename packed_options::header_holder_type
1438 > implementation_defined;
1439 /// @endcond
1440 typedef implementation_defined type;
1441 };
1442
1443
1444 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1445
1446 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1447 template<class T, class O1, class O2, class O3, class O4>
1448 #else
1449 template<class T, class ...Options>
1450 #endif
1451 class list
1452 : public make_list<T,
1453 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1454 O1, O2, O3, O4
1455 #else
1456 Options...
1457 #endif
1458 >::type
1459 {
1460 typedef typename make_list
1461 <T,
1462 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1463 O1, O2, O3, O4
1464 #else
1465 Options...
1466 #endif
1467 >::type Base;
1468 //Assert if passed value traits are compatible with the type
1469 BOOST_STATIC_ASSERT((detail::is_same<typename Base::value_traits::value_type, T>::value));
1470 BOOST_MOVABLE_BUT_NOT_COPYABLE(list)
1471
1472 public:
1473 typedef typename Base::value_traits value_traits;
1474 typedef typename Base::iterator iterator;
1475 typedef typename Base::const_iterator const_iterator;
1476
1477 BOOST_INTRUSIVE_FORCEINLINE list()
1478 : Base()
1479 {}
1480
1481 BOOST_INTRUSIVE_FORCEINLINE explicit list(const value_traits &v_traits)
1482 : Base(v_traits)
1483 {}
1484
1485 template<class Iterator>
1486 BOOST_INTRUSIVE_FORCEINLINE list(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
1487 : Base(b, e, v_traits)
1488 {}
1489
1490 BOOST_INTRUSIVE_FORCEINLINE list(BOOST_RV_REF(list) x)
1491 : Base(BOOST_MOVE_BASE(Base, x))
1492 {}
1493
1494 BOOST_INTRUSIVE_FORCEINLINE list& operator=(BOOST_RV_REF(list) x)
1495 { return static_cast<list &>(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); }
1496
1497 template <class Cloner, class Disposer>
1498 BOOST_INTRUSIVE_FORCEINLINE void clone_from(const list &src, Cloner cloner, Disposer disposer)
1499 { Base::clone_from(src, cloner, disposer); }
1500
1501 template <class Cloner, class Disposer>
1502 BOOST_INTRUSIVE_FORCEINLINE void clone_from(BOOST_RV_REF(list) src, Cloner cloner, Disposer disposer)
1503 { Base::clone_from(BOOST_MOVE_BASE(Base, src), cloner, disposer); }
1504
1505 BOOST_INTRUSIVE_FORCEINLINE static list &container_from_end_iterator(iterator end_iterator) BOOST_NOEXCEPT
1506 { return static_cast<list &>(Base::container_from_end_iterator(end_iterator)); }
1507
1508 BOOST_INTRUSIVE_FORCEINLINE static const list &container_from_end_iterator(const_iterator end_iterator) BOOST_NOEXCEPT
1509 { return static_cast<const list &>(Base::container_from_end_iterator(end_iterator)); }
1510 };
1511
1512 #endif
1513
1514 } //namespace intrusive
1515 } //namespace boost
1516
1517 #include <boost/intrusive/detail/config_end.hpp>
1518
1519 #endif //BOOST_INTRUSIVE_LIST_HPP