comparison DEPENDENCIES/generic/include/boost/intrusive/list.hpp @ 16:2665513ce2d3

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