1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga 2006-2008
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)
10 // See http://www.boost.org/libs/intrusive for documentation.
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTRUSIVE_SLIST_HPP
15 #define BOOST_INTRUSIVE_SLIST_HPP
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/static_assert.hpp>
19 #include <boost/intrusive/detail/assert.hpp>
20 #include <boost/intrusive/intrusive_fwd.hpp>
21 #include <boost/intrusive/slist_hook.hpp>
22 #include <boost/intrusive/circular_slist_algorithms.hpp>
23 #include <boost/intrusive/linear_slist_algorithms.hpp>
24 #include <boost/intrusive/detail/pointer_to_other.hpp>
25 #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
26 #include <boost/intrusive/link_mode.hpp>
27 #include <boost/intrusive/options.hpp>
28 #include <boost/intrusive/detail/utilities.hpp>
32 #include <cstddef> //std::size_t
33 #include <utility> //std::pair
40 template <class ValueTraits
, class SizeType
, bool ConstantTimeSize
, bool Linear
, bool CacheLast
>
43 typedef ValueTraits value_traits
;
44 typedef SizeType size_type
;
45 static const bool constant_time_size
= ConstantTimeSize
;
46 static const bool linear
= Linear
;
47 static const bool cache_last
= CacheLast
;
50 template<class Node
, class NodePtr
, bool>
57 template<class Node
, class NodePtr
>
58 struct root_plus_last
<Node
, NodePtr
, false>
67 , base_hook
<detail::default_slist_hook
>
68 , constant_time_size
<true>
70 , size_type
<std::size_t>
77 //! The class template slist is an intrusive container, that encapsulates
78 //! a singly-linked list. You can use such a list to squeeze the last bit
79 //! of performance from your application. Unfortunately, the little gains
80 //! come with some huge drawbacks. A lot of member functions can't be
81 //! implemented as efficiently as for standard containers. To overcome
82 //! this limitation some other member functions with rather unusual semantics
83 //! have to be introduced.
85 //! The template parameter \c T is the type to be managed by the container.
86 //! The user can specify additional options and if no options are provided
87 //! default options are used.
89 //! The container supports the following options:
90 //! \c base_hook<>/member_hook<>/value_traits<>,
91 //! \c constant_time_size<>, \c size_type<>,
92 //! \c linear<> and \c cache_last<>.
94 //! The iterators of slist are forward iterators. slist provides a static
95 //! function called "previous" to compute the previous iterator of a given iterator.
96 //! This function has linear complexity. To improve the usability esp. with
97 //! the '*_after' functions, ++end() == begin() and previous(begin()) == end()
98 //! are defined. An new special function "before_begin()" is defined, which returns
99 //! an iterator that points one less the beginning of the list: ++before_begin() == begin()
100 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
101 template<class T
, class ...Options
>
103 template<class Config
>
106 : private detail::clear_on_destructor_base
<slist_impl
<Config
> >
108 template<class C
> friend class detail::clear_on_destructor_base
;
111 typedef typename
Config::value_traits value_traits
;
113 static const bool external_value_traits
=
114 detail::external_value_traits_is_true
<value_traits
>::value
;
115 typedef typename
detail::eval_if_c
116 < external_value_traits
117 , detail::eval_value_traits
<value_traits
>
118 , detail::identity
<value_traits
>
119 >::type real_value_traits
;
121 typedef typename
real_value_traits::pointer pointer
;
122 typedef typename
real_value_traits::const_pointer const_pointer
;
123 typedef typename
std::iterator_traits
<pointer
>::value_type value_type
;
124 typedef typename
std::iterator_traits
<pointer
>::reference reference
;
125 typedef typename
std::iterator_traits
<const_pointer
>::reference const_reference
;
126 typedef typename
std::iterator_traits
<pointer
>::difference_type difference_type
;
127 typedef typename
Config::size_type size_type
;
128 typedef slist_iterator
<slist_impl
, false> iterator
;
129 typedef slist_iterator
<slist_impl
, true> const_iterator
;
130 typedef typename
real_value_traits::node_traits node_traits
;
131 typedef typename
node_traits::node node
;
132 typedef typename
boost::pointer_to_other
133 <pointer
, node
>::type node_ptr
;
134 typedef typename
boost::pointer_to_other
135 <pointer
, const node
>::type const_node_ptr
;
136 typedef typename
detail::if_c
138 , linear_slist_algorithms
<node_traits
>
139 , circular_slist_algorithms
<node_traits
>
140 >::type node_algorithms
;
142 static const bool constant_time_size
= Config::constant_time_size
;
143 static const bool stateful_value_traits
= detail::store_cont_ptr_on_it
<slist_impl
>::value
;
144 static const bool linear
= Config::linear
;
145 static const bool cache_last
= Config::cache_last
;
149 typedef detail::size_holder
<constant_time_size
, size_type
> size_traits
;
153 slist_impl (const slist_impl
&);
157 slist_impl
&operator =(const slist_impl
&);
159 enum { safemode_or_autounlink
=
160 (int)real_value_traits::link_mode
== (int)auto_unlink
||
161 (int)real_value_traits::link_mode
== (int)safe_link
};
163 //Constant-time size is incompatible with auto-unlink hooks!
164 BOOST_STATIC_ASSERT(!(constant_time_size
&& ((int)real_value_traits::link_mode
== (int)auto_unlink
)));
165 //Linear singly linked lists are incompatible with auto-unlink hooks!
166 BOOST_STATIC_ASSERT(!(linear
&& ((int)real_value_traits::link_mode
== (int)auto_unlink
)));
167 //A list with cached last node is incompatible with auto-unlink hooks!
168 BOOST_STATIC_ASSERT(!(cache_last
&& ((int)real_value_traits::link_mode
== (int)auto_unlink
)));
170 node_ptr
get_end_node()
171 { return node_ptr(linear
? node_ptr(0) : this->get_root_node()); }
173 const_node_ptr
get_end_node() const
175 return const_node_ptr
176 (linear
? const_node_ptr(0) : this->get_root_node()); }
178 node_ptr
get_root_node()
179 { return node_ptr(&data_
.root_plus_size_
.root_
); }
181 const_node_ptr
get_root_node() const
182 { return const_node_ptr(&data_
.root_plus_size_
.root_
); }
184 node_ptr
get_last_node()
185 { return this->get_last_node(detail::bool_
<cache_last
>()); }
187 const_node_ptr
get_last_node() const
188 { return this->get_last_node(detail::bool_
<cache_last
>()); }
190 void set_last_node(node_ptr n
)
191 { return this->set_last_node(n
, detail::bool_
<cache_last
>()); }
193 static node_ptr
get_last_node(detail::bool_
<false>)
194 { return node_ptr(0); }
196 static void set_last_node(node_ptr
, detail::bool_
<false>)
199 node_ptr
get_last_node(detail::bool_
<true>)
200 { return node_ptr(data_
.root_plus_size_
.last_
); }
202 const_node_ptr
get_last_node(detail::bool_
<true>) const
203 { return const_node_ptr(data_
.root_plus_size_
.last_
); }
205 void set_last_node(node_ptr n
, detail::bool_
<true>)
206 { data_
.root_plus_size_
.last_
= n
; }
208 static node_ptr
uncast(const_node_ptr ptr
)
209 { return node_ptr(const_cast<node
*>(detail::get_pointer(ptr
))); }
211 void set_default_constructed_state()
213 node_algorithms::init_header(this->get_root_node());
214 this->priv_size_traits().set_size(size_type(0));
216 this->set_last_node(this->get_root_node());
220 struct root_plus_size
222 , public root_plus_last
<node
, node_ptr
, cache_last
>
226 : public slist_impl::value_traits
228 typedef typename
slist_impl::value_traits value_traits
;
229 data_t(const value_traits
&val_traits
)
230 : value_traits(val_traits
)
233 root_plus_size root_plus_size_
;
236 size_traits
&priv_size_traits()
237 { return data_
.root_plus_size_
; }
239 const size_traits
&priv_size_traits() const
240 { return data_
.root_plus_size_
; }
242 const real_value_traits
&get_real_value_traits(detail::bool_
<false>) const
245 const real_value_traits
&get_real_value_traits(detail::bool_
<true>) const
246 { return data_
.get_value_traits(*this); }
248 real_value_traits
&get_real_value_traits(detail::bool_
<false>)
251 real_value_traits
&get_real_value_traits(detail::bool_
<true>)
252 { return data_
.get_value_traits(*this); }
258 const real_value_traits
&get_real_value_traits() const
259 { return this->get_real_value_traits(detail::bool_
<external_value_traits
>()); }
261 real_value_traits
&get_real_value_traits()
262 { return this->get_real_value_traits(detail::bool_
<external_value_traits
>()); }
265 //! <b>Effects</b>: constructs an empty list.
267 //! <b>Complexity</b>: Constant
269 //! <b>Throws</b>: If value_traits::node_traits::node
270 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
271 slist_impl(const value_traits
&v_traits
= value_traits())
273 { this->set_default_constructed_state(); }
275 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue of type value_type.
277 //! <b>Effects</b>: Constructs a list equal to [first,last).
279 //! <b>Complexity</b>: Linear in std::distance(b, e). No copy constructors are called.
281 //! <b>Throws</b>: If value_traits::node_traits::node
282 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks).
283 template<class Iterator
>
284 slist_impl(Iterator b
, Iterator e
, const value_traits
&v_traits
= value_traits())
287 this->set_default_constructed_state();
288 this->insert_after(this->cbefore_begin(), b
, e
);
291 //! <b>Effects</b>: If it's a safe-mode
292 //! or auto-unlink value, the destructor does nothing
293 //! (ie. no code is generated). Otherwise it detaches all elements from this.
294 //! In this case the objects in the list are not deleted (i.e. no destructors
295 //! are called), but the hooks according to the value_traits template parameter
296 //! are set to their default value.
298 //! <b>Complexity</b>: Linear to the number of elements in the list, if
299 //! it's a safe-mode or auto-unlink value. Otherwise constant.
303 //! <b>Effects</b>: Erases all the elements of the container.
305 //! <b>Throws</b>: Nothing.
307 //! <b>Complexity</b>: Linear to the number of elements of the list.
308 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
310 //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased elements.
313 if(safemode_or_autounlink
){
314 this->clear_and_dispose(detail::null_disposer());
317 this->set_default_constructed_state();
321 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
323 //! <b>Effects</b>: Erases all the elements of the container
324 //! Disposer::operator()(pointer) is called for the removed elements.
326 //! <b>Throws</b>: Nothing.
328 //! <b>Complexity</b>: Linear to the number of elements of the list.
330 //! <b>Note</b>: Invalidates the iterators to the erased elements.
331 template <class Disposer
>
332 void clear_and_dispose(Disposer disposer
)
334 const_iterator
it(this->begin()), itend(this->end());
336 node_ptr
to_erase(it
.pointed_node());
338 if(safemode_or_autounlink
)
339 node_algorithms::init(to_erase
);
340 disposer(get_real_value_traits().to_value_ptr(to_erase
));
342 this->set_default_constructed_state();
345 //! <b>Requires</b>: value must be an lvalue.
347 //! <b>Effects</b>: Inserts the value in the front of the list.
348 //! No copy constructors are called.
350 //! <b>Throws</b>: Nothing.
352 //! <b>Complexity</b>: Constant.
354 //! <b>Note</b>: Does not affect the validity of iterators and references.
355 void push_front(reference value
)
357 node_ptr to_insert
= get_real_value_traits().to_node_ptr(value
);
358 if(safemode_or_autounlink
)
359 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert
));
362 this->set_last_node(to_insert
);
365 node_algorithms::link_after(this->get_root_node(), to_insert
);
366 this->priv_size_traits().increment();
369 //! <b>Requires</b>: value must be an lvalue.
371 //! <b>Effects</b>: Inserts the value in the back of the list.
372 //! No copy constructors are called.
374 //! <b>Throws</b>: Nothing.
376 //! <b>Complexity</b>: Constant.
378 //! <b>Note</b>: Does not affect the validity of iterators and references.
379 //! This function is only available is cache_last<> is true.
380 void push_back(reference value
)
382 BOOST_STATIC_ASSERT((cache_last
!= 0));
383 this->insert_after(const_iterator(this->get_last_node(), this), value
);
386 //! <b>Effects</b>: Erases the first element of the list.
387 //! No destructors are called.
389 //! <b>Throws</b>: Nothing.
391 //! <b>Complexity</b>: Constant.
393 //! <b>Note</b>: Invalidates the iterators (but not the references) to the erased element.
395 { return this->pop_front_and_dispose(detail::null_disposer()); }
397 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
399 //! <b>Effects</b>: Erases the first element of the list.
400 //! Disposer::operator()(pointer) is called for the removed element.
402 //! <b>Throws</b>: Nothing.
404 //! <b>Complexity</b>: Constant.
406 //! <b>Note</b>: Invalidates the iterators to the erased element.
407 template<class Disposer
>
408 void pop_front_and_dispose(Disposer disposer
)
410 node_ptr to_erase
= node_traits::get_next(this->get_root_node());
411 node_algorithms::unlink_after(this->get_root_node());
412 this->priv_size_traits().decrement();
413 if(safemode_or_autounlink
)
414 node_algorithms::init(to_erase
);
415 disposer(get_real_value_traits().to_value_ptr(to_erase
));
418 this->set_last_node(this->get_root_node());
423 //! <b>Effects</b>: Returns a reference to the first element of the list.
425 //! <b>Throws</b>: Nothing.
427 //! <b>Complexity</b>: Constant.
429 { return *this->get_real_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); }
431 //! <b>Effects</b>: Returns a const_reference to the first element of the list.
433 //! <b>Throws</b>: Nothing.
435 //! <b>Complexity</b>: Constant.
436 const_reference
front() const
437 { return *this->get_real_value_traits().to_value_ptr(uncast(node_traits::get_next(this->get_root_node()))); }
439 //! <b>Effects</b>: Returns a reference to the last element of the list.
441 //! <b>Throws</b>: Nothing.
443 //! <b>Complexity</b>: Constant.
445 //! <b>Note</b>: Does not affect the validity of iterators and references.
446 //! This function is only available is cache_last<> is true.
449 BOOST_STATIC_ASSERT((cache_last
!= 0));
450 return *this->get_real_value_traits().to_value_ptr(this->get_last_node());
453 //! <b>Effects</b>: Returns a const_reference to the last element of the list.
455 //! <b>Throws</b>: Nothing.
457 //! <b>Complexity</b>: Constant.
459 //! <b>Note</b>: Does not affect the validity of iterators and references.
460 //! This function is only available is cache_last<> is true.
461 const_reference
back() const
463 BOOST_STATIC_ASSERT((cache_last
!= 0));
464 return *this->get_real_value_traits().to_value_ptr(this->get_last_node());
467 //! <b>Effects</b>: Returns an iterator to the first element contained in the list.
469 //! <b>Throws</b>: Nothing.
471 //! <b>Complexity</b>: Constant.
473 { return iterator (node_traits::get_next(this->get_root_node()), this); }
475 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
477 //! <b>Throws</b>: Nothing.
479 //! <b>Complexity</b>: Constant.
480 const_iterator
begin() const
481 { return const_iterator (node_traits::get_next(this->get_root_node()), this); }
483 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the list.
485 //! <b>Throws</b>: Nothing.
487 //! <b>Complexity</b>: Constant.
488 const_iterator
cbegin() const
489 { return const_iterator(node_traits::get_next(this->get_root_node()), this); }
491 //! <b>Effects</b>: Returns an iterator to the end of the list.
493 //! <b>Throws</b>: Nothing.
495 //! <b>Complexity</b>: Constant.
497 { return iterator(this->get_end_node(), this); }
499 //! <b>Effects</b>: Returns a const_iterator to the end of the list.
501 //! <b>Throws</b>: Nothing.
503 //! <b>Complexity</b>: Constant.
504 const_iterator
end() const
505 { return const_iterator(uncast(this->get_end_node()), this); }
507 //! <b>Effects</b>: Returns a const_iterator to the end of the list.
509 //! <b>Throws</b>: Nothing.
511 //! <b>Complexity</b>: Constant.
512 const_iterator
cend() const
513 { return this->end(); }
515 //! <b>Effects</b>: Returns an iterator that points to a position
516 //! before the first element. Equivalent to "end()"
518 //! <b>Throws</b>: Nothing.
520 //! <b>Complexity</b>: Constant.
521 iterator
before_begin()
522 { return iterator(this->get_root_node(), this); }
524 //! <b>Effects</b>: Returns an iterator that points to a position
525 //! before the first element. Equivalent to "end()"
527 //! <b>Throws</b>: Nothing.
529 //! <b>Complexity</b>: Constant.
530 const_iterator
before_begin() const
531 { return const_iterator(uncast(this->get_root_node()), this); }
533 //! <b>Effects</b>: Returns an iterator that points to a position
534 //! before the first element. Equivalent to "end()"
536 //! <b>Throws</b>: Nothing.
538 //! <b>Complexity</b>: Constant.
539 const_iterator
cbefore_begin() const
540 { return this->before_begin(); }
542 //! <b>Precondition</b>: end_iterator must be a valid end iterator
545 //! <b>Effects</b>: Returns a const reference to the slist associated to the end iterator
547 //! <b>Throws</b>: Nothing.
549 //! <b>Complexity</b>: Constant.
550 static slist_impl
&container_from_end_iterator(iterator end_iterator
)
551 { return slist_impl::priv_container_from_end_iterator(end_iterator
); }
553 //! <b>Precondition</b>: end_iterator must be a valid end const_iterator
556 //! <b>Effects</b>: Returns a const reference to the slist associated to the end iterator
558 //! <b>Throws</b>: Nothing.
560 //! <b>Complexity</b>: Constant.
561 static const slist_impl
&container_from_end_iterator(const_iterator end_iterator
)
562 { return slist_impl::priv_container_from_end_iterator(end_iterator
); }
564 //! <b>Effects</b>: Returns the number of the elements contained in the list.
566 //! <b>Throws</b>: Nothing.
568 //! <b>Complexity</b>: Linear to the number of elements contained in the list.
569 //! if constant_time_size is false. Constant time otherwise.
571 //! <b>Note</b>: Does not affect the validity of iterators and references.
572 size_type
size() const
574 if(constant_time_size
)
575 return this->priv_size_traits().get_size();
577 return node_algorithms::count(this->get_root_node()) - 1;
580 //! <b>Effects</b>: Returns true if the list contains no elements.
582 //! <b>Throws</b>: Nothing.
584 //! <b>Complexity</b>: Constant.
586 //! <b>Note</b>: Does not affect the validity of iterators and references.
588 { return node_algorithms::unique(this->get_root_node()); }
590 //! <b>Effects</b>: Swaps the elements of x and *this.
592 //! <b>Throws</b>: Nothing.
594 //! <b>Complexity</b>: Linear to the number of elements of both lists.
595 //! Constant-time if linear<> and/or cache_last<> options are used.
597 //! <b>Note</b>: Does not affect the validity of iterators and references.
598 void swap(slist_impl
& other
)
601 priv_swap_cache_last(this, &other
);
604 this->priv_swap_lists(this->get_root_node(), other
.get_root_node(), detail::bool_
<linear
>());
606 if(constant_time_size
){
607 size_type backup
= this->priv_size_traits().get_size();
608 this->priv_size_traits().set_size(other
.priv_size_traits().get_size());
609 other
.priv_size_traits().set_size(backup
);
613 //! <b>Effects</b>: Moves backwards all the elements, so that the first
614 //! element becomes the second, the second becomes the third...
615 //! the last element becomes the first one.
617 //! <b>Throws</b>: Nothing.
619 //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
621 //! <b>Note</b>: Iterators Does not affect the validity of iterators and references.
622 void shift_backwards(size_type n
= 1)
623 { this->priv_shift_backwards(n
, detail::bool_
<linear
>()); }
625 //! <b>Effects</b>: Moves forward all the elements, so that the second
626 //! element becomes the first, the third becomes the second...
627 //! the first element becomes the last one.
629 //! <b>Throws</b>: Nothing.
631 //! <b>Complexity</b>: Linear to the number of elements plus the number shifts.
633 //! <b>Note</b>: Does not affect the validity of iterators and references.
634 void shift_forward(size_type n
= 1)
635 { this->priv_shift_forward(n
, detail::bool_
<linear
>()); }
637 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
638 //! Cloner should yield to nodes equivalent to the original nodes.
640 //! <b>Effects</b>: Erases all the elements from *this
641 //! calling Disposer::operator()(pointer), clones all the
642 //! elements from src calling Cloner::operator()(const_reference )
643 //! and inserts them on *this.
645 //! If cloner throws, all cloned elements are unlinked and disposed
646 //! calling Disposer::operator()(pointer).
648 //! <b>Complexity</b>: Linear to erased plus inserted elements.
650 //! <b>Throws</b>: If cloner throws.
651 template <class Cloner
, class Disposer
>
652 void clone_from(const slist_impl
&src
, Cloner cloner
, Disposer disposer
)
654 this->clear_and_dispose(disposer
);
655 detail::exception_disposer
<slist_impl
, Disposer
>
656 rollback(*this, disposer
);
657 const_iterator
prev(this->cbefore_begin());
658 const_iterator
b(src
.begin()), e(src
.end());
660 prev
= this->insert_after(prev
, *cloner(*b
));
665 //! <b>Requires</b>: value must be an lvalue and prev_p must point to an element
666 //! contained by the list or to end().
668 //! <b>Effects</b>: Inserts the value after the position pointed by prev_p.
669 //! No copy constructor is called.
671 //! <b>Returns</b>: An iterator to the inserted element.
673 //! <b>Throws</b>: Nothing.
675 //! <b>Complexity</b>: Constant.
677 //! <b>Note</b>: Does not affect the validity of iterators and references.
678 iterator
insert_after(const_iterator prev_p
, reference value
)
680 node_ptr n
= get_real_value_traits().to_node_ptr(value
);
681 if(safemode_or_autounlink
)
682 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(n
));
683 node_ptr
prev_n(prev_p
.pointed_node());
684 node_algorithms::link_after(prev_n
, n
);
685 if(cache_last
&& (this->get_last_node() == prev_n
)){
686 this->set_last_node(n
);
688 this->priv_size_traits().increment();
689 return iterator (n
, this);
692 //! <b>Requires</b>: Dereferencing iterator must yield
693 //! an lvalue of type value_type and prev_p must point to an element
694 //! contained by the list or to the end node.
696 //! <b>Effects</b>: Inserts the [first, last)
697 //! after the position prev_p.
699 //! <b>Throws</b>: Nothing.
701 //! <b>Complexity</b>: Linear to the number of elements inserted.
703 //! <b>Note</b>: Does not affect the validity of iterators and references.
704 template<class Iterator
>
705 void insert_after(const_iterator prev_p
, Iterator first
, Iterator last
)
707 for (; first
!= last
; ++first
)
708 prev_p
= this->insert_after(prev_p
, *first
);
711 //! <b>Requires</b>: value must be an lvalue and p must point to an element
712 //! contained by the list or to end().
714 //! <b>Effects</b>: Inserts the value before the position pointed by p.
715 //! No copy constructor is called.
717 //! <b>Throws</b>: Nothing.
719 //! <b>Complexity</b>: Linear to the number of elements before p.
720 //! Constant-time if cache_last<> is true and p == end().
722 //! <b>Note</b>: Does not affect the validity of iterators and references.
723 iterator
insert(const_iterator p
, reference value
)
724 { return this->insert_after(this->previous(p
), value
); }
726 //! <b>Requires</b>: Dereferencing iterator must yield
727 //! an lvalue of type value_type and p must point to an element
728 //! contained by the list or to the end node.
730 //! <b>Effects</b>: Inserts the pointed by b and e
731 //! before the position p. No copy constructors are called.
733 //! <b>Throws</b>: Nothing.
735 //! <b>Complexity</b>: Linear to the number of elements inserted plus linear
736 //! to the elements before b.
737 //! Linear to the number of elements to insert if cache_last<> option is true and p == end().
739 //! <b>Note</b>: Does not affect the validity of iterators and references.
740 template<class Iterator
>
741 void insert(const_iterator p
, Iterator b
, Iterator e
)
742 { return this->insert_after(this->previous(p
), b
, e
); }
744 //! <b>Effects</b>: Erases the element after the element pointed by prev of
745 //! the list. No destructors are called.
747 //! <b>Returns</b>: the first element remaining beyond the removed elements,
748 //! or end() if no such element exists.
750 //! <b>Throws</b>: Nothing.
752 //! <b>Complexity</b>: Constant.
754 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
756 iterator
erase_after(const_iterator prev
)
757 { return this->erase_after_and_dispose(prev
, detail::null_disposer()); }
759 //! <b>Effects</b>: Erases the range (before_first, last) from
760 //! the list. No destructors are called.
762 //! <b>Returns</b>: the first element remaining beyond the removed elements,
763 //! or end() if no such element exists.
765 //! <b>Throws</b>: Nothing.
767 //! <b>Complexity</b>: Linear to the number of erased elements if it's a safe-mode
768 //! , auto-unlink value or constant-time size is activated. Constant time otherwise.
770 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
772 iterator
erase_after(const_iterator before_first
, const_iterator last
)
774 if(safemode_or_autounlink
|| constant_time_size
){
775 return this->erase_after_and_dispose(before_first
, last
, detail::null_disposer());
778 node_ptr bfp
= before_first
.pointed_node();
779 node_ptr lp
= last
.pointed_node();
781 if((lp
== this->get_end_node())){
782 this->set_last_node(bfp
);
785 node_algorithms::unlink_after(bfp
, lp
);
786 return last
.unconst();
790 //! <b>Effects</b>: Erases the range (before_first, last) from
791 //! the list. n must be std::distance(before_first, last) - 1.
792 //! No destructors are called.
794 //! <b>Returns</b>: the first element remaining beyond the removed elements,
795 //! or end() if no such element exists.
797 //! <b>Throws</b>: Nothing.
799 //! <b>Complexity</b>: constant-time if link_mode is normal_link.
800 //! Linear to the elements (last - before_first) otherwise.
802 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
804 iterator
erase_after(const_iterator before_first
, const_iterator last
, difference_type n
)
806 BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(++const_iterator(before_first
), last
) == difference_type(n
));
807 if(safemode_or_autounlink
){
808 return this->erase_after(before_first
, last
);
811 node_ptr bfp
= before_first
.pointed_node();
812 node_ptr lp
= last
.pointed_node();
814 if((lp
== this->get_end_node())){
815 this->set_last_node(bfp
);
818 node_algorithms::unlink_after(bfp
, lp
);
819 if(constant_time_size
){
820 this->priv_size_traits().set_size(this->priv_size_traits().get_size() - n
);
822 return last
.unconst();
826 //! <b>Effects</b>: Erases the element pointed by i of the list.
827 //! No destructors are called.
829 //! <b>Returns</b>: the first element remaining beyond the removed element,
830 //! or end() if no such element exists.
832 //! <b>Throws</b>: Nothing.
834 //! <b>Complexity</b>: Linear to the elements before i.
836 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
838 iterator
erase(const_iterator i
)
839 { return this->erase_after(this->previous(i
)); }
841 //! <b>Requires</b>: first and last must be valid iterator to elements in *this.
843 //! <b>Effects</b>: Erases the range pointed by b and e.
844 //! No destructors are called.
846 //! <b>Returns</b>: the first element remaining beyond the removed elements,
847 //! or end() if no such element exists.
849 //! <b>Throws</b>: Nothing.
851 //! <b>Complexity</b>: Linear to the elements before last.
853 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
855 iterator
erase(const_iterator first
, const_iterator last
)
856 { return this->erase_after(this->previous(first
), last
); }
858 //! <b>Effects</b>: Erases the range [first, last) from
859 //! the list. n must be std::distance(first, last).
860 //! No destructors are called.
862 //! <b>Returns</b>: the first element remaining beyond the removed elements,
863 //! or end() if no such element exists.
865 //! <b>Throws</b>: Nothing.
867 //! <b>Complexity</b>: linear to the elements before first if link_mode is normal_link
868 //! and constant_time_size is activated. Linear to the elements before last otherwise.
870 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
872 iterator
erase(const_iterator first
, const_iterator last
, difference_type n
)
873 { return this->erase_after(this->previous(first
), last
, n
); }
875 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
877 //! <b>Effects</b>: Erases the element after the element pointed by prev of
879 //! Disposer::operator()(pointer) is called for the removed element.
881 //! <b>Returns</b>: the first element remaining beyond the removed elements,
882 //! or end() if no such element exists.
884 //! <b>Throws</b>: Nothing.
886 //! <b>Complexity</b>: Constant.
888 //! <b>Note</b>: Invalidates the iterators to the erased element.
889 template<class Disposer
>
890 iterator
erase_after_and_dispose(const_iterator prev
, Disposer disposer
)
892 const_iterator
it(prev
);
894 node_ptr
to_erase(it
.pointed_node());
896 node_ptr
prev_n(prev
.pointed_node());
897 node_algorithms::unlink_after(prev_n
);
898 if(cache_last
&& (to_erase
== this->get_last_node())){
899 this->set_last_node(prev_n
);
901 if(safemode_or_autounlink
)
902 node_algorithms::init(to_erase
);
903 disposer(get_real_value_traits().to_value_ptr(to_erase
));
904 this->priv_size_traits().decrement();
910 template<class Disposer
>
911 static iterator
s_erase_after_and_dispose(const_iterator prev
, Disposer disposer
)
913 BOOST_STATIC_ASSERT(((!cache_last
)&&(!constant_time_size
)&&(!stateful_value_traits
)));
914 const_iterator
it(prev
);
916 node_ptr
to_erase(it
.pointed_node());
918 node_ptr
prev_n(prev
.pointed_node());
919 node_algorithms::unlink_after(prev_n
);
920 if(safemode_or_autounlink
)
921 node_algorithms::init(to_erase
);
922 disposer(real_value_traits::to_value_ptr(to_erase
));
926 static iterator
s_erase_after(const_iterator prev
)
927 { return s_erase_after_and_dispose(prev
, detail::null_disposer()); }
931 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
933 //! <b>Effects</b>: Erases the range (before_first, last) from
935 //! Disposer::operator()(pointer) is called for the removed elements.
937 //! <b>Returns</b>: the first element remaining beyond the removed elements,
938 //! or end() if no such element exists.
940 //! <b>Throws</b>: Nothing.
942 //! <b>Complexity</b>: Lineal to the elements (last - before_first + 1).
944 //! <b>Note</b>: Invalidates the iterators to the erased element.
945 template<class Disposer
>
946 iterator
erase_after_and_dispose(const_iterator before_first
, const_iterator last
, Disposer disposer
)
948 node_ptr
bfp(before_first
.pointed_node()), lp(last
.pointed_node());
949 node_ptr
fp(node_traits::get_next(bfp
));
950 node_algorithms::unlink_after(bfp
, lp
);
952 node_ptr
to_erase(fp
);
953 fp
= node_traits::get_next(fp
);
954 if(safemode_or_autounlink
)
955 node_algorithms::init(to_erase
);
956 disposer(get_real_value_traits().to_value_ptr(to_erase
));
957 this->priv_size_traits().decrement();
959 if(cache_last
&& (node_traits::get_next(bfp
) == this->get_end_node())){
960 this->set_last_node(bfp
);
962 return last
.unconst();
965 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
967 //! <b>Effects</b>: Erases the element pointed by i of the list.
968 //! No destructors are called.
969 //! Disposer::operator()(pointer) is called for the removed element.
971 //! <b>Returns</b>: the first element remaining beyond the removed element,
972 //! or end() if no such element exists.
974 //! <b>Throws</b>: Nothing.
976 //! <b>Complexity</b>: Linear to the elements before i.
978 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
980 template<class Disposer
>
981 iterator
erase_and_dispose(const_iterator i
, Disposer disposer
)
982 { return this->erase_after_and_dispose(this->previous(i
), disposer
); }
984 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
985 template<class Disposer
>
986 iterator
erase_and_dispose(iterator i
, Disposer disposer
)
987 { return this->erase_and_dispose(const_iterator(i
), disposer
); }
990 //! <b>Requires</b>: first and last must be valid iterator to elements in *this.
991 //! Disposer::operator()(pointer) shouldn't throw.
993 //! <b>Effects</b>: Erases the range pointed by b and e.
994 //! No destructors are called.
995 //! Disposer::operator()(pointer) is called for the removed elements.
997 //! <b>Returns</b>: the first element remaining beyond the removed elements,
998 //! or end() if no such element exists.
1000 //! <b>Throws</b>: Nothing.
1002 //! <b>Complexity</b>: Linear to the number of erased elements plus linear
1003 //! to the elements before first.
1005 //! <b>Note</b>: Invalidates the iterators (but not the references) to the
1006 //! erased elements.
1007 template<class Disposer
>
1008 iterator
erase_and_dispose(const_iterator first
, const_iterator last
, Disposer disposer
)
1009 { return this->erase_after_and_dispose(this->previous(first
), last
, disposer
); }
1011 //! <b>Requires</b>: Dereferencing iterator must yield
1012 //! an lvalue of type value_type.
1014 //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
1015 //! No destructors or copy constructors are called.
1017 //! <b>Throws</b>: Nothing.
1019 //! <b>Complexity</b>: Linear to the number of elements inserted plus
1020 //! linear to the elements contained in the list if it's a safe-mode
1021 //! or auto-unlink value.
1022 //! Linear to the number of elements inserted in the list otherwise.
1024 //! <b>Note</b>: Invalidates the iterators (but not the references)
1025 //! to the erased elements.
1026 template<class Iterator
>
1027 void assign(Iterator b
, Iterator e
)
1030 this->insert_after(this->cbefore_begin(), b
, e
);
1033 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1035 //! <b>Requires</b>: Dereferencing iterator must yield
1036 //! an lvalue of type value_type.
1038 //! <b>Effects</b>: Clears the list and inserts the range pointed by b and e.
1039 //! No destructors or copy constructors are called.
1040 //! Disposer::operator()(pointer) is called for the removed elements.
1042 //! <b>Throws</b>: Nothing.
1044 //! <b>Complexity</b>: Linear to the number of elements inserted plus
1045 //! linear to the elements contained in the list.
1047 //! <b>Note</b>: Invalidates the iterators (but not the references)
1048 //! to the erased elements.
1049 template<class Iterator
, class Disposer
>
1050 void dispose_and_assign(Disposer disposer
, Iterator b
, Iterator e
)
1052 this->clear_and_dispose(disposer
);
1053 this->insert_after(this->cbefore_begin(), b
, e
, disposer
);
1056 //! <b>Requires</b>: prev is an iterator to an element or x.end()/x.before_begin() in x.
1058 //! <b>Effects</b>: Transfers all the elements of list x to this list, after the
1059 //! the element pointed by prev. No destructors or copy constructors are called.
1061 //! <b>Returns</b>: The last element inserted of x or prev if x is empty.
1062 //! This iterator can be used as new "prev" iterator for a new splice_after call.
1063 //! that will splice new values after the previously spliced values.
1065 //! <b>Throws</b>: Nothing.
1067 //! <b>Complexity</b>: Linear to the elements contained in x.
1068 //! Constant-time if cache_last<> option is true.
1070 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1071 //! list. Iterators of this list and all the references are not invalidated.
1072 iterator
splice_after(const_iterator prev
, slist_impl
&x
)
1075 const_iterator
last_x(x
.previous(x
.end())); //<- constant time if cache_last is active
1076 node_ptr
prev_n(prev
.pointed_node());
1077 node_ptr
last_x_n(last_x
.pointed_node());
1079 x
.set_last_node(x
.get_root_node());
1080 if(node_traits::get_next(prev_n
) == this->get_end_node()){
1081 this->set_last_node(last_x_n
);
1084 node_algorithms::transfer_after( prev_n
, x
.before_begin().pointed_node(), last_x_n
);
1085 this->priv_size_traits().set_size(this->priv_size_traits().get_size() + x
.priv_size_traits().get_size());
1086 x
.priv_size_traits().set_size(size_type(0));
1087 return last_x
.unconst();
1090 return prev
.unconst();
1094 //! <b>Requires</b>: prev must point to an element contained by this list or
1095 //! to the before_begin() element. prev_ele must point to an element contained in list
1096 //! x or must be x.before_begin().
1098 //! <b>Effects</b>: Transfers the element after prev_ele, from list x to this list,
1099 //! after the element pointed by prev. No destructors or copy constructors are called.
1101 //! <b>Throws</b>: Nothing.
1103 //! <b>Complexity</b>: Constant.
1105 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1106 //! list. Iterators of this list and all the references are not invalidated.
1107 void splice_after(const_iterator prev_pos
, slist_impl
&x
, const_iterator prev_ele
)
1109 const_iterator elem
= prev_ele
;
1110 this->splice_after(prev_pos
, x
, prev_ele
, ++elem
, 1);
1113 //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
1114 //! before_begin(), and before_first and before_last belong to x and
1115 //! ++before_first != x.end() && before_last != x.end().
1117 //! <b>Effects</b>: Transfers the range (before_first, before_last] from list x to this
1118 //! list, after the element pointed by prev_pos.
1119 //! No destructors or copy constructors are called.
1121 //! <b>Throws</b>: Nothing.
1123 //! <b>Complexity</b>: Linear to the number of elements transferred
1124 //! if constant_time_size is true. Constant-time otherwise.
1126 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1127 //! list. Iterators of this list and all the references are not invalidated.
1128 void splice_after(const_iterator prev_pos
, slist_impl
&x
, const_iterator before_first
, const_iterator before_last
)
1130 if(constant_time_size
)
1131 this->splice_after(prev_pos
, x
, before_first
, before_last
, std::distance(before_first
, before_last
));
1133 this->priv_splice_after
1134 (prev_pos
.pointed_node(), x
, before_first
.pointed_node(), before_last
.pointed_node());
1137 //! <b>Requires</b>: prev_pos must be a dereferenceable iterator in *this or be
1138 //! before_begin(), and before_first and before_last belong to x and
1139 //! ++before_first != x.end() && before_last != x.end() and
1140 //! n == std::distance(before_first, before_last).
1142 //! <b>Effects</b>: Transfers the range (before_first, before_last] from list x to this
1143 //! list, after the element pointed by p. No destructors or copy constructors are called.
1145 //! <b>Throws</b>: Nothing.
1147 //! <b>Complexity</b>: Constant time.
1149 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1150 //! list. Iterators of this list and all the references are not invalidated.
1151 void splice_after(const_iterator prev_pos
, slist_impl
&x
, const_iterator before_first
, const_iterator before_last
, difference_type n
)
1154 BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(before_first
, before_last
) == n
);
1155 this->priv_splice_after
1156 (prev_pos
.pointed_node(), x
, before_first
.pointed_node(), before_last
.pointed_node());
1157 if(constant_time_size
){
1158 this->priv_size_traits().set_size(this->priv_size_traits().get_size() + n
);
1159 x
.priv_size_traits().set_size(x
.priv_size_traits().get_size() - n
);
1164 //! <b>Requires</b>: it is an iterator to an element in x.
1166 //! <b>Effects</b>: Transfers all the elements of list x to this list, before the
1167 //! the element pointed by it. No destructors or copy constructors are called.
1169 //! <b>Returns</b>: The last element inserted of x or the previous element
1170 //! of it if x is empty.
1171 //! This iterator can be used as new "prev" iterator for a new splice call.
1172 //! that will splice new values after the previously spliced values.
1174 //! <b>Throws</b>: Nothing.
1176 //! <b>Complexity</b>: Linear to the elements contained in x plus linear to
1177 //! the elements before it.
1178 //! Linear to the elements before it if cache_last<> option is true.
1179 //! Constant-time if cache_last<> option is true and it == end().
1181 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1182 //! list. Iterators of this list and all the references are not invalidated.
1183 iterator
splice(const_iterator it
, slist_impl
&x
)
1184 { return this->splice_after(this->previous(it
), x
); }
1186 //! <b>Requires</b>: it p must be a valid iterator of *this.
1187 //! elem must point to an element contained in list
1190 //! <b>Effects</b>: Transfers the element elem, from list x to this list,
1191 //! before the element pointed by pos. No destructors or copy constructors are called.
1193 //! <b>Throws</b>: Nothing.
1195 //! <b>Complexity</b>: Linear to the elements before pos and before elem.
1196 //! Linear to the elements before elem if cache_last<> option is true and pos == end().
1198 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1199 //! list. Iterators of this list and all the references are not invalidated.
1200 void splice(const_iterator pos
, slist_impl
&x
, const_iterator elem
)
1201 { return this->splice_after(this->previous(pos
), x
, x
.previous(elem
)); }
1203 //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
1204 //! and first and last belong to x and first and last a valid range on x.
1206 //! <b>Effects</b>: Transfers the range [first, last) from list x to this
1207 //! list, before the element pointed by pos.
1208 //! No destructors or copy constructors are called.
1210 //! <b>Throws</b>: Nothing.
1212 //! <b>Complexity</b>: Linear to the sum of elements before pos, first, and last
1213 //! plus linear to the number of elements transferred if constant_time_size is true.
1214 //! Linear to the sum of elements before first, and last
1215 //! plus linear to the number of elements transferred if constant_time_size is true
1216 //! if cache_last<> is true and pos == end()
1218 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1219 //! list. Iterators of this list and all the references are not invalidated.
1220 void splice(const_iterator pos
, slist_impl
&x
, const_iterator first
, const_iterator last
)
1221 { return this->splice_after(this->previous(pos
), x
, x
.previous(first
), x
.previous(last
)); }
1223 //! <b>Requires</b>: pos must be a dereferenceable iterator in *this
1224 //! and first and last belong to x and first and last a valid range on x.
1225 //! n == std::distance(first, last).
1227 //! <b>Effects</b>: Transfers the range [first, last) from list x to this
1228 //! list, before the element pointed by pos.
1229 //! No destructors or copy constructors are called.
1231 //! <b>Throws</b>: Nothing.
1233 //! <b>Complexity</b>: Linear to the sum of elements before pos, first, and last.
1234 //! Linear to the sum of elements before first and last
1235 //! if cache_last<> is true and pos == end().
1237 //! <b>Note</b>: Iterators of values obtained from list x now point to elements of this
1238 //! list. Iterators of this list and all the references are not invalidated.
1239 void splice(const_iterator pos
, slist_impl
&x
, const_iterator first
, const_iterator last
, difference_type n
)
1240 { return this->splice_after(this->previous(pos
), x
, x
.previous(first
), x
.previous(last
), n
); }
1242 //! <b>Effects</b>: This function sorts the list *this according to std::less<value_type>.
1243 //! The sort is stable, that is, the relative order of equivalent elements is preserved.
1245 //! <b>Throws</b>: If value_traits::node_traits::node
1246 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1247 //! or the predicate throws. Basic guarantee.
1249 //! <b>Complexity</b>: The number of comparisons is approximately N log N, where N
1250 //! is the list's size.
1252 //! <b>Note</b>: Iterators and references are not invalidated
1253 template<class Predicate
>
1254 void sort(Predicate p
)
1256 if (node_traits::get_next(node_traits::get_next(this->get_root_node()))
1257 != this->get_root_node()) {
1259 slist_impl counter
[64];
1261 const_iterator last_inserted
;
1262 while(!this->empty()){
1263 last_inserted
= this->cbegin();
1264 carry
.splice_after(carry
.cbefore_begin(), *this, this->cbefore_begin());
1266 while(i
< fill
&& !counter
[i
].empty()) {
1267 carry
.swap(counter
[i
]);
1268 last_inserted
= carry
.merge(counter
[i
++], p
);
1270 BOOST_INTRUSIVE_INVARIANT_ASSERT(counter
[i
].empty());
1272 node_ptr p
= node_algorithms::get_previous_node
1273 (last_inserted
.pointed_node(), carry
.cend().pointed_node());
1274 const_iterator
last_element(p
, this);
1275 if(constant_time_size
){
1276 counter
[i
].splice_after( counter
[i
].cbefore_begin(), carry
1277 , carry
.cbefore_begin(), last_element
1281 counter
[i
].splice_after( counter
[i
].cbefore_begin(), carry
1282 , carry
.cbefore_begin(), last_element
);
1288 for (int i
= 1; i
< fill
; ++i
)
1289 last_inserted
= counter
[i
].merge(counter
[i
-1], p
);
1290 BOOST_INTRUSIVE_INVARIANT_ASSERT(this->empty());
1292 node_ptr p
= node_algorithms::get_previous_node
1293 (last_inserted
.pointed_node(), counter
[--fill
].end().pointed_node());
1294 const_iterator
last_element(p
, this);
1295 if(constant_time_size
){
1296 this->splice_after( cbefore_begin(), counter
[fill
], counter
[fill
].cbefore_begin()
1297 , last_element
, counter
[fill
].size());
1300 this->splice_after( cbefore_begin(), counter
[fill
], counter
[fill
].cbefore_begin()
1306 //! <b>Requires</b>: p must be a comparison function that induces a strict weak
1307 //! ordering and both *this and x must be sorted according to that ordering
1308 //! The lists x and *this must be distinct.
1310 //! <b>Effects</b>: This function removes all of x's elements and inserts them
1311 //! in order into *this. The merge is stable; that is, if an element from *this is
1312 //! equivalent to one from x, then the element from *this will precede the one from x.
1314 //! <b>Throws</b>: If value_traits::node_traits::node
1315 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1316 //! or std::less<value_type> throws. Basic guarantee.
1318 //! <b>Complexity</b>: This function is linear time: it performs at most
1319 //! size() + x.size() - 1 comparisons.
1321 //! <b>Note</b>: Iterators and references are not invalidated.
1323 { this->sort(std::less
<value_type
>()); }
1325 //! <b>Requires</b>: p must be a comparison function that induces a strict weak
1326 //! ordering and both *this and x must be sorted according to that ordering
1327 //! The lists x and *this must be distinct.
1329 //! <b>Effects</b>: This function removes all of x's elements and inserts them
1330 //! in order into *this. The merge is stable; that is, if an element from *this is
1331 //! equivalent to one from x, then the element from *this will precede the one from x.
1333 //! <b>Returns</b>: An iterator to the last transferred value, end() is x is empty.
1335 //! <b>Throws</b>: If the predicate throws. Basic guarantee.
1337 //! <b>Complexity</b>: This function is linear time: it performs at most
1338 //! size() + x.size() - 1 comparisons.
1340 //! <b>Note</b>: Iterators and references are not invalidated.
1341 template<class Predicate
>
1342 iterator
merge(slist_impl
& x
, Predicate p
)
1344 const_iterator
e(this->cend()), ex(x
.cend()), bb(this->cbefore_begin()),
1345 bb_next
, last_inserted(e
);
1347 const_iterator
ibx_next(x
.cbefore_begin()), ibx(ibx_next
++);
1348 while (++(bb_next
= bb
) != e
&& !p(*ibx_next
, *bb_next
)){
1352 //Now transfer the rest to the end of the container
1353 last_inserted
= this->splice_after(bb
, x
);
1359 ibx
= ibx_next
; ++n
;
1360 } while(++(ibx_next
= ibx
) != ex
&& p(*ibx_next
, *bb_next
));
1361 this->splice_after(bb
, x
, x
.before_begin(), ibx
, n
);
1362 last_inserted
= ibx
;
1365 return last_inserted
.unconst();
1368 //! <b>Effects</b>: This function removes all of x's elements and inserts them
1369 //! in order into *this according to std::less<value_type>. The merge is stable;
1370 //! that is, if an element from *this is equivalent to one from x, then the element
1371 //! from *this will precede the one from x.
1373 //! <b>Throws</b>: if std::less<value_type> throws. Basic guarantee.
1375 //! <b>Complexity</b>: This function is linear time: it performs at most
1376 //! size() + x.size() - 1 comparisons.
1378 //! <b>Note</b>: Iterators and references are not invalidated
1379 void merge(slist_impl
& x
)
1380 { this->merge(x
, std::less
<value_type
>()); }
1382 //! <b>Effects</b>: Reverses the order of elements in the list.
1384 //! <b>Throws</b>: Nothing.
1386 //! <b>Complexity</b>: This function is linear to the contained elements.
1388 //! <b>Note</b>: Iterators and references are not invalidated
1391 if(cache_last
&& !this->empty()){
1392 this->set_last_node(node_traits::get_next(this->get_root_node()));
1394 this->priv_reverse(detail::bool_
<linear
>());
1397 //! <b>Effects</b>: Removes all the elements that compare equal to value.
1398 //! No destructors are called.
1400 //! <b>Throws</b>: If std::equal_to<value_type> throws. Basic guarantee.
1402 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1404 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1405 //! and iterators to elements that are not removed remain valid. This function is
1406 //! linear time: it performs exactly size() comparisons for equality.
1407 void remove(const_reference value
)
1408 { this->remove_if(detail::equal_to_value
<const_reference
>(value
)); }
1410 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1412 //! <b>Effects</b>: Removes all the elements that compare equal to value.
1413 //! Disposer::operator()(pointer) is called for every removed element.
1415 //! <b>Throws</b>: If std::equal_to<value_type> throws. Basic guarantee.
1417 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1419 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1420 //! and iterators to elements that are not removed remain valid.
1421 template<class Disposer
>
1422 void remove_and_dispose(const_reference value
, Disposer disposer
)
1423 { this->remove_and_dispose_if(detail::equal_to_value
<const_reference
>(value
), disposer
); }
1425 //! <b>Effects</b>: Removes all the elements for which a specified
1426 //! predicate is satisfied. No destructors are called.
1428 //! <b>Throws</b>: If pred throws. Basic guarantee.
1430 //! <b>Complexity</b>: Linear time. It performs exactly size() calls to the predicate.
1432 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1433 //! and iterators to elements that are not removed remain valid.
1434 template<class Pred
>
1435 void remove_if(Pred pred
)
1436 { this->remove_and_dispose_if(pred
, detail::null_disposer()); }
1438 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1440 //! <b>Effects</b>: Removes all the elements for which a specified
1441 //! predicate is satisfied.
1442 //! Disposer::operator()(pointer) is called for every removed element.
1444 //! <b>Throws</b>: If pred throws. Basic guarantee.
1446 //! <b>Complexity</b>: Linear time. It performs exactly size() comparisons for equality.
1448 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1449 //! and iterators to elements that are not removed remain valid.
1450 template<class Pred
, class Disposer
>
1451 void remove_and_dispose_if(Pred pred
, Disposer disposer
)
1453 const_iterator
bcur(this->before_begin()), cur(this->begin()), e(this->end());
1457 cur
= this->erase_after_and_dispose(bcur
, disposer
);
1465 this->set_last_node(bcur
.pointed_node());
1469 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1470 //! elements that are equal from the list. No destructors are called.
1472 //! <b>Throws</b>: If std::equal_to<value_type> throws. Basic guarantee.
1474 //! <b>Complexity</b>: Linear time (size()-1) comparisons calls to pred()).
1476 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1477 //! and iterators to elements that are not removed remain valid.
1479 { this->unique_and_dispose(std::equal_to
<value_type
>(), detail::null_disposer()); }
1481 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1482 //! elements that satisfy some binary predicate from the list.
1483 //! No destructors are called.
1485 //! <b>Throws</b>: If the predicate throws. Basic guarantee.
1487 //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
1489 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1490 //! and iterators to elements that are not removed remain valid.
1491 template<class BinaryPredicate
>
1492 void unique(BinaryPredicate pred
)
1493 { this->unique_and_dispose(pred
, detail::null_disposer()); }
1495 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1497 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1498 //! elements that satisfy some binary predicate from the list.
1499 //! Disposer::operator()(pointer) is called for every removed element.
1501 //! <b>Throws</b>: If std::equal_to<value_type> throws. Basic guarantee.
1503 //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
1505 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1506 //! and iterators to elements that are not removed remain valid.
1507 template<class Disposer
>
1508 void unique_and_dispose(Disposer disposer
)
1509 { this->unique(std::equal_to
<value_type
>(), disposer
); }
1511 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1513 //! <b>Effects</b>: Removes adjacent duplicate elements or adjacent
1514 //! elements that satisfy some binary predicate from the list.
1515 //! Disposer::operator()(pointer) is called for every removed element.
1517 //! <b>Throws</b>: If the predicate throws. Basic guarantee.
1519 //! <b>Complexity</b>: Linear time (size()-1) comparisons equality comparisons.
1521 //! <b>Note</b>: The relative order of elements that are not removed is unchanged,
1522 //! and iterators to elements that are not removed remain valid.
1523 template<class BinaryPredicate
, class Disposer
>
1524 void unique_and_dispose(BinaryPredicate pred
, Disposer disposer
)
1526 const_iterator
end_n(this->cend());
1527 const_iterator
bcur(this->cbegin());
1529 const_iterator
cur(bcur
);
1531 while(cur
!= end_n
) {
1532 if (pred(*bcur
, *cur
)){
1533 cur
= this->erase_after_and_dispose(bcur
, disposer
);
1541 this->set_last_node(bcur
.pointed_node());
1546 //! <b>Requires</b>: value must be a reference to a value inserted in a list.
1548 //! <b>Effects</b>: This function returns a const_iterator pointing to the element
1550 //! <b>Throws</b>: Nothing.
1552 //! <b>Complexity</b>: Constant time.
1554 //! <b>Note</b>: Iterators and references are not invalidated.
1555 //! This static function is available only if the <i>value traits</i>
1557 static iterator
s_iterator_to(reference value
)
1559 BOOST_STATIC_ASSERT((!stateful_value_traits
));
1560 //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(value)));
1561 return iterator (value_traits::to_node_ptr(value
), 0);
1564 //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
1566 //! <b>Effects</b>: This function returns an iterator pointing to the element.
1568 //! <b>Throws</b>: Nothing.
1570 //! <b>Complexity</b>: Constant time.
1572 //! <b>Note</b>: Iterators and references are not invalidated.
1573 //! This static function is available only if the <i>value traits</i>
1575 static const_iterator
s_iterator_to(const_reference value
)
1577 BOOST_STATIC_ASSERT((!stateful_value_traits
));
1578 //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(const_cast<reference> (value))));
1579 return const_iterator (value_traits::to_node_ptr(const_cast<reference
> (value
)), 0);
1582 //! <b>Requires</b>: value must be a reference to a value inserted in a list.
1584 //! <b>Effects</b>: This function returns a const_iterator pointing to the element
1586 //! <b>Throws</b>: Nothing.
1588 //! <b>Complexity</b>: Constant time.
1590 //! <b>Note</b>: Iterators and references are not invalidated.
1591 iterator
iterator_to(reference value
)
1593 //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(value)));
1594 return iterator (value_traits::to_node_ptr(value
), this);
1597 //! <b>Requires</b>: value must be a const reference to a value inserted in a list.
1599 //! <b>Effects</b>: This function returns an iterator pointing to the element.
1601 //! <b>Throws</b>: Nothing.
1603 //! <b>Complexity</b>: Constant time.
1605 //! <b>Note</b>: Iterators and references are not invalidated.
1606 const_iterator
iterator_to(const_reference value
) const
1608 //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(const_cast<reference> (value))));
1609 return const_iterator (value_traits::to_node_ptr(const_cast<reference
> (value
)), this);
1612 //! <b>Returns</b>: The iterator to the element before i in the list.
1613 //! Returns the end-iterator, if either i is the begin-iterator or the
1616 //! <b>Throws</b>: Nothing.
1618 //! <b>Complexity</b>: Linear to the number of elements before i.
1619 //! Constant if cache_last<> is true and i == end().
1620 iterator
previous(iterator i
)
1622 if(cache_last
&& (i
.pointed_node() == this->get_end_node())){
1623 return iterator(this->get_last_node(), this);
1626 (node_algorithms::get_previous_node
1627 (this->before_begin().pointed_node(), i
.pointed_node()), this);
1630 //! <b>Returns</b>: The const_iterator to the element before i in the list.
1631 //! Returns the end-const_iterator, if either i is the begin-const_iterator or
1632 //! the list is empty.
1634 //! <b>Throws</b>: Nothing.
1636 //! <b>Complexity</b>: Linear to the number of elements before i.
1637 //! Constant if cache_last<> is true and i == end().
1638 const_iterator
previous(const_iterator i
) const
1640 if(cache_last
&& (i
.pointed_node() == this->get_end_node())){
1641 return const_iterator(uncast(this->get_last_node()), this);
1643 return const_iterator
1644 (node_algorithms::get_previous_node
1645 (this->before_begin().pointed_node(), i
.pointed_node()), this);
1649 void priv_splice_after(node_ptr prev_pos_n
, slist_impl
&x
, node_ptr before_first_n
, node_ptr before_last_n
)
1651 if (before_first_n
!= before_last_n
&& prev_pos_n
!= before_first_n
&& prev_pos_n
!= before_last_n
)
1654 if(node_traits::get_next(prev_pos_n
) == this->get_end_node()){
1655 this->set_last_node(before_last_n
);
1657 if(node_traits::get_next(before_last_n
) == x
.get_end_node()){
1658 x
.set_last_node(before_first_n
);
1661 node_algorithms::transfer_after(prev_pos_n
, before_first_n
, before_last_n
);
1665 void priv_reverse(detail::bool_
<false>)
1666 { node_algorithms::reverse(this->get_root_node()); }
1668 void priv_reverse(detail::bool_
<true>)
1670 node_ptr new_first
= node_algorithms::reverse
1671 (node_traits::get_next(this->get_root_node()));
1672 node_traits::set_next(this->get_root_node(), new_first
);
1675 void priv_shift_backwards(size_type n
, detail::bool_
<false>)
1677 node_ptr last
= node_algorithms::move_forward(this->get_root_node(), (std::size_t)n
);
1678 if(cache_last
&& last
){
1679 this->set_last_node(last
);
1683 void priv_shift_backwards(size_type n
, detail::bool_
<true>)
1685 std::pair
<node_ptr
, node_ptr
> ret(
1686 node_algorithms::move_first_n_forward
1687 (node_traits::get_next(this->get_root_node()), (std::size_t)n
));
1689 node_traits::set_next(this->get_root_node(), ret
.first
);
1691 this->set_last_node(ret
.second
);
1696 void priv_shift_forward(size_type n
, detail::bool_
<false>)
1698 node_ptr last
= node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n
);
1699 if(cache_last
&& last
){
1700 this->set_last_node(last
);
1704 void priv_shift_forward(size_type n
, detail::bool_
<true>)
1706 std::pair
<node_ptr
, node_ptr
> ret(
1707 node_algorithms::move_first_n_backwards
1708 (node_traits::get_next(this->get_root_node()), (std::size_t)n
));
1710 node_traits::set_next(this->get_root_node(), ret
.first
);
1712 this->set_last_node(ret
.second
);
1717 static void priv_swap_cache_last(slist_impl
*this_impl
, slist_impl
*other_impl
)
1719 bool other_was_empty
= false;
1720 if(this_impl
->empty()){
1721 //Check if both are empty or
1722 if(other_impl
->empty())
1724 //If this is empty swap pointers
1725 slist_impl
*tmp
= this_impl
;
1726 this_impl
= other_impl
;
1728 other_was_empty
= true;
1731 other_was_empty
= other_impl
->empty();
1734 //Precondition: this is not empty
1735 node_ptr
other_old_last(other_impl
->get_last_node());
1736 node_ptr
other_bfirst(other_impl
->get_root_node());
1737 node_ptr
this_bfirst(this_impl
->get_root_node());
1738 node_ptr
this_old_last(this_impl
->get_last_node());
1740 //Move all nodes from this to other's beginning
1741 node_algorithms::transfer_after(other_bfirst
, this_bfirst
, this_old_last
);
1742 other_impl
->set_last_node(this_old_last
);
1744 if(other_was_empty
){
1745 this_impl
->set_last_node(this_bfirst
);
1748 //Move trailing nodes from other to this
1749 node_algorithms::transfer_after(this_bfirst
, this_old_last
, other_old_last
);
1750 this_impl
->set_last_node(other_old_last
);
1755 static void priv_swap_lists(node_ptr this_node
, node_ptr other_node
, detail::bool_
<false>)
1756 { node_algorithms::swap_nodes(this_node
, other_node
); }
1759 static void priv_swap_lists(node_ptr this_node
, node_ptr other_node
, detail::bool_
<true>)
1760 { node_algorithms::swap_trailing_nodes(this_node
, other_node
); }
1762 static slist_impl
&priv_container_from_end_iterator(const const_iterator
&end_iterator
)
1764 //Obtaining the container from the end iterator is not possible with linear
1765 //singly linked lists (because "end" is represented by the null pointer)
1766 BOOST_STATIC_ASSERT(!linear
);
1767 root_plus_size
*r
= detail::parent_from_member
<root_plus_size
, node
>
1768 ( detail::get_pointer(end_iterator
.pointed_node()), (&root_plus_size::root_
));
1769 data_t
*d
= detail::parent_from_member
<data_t
, root_plus_size
>
1770 ( r
, &data_t::root_plus_size_
);
1771 slist_impl
*s
= detail::parent_from_member
<slist_impl
, data_t
>(d
, &slist_impl::data_
);
1776 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1777 template<class T
, class ...Options
>
1779 template<class Config
>
1781 inline bool operator<
1782 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1783 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1785 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1787 { return std::lexicographical_compare(x
.begin(), x
.end(), y
.begin(), y
.end()); }
1789 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1790 template<class T
, class ...Options
>
1792 template<class Config
>
1795 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1796 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1798 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1801 typedef slist_impl
<Config
> slist_type
;
1802 typedef typename
slist_type::const_iterator const_iterator
;
1803 const bool C
= slist_type::constant_time_size
;
1804 if(C
&& x
.size() != y
.size()){
1807 const_iterator end1
= x
.end();
1809 const_iterator i1
= x
.begin();
1810 const_iterator i2
= y
.begin();
1812 while (i1
!= end1
&& *i1
== *i2
) {
1819 const_iterator end2
= y
.end();
1820 while (i1
!= end1
&& i2
!= end2
&& *i1
== *i2
) {
1824 return i1
== end1
&& i2
== end2
;
1828 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1829 template<class T
, class ...Options
>
1831 template<class Config
>
1833 inline bool operator!=
1834 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1835 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1837 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1839 { return !(x
== y
); }
1841 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1842 template<class T
, class ...Options
>
1844 template<class Config
>
1846 inline bool operator>
1847 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1848 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1850 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1854 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1855 template<class T
, class ...Options
>
1857 template<class Config
>
1859 inline bool operator<=
1860 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1861 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1863 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1865 { return !(y
< x
); }
1867 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1868 template<class T
, class ...Options
>
1870 template<class Config
>
1872 inline bool operator>=
1873 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1874 (const slist_impl
<T
, Options
...> &x
, const slist_impl
<T
, Options
...> &y
)
1876 (const slist_impl
<Config
> &x
, const slist_impl
<Config
> &y
)
1878 { return !(x
< y
); }
1880 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1881 template<class T
, class ...Options
>
1883 template<class Config
>
1886 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1887 (slist_impl
<T
, Options
...> &x
, slist_impl
<T
, Options
...> &y
)
1889 (slist_impl
<Config
> &x
, slist_impl
<Config
> &y
)
1893 //! Helper metafunction to define a \c slist that yields to the same type when the
1894 //! same options (either explicitly or implicitly) are used.
1895 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1896 template<class T
, class ...Options
>
1898 template<class T
, class O1
= none
, class O2
= none
, class O3
= none
, class O4
= none
, class O5
= none
>
1903 typedef typename pack_options
1904 < slist_defaults
<T
>,
1905 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1910 >::type packed_options
;
1911 typedef typename
detail::get_value_traits
1912 <T
, typename
packed_options::value_traits
>::type value_traits
;
1917 , typename
packed_options::size_type
1918 , packed_options::constant_time_size
1919 , packed_options::linear
1920 , packed_options::cache_last
1922 > implementation_defined
;
1924 typedef implementation_defined type
;
1928 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
1930 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1931 template<class T
, class O1
, class O2
, class O3
, class O4
, class O5
>
1933 template<class T
, class ...Options
>
1936 : public make_slist
<T
,
1937 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1944 typedef typename make_slist
1946 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
1952 typedef typename
Base::real_value_traits real_value_traits
;
1953 //Assert if passed value traits are compatible with the type
1954 BOOST_STATIC_ASSERT((detail::is_same
<typename
real_value_traits::value_type
, T
>::value
));
1956 typedef typename
Base::value_traits value_traits
;
1957 typedef typename
Base::iterator iterator
;
1958 typedef typename
Base::const_iterator const_iterator
;
1960 slist(const value_traits
&v_traits
= value_traits())
1964 template<class Iterator
>
1965 slist(Iterator b
, Iterator e
, const value_traits
&v_traits
= value_traits())
1966 : Base(b
, e
, v_traits
)
1969 static slist
&container_from_end_iterator(iterator end_iterator
)
1970 { return static_cast<slist
&>(Base::container_from_end_iterator(end_iterator
)); }
1972 static const slist
&container_from_end_iterator(const_iterator end_iterator
)
1973 { return static_cast<const slist
&>(Base::container_from_end_iterator(end_iterator
)); }
1978 } //namespace intrusive
1981 #include <boost/intrusive/detail/config_end.hpp>
1983 #endif //BOOST_INTRUSIVE_SLIST_HPP