Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@101: // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/container for documentation. Chris@16: // Chris@101: ////////////////////////////////////////////////////////////////////////////// Chris@16: #ifndef BOOST_CONTAINER_LIST_HPP Chris@16: #define BOOST_CONTAINER_LIST_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@101: Chris@101: // container Chris@16: #include Chris@101: #include //new_allocator Chris@101: #include Chris@101: // container/detail Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: // move Chris@101: #include Chris@16: #include Chris@101: #include Chris@101: // move/detail Chris@101: #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@101: # include Chris@101: #endif Chris@16: #include Chris@101: Chris@101: // intrusive Chris@16: #include Chris@16: #include Chris@101: // other Chris@16: #include Chris@101: // std Chris@101: #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: #include Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace container { Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: namespace container_detail { Chris@16: Chris@16: template Chris@16: struct list_hook Chris@16: { Chris@16: typedef typename container_detail::bi::make_list_base_hook Chris@16: , container_detail::bi::link_mode >::type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct list_node Chris@16: : public list_hook::type Chris@16: { Chris@16: private: Chris@16: list_node(); Chris@16: Chris@16: public: Chris@16: typedef T value_type; Chris@16: typedef typename list_hook::type hook_type; Chris@16: Chris@16: T m_data; Chris@16: Chris@16: T &get_data() Chris@16: { return this->m_data; } Chris@16: Chris@16: const T &get_data() const Chris@16: { return this->m_data; } Chris@16: }; Chris@16: Chris@101: template Chris@101: struct iiterator_node_value_type< list_node > { Chris@101: typedef T type; Chris@101: }; Chris@101: Chris@16: template Chris@16: struct intrusive_list_type Chris@16: { Chris@16: typedef boost::container::allocator_traits allocator_traits_type; Chris@16: typedef typename allocator_traits_type::value_type value_type; Chris@16: typedef typename boost::intrusive::pointer_traits Chris@16: ::template Chris@16: rebind_pointer::type Chris@16: void_pointer; Chris@16: typedef typename container_detail::list_node Chris@16: node_type; Chris@16: typedef typename container_detail::bi::make_list Chris@16: < node_type Chris@16: , container_detail::bi::base_hook::type> Chris@16: , container_detail::bi::constant_time_size Chris@16: , container_detail::bi::size_type Chris@16: Chris@16: >::type container_type; Chris@16: typedef container_type type ; Chris@16: }; Chris@16: Chris@16: } //namespace container_detail { Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: //! A list is a doubly linked list. That is, it is a Sequence that supports both Chris@16: //! forward and backward traversal, and (amortized) constant time insertion and Chris@16: //! removal of elements at the beginning or the end, or in the middle. Lists have Chris@16: //! the important property that insertion and splicing do not invalidate iterators Chris@16: //! to list elements, and that even removal invalidates only the iterators that point Chris@16: //! to the elements that are removed. The ordering of iterators may be changed Chris@16: //! (that is, list::iterator might have a different predecessor or successor Chris@16: //! after a list operation than it did before), but the iterators themselves will Chris@16: //! not be invalidated or made to point to different elements unless that invalidation Chris@16: //! or mutation is explicit. Chris@101: //! Chris@101: //! \tparam T The type of object that is stored in the list Chris@101: //! \tparam Allocator The allocator used for all internal memory management Chris@16: #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@101: template > Chris@16: #else Chris@16: template Chris@16: #endif Chris@16: class list Chris@16: : protected container_detail::node_alloc_holder Chris@16: ::type> Chris@16: { Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: typedef typename Chris@16: container_detail::intrusive_list_type::type Icont; Chris@16: typedef container_detail::node_alloc_holder AllocHolder; Chris@16: typedef typename AllocHolder::NodePtr NodePtr; Chris@16: typedef typename AllocHolder::NodeAlloc NodeAlloc; Chris@16: typedef typename AllocHolder::ValAlloc ValAlloc; Chris@16: typedef typename AllocHolder::Node Node; Chris@16: typedef container_detail::allocator_destroyer Destroyer; Chris@16: typedef typename AllocHolder::alloc_version alloc_version; Chris@16: typedef boost::container::allocator_traits allocator_traits_type; Chris@101: typedef boost::container::equal_to_value equal_to_value_type; Chris@16: Chris@16: BOOST_COPYABLE_AND_MOVABLE(list) Chris@16: Chris@101: typedef container_detail::iterator_from_iiterator iterator_impl; Chris@101: typedef container_detail::iterator_from_iiterator const_iterator_impl; Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // types Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: typedef T value_type; Chris@16: typedef typename ::boost::container::allocator_traits::pointer pointer; Chris@16: typedef typename ::boost::container::allocator_traits::const_pointer const_pointer; Chris@16: typedef typename ::boost::container::allocator_traits::reference reference; Chris@16: typedef typename ::boost::container::allocator_traits::const_reference const_reference; Chris@16: typedef typename ::boost::container::allocator_traits::size_type size_type; Chris@16: typedef typename ::boost::container::allocator_traits::difference_type difference_type; Chris@16: typedef Allocator allocator_type; Chris@16: typedef BOOST_CONTAINER_IMPDEF(NodeAlloc) stored_allocator_type; Chris@16: typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator; Chris@16: typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator; Chris@101: typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator) reverse_iterator; Chris@101: typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator) const_reverse_iterator; Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // construct/copy/destroy Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Effects: Default constructs a list. Chris@16: //! Chris@16: //! Throws: If allocator_type's default constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: list() Chris@16: : AllocHolder() Chris@16: {} Chris@16: Chris@16: //! Effects: Constructs a list taking the allocator as parameter. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: explicit list(const allocator_type &a) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: : AllocHolder(a) Chris@16: {} Chris@16: Chris@101: //! Effects: Constructs a list Chris@101: //! and inserts n value-initialized value_types. Chris@16: //! Chris@101: //! Throws: If allocator_type's default constructor Chris@16: //! throws or T's default or copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to n. Chris@16: explicit list(size_type n) Chris@16: : AllocHolder(Allocator()) Chris@16: { this->resize(n); } Chris@16: Chris@16: //! Effects: Constructs a list that will use a copy of allocator a Chris@16: //! and inserts n copies of value. Chris@16: //! Chris@101: //! Throws: If allocator_type's default constructor Chris@101: //! throws or T's default or copy constructor throws. Chris@101: //! Chris@101: //! Complexity: Linear to n. Chris@101: list(size_type n, const allocator_type &a) Chris@101: : AllocHolder(a) Chris@101: { this->resize(n); } Chris@101: Chris@101: //! Effects: Constructs a list that will use a copy of allocator a Chris@101: //! and inserts n copies of value. Chris@101: //! Chris@101: //! Throws: If allocator_type's default constructor Chris@16: //! throws or T's default or copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to n. Chris@16: list(size_type n, const T& value, const Allocator& a = Allocator()) Chris@16: : AllocHolder(a) Chris@16: { this->insert(this->cbegin(), n, value); } Chris@16: Chris@16: //! Effects: Copy constructs a list. Chris@16: //! Chris@16: //! Postcondition: x == *this. Chris@16: //! Chris@101: //! Throws: If allocator_type's default constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to the elements x contains. Chris@16: list(const list& x) Chris@16: : AllocHolder(x) Chris@16: { this->insert(this->cbegin(), x.begin(), x.end()); } Chris@16: Chris@101: //! Effects: Move constructor. Moves x's resources to *this. Chris@16: //! Chris@16: //! Throws: If allocator_type's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: list(BOOST_RV_REF(list) x) Chris@101: : AllocHolder(BOOST_MOVE_BASE(AllocHolder, x)) Chris@16: {} Chris@16: Chris@16: //! Effects: Copy constructs a list using the specified allocator. Chris@16: //! Chris@16: //! Postcondition: x == *this. Chris@16: //! Chris@16: //! Throws: If allocator_type's default constructor or copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to the elements x contains. Chris@16: list(const list& x, const allocator_type &a) Chris@16: : AllocHolder(a) Chris@16: { this->insert(this->cbegin(), x.begin(), x.end()); } Chris@16: Chris@16: //! Effects: Move constructor sing the specified allocator. Chris@101: //! Moves x's resources to *this. Chris@16: //! Chris@16: //! Throws: If allocation or value_type's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant if a == x.get_allocator(), linear otherwise. Chris@16: list(BOOST_RV_REF(list) x, const allocator_type &a) Chris@16: : AllocHolder(a) Chris@16: { Chris@16: if(this->node_alloc() == x.node_alloc()){ Chris@16: this->icont().swap(x.icont()); Chris@16: } Chris@16: else{ Chris@101: this->insert(this->cbegin(), boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end())); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Constructs a list that will use a copy of allocator a Chris@16: //! and inserts a copy of the range [first, last) in the list. Chris@16: //! Chris@101: //! Throws: If allocator_type's default constructor Chris@101: //! throws or T's constructor taking a dereferenced InIt throws. Chris@16: //! Chris@16: //! Complexity: Linear to the range [first, last). Chris@16: template Chris@16: list(InpIt first, InpIt last, const Allocator &a = Allocator()) Chris@16: : AllocHolder(a) Chris@16: { this->insert(this->cbegin(), first, last); } Chris@16: Chris@101: Chris@101: #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: //! Effects: Constructs a list that will use a copy of allocator a Chris@101: //! and inserts a copy of the range [il.begin(), il.end()) in the list. Chris@101: //! Chris@101: //! Throws: If allocator_type's default constructor Chris@101: //! throws or T's constructor taking a dereferenced Chris@101: //! std::initializer_list iterator throws. Chris@101: //! Chris@101: //! Complexity: Linear to the range [il.begin(), il.end()). Chris@101: list(std::initializer_list il, const Allocator &a = Allocator()) Chris@101: : AllocHolder(a) Chris@101: { this->insert(this->cbegin(), il.begin(), il.end()); } Chris@101: #endif Chris@101: Chris@16: //! Effects: Destroys the list. All stored values are destroyed Chris@16: //! and used memory is deallocated. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements. Chris@101: ~list() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: {} //AllocHolder clears the list Chris@16: Chris@16: //! Effects: Makes *this contain the same elements as x. Chris@16: //! Chris@16: //! Postcondition: this->size() == x.size(). *this contains a copy Chris@16: //! of each of x's elements. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements in x. Chris@16: list& operator=(BOOST_COPY_ASSIGN_REF(list) x) Chris@16: { Chris@16: if (&x != this){ Chris@16: NodeAlloc &this_alloc = this->node_alloc(); Chris@16: const NodeAlloc &x_alloc = x.node_alloc(); Chris@16: container_detail::bool_ flag; Chris@16: if(flag && this_alloc != x_alloc){ Chris@16: this->clear(); Chris@16: } Chris@16: this->AllocHolder::copy_assign_alloc(x); Chris@16: this->assign(x.begin(), x.end()); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@101: //! Effects: Move assignment. All x's values are transferred to *this. Chris@16: //! Chris@16: //! Postcondition: x.empty(). *this contains a the elements x had Chris@16: //! before the function. Chris@16: //! Chris@101: //! Throws: If allocator_traits_type::propagate_on_container_move_assignment Chris@101: //! is false and (allocation throws or value_type's move constructor throws) Chris@16: //! Chris@101: //! Complexity: Constant if allocator_traits_type:: Chris@101: //! propagate_on_container_move_assignment is true or Chris@101: //! this->get>allocator() == x.get_allocator(). Linear otherwise. Chris@16: list& operator=(BOOST_RV_REF(list) x) Chris@101: BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value Chris@101: || allocator_traits_type::is_always_equal::value) Chris@16: { Chris@101: BOOST_ASSERT(this != &x); Chris@101: NodeAlloc &this_alloc = this->node_alloc(); Chris@101: NodeAlloc &x_alloc = x.node_alloc(); Chris@101: const bool propagate_alloc = allocator_traits_type:: Chris@101: propagate_on_container_move_assignment::value; Chris@101: const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal; Chris@101: //Resources can be transferred if both allocators are Chris@101: //going to be equal after this function (either propagated or already equal) Chris@101: if(propagate_alloc || allocators_equal){ Chris@101: //Destroy Chris@101: this->clear(); Chris@101: //Move allocator if needed Chris@101: this->AllocHolder::move_assign_alloc(x); Chris@101: //Obtain resources Chris@101: this->icont() = boost::move(x.icont()); Chris@101: } Chris@101: //Else do a one by one move Chris@101: else{ Chris@101: this->assign( boost::make_move_iterator(x.begin()) Chris@101: , boost::make_move_iterator(x.end())); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: //! Effects: Makes *this contain the same elements as il. Chris@101: //! Chris@101: //! Postcondition: this->size() == il.size(). *this contains a copy Chris@101: //! of each of x's elements. Chris@101: //! Chris@101: //! Throws: If memory allocation throws or T's copy constructor throws. Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in x. Chris@101: list& operator=(std::initializer_list il) Chris@101: { Chris@101: assign(il.begin(), il.end()); Chris@101: return *this; Chris@101: } Chris@101: #endif Chris@101: Chris@16: //! Effects: Assigns the n copies of val to *this. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to n. Chris@16: void assign(size_type n, const T& val) Chris@16: { Chris@16: typedef constant_iterator cvalue_iterator; Chris@16: return this->assign(cvalue_iterator(val, n), cvalue_iterator()); Chris@16: } Chris@16: Chris@16: //! Effects: Assigns the the range [first, last) to *this. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's constructor from dereferencing InpIt throws. Chris@16: //! Chris@16: //! Complexity: Linear to n. Chris@16: template Chris@16: void assign(InpIt first, InpIt last Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: >::type * = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: iterator first1 = this->begin(); Chris@16: const iterator last1 = this->end(); Chris@16: for ( ; first1 != last1 && first != last; ++first1, ++first) Chris@16: *first1 = *first; Chris@16: if (first == last) Chris@16: this->erase(first1, last1); Chris@16: else{ Chris@16: this->insert(last1, first, last); Chris@16: } Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: //! Effects: Assigns the the range [il.begin(), il.end()) to *this. Chris@101: //! Chris@101: //! Throws: If memory allocation throws or Chris@101: //! T's constructor from dereferencing std::initializer_list iterator throws. Chris@101: //! Chris@101: //! Complexity: Linear to n. Chris@101: void assign(std::initializer_list il) Chris@101: { assign(il.begin(), il.end()); } Chris@101: #endif Chris@101: Chris@16: //! Effects: Returns a copy of the internal allocator. Chris@16: //! Chris@16: //! Throws: If allocator's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return allocator_type(this->node_alloc()); } Chris@16: Chris@16: //! Effects: Returns a reference to the internal allocator. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Non-standard extension. Chris@101: stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->node_alloc(); } Chris@16: Chris@16: //! Effects: Returns a reference to the internal allocator. Chris@16: //! Chris@16: //! Throws: Nothing Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: //! Chris@16: //! Note: Non-standard extension. Chris@101: const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->node_alloc(); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // iterators Chris@16: // Chris@16: ////////////////////////////////////////////// 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@101: iterator begin() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return iterator(this->icont().begin()); } 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@101: const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->cbegin(); } 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@101: iterator end() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return iterator(this->icont().end()); } 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@101: const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->cend(); } 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@101: reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return reverse_iterator(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@101: const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->crbegin(); } 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@101: reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW 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@101: const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->crend(); } 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@101: const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_iterator(this->non_const_icont().begin()); } 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@101: const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_iterator(this->non_const_icont().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@101: const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_reverse_iterator(this->cend()); } 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@101: const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return const_reverse_iterator(this->cbegin()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // capacity Chris@16: // 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@101: bool empty() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return !this->size(); } 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: Constant. Chris@101: size_type size() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return this->icont().size(); } Chris@16: Chris@16: //! Effects: Returns the largest possible size of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return AllocHolder::max_size(); } Chris@16: Chris@16: //! Effects: Inserts or erases elements at the end such that Chris@16: //! the size becomes n. New elements are value initialized. Chris@16: //! Chris@16: //! Throws: If memory allocation throws, or T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to the difference between size() and new_size. Chris@16: void resize(size_type new_size) Chris@16: { Chris@16: if(!priv_try_shrink(new_size)){ Chris@16: typedef value_init_construct_iterator value_init_iterator; Chris@16: this->insert(this->cend(), value_init_iterator(new_size - this->size()), value_init_iterator()); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Effects: Inserts or erases elements at the end such that Chris@16: //! the size becomes n. New elements are copy constructed from x. Chris@16: //! Chris@16: //! Throws: If memory allocation throws, or T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to the difference between size() and new_size. Chris@16: void resize(size_type new_size, const T& x) Chris@16: { Chris@16: if(!priv_try_shrink(new_size)){ Chris@16: this->insert(this->cend(), new_size - this->size(), x); Chris@16: } Chris@16: } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // element access Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Requires: !empty() Chris@16: //! Chris@16: //! Effects: Returns a reference to the first element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: reference front() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *this->begin(); } Chris@16: Chris@16: //! Requires: !empty() Chris@16: //! Chris@16: //! Effects: Returns a const reference to the first element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *this->begin(); } Chris@16: Chris@16: //! Requires: !empty() Chris@16: //! Chris@16: //! Effects: Returns a reference to the first element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: reference back() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *(--this->end()); } Chris@16: Chris@16: //! Requires: !empty() Chris@16: //! Chris@16: //! Effects: Returns a const reference to the first element Chris@16: //! from the beginning of the container. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@101: const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return *(--this->end()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // modifiers Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: Chris@16: //! Effects: Inserts an object of type T constructed with Chris@16: //! std::forward(args)... in the end of the list. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's in-place constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant Chris@16: template Chris@101: void emplace_back(BOOST_FWD_REF(Args)... args) Chris@16: { this->emplace(this->cend(), boost::forward(args)...); } Chris@16: Chris@16: //! Effects: Inserts an object of type T constructed with Chris@16: //! std::forward(args)... in the beginning of the list. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's in-place constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant Chris@16: template Chris@101: void emplace_front(BOOST_FWD_REF(Args)... args) Chris@16: { this->emplace(this->cbegin(), boost::forward(args)...); } Chris@16: Chris@16: //! Effects: Inserts an object of type T constructed with Chris@16: //! std::forward(args)... before p. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's in-place constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant Chris@16: template Chris@101: iterator emplace(const_iterator p, BOOST_FWD_REF(Args)... args) Chris@16: { Chris@16: NodePtr pnode(AllocHolder::create_node(boost::forward(args)...)); Chris@16: return iterator(this->icont().insert(p.get(), *pnode)); Chris@16: } Chris@16: Chris@101: #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@101: #define BOOST_CONTAINER_LIST_EMPLACE_CODE(N) \ Chris@101: BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \ Chris@101: void emplace_back(BOOST_MOVE_UREF##N)\ Chris@101: { this->emplace(this->cend() BOOST_MOVE_I##N BOOST_MOVE_FWD##N); }\ Chris@101: \ Chris@101: BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \ Chris@101: void emplace_front(BOOST_MOVE_UREF##N)\ Chris@101: { this->emplace(this->cbegin() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);}\ Chris@101: \ Chris@101: BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \ Chris@101: iterator emplace(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\ Chris@101: {\ Chris@101: NodePtr pnode (AllocHolder::create_node(BOOST_MOVE_FWD##N));\ Chris@101: return iterator(this->icont().insert(p.get(), *pnode));\ Chris@101: }\ Chris@101: // Chris@101: BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_LIST_EMPLACE_CODE) Chris@101: #undef BOOST_CONTAINER_LIST_EMPLACE_CODE Chris@16: Chris@101: #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: //! Effects: Inserts a copy of x at the beginning of the list. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: void push_front(const T &x); Chris@16: Chris@16: //! Effects: Constructs a new element in the beginning of the list Chris@101: //! and moves the resources of x to this new element. Chris@16: //! Chris@16: //! Throws: If memory allocation throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: void push_front(T &&x); Chris@16: #else Chris@16: BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front) Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: //! Effects: Inserts a copy of x at the end of the list. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or Chris@16: //! T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: void push_back(const T &x); Chris@16: Chris@16: //! Effects: Constructs a new element in the end of the list Chris@101: //! and moves the resources of x to this new element. Chris@16: //! Chris@16: //! Throws: If memory allocation throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@16: void push_back(T &&x); Chris@16: #else Chris@16: BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back) Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@101: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@101: //! Effects: Insert a copy of x before p. Chris@16: //! Chris@16: //! Returns: an iterator to the inserted element. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or x's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@101: iterator insert(const_iterator p, const T &x); Chris@16: Chris@101: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@101: //! Effects: Insert a new element before p with x's resources. Chris@16: //! Chris@16: //! Returns: an iterator to the inserted element. Chris@16: //! Chris@16: //! Throws: If memory allocation throws. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@101: iterator insert(const_iterator p, T &&x); Chris@16: #else Chris@16: BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator) Chris@16: #endif Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Inserts n copies of x before p. Chris@16: //! Chris@16: //! Returns: an iterator to the first inserted element or p if n is 0. Chris@16: //! Chris@16: //! Throws: If memory allocation throws or T's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Linear to n. Chris@16: iterator insert(const_iterator p, size_type n, const T& x) Chris@16: { Chris@16: typedef constant_iterator cvalue_iterator; Chris@16: return this->insert(p, cvalue_iterator(x, n), cvalue_iterator()); Chris@16: } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Insert a copy of the [first, last) range before p. Chris@16: //! Chris@16: //! Returns: an iterator to the first inserted element or p if first == last. Chris@16: //! Chris@16: //! Throws: If memory allocation throws, T's constructor from a Chris@16: //! dereferenced InpIt throws. Chris@16: //! Chris@101: //! Complexity: Linear to distance [first, last). Chris@16: template Chris@16: iterator insert(const_iterator p, InpIt first, InpIt last Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && (container_detail::is_input_iterator::value Chris@101: || container_detail::is_same::value Chris@16: ) Chris@16: >::type * = 0 Chris@16: #endif Chris@16: ) Chris@16: { Chris@16: const typename Icont::iterator ipos(p.get()); Chris@16: iterator ret_it(ipos); Chris@16: if(first != last){ Chris@16: ret_it = iterator(this->icont().insert(ipos, *this->create_node_from_it(first))); Chris@16: ++first; Chris@16: } Chris@16: for (; first != last; ++first){ Chris@16: this->icont().insert(ipos, *this->create_node_from_it(first)); Chris@16: } Chris@16: return ret_it; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: template Chris@16: iterator insert(const_iterator p, FwdIt first, FwdIt last Chris@16: , typename container_detail::enable_if_c Chris@16: < !container_detail::is_convertible::value Chris@16: && !(container_detail::is_input_iterator::value Chris@101: || container_detail::is_same::value Chris@16: ) Chris@16: >::type * = 0 Chris@16: ) Chris@16: { Chris@16: //Optimized allocation and construction Chris@16: insertion_functor func(this->icont(), p.get()); Chris@16: iterator before_p(p.get()); Chris@16: --before_p; Chris@101: this->allocate_many_and_construct(first, boost::container::iterator_distance(first, last), func); Chris@16: return ++before_p; Chris@16: } Chris@16: #endif Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: //! Requires: p must be a valid iterator of *this. Chris@101: //! Chris@101: //! Effects: Insert a copy of the [il.begin(), il.end()) range before p. Chris@101: //! Chris@101: //! Returns: an iterator to the first inserted element or p if if.begin() == il.end(). Chris@101: //! Chris@101: //! Throws: If memory allocation throws, T's constructor from a Chris@101: //! dereferenced std::initializer_list iterator throws. Chris@101: //! Chris@101: //! Complexity: Linear to distance [il.begin(), il.end()). Chris@101: iterator insert(const_iterator p, std::initializer_list il) Chris@101: { return insert(p, il.begin(), il.end()); } Chris@101: #endif Chris@101: Chris@16: //! Effects: Removes the first element from the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@101: void pop_front() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { this->erase(this->cbegin()); } Chris@16: Chris@16: //! Effects: Removes the last element from the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@101: void pop_back() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { const_iterator tmp = this->cend(); this->erase(--tmp); } Chris@16: Chris@16: //! Requires: p must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Erases the element at p p. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Amortized constant time. Chris@101: iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return iterator(this->icont().erase_and_dispose(p.get(), Destroyer(this->node_alloc()))); } Chris@16: Chris@16: //! Requires: first and last must be valid iterator to elements in *this. Chris@16: //! Chris@16: //! Effects: Erases the elements pointed by [first, last). Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the distance between first and last. Chris@101: iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version())); } Chris@16: Chris@16: //! Effects: Swaps the contents of *this and x. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: void swap(list& x) Chris@101: BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value Chris@101: || allocator_traits_type::is_always_equal::value) Chris@16: { AllocHolder::swap(x); } Chris@16: Chris@16: //! Effects: Erases all the elements of the list. Chris@16: //! Chris@16: //! Throws: Nothing. Chris@16: //! Chris@16: //! Complexity: Linear to the number of elements in the list. Chris@101: void clear() BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { AllocHolder::clear(alloc_version()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // slist operations Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by the list. x != *this. this' allocator and x's allocator shall compare equal 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@101: void splice(const_iterator p, list& x) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: BOOST_ASSERT(this != &x); Chris@16: BOOST_ASSERT(this->node_alloc() == x.node_alloc()); Chris@16: this->icont().splice(p.get(), x.icont()); Chris@16: } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by the list. x != *this. this' allocator and x's allocator shall compare equal 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@101: void splice(const_iterator p, BOOST_RV_REF(list) x) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { this->splice(p, static_cast(x)); } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. i must point to an element contained in list x. Chris@16: //! this' allocator and x's allocator shall compare equal Chris@16: //! Chris@16: //! Effects: Transfers the value pointed by i, from list x to this list, Chris@16: //! before the the element pointed by p. No destructors or copy constructors are called. Chris@16: //! If p == i or p == ++i, 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@101: void splice(const_iterator p, list &x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: //BOOST_ASSERT(this != &x); Chris@16: BOOST_ASSERT(this->node_alloc() == x.node_alloc()); Chris@16: this->icont().splice(p.get(), x.icont(), i.get()); Chris@16: } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. i must point to an element contained in list x. Chris@16: //! this' allocator and x's allocator shall compare equal. Chris@16: //! Chris@16: //! Effects: Transfers the value pointed by i, from list x to this list, Chris@16: //! before the the element pointed by p. No destructors or copy constructors are called. Chris@16: //! If p == i or p == ++i, 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@101: void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator i) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { this->splice(p, static_cast(x), i); } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. first and last must point to elements contained in list x. Chris@16: //! this' allocator and x's allocator shall compare equal Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by first and last from list x to this list, Chris@16: //! before the 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: //! 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 &x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: BOOST_ASSERT(this->node_alloc() == x.node_alloc()); Chris@16: this->icont().splice(p.get(), x.icont(), first.get(), last.get()); Chris@16: } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. first and last must point to elements contained in list x. Chris@16: //! this' allocator and x's allocator shall compare equal. Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by first and last from list x to this list, Chris@16: //! before the 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: //! 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, BOOST_RV_REF(list) x, const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { this->splice(p, static_cast(x), first, last); } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. first and last must point to elements contained in list x. Chris@101: //! n == distance(first, last). this' allocator and x's allocator shall compare equal Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by first and last from list x to this list, Chris@16: //! before the 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@16: //! Chris@16: //! Note: Non-standard extension Chris@101: void splice(const_iterator p, list &x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { Chris@16: BOOST_ASSERT(this->node_alloc() == x.node_alloc()); Chris@16: this->icont().splice(p.get(), x.icont(), first.get(), last.get(), n); Chris@16: } Chris@16: Chris@16: //! Requires: p must point to an element contained Chris@16: //! by this list. first and last must point to elements contained in list x. Chris@101: //! n == distance(first, last). this' allocator and x's allocator shall compare equal Chris@16: //! Chris@16: //! Effects: Transfers the range pointed by first and last from list x to this list, Chris@16: //! before the 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@16: //! Chris@16: //! Note: Non-standard extension Chris@101: void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last, size_type n) BOOST_NOEXCEPT_OR_NOTHROW Chris@16: { this->splice(p, static_cast(x), first, last, n); } Chris@16: Chris@16: //! Effects: Removes all the elements that compare equal to value. Chris@16: //! Chris@16: //! Throws: If comparison throws. 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 T& value) Chris@101: { this->remove_if(equal_to_value_type(value)); } Chris@16: Chris@16: //! Effects: Removes all the elements for which a specified Chris@16: //! predicate is satisfied. Chris@16: //! Chris@16: //! Throws: If pred throws. 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@16: { Chris@101: typedef value_to_node_compare value_to_node_compare_type; Chris@101: this->icont().remove_and_dispose_if(value_to_node_compare_type(pred), Destroyer(this->node_alloc())); Chris@16: } Chris@16: Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that are equal from the list. Chris@16: //! Chris@16: //! Throws: If comparison throws. 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: void unique() Chris@16: { this->unique(value_equal()); } Chris@16: Chris@16: //! Effects: Removes adjacent duplicate elements or adjacent Chris@16: //! elements that satisfy some binary predicate from the list. Chris@16: //! Chris@16: //! Throws: If pred throws. Chris@16: //! Chris@16: //! Complexity: 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: template Chris@16: void unique(BinaryPredicate binary_pred) Chris@16: { Chris@101: typedef value_to_node_compare value_to_node_compare_type; Chris@101: this->icont().unique_and_dispose(value_to_node_compare_type(binary_pred), Destroyer(this->node_alloc())); Chris@16: } Chris@16: Chris@16: //! Requires: 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 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 comparison throws. Chris@16: //! Chris@16: //! Complexity: This function is linear time: it performs at most Chris@16: //! size() + x.size() - 1 comparisons. Chris@16: void merge(list &x) Chris@16: { this->merge(x, value_less()); } Chris@16: Chris@16: //! Requires: 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 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 comparison throws. Chris@16: //! Chris@16: //! Complexity: This function is linear time: it performs at most Chris@16: //! size() + x.size() - 1 comparisons. Chris@16: void merge(BOOST_RV_REF(list) x) Chris@16: { this->merge(static_cast(x)); } 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 comp throws. 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 to *this are not invalidated. Chris@16: template Chris@16: void merge(list &x, const StrictWeakOrdering &comp) Chris@16: { Chris@16: BOOST_ASSERT(this->node_alloc() == x.node_alloc()); Chris@101: typedef value_to_node_compare value_to_node_compare_type; Chris@101: this->icont().merge(x.icont(), value_to_node_compare_type(comp)); Chris@16: } 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 comp throws. 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 to *this are not invalidated. Chris@16: template Chris@16: void merge(BOOST_RV_REF(list) x, StrictWeakOrdering comp) Chris@16: { this->merge(static_cast(x), comp); } 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@16: //! Throws: If comparison throws. Chris@16: //! Chris@16: //! Notes: Iterators and references are not invalidated. Chris@101: //! 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(value_less()); } 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@16: //! Throws: If comp throws. 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: template Chris@16: void sort(StrictWeakOrdering comp) Chris@16: { Chris@16: // nothing if the list has length 0 or 1. Chris@16: if (this->size() < 2) Chris@16: return; Chris@101: typedef value_to_node_compare value_to_node_compare_type; Chris@101: this->icont().sort(value_to_node_compare_type(comp)); 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@101: void reverse() BOOST_NOEXCEPT_OR_NOTHROW Chris@101: { this->icont().reverse(); } Chris@16: Chris@101: //! Effects: Returns true if x and y are equal Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator==(const list& x, const list& y) Chris@101: { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); } Chris@101: Chris@101: //! Effects: Returns true if x and y are unequal Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator!=(const list& x, const list& y) Chris@101: { return !(x == y); } Chris@101: Chris@101: //! Effects: Returns true if x is less than y Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator<(const list& x, const list& y) Chris@101: { return boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); } Chris@101: Chris@101: //! Effects: Returns true if x is greater than y Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator>(const list& x, const list& y) Chris@101: { return y < x; } Chris@101: Chris@101: //! Effects: Returns true if x is equal or less than y Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator<=(const list& x, const list& y) Chris@101: { return !(y < x); } Chris@101: Chris@101: //! Effects: Returns true if x is equal or greater than y Chris@101: //! Chris@101: //! Complexity: Linear to the number of elements in the container. Chris@101: friend bool operator>=(const list& x, const list& y) Chris@101: { return !(x < y); } Chris@101: Chris@101: //! Effects: x.swap(y) Chris@101: //! Chris@101: //! Complexity: Constant. Chris@101: friend void swap(list& x, list& y) Chris@101: { x.swap(y); } Chris@101: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: private: Chris@16: Chris@16: bool priv_try_shrink(size_type new_size) Chris@16: { Chris@16: const size_type len = this->size(); Chris@16: if(len > new_size){ Chris@16: const const_iterator iend = this->cend(); Chris@16: size_type to_erase = len - new_size; Chris@16: const_iterator ifirst; Chris@16: if(to_erase < len/2u){ Chris@16: ifirst = iend; Chris@16: while(to_erase--){ Chris@16: --ifirst; Chris@16: } Chris@16: } Chris@16: else{ Chris@16: ifirst = this->cbegin(); Chris@16: size_type to_skip = len - to_erase; Chris@16: while(to_skip--){ Chris@16: ++ifirst; Chris@16: } Chris@16: } Chris@16: this->erase(ifirst, iend); Chris@16: return true; Chris@16: } Chris@16: else{ Chris@16: return false; Chris@16: } Chris@16: } Chris@16: Chris@16: iterator priv_insert(const_iterator p, const T &x) Chris@16: { Chris@16: NodePtr tmp = AllocHolder::create_node(x); Chris@16: return iterator(this->icont().insert(p.get(), *tmp)); Chris@16: } Chris@16: Chris@16: iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x) Chris@16: { Chris@16: NodePtr tmp = AllocHolder::create_node(boost::move(x)); Chris@16: return iterator(this->icont().insert(p.get(), *tmp)); Chris@16: } Chris@16: Chris@101: void priv_push_back (const T &x) Chris@16: { this->insert(this->cend(), x); } Chris@16: Chris@16: void priv_push_back (BOOST_RV_REF(T) x) Chris@16: { this->insert(this->cend(), boost::move(x)); } Chris@16: Chris@101: void priv_push_front (const T &x) Chris@16: { this->insert(this->cbegin(), x); } Chris@16: Chris@16: void priv_push_front (BOOST_RV_REF(T) x) Chris@16: { this->insert(this->cbegin(), boost::move(x)); } Chris@16: Chris@16: class insertion_functor; Chris@16: friend class insertion_functor; Chris@16: Chris@16: class insertion_functor Chris@16: { Chris@16: Icont &icont_; Chris@16: typedef typename Icont::const_iterator iconst_iterator; Chris@16: const iconst_iterator pos_; Chris@16: Chris@16: public: Chris@16: insertion_functor(Icont &icont, typename Icont::const_iterator pos) Chris@16: : icont_(icont), pos_(pos) Chris@16: {} Chris@16: Chris@16: void operator()(Node &n) Chris@16: { Chris@16: this->icont_.insert(pos_, n); Chris@16: } Chris@16: }; Chris@16: Chris@16: //Functors for member algorithm defaults Chris@16: struct value_less Chris@16: { Chris@16: bool operator()(const value_type &a, const value_type &b) const Chris@16: { return a < b; } Chris@16: }; Chris@16: Chris@16: struct value_equal Chris@16: { Chris@16: bool operator()(const value_type &a, const value_type &b) const Chris@16: { return a == b; } Chris@16: }; Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: }; Chris@16: Chris@101: #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: } //namespace container { Chris@16: Chris@16: //!has_trivial_destructor_after_move<> == true_type Chris@16: //!specialization for optimizations Chris@16: template Chris@16: struct has_trivial_destructor_after_move > Chris@101: { Chris@101: typedef typename ::boost::container::allocator_traits::pointer pointer; Chris@101: static const bool value = ::boost::has_trivial_destructor_after_move::value && Chris@101: ::boost::has_trivial_destructor_after_move::value; Chris@101: }; Chris@16: Chris@16: namespace container { Chris@16: Chris@101: #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: Chris@16: }} Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_CONTAINER_LIST_HPP