Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. 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@16: Chris@16: #ifndef BOOST_CONTAINER_LIST_HPP Chris@16: #define BOOST_CONTAINER_LIST_HPP Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) Chris@16: #else Chris@16: //Preprocessor library to emulate perfect forwarding Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace container { Chris@16: Chris@16: /// @cond 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@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@16: /// @endcond 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@16: #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED Chris@16: 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@16: /// @cond 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::allocator_v1 allocator_v1; Chris@16: typedef typename AllocHolder::allocator_v2 allocator_v2; Chris@16: typedef typename AllocHolder::alloc_version alloc_version; Chris@16: typedef boost::container::allocator_traits allocator_traits_type; Chris@16: Chris@16: class equal_to_value Chris@16: { Chris@16: typedef typename AllocHolder::value_type value_type; Chris@16: const value_type &t_; Chris@16: Chris@16: public: Chris@16: equal_to_value(const value_type &t) Chris@16: : t_(t) Chris@16: {} Chris@16: Chris@16: bool operator()(const value_type &t)const Chris@16: { return t_ == t; } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct ValueCompareToNodeCompare Chris@16: : Pred Chris@16: { Chris@16: ValueCompareToNodeCompare(Pred pred) Chris@16: : Pred(pred) Chris@16: {} Chris@16: Chris@16: bool operator()(const Node &a, const Node &b) const Chris@16: { return static_cast(*this)(a.m_data, b.m_data); } Chris@16: Chris@16: bool operator()(const Node &a) const Chris@16: { return static_cast(*this)(a.m_data); } Chris@16: }; Chris@16: Chris@16: BOOST_COPYABLE_AND_MOVABLE(list) Chris@16: Chris@16: typedef container_detail::iterator iterator_impl; Chris@16: typedef container_detail::iterator const_iterator_impl; Chris@16: /// @endcond 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@16: typedef BOOST_CONTAINER_IMPDEF(std::reverse_iterator) reverse_iterator; Chris@16: typedef BOOST_CONTAINER_IMPDEF(std::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@16: explicit list(const allocator_type &a) BOOST_CONTAINER_NOEXCEPT Chris@16: : AllocHolder(a) Chris@16: {} 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@16: //! Throws: If allocator_type's default constructor or copy 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@16: //! Throws: If allocator_type's default constructor or copy 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@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) Chris@16: : AllocHolder(x) Chris@16: { this->insert(this->cbegin(), x.begin(), x.end()); } Chris@16: Chris@16: //! Effects: Move constructor. Moves mx'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@16: : AllocHolder(boost::move(static_cast(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@16: //! Moves mx'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@16: this->insert(this->cbegin(), x.begin(), 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@16: //! Throws: If allocator_type's default constructor or copy constructor Chris@16: //! throws or T's constructor taking an 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@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@16: ~list() BOOST_CONTAINER_NOEXCEPT 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@16: //! Effects: Move assignment. All mx'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@16: //! Throws: If allocator_type's copy constructor throws. Chris@16: //! Chris@16: //! Complexity: Constant. Chris@16: list& operator=(BOOST_RV_REF(list) x) Chris@16: { Chris@16: if (&x != this){ Chris@16: NodeAlloc &this_alloc = this->node_alloc(); Chris@16: NodeAlloc &x_alloc = x.node_alloc(); Chris@16: //If allocators are equal we can just swap pointers Chris@16: if(this_alloc == x_alloc){ Chris@16: //Destroy and swap pointers Chris@16: this->clear(); Chris@16: this->icont() = boost::move(x.icont()); Chris@16: //Move allocator if needed Chris@16: this->AllocHolder::move_assign_alloc(x); Chris@16: } Chris@16: //If unequal allocators, then do a one by one move Chris@16: else{ Chris@16: this->assign( boost::make_move_iterator(x.begin()) Chris@16: , boost::make_move_iterator(x.end())); Chris@16: } Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: 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@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@16: allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT 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@16: stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT 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@16: const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT 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@16: iterator begin() BOOST_CONTAINER_NOEXCEPT 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@16: const_iterator begin() const BOOST_CONTAINER_NOEXCEPT 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@16: iterator end() BOOST_CONTAINER_NOEXCEPT 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@16: const_iterator end() const BOOST_CONTAINER_NOEXCEPT 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@16: reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT 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@16: const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT 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@16: reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT 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 BOOST_CONTAINER_NOEXCEPT 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@16: const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT 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@16: const_iterator cend() const BOOST_CONTAINER_NOEXCEPT 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@16: const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT 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@16: const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT 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@16: bool empty() const BOOST_CONTAINER_NOEXCEPT 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@16: size_type size() const BOOST_CONTAINER_NOEXCEPT 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@16: size_type max_size() const BOOST_CONTAINER_NOEXCEPT 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@16: reference front() BOOST_CONTAINER_NOEXCEPT 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@16: const_reference front() const BOOST_CONTAINER_NOEXCEPT 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@16: reference back() BOOST_CONTAINER_NOEXCEPT 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@16: const_reference back() const BOOST_CONTAINER_NOEXCEPT Chris@16: { return *(--this->end()); } Chris@16: Chris@16: ////////////////////////////////////////////// Chris@16: // Chris@16: // modifiers Chris@16: // Chris@16: ////////////////////////////////////////////// Chris@16: Chris@16: #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || 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@16: void emplace_back(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@16: void emplace_front(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@16: iterator emplace(const_iterator p, 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@16: #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING Chris@16: Chris@16: #define BOOST_PP_LOCAL_MACRO(n) \ Chris@16: BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ Chris@16: void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ Chris@16: { \ Chris@16: this->emplace(this->cend() \ Chris@16: BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ Chris@16: } \ Chris@16: \ Chris@16: BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ Chris@16: void emplace_front(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ Chris@16: { \ Chris@16: this->emplace(this->cbegin() \ Chris@16: BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \ Chris@16: } \ Chris@16: \ Chris@16: BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \ Chris@16: iterator emplace(const_iterator p \ Chris@16: BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ Chris@16: { \ Chris@16: NodePtr pnode (AllocHolder::create_node \ Chris@16: (BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _))); \ Chris@16: return iterator(this->icont().insert(p.get(), *pnode)); \ Chris@16: } \ Chris@16: //! Chris@16: #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) Chris@16: #include BOOST_PP_LOCAL_ITERATE() Chris@16: Chris@16: #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING 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@16: //! and moves the resources of mx 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@16: //! and moves the resources of mx 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@16: //! Requires: position must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Insert a copy of x before position. 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@16: iterator insert(const_iterator position, const T &x); Chris@16: Chris@16: //! Requires: position must be a valid iterator of *this. Chris@16: //! Chris@16: //! Effects: Insert a new element before position with mx'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@16: iterator insert(const_iterator position, 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@16: //! Complexity: Linear to std::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@16: || 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@16: || 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@16: this->allocate_many_and_construct(first, std::distance(first, last), func); Chris@16: return ++before_p; Chris@16: } Chris@16: #endif Chris@16: 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@16: void pop_front() BOOST_CONTAINER_NOEXCEPT 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@16: void pop_back() BOOST_CONTAINER_NOEXCEPT 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@16: iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT 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@16: iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT 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@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@16: void clear() BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, list& x) BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, BOOST_RV_REF(list) x) BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, list &x, const_iterator i) BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator i) BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, list &x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT 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@16: void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT 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@16: //! n == std::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@16: void splice(const_iterator p, list &x, const_iterator first, const_iterator last, size_type n) BOOST_CONTAINER_NOEXCEPT 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@16: //! n == std::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@16: void splice(const_iterator p, BOOST_RV_REF(list) x, const_iterator first, const_iterator last, size_type n) BOOST_CONTAINER_NOEXCEPT 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@16: { this->remove_if(equal_to_value(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@16: typedef ValueCompareToNodeCompare Predicate; Chris@16: this->icont().remove_and_dispose_if(Predicate(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@16: typedef ValueCompareToNodeCompare Predicate; Chris@16: this->icont().unique_and_dispose(Predicate(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@16: this->icont().merge(x.icont(), Chris@16: ValueCompareToNodeCompare(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@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(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@16: this->icont().sort(ValueCompareToNodeCompare(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@16: void reverse() BOOST_CONTAINER_NOEXCEPT Chris@16: { this->icont().reverse(); } Chris@16: Chris@16: /// @cond 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@16: 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@16: 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@16: /// @endcond Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: inline bool operator==(const list& x, const list& y) Chris@16: { Chris@16: if(x.size() != y.size()){ Chris@16: return false; Chris@16: } Chris@16: typedef typename list::const_iterator const_iterator; Chris@16: const_iterator end1 = x.end(); Chris@16: Chris@16: const_iterator i1 = x.begin(); Chris@16: const_iterator i2 = y.begin(); Chris@16: while (i1 != end1 && *i1 == *i2) { Chris@16: ++i1; Chris@16: ++i2; Chris@16: } Chris@16: return i1 == end1; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool operator<(const list& x, Chris@16: const list& y) Chris@16: { Chris@16: return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool operator!=(const list& x, const list& y) Chris@16: { Chris@16: return !(x == y); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool operator>(const list& x, const list& y) Chris@16: { Chris@16: return y < x; Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool operator<=(const list& x, const list& y) Chris@16: { Chris@16: return !(y < x); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool operator>=(const list& x, const list& y) Chris@16: { Chris@16: return !(x < y); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void swap(list& x, list& y) Chris@16: { Chris@16: x.swap(y); Chris@16: } Chris@16: Chris@16: /// @cond 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@16: : public ::boost::has_trivial_destructor_after_move Chris@16: {}; Chris@16: Chris@16: namespace container { Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: }} Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_CONTAINER_LIST_HPP