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