Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Olaf Krzikalla 2004-2006. Chris@101: // (C) Copyright Ion Gaztanaga 2006-2014 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_LIST_HPP Chris@16: #define BOOST_INTRUSIVE_LIST_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: Chris@101: #include Chris@16: #include Chris@101: Chris@101: #include //std::less Chris@101: #include //std::size_t, etc. Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: Chris@101: struct default_list_hook_applier Chris@101: { template struct apply{ typedef typename T::default_list_hook type; }; }; Chris@101: Chris@101: template<> Chris@101: struct is_default_hook_tag Chris@101: { static const bool value = true; }; Chris@101: Chris@16: struct list_defaults Chris@16: { Chris@101: typedef default_list_hook_applier proto_value_traits; Chris@16: static const bool constant_time_size = true; Chris@16: typedef std::size_t size_type; Chris@101: typedef void header_holder_type; Chris@16: }; Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: //! The class template list is an intrusive container that mimics most of the Chris@16: //! interface of std::list as described in the C++ standard. Chris@16: //! Chris@16: //! The template parameter \c T is the type to be managed by the container. Chris@16: //! The user can specify additional options and if no options are provided Chris@16: //! default options are used. Chris@16: //! Chris@16: //! The container supports the following options: Chris@16: //! \c base_hook<>/member_hook<>/value_traits<>, Chris@16: //! \c constant_time_size<> and \c size_type<>. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: class list_impl Chris@16: { Chris@16: //Public typedefs Chris@16: public: Chris@101: typedef ValueTraits value_traits; Chris@101: typedef typename value_traits::pointer pointer; Chris@101: typedef typename value_traits::const_pointer const_pointer; Chris@16: typedef typename pointer_traits::element_type value_type; Chris@16: typedef typename pointer_traits::reference reference; Chris@16: typedef typename pointer_traits::reference const_reference; Chris@16: typedef typename pointer_traits::difference_type difference_type; Chris@16: typedef SizeType size_type; Chris@101: typedef list_iterator iterator; Chris@101: typedef list_iterator const_iterator; Chris@101: typedef boost::intrusive::reverse_iterator reverse_iterator; Chris@101: typedef boost::intrusive::reverse_iterator const_reverse_iterator; Chris@101: typedef typename value_traits::node_traits node_traits; Chris@16: typedef typename node_traits::node node; Chris@16: typedef typename node_traits::node_ptr node_ptr; Chris@16: typedef typename node_traits::const_node_ptr const_node_ptr; Chris@16: typedef circular_list_algorithms node_algorithms; Chris@101: typedef typename detail::get_header_holder_type Chris@101: < value_traits, HeaderHolder >::type header_holder_type; Chris@16: Chris@16: static const bool constant_time_size = ConstantTimeSize; Chris@101: static const bool stateful_value_traits = detail::is_stateful_value_traits::value; Chris@101: static const bool has_container_from_iterator = Chris@101: detail::is_same< header_holder_type, detail::default_header_holder< node_traits > >::value; Chris@16: Chris@16: /// @cond Chris@16: Chris@16: private: Chris@16: typedef detail::size_holder size_traits; Chris@16: Chris@16: //noncopyable Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(list_impl) Chris@16: Chris@101: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@16: Chris@16: //Constant-time size is incompatible with auto-unlink hooks! Chris@16: BOOST_STATIC_ASSERT(!(constant_time_size && Chris@101: ((int)value_traits::link_mode == (int)auto_unlink) Chris@16: )); Chris@16: Chris@16: node_ptr get_root_node() Chris@101: { return data_.root_plus_size_.m_header.get_node(); } Chris@16: Chris@16: const_node_ptr get_root_node() const Chris@101: { return data_.root_plus_size_.m_header.get_node(); } Chris@16: Chris@16: struct root_plus_size : public size_traits Chris@16: { Chris@101: header_holder_type m_header; Chris@16: }; Chris@16: Chris@16: struct data_t : public value_traits Chris@16: { Chris@16: typedef typename list_impl::value_traits value_traits; Chris@16: explicit data_t(const value_traits &val_traits) Chris@16: : value_traits(val_traits) Chris@16: {} Chris@16: Chris@16: root_plus_size root_plus_size_; Chris@16: } data_; Chris@16: Chris@16: size_traits &priv_size_traits() Chris@16: { return data_.root_plus_size_; } Chris@16: Chris@16: const size_traits &priv_size_traits() const Chris@16: { return data_.root_plus_size_; } Chris@16: Chris@16: const value_traits &priv_value_traits() const Chris@16: { return data_; } Chris@16: Chris@16: value_traits &priv_value_traits() Chris@16: { return data_; } Chris@16: Chris@101: typedef typename boost::intrusive::value_traits_pointers Chris@101: ::const_value_traits_ptr const_value_traits_ptr; Chris@16: Chris@101: const_value_traits_ptr priv_value_traits_ptr() const Chris@101: { return pointer_traits::pointer_to(this->priv_value_traits()); } Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: public: Chris@16: Chris@16: //! Effects: constructs an empty list. Chris@16: //! Chris@16: //! Complexity: Constant Chris@16: //! Chris@101: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks). Chris@16: explicit list_impl(const value_traits &v_traits = value_traits()) Chris@16: : data_(v_traits) Chris@16: { Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: node_algorithms::init_header(this->get_root_node()); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield an lvalue of type value_type. Chris@16: //! Chris@16: //! Effects: Constructs a list equal to the range [first,last). Chris@16: //! Chris@101: //! Complexity: Linear in distance(b, e). No copy constructors are called. Chris@16: //! Chris@101: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks). Chris@16: template Chris@16: list_impl(Iterator b, Iterator e, const value_traits &v_traits = value_traits()) Chris@16: : data_(v_traits) Chris@16: { Chris@101: //nothrow, no need to rollback to release elements on exception Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: node_algorithms::init_header(this->get_root_node()); Chris@101: //nothrow, no need to rollback to release elements on exception Chris@16: this->insert(this->cend(), b, e); Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: list_impl(BOOST_RV_REF(list_impl) x) Chris@16: : data_(::boost::move(x.priv_value_traits())) Chris@16: { Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: node_algorithms::init_header(this->get_root_node()); Chris@101: //nothrow, no need to rollback to release elements on exception Chris@16: this->swap(x); Chris@16: } Chris@16: Chris@16: //! Effects: to-do Chris@16: //! Chris@16: list_impl& operator=(BOOST_RV_REF(list_impl) x) Chris@16: { this->swap(x); return *this; } Chris@16: Chris@16: //! Effects: If it's not a safe-mode or an auto-unlink value_type Chris@16: //! the destructor does nothing Chris@16: //! (ie. no code is generated). Otherwise it detaches all elements from this. Chris@16: //! In this case the objects in the list are not deleted (i.e. no destructors Chris@16: //! are called), but the hooks according to the ValueTraits template parameter Chris@16: //! are set to their default value. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements in the list, if Chris@16: //! it's a safe-mode or auto-unlink value . Otherwise constant. Chris@16: ~list_impl() Chris@101: { Chris@101: if(is_safe_autounlink::value){ Chris@101: this->clear(); Chris@101: node_algorithms::init(this->get_root_node()); Chris@101: } Chris@101: } Chris@16: Chris@16: //! Requires: value must be an lvalue. Chris@16: //! Chris@16: //! Effects: Inserts the value in the back of the list. Chris@16: //! No copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: void push_back(reference value) Chris@16: { Chris@101: node_ptr to_insert = priv_value_traits().to_node_ptr(value); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); Chris@16: node_algorithms::link_before(this->get_root_node(), to_insert); Chris@16: this->priv_size_traits().increment(); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue. Chris@16: //! Chris@16: //! Effects: Inserts the value in the front of the list. Chris@16: //! No copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: void push_front(reference value) Chris@16: { Chris@101: node_ptr to_insert = priv_value_traits().to_node_ptr(value); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); Chris@16: node_algorithms::link_before(node_traits::get_next(this->get_root_node()), to_insert); Chris@16: this->priv_size_traits().increment(); Chris@16: } Chris@16: Chris@16: //! Effects: Erases the last element of the list. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the erased element. Chris@16: void pop_back() Chris@16: { return this->pop_back_and_dispose(detail::null_disposer()); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the last element of the list. Chris@16: //! No destructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed element. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators to the erased element. Chris@16: template Chris@16: void pop_back_and_dispose(Disposer disposer) Chris@16: { Chris@16: node_ptr to_erase = node_traits::get_previous(this->get_root_node()); Chris@16: node_algorithms::unlink(to_erase); Chris@16: this->priv_size_traits().decrement(); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@101: disposer(priv_value_traits().to_value_ptr(to_erase)); Chris@16: } Chris@16: Chris@16: //! Effects: Erases the first element of the list. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the erased element. Chris@16: void pop_front() Chris@16: { return this->pop_front_and_dispose(detail::null_disposer()); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the first element of the list. Chris@16: //! No destructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed element. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators to the erased element. Chris@16: template Chris@16: void pop_front_and_dispose(Disposer disposer) Chris@16: { Chris@16: node_ptr to_erase = node_traits::get_next(this->get_root_node()); Chris@16: node_algorithms::unlink(to_erase); Chris@16: this->priv_size_traits().decrement(); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@101: disposer(priv_value_traits().to_value_ptr(to_erase)); Chris@16: } Chris@16: Chris@16: //! Effects: Returns a reference to the first element of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: reference front() Chris@101: { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); } Chris@16: Chris@16: //! Effects: Returns a const_reference to the first element of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reference front() const Chris@101: { return *priv_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); } Chris@16: Chris@16: //! Effects: Returns a reference to the last element of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: reference back() Chris@101: { return *priv_value_traits().to_value_ptr(node_traits::get_previous(this->get_root_node())); } Chris@16: Chris@16: //! Effects: Returns a const_reference to the last element of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reference back() const Chris@101: { return *priv_value_traits().to_value_ptr(detail::uncast(node_traits::get_previous(this->get_root_node()))); } Chris@16: Chris@16: //! Effects: Returns an iterator to the first element contained in the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: iterator begin() Chris@101: { return iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the first element contained in the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_iterator begin() const Chris@16: { return this->cbegin(); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the first element contained in the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_iterator cbegin() const Chris@101: { return const_iterator(node_traits::get_next(this->get_root_node()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Effects: Returns an iterator to the end of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: iterator end() Chris@101: { return iterator(this->get_root_node(), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Effects: Returns a const_iterator to the end of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_iterator end() const Chris@16: { return this->cend(); } Chris@16: Chris@16: //! Effects: Returns a constant iterator to the end of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_iterator cend() const Chris@101: { return const_iterator(detail::uncast(this->get_root_node()), this->priv_value_traits_ptr()); } Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the beginning Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: reverse_iterator rbegin() Chris@16: { return reverse_iterator(this->end()); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reverse_iterator rbegin() const Chris@16: { return this->crbegin(); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the beginning Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reverse_iterator crbegin() const Chris@16: { return const_reverse_iterator(end()); } Chris@16: Chris@16: //! Effects: Returns a reverse_iterator pointing to the end Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: reverse_iterator rend() Chris@16: { return reverse_iterator(begin()); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reverse_iterator rend() const Chris@16: { return this->crend(); } Chris@16: Chris@16: //! Effects: Returns a const_reverse_iterator pointing to the end Chris@16: //! of the reversed list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: const_reverse_iterator crend() const Chris@16: { return const_reverse_iterator(this->begin()); } Chris@16: Chris@16: //! Precondition: end_iterator must be a valid end iterator Chris@16: //! of list. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the list associated to the end iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: static list_impl &container_from_end_iterator(iterator end_iterator) Chris@16: { return list_impl::priv_container_from_end_iterator(end_iterator); } Chris@16: Chris@16: //! Precondition: end_iterator must be a valid end const_iterator Chris@16: //! of list. Chris@16: //! Chris@16: //! Effects: Returns a const reference to the list associated to the end iterator Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: static const list_impl &container_from_end_iterator(const_iterator end_iterator) Chris@16: { return list_impl::priv_container_from_end_iterator(end_iterator); } Chris@16: Chris@16: //! Effects: Returns the number of the elements contained in the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements contained in the list. Chris@16: //! if constant-time size option is disabled. Constant time otherwise. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: size_type size() const Chris@16: { Chris@16: if(constant_time_size) Chris@16: return this->priv_size_traits().get_size(); Chris@16: else Chris@16: return node_algorithms::count(this->get_root_node()) - 1; Chris@16: } Chris@16: Chris@16: //! Effects: Returns true if the list contains no elements. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: bool empty() const Chris@16: { return node_algorithms::unique(this->get_root_node()); } Chris@16: Chris@16: //! Effects: Swaps the elements of x and *this. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: void swap(list_impl& other) Chris@16: { Chris@16: node_algorithms::swap_nodes(this->get_root_node(), other.get_root_node()); Chris@16: if(constant_time_size){ Chris@16: size_type backup = this->priv_size_traits().get_size(); Chris@16: this->priv_size_traits().set_size(other.priv_size_traits().get_size()); Chris@16: other.priv_size_traits().set_size(backup); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Moves backwards all the elements, so that the first Chris@16: //! element becomes the second, the second becomes the third... Chris@16: //! the last element becomes the first one. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of shifts. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: void shift_backwards(size_type n = 1) Chris@16: { node_algorithms::move_forward(this->get_root_node(), n); } Chris@16: Chris@16: //! Effects: Moves forward all the elements, so that the second Chris@16: //! element becomes the first, the third becomes the second... Chris@16: //! the first element becomes the last one. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of shifts. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: void shift_forward(size_type n = 1) Chris@16: { node_algorithms::move_backwards(this->get_root_node(), n); } Chris@16: Chris@16: //! Effects: Erases the element pointed by i of the list. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Returns: the first element remaining beyond the removed element, Chris@16: //! or end() if no such element exists. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the Chris@16: //! erased element. Chris@16: iterator erase(const_iterator i) Chris@16: { return this->erase_and_dispose(i, detail::null_disposer()); } Chris@16: Chris@16: //! Requires: b and e must be valid iterators to elements in *this. Chris@16: //! Chris@16: //! Effects: Erases the element range pointed by b and e Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Returns: the first element remaining beyond the removed elements, Chris@16: //! or end() if no such element exists. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of erased elements if it's a safe-mode Chris@16: //! or auto-unlink value, or constant-time size is enabled. Constant-time otherwise. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the Chris@16: //! erased elements. Chris@16: iterator erase(const_iterator b, const_iterator e) Chris@16: { Chris@16: if(safemode_or_autounlink || constant_time_size){ Chris@16: return this->erase_and_dispose(b, e, detail::null_disposer()); Chris@16: } Chris@16: else{ Chris@16: node_algorithms::unlink(b.pointed_node(), e.pointed_node()); Chris@16: return e.unconst(); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: b and e must be valid iterators to elements in *this. Chris@101: //! n must be distance(b, e). Chris@16: //! Chris@16: //! Effects: Erases the element range pointed by b and e Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Returns: the first element remaining beyond the removed elements, Chris@16: //! or end() if no such element exists. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of erased elements if it's a safe-mode Chris@16: //! or auto-unlink value is enabled. Constant-time otherwise. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the Chris@16: //! erased elements. Chris@101: iterator erase(const_iterator b, const_iterator e, size_type n) Chris@16: { Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(node_algorithms::distance(b.pointed_node(), e.pointed_node()) == n); Chris@16: if(safemode_or_autounlink || constant_time_size){ Chris@16: return this->erase_and_dispose(b, e, detail::null_disposer()); Chris@16: } Chris@16: else{ Chris@16: if(constant_time_size){ Chris@16: this->priv_size_traits().decrease(n); Chris@16: } Chris@16: node_algorithms::unlink(b.pointed_node(), e.pointed_node()); Chris@16: return e.unconst(); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the element pointed by i of the list. Chris@16: //! No destructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed element. Chris@16: //! Chris@16: //! Returns: the first element remaining beyond the removed element, Chris@16: //! or end() if no such element exists. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Invalidates the iterators to the erased element. Chris@16: template Chris@16: iterator erase_and_dispose(const_iterator i, Disposer disposer) Chris@16: { Chris@16: node_ptr to_erase(i.pointed_node()); Chris@16: ++i; Chris@16: node_algorithms::unlink(to_erase); Chris@16: this->priv_size_traits().decrement(); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@101: disposer(this->priv_value_traits().to_value_ptr(to_erase)); Chris@16: return i.unconst(); Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: iterator erase_and_dispose(iterator i, Disposer disposer) Chris@16: { return this->erase_and_dispose(const_iterator(i), disposer); } Chris@16: #endif Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases the element range pointed by b and e Chris@16: //! No destructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Returns: the first element remaining beyond the removed elements, Chris@16: //! or end() if no such element exists. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements erased. Chris@16: //! Chris@16: //! Note: Invalidates the iterators to the erased elements. Chris@16: template Chris@16: iterator erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer) Chris@16: { Chris@16: node_ptr bp(b.pointed_node()), ep(e.pointed_node()); Chris@16: node_algorithms::unlink(bp, ep); Chris@16: while(bp != ep){ Chris@16: node_ptr to_erase(bp); Chris@16: bp = node_traits::get_next(bp); Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@101: disposer(priv_value_traits().to_value_ptr(to_erase)); Chris@16: this->priv_size_traits().decrement(); Chris@16: } Chris@16: return e.unconst(); Chris@16: } Chris@16: Chris@16: //! Effects: Erases all the elements of the container. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements of the list. Chris@16: //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) to the erased elements. Chris@16: void clear() Chris@16: { Chris@16: if(safemode_or_autounlink){ Chris@16: this->clear_and_dispose(detail::null_disposer()); Chris@16: } Chris@16: else{ Chris@16: node_algorithms::init_header(this->get_root_node()); Chris@16: this->priv_size_traits().set_size(size_type(0)); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Erases all the elements of the container. Chris@16: //! No destructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements of the list. Chris@16: //! Chris@16: //! Note: Invalidates the iterators to the erased elements. Chris@16: template Chris@16: void clear_and_dispose(Disposer disposer) Chris@16: { Chris@16: const_iterator it(this->begin()), itend(this->end()); Chris@16: while(it != itend){ Chris@16: node_ptr to_erase(it.pointed_node()); Chris@16: ++it; Chris@16: if(safemode_or_autounlink) Chris@16: node_algorithms::init(to_erase); Chris@101: disposer(priv_value_traits().to_value_ptr(to_erase)); Chris@16: } Chris@16: node_algorithms::init_header(this->get_root_node()); Chris@16: this->priv_size_traits().set_size(0); Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Cloner should yield to nodes equivalent to the original nodes. Chris@16: //! Chris@16: //! Effects: Erases all the elements from *this Chris@16: //! calling Disposer::operator()(pointer), clones all the Chris@16: //! elements from src calling Cloner::operator()(const_reference ) Chris@16: //! and inserts them on *this. Chris@16: //! Chris@16: //! If cloner throws, all cloned elements are unlinked and disposed Chris@16: //! calling Disposer::operator()(pointer). Chris@16: //! Chris@16: //! Complexity: Linear to erased plus inserted elements. Chris@16: //! Chris@16: //! Throws: If cloner throws. Basic guarantee. Chris@16: template Chris@16: void clone_from(const list_impl &src, Cloner cloner, Disposer disposer) Chris@16: { Chris@16: this->clear_and_dispose(disposer); Chris@16: detail::exception_disposer Chris@16: rollback(*this, disposer); Chris@16: const_iterator b(src.begin()), e(src.end()); Chris@16: for(; b != e; ++b){ Chris@16: this->push_back(*cloner(*b)); Chris@16: } Chris@16: rollback.release(); Chris@16: } Chris@16: Chris@16: //! Requires: value must be an lvalue and p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Inserts the value before the position pointed by p. Chris@16: //! Chris@16: //! Returns: An iterator to the inserted element. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. No copy constructors are called. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: iterator insert(const_iterator p, reference value) Chris@16: { Chris@101: node_ptr to_insert = this->priv_value_traits().to_node_ptr(value); Chris@16: if(safemode_or_autounlink) Chris@16: BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); Chris@16: node_algorithms::link_before(p.pointed_node(), to_insert); Chris@16: this->priv_size_traits().increment(); Chris@101: return iterator(to_insert, this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield Chris@16: //! an lvalue of type value_type and p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Inserts the range pointed by b and e before the position p. Chris@16: //! No copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements inserted. Chris@16: //! Chris@16: //! Note: Does not affect the validity of iterators and references. Chris@16: template Chris@16: void insert(const_iterator p, Iterator b, Iterator e) Chris@16: { Chris@16: for (; b != e; ++b) Chris@16: this->insert(p, *b); Chris@16: } Chris@16: Chris@16: //! Requires: Dereferencing iterator must yield Chris@16: //! an lvalue of type value_type. Chris@16: //! Chris@16: //! Effects: Clears the list and inserts the range pointed by b and e. Chris@16: //! No destructors or copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements inserted plus Chris@16: //! linear to the elements contained in the list if it's a safe-mode Chris@16: //! or auto-unlink value. Chris@16: //! Linear to the number of elements inserted in the list otherwise. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. Chris@16: template Chris@16: void assign(Iterator b, Iterator e) Chris@16: { Chris@16: this->clear(); Chris@16: this->insert(this->cend(), b, e); Chris@16: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Requires: Dereferencing iterator must yield Chris@16: //! an lvalue of type value_type. Chris@16: //! Chris@16: //! Effects: Clears the list and inserts the range pointed by b and e. Chris@16: //! No destructors or copy constructors are called. Chris@16: //! Disposer::operator()(pointer) is called for the removed elements. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements inserted plus Chris@16: //! linear to the elements contained in the list. Chris@16: //! Chris@16: //! Note: Invalidates the iterators (but not the references) Chris@16: //! to the erased elements. Chris@16: template Chris@16: void dispose_and_assign(Disposer disposer, Iterator b, Iterator e) Chris@16: { Chris@16: this->clear_and_dispose(disposer); Chris@16: this->insert(this->cend(), b, e); Chris@16: } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Transfers all the elements of list x to this list, before the Chris@16: //! the element pointed by p. No destructors or copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Iterators of values obtained from list x now point to elements of Chris@16: //! this list. Iterators of this list and all the references are not invalidated. Chris@16: void splice(const_iterator p, list_impl& x) Chris@16: { Chris@16: if(!x.empty()){ Chris@16: node_algorithms::transfer Chris@16: (p.pointed_node(), x.begin().pointed_node(), x.end().pointed_node()); Chris@16: size_traits &thist = this->priv_size_traits(); Chris@16: size_traits &xt = x.priv_size_traits(); Chris@16: thist.increase(xt.get_size()); Chris@16: xt.set_size(size_type(0)); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! new_ele must point to an element contained in list x. Chris@16: //! Chris@16: //! Effects: Transfers the value pointed by new_ele, from list x to this list, Chris@101: //! before the element pointed by p. No destructors or copy constructors are called. Chris@16: //! If p == new_ele or p == ++new_ele, this function is a null operation. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Iterators of values obtained from list x now point to elements of this Chris@16: //! list. Iterators of this list and all the references are not invalidated. Chris@16: void splice(const_iterator p, list_impl&x, const_iterator new_ele) Chris@16: { Chris@16: node_algorithms::transfer(p.pointed_node(), new_ele.pointed_node()); Chris@16: x.priv_size_traits().decrement(); Chris@16: this->priv_size_traits().increment(); Chris@16: } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! f and e must point to elements contained in list x. Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by f and e from list x to this list, Chris@101: //! before the element pointed by p. No destructors or copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements transferred Chris@16: //! if constant-time size option is enabled. Constant-time otherwise. Chris@16: //! Chris@16: //! Note: Iterators of values obtained from list x now point to elements of this Chris@16: //! list. Iterators of this list and all the references are not invalidated. Chris@16: void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e) Chris@16: { Chris@16: if(constant_time_size) Chris@101: this->splice(p, x, f, e, node_algorithms::distance(f.pointed_node(), e.pointed_node())); Chris@16: else Chris@101: this->splice(p, x, f, e, 1);//intrusive::iterator_distance is a dummy value Chris@16: } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! f and e must point to elements contained in list x. Chris@101: //! n == distance(f, e) Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by f and e from list x to this list, Chris@101: //! before the element pointed by p. No destructors or copy constructors are called. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Iterators of values obtained from list x now point to elements of this Chris@16: //! list. Iterators of this list and all the references are not invalidated. Chris@101: void splice(const_iterator p, list_impl&x, const_iterator f, const_iterator e, size_type n) Chris@16: { Chris@16: if(n){ Chris@16: if(constant_time_size){ Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(n == node_algorithms::distance(f.pointed_node(), e.pointed_node())); Chris@16: node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node()); Chris@16: size_traits &thist = this->priv_size_traits(); Chris@16: size_traits &xt = x.priv_size_traits(); Chris@16: thist.increase(n); Chris@16: xt.decrease(n); Chris@16: } Chris@16: else{ Chris@16: node_algorithms::transfer(p.pointed_node(), f.pointed_node(), e.pointed_node()); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: This function sorts the list *this according to std::less. Chris@16: //! The sort is stable, that is, the relative order of equivalent elements is preserved. Chris@16: //! Chris@101: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) Chris@16: //! or std::less throws. Basic guarantee. Chris@16: //! Chris@16: //! Notes: Iterators and references are not invalidated. Chris@16: //! Chris@16: //! Complexity: The number of comparisons is approximately N log N, where N Chris@16: //! is the list's size. Chris@16: void sort() Chris@16: { this->sort(std::less()); } Chris@16: Chris@16: //! Requires: p must be a comparison function that induces a strict weak ordering Chris@16: //! Chris@16: //! Effects: This function sorts the list *this according to p. The sort is Chris@16: //! stable, that is, the relative order of equivalent elements is preserved. Chris@16: //! Chris@101: //! Throws: If value_traits::node_traits::node Chris@16: //! constructor throws (this does not happen with predefined Boost.Intrusive hooks) Chris@16: //! or the predicate throws. Basic guarantee. Chris@16: //! Chris@16: //! Notes: This won't throw if list_base_hook<> or Chris@16: //! list_member_hook are used. Chris@16: //! Iterators and references are not invalidated. Chris@16: //! Chris@16: //! Complexity: The number of comparisons is approximately N log N, where N Chris@16: //! is the list's size. Chris@16: template Chris@16: void sort(Predicate p) Chris@16: { Chris@16: if(node_traits::get_next(this->get_root_node()) Chris@16: != node_traits::get_previous(this->get_root_node())){ Chris@16: list_impl carry(this->priv_value_traits()); Chris@16: detail::array_initializer counter(this->priv_value_traits()); Chris@16: int fill = 0; Chris@16: while(!this->empty()){ Chris@16: carry.splice(carry.cbegin(), *this, this->cbegin()); Chris@16: int i = 0; Chris@16: while(i < fill && !counter[i].empty()) { Chris@16: counter[i].merge(carry, p); Chris@16: carry.swap(counter[i++]); Chris@16: } Chris@16: carry.swap(counter[i]); Chris@16: if(i == fill) Chris@16: ++fill; Chris@16: } Chris@16: for (int i = 1; i < fill; ++i) Chris@16: counter[i].merge(counter[i-1], p); Chris@16: this->swap(counter[fill-1]); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: This function removes all of x's elements and inserts them Chris@16: //! in order into *this according to std::less. The merge is stable; Chris@16: //! that is, if an element from *this is equivalent to one from x, then the element Chris@16: //! from *this will precede the one from x. Chris@16: //! Chris@16: //! Throws: If std::less throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: This function is linear time: it performs at most Chris@16: //! size() + x.size() - 1 comparisons. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated Chris@16: void merge(list_impl& x) Chris@16: { this->merge(x, std::less()); } Chris@16: Chris@16: //! Requires: p must be a comparison function that induces a strict weak Chris@16: //! ordering and both *this and x must be sorted according to that ordering Chris@16: //! The lists x and *this must be distinct. Chris@16: //! Chris@16: //! Effects: This function removes all of x's elements and inserts them Chris@16: //! in order into *this. The merge is stable; that is, if an element from *this is Chris@16: //! equivalent to one from x, then the element from *this will precede the one from x. Chris@16: //! Chris@16: //! Throws: If the predicate throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: This function is linear time: it performs at most Chris@16: //! size() + x.size() - 1 comparisons. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated. Chris@16: template Chris@16: void merge(list_impl& x, Predicate p) Chris@16: { Chris@16: const_iterator e(this->cend()), ex(x.cend()); Chris@16: const_iterator b(this->cbegin()); Chris@16: while(!x.empty()){ Chris@16: const_iterator ix(x.cbegin()); Chris@16: while (b != e && !p(*ix, *b)){ Chris@16: ++b; Chris@16: } Chris@16: if(b == e){ Chris@16: //Now transfer the rest to the end of the container Chris@16: this->splice(e, x); Chris@16: break; Chris@16: } Chris@16: else{ Chris@16: size_type n(0); Chris@16: do{ Chris@16: ++ix; ++n; Chris@16: } while(ix != ex && p(*ix, *b)); Chris@16: this->splice(b, x, x.begin(), ix, n); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Reverses the order of elements in the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: This function is linear time. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated Chris@16: void reverse() Chris@16: { node_algorithms::reverse(this->get_root_node()); } Chris@16: Chris@16: //! Effects: Removes all the elements that compare equal to value. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Throws: If std::equal_to throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time. It performs exactly size() comparisons for equality. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: void remove(const_reference value) Chris@16: { this->remove_if(detail::equal_to_value(value)); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Removes all the elements that compare equal to value. Chris@16: //! Disposer::operator()(pointer) is called for every removed element. Chris@16: //! Chris@16: //! Throws: If std::equal_to throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time. It performs exactly size() comparisons for equality. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void remove_and_dispose(const_reference value, Disposer disposer) Chris@16: { this->remove_and_dispose_if(detail::equal_to_value(value), disposer); } Chris@16: Chris@16: //! Effects: Removes all the elements for which a specified Chris@16: //! predicate is satisfied. No destructors are called. Chris@16: //! Chris@16: //! Throws: If pred throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time. It performs exactly size() calls to the predicate. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void remove_if(Pred pred) Chris@101: { Chris@101: const node_ptr root_node = this->get_root_node(); Chris@101: typename node_algorithms::stable_partition_info info; Chris@101: node_algorithms::stable_partition Chris@101: (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp(pred, &this->priv_value_traits()), info); Chris@101: //Invariants preserved by stable_partition so erase can be safely called Chris@101: //The first element might have changed so calculate it again Chris@101: this->erase( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr()) Chris@101: , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr()) Chris@101: , info.num_1st_partition); Chris@101: } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Removes all the elements for which a specified Chris@16: //! predicate is satisfied. Chris@16: //! Disposer::operator()(pointer) is called for every removed element. Chris@16: //! Chris@16: //! Throws: If pred throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time. It performs exactly size() comparisons for equality. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void remove_and_dispose_if(Pred pred, Disposer disposer) Chris@16: { Chris@101: const node_ptr root_node = this->get_root_node(); Chris@101: typename node_algorithms::stable_partition_info info; Chris@101: node_algorithms::stable_partition Chris@101: (node_traits::get_next(root_node), root_node, detail::key_nodeptr_comp(pred, &this->priv_value_traits()), info); Chris@101: //Invariants preserved by stable_partition so erase can be safely called Chris@101: //The first element might have changed so calculate it again Chris@101: this->erase_and_dispose( const_iterator(node_traits::get_next(root_node), this->priv_value_traits_ptr()) Chris@101: , const_iterator(info.beg_2st_partition, this->priv_value_traits_ptr()) Chris@101: , disposer); Chris@16: } Chris@16: Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that are equal from the list. No destructors are called. Chris@16: //! Chris@16: //! Throws: If std::equal_toComplexity: Linear time (size()-1 comparisons calls to pred()). Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: void unique() Chris@16: { this->unique_and_dispose(std::equal_to(), detail::null_disposer()); } Chris@16: Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that satisfy some binary predicate from the list. Chris@16: //! No destructors are called. Chris@16: //! Chris@16: //! Throws: If pred throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time (size()-1 comparisons equality comparisons). Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void unique(BinaryPredicate pred) Chris@16: { this->unique_and_dispose(pred, detail::null_disposer()); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that are equal from the list. Chris@16: //! Disposer::operator()(pointer) is called for every removed element. Chris@16: //! Chris@16: //! Throws: If std::equal_toComplexity: Linear time (size()-1) comparisons equality comparisons. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void unique_and_dispose(Disposer disposer) Chris@16: { this->unique_and_dispose(std::equal_to(), disposer); } Chris@16: Chris@16: //! Requires: Disposer::operator()(pointer) shouldn't throw. Chris@16: //! Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that satisfy some binary predicate from the list. Chris@16: //! Disposer::operator()(pointer) is called for every removed element. Chris@16: //! Chris@16: //! Throws: If pred throws. Basic guarantee. Chris@16: //! Chris@16: //! Complexity: Linear time (size()-1) comparisons equality comparisons. Chris@16: //! Chris@16: //! Note: The relative order of elements that are not removed is unchanged, Chris@16: //! and iterators to elements that are not removed remain valid. Chris@16: template Chris@16: void unique_and_dispose(BinaryPredicate pred, Disposer disposer) Chris@16: { Chris@16: const_iterator itend(this->cend()); Chris@16: const_iterator cur(this->cbegin()); Chris@16: Chris@16: if(cur != itend){ Chris@16: const_iterator after(cur); Chris@16: ++after; Chris@16: while(after != itend){ Chris@16: if(pred(*cur, *after)){ Chris@16: after = this->erase_and_dispose(after, disposer); Chris@16: } Chris@16: else{ Chris@16: cur = after; Chris@16: ++after; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: //! Requires: value must be a reference to a value inserted in a list. Chris@16: //! Chris@16: //! Effects: This function returns a const_iterator pointing to the element Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated. Chris@16: //! This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static iterator s_iterator_to(reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(value))); Chris@101: return iterator(value_traits::to_node_ptr(value), const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be a const reference to a value inserted in a list. Chris@16: //! Chris@16: //! Effects: This function returns an iterator pointing to the element. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated. Chris@16: //! This static function is available only if the value traits Chris@16: //! is stateless. Chris@16: static const_iterator s_iterator_to(const_reference value) Chris@16: { Chris@16: BOOST_STATIC_ASSERT((!stateful_value_traits)); Chris@101: reference r =*detail::uncast(pointer_traits::pointer_to(value)); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(value_traits::to_node_ptr(r))); Chris@101: return const_iterator(value_traits::to_node_ptr(r), const_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be a reference to a value inserted in a list. Chris@16: //! Chris@16: //! Effects: This function returns a const_iterator pointing to the element Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated. Chris@16: iterator iterator_to(reference value) Chris@16: { Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(value))); Chris@101: return iterator(this->priv_value_traits().to_node_ptr(value), this->priv_value_traits_ptr()); Chris@16: } Chris@16: Chris@16: //! Requires: value must be a const reference to a value inserted in a list. Chris@16: //! Chris@16: //! Effects: This function returns an iterator pointing to the element. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant time. Chris@16: //! Chris@16: //! Note: Iterators and references are not invalidated. Chris@16: const_iterator iterator_to(const_reference value) const Chris@16: { Chris@101: reference r = *detail::uncast(pointer_traits::pointer_to(value)); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(this->priv_value_traits().to_node_ptr(r))); Chris@101: return const_iterator(this->priv_value_traits().to_node_ptr(r), this->priv_value_traits_ptr()); Chris@101: } Chris@101: Chris@101: //! Effects: Asserts the integrity of the container. Chris@101: //! Chris@101: //! Complexity: Linear time. Chris@101: //! Chris@101: //! Note: The method has no effect when asserts are turned off (e.g., with NDEBUG). Chris@101: //! Experimental function, interface might change in future versions. Chris@101: void check() const Chris@101: { Chris@101: const_node_ptr header_ptr = get_root_node(); Chris@101: // header's next and prev are never null Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_next(header_ptr)); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(header_ptr)); Chris@101: // header's next and prev either both point to header (empty list) or neither does Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT((node_traits::get_next(header_ptr) == header_ptr) Chris@101: == (node_traits::get_previous(header_ptr) == header_ptr)); Chris@101: if (node_traits::get_next(header_ptr) == header_ptr) Chris@101: { Chris@101: if (constant_time_size) Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == 0); Chris@101: return; Chris@101: } Chris@101: size_t node_count = 0; Chris@101: const_node_ptr p = header_ptr; Chris@101: while (true) Chris@101: { Chris@101: const_node_ptr next_p = node_traits::get_next(p); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(next_p); Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(node_traits::get_previous(next_p) == p); Chris@101: p = next_p; Chris@101: if (p == header_ptr) break; Chris@101: ++node_count; Chris@101: } Chris@101: if (constant_time_size) Chris@101: BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_size_traits().get_size() == node_count); Chris@16: } Chris@16: Chris@16: /// @cond Chris@16: Chris@16: private: Chris@16: static list_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) Chris@16: { Chris@101: BOOST_STATIC_ASSERT((has_container_from_iterator)); Chris@101: node_ptr p = end_iterator.pointed_node(); Chris@101: header_holder_type* h = header_holder_type::get_holder(p); Chris@101: root_plus_size* r = detail::parent_from_member Chris@101: < root_plus_size, header_holder_type>(h, &root_plus_size::m_header); Chris@16: data_t *d = detail::parent_from_member Chris@16: ( r, &data_t::root_plus_size_); Chris@16: list_impl *s = detail::parent_from_member(d, &list_impl::data_); Chris@16: return *s; Chris@16: } Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator< Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@101: { return ::boost::intrusive::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: bool operator== Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@16: { Chris@101: typedef list_impl list_type; Chris@16: const bool C = list_type::constant_time_size; Chris@16: if(C && x.size() != y.size()){ Chris@16: return false; Chris@16: } Chris@101: return ::boost::intrusive::algo_equal(x.cbegin(), x.cend(), y.cbegin(), y.cend()); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator!= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@16: { return !(x == y); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator> Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@16: { return y < x; } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator<= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@16: { return !(y < x); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline bool operator>= Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (const list_impl &x, const list_impl &y) Chris@16: #else Chris@101: (const list_impl &x, const list_impl &y) Chris@16: #endif Chris@16: { return !(x < y); } Chris@16: Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: inline void swap Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) Chris@16: (list_impl &x, list_impl &y) Chris@16: #else Chris@101: (list_impl &x, list_impl &y) Chris@16: #endif Chris@16: { x.swap(y); } Chris@16: Chris@16: //! Helper metafunction to define a \c list that yields to the same type when the Chris@16: //! same options (either explicitly or implicitly) are used. Chris@16: #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@16: template Chris@16: #else Chris@101: template Chris@16: #endif Chris@16: struct make_list Chris@16: { Chris@16: /// @cond Chris@16: typedef typename pack_options Chris@16: < list_defaults, Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@101: O1, O2, O3, O4 Chris@16: #else Chris@16: Options... Chris@16: #endif Chris@16: >::type packed_options; Chris@16: Chris@16: typedef typename detail::get_value_traits Chris@16: ::type value_traits; Chris@16: typedef list_impl Chris@16: < Chris@16: value_traits, Chris@16: typename packed_options::size_type, Chris@101: packed_options::constant_time_size, Chris@101: typename packed_options::header_holder_type Chris@16: > implementation_defined; Chris@16: /// @endcond Chris@16: typedef implementation_defined type; Chris@16: }; Chris@16: Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: Chris@16: #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES) Chris@101: template Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class list Chris@16: : public make_list::type Chris@16: { Chris@16: typedef typename make_list Chris@16: ::type Base; Chris@16: //Assert if passed value traits are compatible with the type Chris@101: BOOST_STATIC_ASSERT((detail::is_same::value)); Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(list) Chris@16: Chris@16: public: Chris@16: typedef typename Base::value_traits value_traits; Chris@16: typedef typename Base::iterator iterator; Chris@16: typedef typename Base::const_iterator const_iterator; Chris@16: Chris@16: explicit list(const value_traits &v_traits = value_traits()) Chris@16: : Base(v_traits) Chris@16: {} Chris@16: Chris@16: template Chris@16: list(Iterator b, Iterator e, const value_traits &v_traits = value_traits()) Chris@16: : Base(b, e, v_traits) Chris@16: {} Chris@16: Chris@16: list(BOOST_RV_REF(list) x) Chris@101: : Base(BOOST_MOVE_BASE(Base, x)) Chris@16: {} Chris@16: Chris@16: list& operator=(BOOST_RV_REF(list) x) Chris@101: { return static_cast(this->Base::operator=(BOOST_MOVE_BASE(Base, x))); } Chris@16: Chris@16: static list &container_from_end_iterator(iterator end_iterator) Chris@16: { return static_cast(Base::container_from_end_iterator(end_iterator)); } Chris@16: Chris@16: static const list &container_from_end_iterator(const_iterator end_iterator) Chris@16: { return static_cast(Base::container_from_end_iterator(end_iterator)); } Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_LIST_HPP